Skip to content

Commit 4735a75

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

File tree

4 files changed

+114
-43
lines changed

4 files changed

+114
-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: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright the GradleX team.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.gradlex.javamodule.packaging.internal;
18+
19+
public class Validator {
20+
public static void validateHostSystem(String arch, String os) {
21+
String hostOs = System.getProperty("os.name").replace(" ", "").toLowerCase();
22+
String hostArch = System.getProperty("os.arch");
23+
24+
if (os.contains("macos")) {
25+
if (!hostOs.contains(os)) {
26+
wrongHostSystemError(hostOs, os);
27+
}
28+
} else if (os.contains("windows")) {
29+
if (!hostOs.contains(os)) {
30+
wrongHostSystemError(hostOs, os);
31+
}
32+
} else {
33+
if (hostOs.contains("windows") || hostOs.contains("macos")) {
34+
wrongHostSystemError(hostOs, os);
35+
}
36+
}
37+
38+
if (arch.contains("64") && !hostArch.contains("64")) {
39+
wrongHostSystemError(hostArch, arch);
40+
}
41+
if (arch.contains("aarch") && !hostArch.contains("aarch")) {
42+
wrongHostSystemError(hostArch, arch);
43+
}
44+
if (!arch.contains("aarch") && hostArch.contains("aarch")) {
45+
wrongHostSystemError(hostArch, arch);
46+
}
47+
}
48+
49+
public static void wrongHostSystemError(String hostOs, String os) {
50+
throw new RuntimeException("Running on " + hostOs + "; cannot build for " + os);
51+
}
52+
}

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: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright the GradleX team.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.gradlex.javamodule.packaging.tasks;
18+
19+
import org.gradle.api.Action;
20+
import org.gradle.api.Task;
21+
22+
import java.util.Map;
23+
24+
import static org.gradlex.javamodule.packaging.internal.Validator.validateHostSystem;
25+
26+
public class ValidateHostSystemAction implements Action<Task> {
27+
@Override
28+
public void execute(Task task) {
29+
Map<String, Object> inputs = task.getInputs().getProperties();
30+
validateHostSystem((String) inputs.get("architecture"), (String) inputs.get("operatingSystem"));
31+
}
32+
}

0 commit comments

Comments
 (0)