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, and bumps up to 0.2.6-SNAPSHOT along with
the change.
  • Loading branch information
dmikurube committed Aug 23, 2019
1 parent e98356f commit 9af7580
Show file tree
Hide file tree
Showing 7 changed files with 341 additions and 165 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ plugins {
// They want Gradle plugins to be published under the "gradle.plugin" prefix for some security reasons.
// https://plugins.gradle.org/docs/publish-plugin
group = "org.embulk"
version = "0.2.5-SNAPSHOT"
version = "0.2.6-SNAPSHOT"
description = "A Gradle plugin to build and publish Embulk plugins"

repositories {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* 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);

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,66 @@
/*
* 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);

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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public Property<String> getType() {
return this.type;
}

void checkValidity() {
boolean isValidEmbulkPluginDefined() {
final ArrayList<String> errors = new ArrayList<>();
if ((!this.mainClass.isPresent()) || this.mainClass.get().isEmpty()) {
errors.add("\"mainClass\"");
Expand All @@ -71,17 +71,21 @@ void checkValidity() {
errors.add("\"type\"");
}

if (!errors.isEmpty()) {
throw new GradleException(
"Failed to configure \"embulkPlugin\" because of insufficient settings: [ "
+ String.join(", ", errors) + " ]");
}

if (!CATEGORIES.contains(this.category.get())) {
throw new GradleException(
"Failed to configure \"embulkPlugin\" because \"category\" must be one of: [ "
+ String.join(", ", CATEGORIES_ARRAY) + " ]");
}

if (errors.isEmpty()) {
return true;
}

this.project.getLogger().lifecycle(
"The project \"{}\" is not configured for an Embulk plugin.", this.project.getPath());
this.project.getLogger().lifecycle(
"\"embulkPlugin { ... }\" is not fully configured. Insufficient setting(s): [ {} ]", String.join(", ", errors));
return false;
}

private static final String[] CATEGORIES_ARRAY = {
Expand Down
Loading

0 comments on commit 9af7580

Please sign in to comment.