Skip to content

Commit a676450

Browse files
author
Dai MIKURUBE
committed
Configure tasks that need "embulkPluginExtension" only when they are needed
"EmbulkPluginExtension" has been always validated when running Gradle so that the "jar" and "gem" tasks can use the extension values. The validation has been sometimes annoying, however. For example, just running "./gradlew wrapper --gradle-version=5.6" does not work if "build.gradle" does not contain "embulkPlugin { ... }" configured properly. This commit makes the validation lazy so that it works only when the "jar" or "gem" tasks are executed, by externaizing the initializer methods into separate tasks, and getting them executed by the "dependsOn" mechanism of Gradle. It adds task groups and descriptions along with the change.
1 parent e98356f commit a676450

5 files changed

Lines changed: 326 additions & 166 deletions

File tree

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
* Copyright 2019 The Embulk project
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.embulk.gradle.embulk_plugins;
18+
19+
import java.io.File;
20+
import java.util.Arrays;
21+
import java.util.List;
22+
import javax.inject.Inject;
23+
import org.gradle.api.DefaultTask;
24+
import org.gradle.api.GradleException;
25+
import org.gradle.api.Project;
26+
import org.gradle.api.artifacts.Configuration;
27+
import org.gradle.api.tasks.TaskAction;
28+
import org.gradle.api.tasks.TaskProvider;
29+
import org.gradle.api.tasks.bundling.Jar;
30+
31+
/**
32+
* Configure a {@code "gem"} task to be available as an Embulk plugin.
33+
*/
34+
class ConfigureGemForEmbulkPlugin extends DefaultTask {
35+
@Inject
36+
public ConfigureGemForEmbulkPlugin() {
37+
super();
38+
}
39+
40+
@TaskAction
41+
public void configure() {
42+
final Project project = this.getProject();
43+
44+
final EmbulkPluginExtension extension = project.getExtensions().getByType(EmbulkPluginExtension.class);
45+
extension.checkValidity();
46+
47+
final Configuration runtimeConfiguration = project.getConfigurations().getByName("runtime");
48+
49+
final TaskProvider<Gem> gemTask = project.getTasks().named("gem", Gem.class, task -> {
50+
task.setEmbulkPluginMainClass(extension.getMainClass().get());
51+
task.setEmbulkPluginCategory(extension.getCategory().get());
52+
task.setEmbulkPluginType(extension.getType().get());
53+
54+
if ((!task.getArchiveBaseName().isPresent())) {
55+
// project.getName() never returns null.
56+
// https://docs.gradle.org/5.5.1/javadoc/org/gradle/api/Project.html#getName--
57+
task.getArchiveBaseName().set(project.getName());
58+
}
59+
// summary is kept empty -- mandatory.
60+
if ((!task.getArchiveVersion().isPresent()) && (!project.getVersion().toString().equals("unspecified"))) {
61+
// project.getVersion() never returns null.
62+
// https://docs.gradle.org/5.5.1/javadoc/org/gradle/api/Project.html#getVersion--
63+
task.getArchiveVersion().set(buildGemVersionFromMavenVersion(project.getVersion().toString()));
64+
}
65+
66+
task.getDestinationDirectory().set(((File) project.property("buildDir")).toPath().resolve("gems").toFile());
67+
task.from(runtimeConfiguration, copySpec -> {
68+
copySpec.into("classpath");
69+
});
70+
task.from(((Jar) project.getTasks().getByName("jar")).getArchiveFile(), copySpec -> {
71+
copySpec.into("classpath");
72+
});
73+
});
74+
75+
project.getTasks().named("gemPush", GemPush.class, task -> {
76+
if (!task.getGem().isPresent()) {
77+
task.getGem().set(gemTask.get().getArchiveFile());
78+
}
79+
});
80+
}
81+
82+
private static String buildGemVersionFromMavenVersion(final String mavenVersion) {
83+
if (mavenVersion.contains("-")) {
84+
final List<String> versionTokens = Arrays.asList(mavenVersion.split("-"));
85+
if (versionTokens.size() != 2) {
86+
throw new GradleException("Failed to convert the version \"" + mavenVersion + "\" to Gem-style.");
87+
}
88+
return versionTokens.get(0) + '.' + versionTokens.get(1).toLowerCase();
89+
} else {
90+
return mavenVersion;
91+
}
92+
}
93+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Copyright 2019 The Embulk project
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.embulk.gradle.embulk_plugins;
18+
19+
import javax.inject.Inject;
20+
import org.gradle.api.DefaultTask;
21+
import org.gradle.api.Project;
22+
import org.gradle.api.model.ObjectFactory;
23+
import org.gradle.api.provider.Property;
24+
import org.gradle.api.tasks.TaskAction;
25+
import org.gradle.api.tasks.bundling.Jar;
26+
27+
/**
28+
* Configure a {@code "jar"} task to be available as an Embulk plugin.
29+
*
30+
* <p>It configures the {@code "jar"} task with required MANIFEST.
31+
*/
32+
public class ConfigureJarForEmbulkPlugin extends DefaultTask {
33+
@Inject
34+
public ConfigureJarForEmbulkPlugin() {
35+
super();
36+
37+
final ObjectFactory objectFactory = this.getProject().getObjects();
38+
39+
this.jar = objectFactory.property(String.class);
40+
this.jar.set("jar");
41+
}
42+
43+
@TaskAction
44+
public void configure() {
45+
final Project project = this.getProject();
46+
47+
final EmbulkPluginExtension extension = project.getExtensions().getByType(EmbulkPluginExtension.class);
48+
extension.checkValidity();
49+
50+
project.getTasks().named(this.jar.get(), Jar.class, jarTask -> {
51+
jarTask.manifest(UpdateManifestAction.builder()
52+
.add("Embulk-Plugin-Main-Class", extension.getMainClass().get())
53+
.add("Embulk-Plugin-Category", extension.getCategory().get())
54+
.add("Embulk-Plugin-Type", extension.getType().get())
55+
.add("Embulk-Plugin-Spi-Version", "0")
56+
.add("Implementation-Title", project.getName())
57+
.add("Implementation-Version", project.getVersion().toString())
58+
.build());
59+
});
60+
}
61+
62+
public Property<String> getJar() {
63+
return this.jar;
64+
}
65+
66+
private final Property<String> jar;
67+
}

0 commit comments

Comments
 (0)