Skip to content

Commit

Permalink
Configure tasks that need "embulkPluginExtension" only when they are …
Browse files Browse the repository at this point in the history
…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.
  • Loading branch information
dmikurube committed Aug 23, 2019
1 parent e98356f commit a676450
Show file tree
Hide file tree
Showing 5 changed files with 326 additions and 166 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* Copyright 2019 The Embulk project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.embulk.gradle.embulk_plugins;

import java.io.File;
import java.util.Arrays;
import java.util.List;
import javax.inject.Inject;
import org.gradle.api.DefaultTask;
import org.gradle.api.GradleException;
import org.gradle.api.Project;
import org.gradle.api.artifacts.Configuration;
import org.gradle.api.tasks.TaskAction;
import org.gradle.api.tasks.TaskProvider;
import org.gradle.api.tasks.bundling.Jar;

/**
* Configure a {@code "gem"} task to be available as an Embulk plugin.
*/
class ConfigureGemForEmbulkPlugin extends DefaultTask {
@Inject
public ConfigureGemForEmbulkPlugin() {
super();
}

@TaskAction
public void configure() {
final Project project = this.getProject();

final EmbulkPluginExtension extension = project.getExtensions().getByType(EmbulkPluginExtension.class);
extension.checkValidity();

final Configuration runtimeConfiguration = project.getConfigurations().getByName("runtime");

final TaskProvider<Gem> gemTask = project.getTasks().named("gem", Gem.class, task -> {
task.setEmbulkPluginMainClass(extension.getMainClass().get());
task.setEmbulkPluginCategory(extension.getCategory().get());
task.setEmbulkPluginType(extension.getType().get());

if ((!task.getArchiveBaseName().isPresent())) {
// project.getName() never returns null.
// https://docs.gradle.org/5.5.1/javadoc/org/gradle/api/Project.html#getName--
task.getArchiveBaseName().set(project.getName());
}
// summary is kept empty -- mandatory.
if ((!task.getArchiveVersion().isPresent()) && (!project.getVersion().toString().equals("unspecified"))) {
// project.getVersion() never returns null.
// https://docs.gradle.org/5.5.1/javadoc/org/gradle/api/Project.html#getVersion--
task.getArchiveVersion().set(buildGemVersionFromMavenVersion(project.getVersion().toString()));
}

task.getDestinationDirectory().set(((File) project.property("buildDir")).toPath().resolve("gems").toFile());
task.from(runtimeConfiguration, copySpec -> {
copySpec.into("classpath");
});
task.from(((Jar) project.getTasks().getByName("jar")).getArchiveFile(), copySpec -> {
copySpec.into("classpath");
});
});

project.getTasks().named("gemPush", GemPush.class, task -> {
if (!task.getGem().isPresent()) {
task.getGem().set(gemTask.get().getArchiveFile());
}
});
}

private static String buildGemVersionFromMavenVersion(final String mavenVersion) {
if (mavenVersion.contains("-")) {
final List<String> versionTokens = Arrays.asList(mavenVersion.split("-"));
if (versionTokens.size() != 2) {
throw new GradleException("Failed to convert the version \"" + mavenVersion + "\" to Gem-style.");
}
return versionTokens.get(0) + '.' + versionTokens.get(1).toLowerCase();
} else {
return mavenVersion;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright 2019 The Embulk project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.embulk.gradle.embulk_plugins;

import javax.inject.Inject;
import org.gradle.api.DefaultTask;
import org.gradle.api.Project;
import org.gradle.api.model.ObjectFactory;
import org.gradle.api.provider.Property;
import org.gradle.api.tasks.TaskAction;
import org.gradle.api.tasks.bundling.Jar;

/**
* Configure a {@code "jar"} task to be available as an Embulk plugin.
*
* <p>It configures the {@code "jar"} task with required MANIFEST.
*/
public class ConfigureJarForEmbulkPlugin extends DefaultTask {
@Inject
public ConfigureJarForEmbulkPlugin() {
super();

final ObjectFactory objectFactory = this.getProject().getObjects();

this.jar = objectFactory.property(String.class);
this.jar.set("jar");
}

@TaskAction
public void configure() {
final Project project = this.getProject();

final EmbulkPluginExtension extension = project.getExtensions().getByType(EmbulkPluginExtension.class);
extension.checkValidity();

project.getTasks().named(this.jar.get(), Jar.class, jarTask -> {
jarTask.manifest(UpdateManifestAction.builder()
.add("Embulk-Plugin-Main-Class", extension.getMainClass().get())
.add("Embulk-Plugin-Category", extension.getCategory().get())
.add("Embulk-Plugin-Type", extension.getType().get())
.add("Embulk-Plugin-Spi-Version", "0")
.add("Implementation-Title", project.getName())
.add("Implementation-Version", project.getVersion().toString())
.build());
});
}

public Property<String> getJar() {
return this.jar;
}

private final Property<String> jar;
}
Loading

0 comments on commit a676450

Please sign in to comment.