Skip to content

Commit da00487

Browse files
committed
Add validation step to target-specific test tasks
1 parent e888855 commit da00487

File tree

4 files changed

+82
-43
lines changed

4 files changed

+82
-43
lines changed

src/main/java/org/gradlex/javamodule/packaging/JavaModulePackagingExtension.java

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
import org.gradle.testing.base.TestSuite;
4949
import org.gradlex.javamodule.packaging.model.Target;
5050
import org.gradlex.javamodule.packaging.tasks.Jpackage;
51+
import org.gradlex.javamodule.packaging.tasks.ValidateHostSystemAction;
5152

5253
import javax.inject.Inject;
5354
import java.util.Arrays;
@@ -86,10 +87,19 @@ abstract public class JavaModulePackagingExtension {
8687
abstract protected Project getProject();
8788

8889

90+
/**
91+
* Retrieve the target with the given 'label'. If the target does not yet exist, it will be created.
92+
*/
93+
@SuppressWarnings("unused")
8994
public Target target(String label) {
9095
return target(label, target -> {});
9196
}
9297

98+
/**
99+
* Register or update a target with the given 'label'. The 'label' uniquely identifies the target.
100+
* It is used for task names and can be chosen freely.
101+
* Details of the target are configured in the {@link Target} configuration action.
102+
*/
93103
public Target target(String label, Action<? super Target> action) {
94104
Target target;
95105
if (targets.getNames().contains(label)) {
@@ -102,6 +112,11 @@ public Target target(String label, Action<? super Target> action) {
102112
return target;
103113
}
104114

115+
/**
116+
* Set a 'primary target'. Standard Gradle tasks that are not bound to a specific target – like 'assemble' – use
117+
* this 'primary target'.
118+
*/
119+
@SuppressWarnings("unused")
105120
public Target primaryTarget(Target target) {
106121
SourceSetContainer sourceSets = getProject().getExtensions().getByType(SourceSetContainer.class);
107122
ConfigurationContainer configurations = getProject().getConfigurations();
@@ -117,22 +132,25 @@ public Target primaryTarget(Target target) {
117132
return target;
118133
}
119134

135+
/**
136+
* Set a test suite to be 'multi-target'. This registers an additional 'test' task for each target.
137+
*/
120138
public TestSuite multiTargetTestSuite(TestSuite testSuite) {
121139
if (!(testSuite instanceof JvmTestSuite)) {
122140
return testSuite;
123141
}
124142

125143
JvmTestSuite suite = (JvmTestSuite) testSuite;
126-
targets.all(target -> {
127-
suite.getTargets().register(testSuite.getName() + capitalize(target.getName()), testTarget -> {
128-
testTarget.getTestTask().configure(task -> {
129-
ConfigurationContainer configurations = getProject().getConfigurations();
130-
task.setClasspath(configurations.getByName(target.getName() + capitalize(suite.getSources().getRuntimeClasspathConfigurationName())).plus(
131-
getObjects().fileCollection().from(getProject().getTasks().named(suite.getSources().getJarTaskName())))
132-
);
133-
});
134-
});
135-
});
144+
targets.all(target -> suite.getTargets().register(testSuite.getName() + capitalize(target.getName()), testTarget -> testTarget.getTestTask().configure(task -> {
145+
task.getInputs().property("operatingSystem", target.getOperatingSystem());
146+
task.getInputs().property("architecture", target.getArchitecture());
147+
148+
ConfigurationContainer configurations = getProject().getConfigurations();
149+
task.setClasspath(configurations.getByName(target.getName() + capitalize(suite.getSources().getRuntimeClasspathConfigurationName())).plus(
150+
getObjects().fileCollection().from(getProject().getTasks().named(suite.getSources().getJarTaskName())))
151+
);
152+
task.doFirst(new ValidateHostSystemAction());
153+
})));
136154

137155
return testSuite;
138156
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package org.gradlex.javamodule.packaging.internal;
2+
3+
public class Validator {
4+
public static void validateHostSystem(String arch, String os) {
5+
String hostOs = System.getProperty("os.name").replace(" ", "").toLowerCase();
6+
String hostArch = System.getProperty("os.arch");
7+
8+
if (os.contains("macos")) {
9+
if (!hostOs.contains(os)) {
10+
wrongHostSystemError(hostOs, os);
11+
}
12+
} else if (os.contains("windows")) {
13+
if (!hostOs.contains(os)) {
14+
wrongHostSystemError(hostOs, os);
15+
}
16+
} else {
17+
if (hostOs.contains("windows") || hostOs.contains("macos")) {
18+
wrongHostSystemError(hostOs, os);
19+
}
20+
}
21+
22+
if (arch.contains("64") && !hostArch.contains("64")) {
23+
wrongHostSystemError(hostArch, arch);
24+
}
25+
if (arch.contains("aarch") && !hostArch.contains("aarch")) {
26+
wrongHostSystemError(hostArch, arch);
27+
}
28+
if (!arch.contains("aarch") && hostArch.contains("aarch")) {
29+
wrongHostSystemError(hostArch, arch);
30+
}
31+
}
32+
33+
public static void wrongHostSystemError(String hostOs, String os) {
34+
throw new RuntimeException("Running on " + hostOs + "; cannot build for " + os);
35+
}
36+
}

src/main/java/org/gradlex/javamodule/packaging/tasks/Jpackage.java

Lines changed: 2 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050

5151
import static java.util.Objects.requireNonNull;
5252
import static org.gradle.nativeplatform.OperatingSystemFamily.WINDOWS;
53+
import static org.gradlex.javamodule.packaging.internal.Validator.validateHostSystem;
5354

5455
@CacheableTask
5556
abstract public class Jpackage extends DefaultTask {
@@ -123,10 +124,8 @@ public void runJpackage() throws Exception {
123124

124125
String os = getOperatingSystem().get();
125126
String arch = getArchitecture().get();
126-
String hostOs = System.getProperty("os.name").replace(" ", "").toLowerCase();
127-
String hostArch = System.getProperty("os.arch");
128127

129-
validateHostSystem(arch, hostArch, os, hostOs);
128+
validateHostSystem(arch, os);
130129

131130
Directory resourcesDir = getTempDirectory().get().dir("jpackage-resources");
132131
Directory appImageParent = getTempDirectory().get().dir("app-image");
@@ -233,34 +232,4 @@ private String bytesToHex(byte[] hash) {
233232
}
234233
return hexString.toString();
235234
}
236-
237-
private void validateHostSystem(String arch, String hostArch, String os, String hostOs) {
238-
if (os.contains("macos")) {
239-
if (!hostOs.contains(os)) {
240-
wrongHostSystemError(hostOs, os);
241-
}
242-
} else if (os.contains("windows")) {
243-
if (!hostOs.contains(os)) {
244-
wrongHostSystemError(hostOs, os);
245-
}
246-
} else {
247-
if (hostOs.contains("windows") || hostOs.contains("macos")) {
248-
wrongHostSystemError(hostOs, os);
249-
}
250-
}
251-
252-
if (arch.contains("64") && !hostArch.contains("64")) {
253-
wrongHostSystemError(hostArch, arch);
254-
}
255-
if (arch.contains("aarch") && !hostArch.contains("aarch")) {
256-
wrongHostSystemError(hostArch, arch);
257-
}
258-
if (!arch.contains("aarch") && hostArch.contains("aarch")) {
259-
wrongHostSystemError(hostArch, arch);
260-
}
261-
}
262-
263-
private void wrongHostSystemError(String hostOs, String os) {
264-
throw new RuntimeException("Running on " + hostOs + "; cannot build for " + os);
265-
}
266235
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package org.gradlex.javamodule.packaging.tasks;
2+
3+
import org.gradle.api.Action;
4+
import org.gradle.api.Task;
5+
6+
import java.util.Map;
7+
8+
import static org.gradlex.javamodule.packaging.internal.Validator.validateHostSystem;
9+
10+
public class ValidateHostSystemAction implements Action<Task> {
11+
@Override
12+
public void execute(Task task) {
13+
Map<String, Object> inputs = task.getInputs().getProperties();
14+
validateHostSystem((String) inputs.get("architecture"), (String) inputs.get("operatingSystem"));
15+
}
16+
}

0 commit comments

Comments
 (0)