Skip to content

Commit

Permalink
Merge pull request #8 from kamatama41/some-fixes
Browse files Browse the repository at this point in the history
Some fixes
  • Loading branch information
kamatama41 authored Mar 18, 2017
2 parents 47a2cbb + 44f30c8 commit 9c854ac
Show file tree
Hide file tree
Showing 10 changed files with 145 additions and 115 deletions.
35 changes: 23 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
[![CircleCI](https://circleci.com/gh/kamatama41/gradle-embulk-plugin.svg?style=svg)](https://circleci.com/gh/kamatama41/gradle-embulk-plugin)

# gradle-plugin-embulk
Add tasks for [Embulk](http://www.embulk.org) plugin for Java.
This Gradle plugin provides you with some tasks to help your [Embulk](http://www.embulk.org) plugin development.

## Requirement
- Gradle 3.2.1 or later

## Installation and configuration
Add the snippets to your build.gradle
Expand All @@ -20,38 +23,46 @@ buildscript {
apply plugin: "com.github.kamatama41.embulk"
embulk {
embulkVersion = "0.8.18"
version = "0.8.18"
category = "filter"
name = "myfilter"
homepage = "https://github.com/someuser/embulk-filter-myfilter"
}
```

So that you can generate your plugin template with the command `(./gradlew|gradle) newPlugin`

## Dependencies
This plugin automatically adds the following dependency into your project.
- `org.embulk:embulk-core:<version>`

## Tasks
- `newPlugin`: Generate a new plugin template
- `classpath`: Copy jar files to classpath directory
- `gemspec`: Generate a gemspec file for this plugin
- `gem`: Generate a gem file
- `package`: Generate a package to run this plugin locally
- `checkstyle`: Check code style
- `package`: Generate a package, which is needed to run this plugin locally
- `checkstyle`: Run a Checkstyle process
- `gemPush`: Push gem file to rubygems.org
- `embulkSetup`: Install embulk binary
- `embulk_#{command}`: Run embulk command
- `embulkSetup`: Install a version of Embulk, which you specified by `version`
- `embulk_#{command}`: Run an embulk command ([details](#embulk_command))

### embulk_${command}
You can run a embulk command with the task and specify any command argument with `_`.
And this plugin implicitly set some config arguments when running commands such as `run` or `guess` so you don't need to specify config yaml path and project package path by default.
You can run an embulk command with a gradle task `embulk_*`, which can be added command arguments with `_`.
Also, this plugin automatically sets some arguments when executing a command such as `run` or `guess`,
so you don't have to specify yaml path and package path by default.

#### Examples
- `./gradlew embulk_--version`
- `(./gradlew|gradle) embulk_--version`
- This is equivalent to `embulk --version`

- `./gradlew embulk_run`
- `(./gradlew|gradle) embulk_run`
- This is equivalent to `embulk run config.yml -L <project_root>`

- `./gradlew embulk_guess`
- `(./gradlew|gradle) embulk_guess`
- This is equivalent to `embulk guess config.yml -o output.yml -L <project_root>`

If you want to configure yaml file name, you can do it with the params `configYaml` and `outputYaml` such as
If you want to change `config.yml` or `output.yml` to other file name, you can do it with `configYaml` or `outputYaml` option such as the following

```gradle
embulk {
Expand Down
38 changes: 38 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
buildscript {
ext.kotlinVersion = "1.1.1"
repositories {
jcenter()
maven { url "http://kamatama41.github.com/maven-repository/repository" }
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"
classpath "com.github.kamatama41:gradle-git-release-plugin:0.1.1"
}
}

apply plugin: "idea"
apply plugin: "kotlin"
apply plugin: "com.github.kamatama41.git-release"

repositories {
jcenter()
}

sourceCompatibility = JavaVersion.VERSION_1_7
targetCompatibility = JavaVersion.VERSION_1_7

dependencies {
compile gradleApi()
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion"
compile "org.eclipse.jgit:org.eclipse.jgit:4.3.1.201605051710-r"
compile "com.google.guava:guava:20.0"
compile "com.github.jruby-gradle:jruby-gradle-plugin:0.1.5"
testCompile "junit:junit:4.12"
}

gitRelease {
groupId = "com.github.kamatama41"
artifactId = "gradle-embulk-plugin"
repoUri = "[email protected]:kamatama41/maven-repository.git"
repoDir = file("${System.getProperty("user.home")}/gh-maven-repository")
}
46 changes: 0 additions & 46 deletions build.gradle.kts

This file was deleted.

2 changes: 1 addition & 1 deletion circle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ deployment:
branch: release
commands:
- git checkout master
- git pull
- git reset --hard origin/master
- ./gradlew release -Prelease.useAutomaticVersion=true
Binary file modified gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Sun Mar 12 18:04:53 JST 2017
#Sun Mar 19 02:07:37 JST 2017
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://repo.gradle.org/gradle/dist-snapshots/gradle-script-kotlin-3.5-20170305000422+0000-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-3.2.1-all.zip
1 change: 0 additions & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
rootProject.name = 'gradle-embulk-plugin'
rootProject.buildFileName = 'build.gradle.kts'
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
public class JRubyExecTasks {
static void gemPushTask(Project project, final EmbulkExtension extension) {
final JRubyExec task = project.getTasks().create("gemPush", JRubyExec.class);
task.dependsOn("gem");
task.setGroup(EmbulkPlugin.getGroupName());

project.afterEvaluate(new Action<Project>() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import org.gradle.api.Project
import java.io.File

open class EmbulkExtension(project: Project) {
lateinit var version: String
lateinit var category: String
lateinit var name: String
lateinit var embulkVersion: String
var authors: Array<String> = emptyArray()
var summary: String? = null
var description: String? = null
Expand All @@ -21,7 +21,7 @@ open class EmbulkExtension(project: Project) {
var checkstyleIgnoreFailures = true

var workDir = project.file("${project.projectDir.absolutePath}/.gradle/embulk")
val binFile get() = File("${workDir.absolutePath}/$embulkVersion/embulk")
val binFile get() = File("${workDir.absolutePath}/$version/embulk")
var configYaml = "config.yml"
var outputYaml = "output.yml"
}
129 changes: 78 additions & 51 deletions src/main/kotlin/com/github/kamatama41/gradle/embulk/EmbulkPlugin.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.github.kamatama41.gradle.embulk

import com.github.jrubygradle.JRubyPlugin
import org.apache.tools.ant.DirectoryScanner
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.Rule
import org.gradle.api.internal.artifacts.dsl.DefaultRepositoryHandler
import org.gradle.api.plugins.JavaPlugin
import org.gradle.api.plugins.JavaPluginConvention
Expand Down Expand Up @@ -36,6 +38,7 @@ class EmbulkPlugin : Plugin<Project> {
project.plugins.apply(JRubyPlugin::class.java)
project.configurations.maybeCreate("provided")

newPluginTask()
classpathTask()
gemspecTask()
gemTask()
Expand All @@ -48,6 +51,24 @@ class EmbulkPlugin : Plugin<Project> {
embulkDependencies()
}

fun newPluginTask() {
project.tasks.create("newPlugin", Copy::class.java) { task ->
task.group = groupName
project.afterEvaluate {
task.dependsOn("embulk_new_java-${extension.category}_${extension.name}")
task.from("embulk-${extension.category}-${extension.name}") { spec ->
DirectoryScanner.removeDefaultExclude("**/.gitignore")
spec.include("lib/**", "src/**", "README.md", "LICENSE.txt", ".gitignore")
}
task.into(".")

task.doLast {
project.delete("embulk-${extension.category}-${extension.name}")
}
}
}
}

fun classpathTask() {
project.tasks.create("classpath", Copy::class.java) { task ->
project.afterEvaluate {
Expand Down Expand Up @@ -122,33 +143,33 @@ class EmbulkPlugin : Plugin<Project> {

fun cleanTask() {
project.tasks.findByName("clean").apply {
doLast {
project.afterEvaluate { project.delete(classpathDir, gemspecFile) }
}
doLast { project.delete(classpathDir, gemspecFile) }
}
}

fun checkstyleTask() {
project.plugins.apply("checkstyle")
project.extensions.configure(CheckstyleExtension::class.java) { checkstyle ->
checkstyle.configFile = extension.checkstyleConfig
checkstyle.toolVersion = extension.checkstyleVersion
}
project.afterEvaluate {
project.extensions.configure(CheckstyleExtension::class.java) { checkstyle ->
checkstyle.configFile = extension.checkstyleConfig
checkstyle.toolVersion = extension.checkstyleVersion
}

val checkstyleMain = project.tasks.findByName("checkstyleMain") as Checkstyle
checkstyleMain.configFile = extension.checkstyleDefaultConfig
checkstyleMain.ignoreFailures = extension.checkstyleIgnoreFailures
val checkstyleMain = project.tasks.findByName("checkstyleMain") as Checkstyle
checkstyleMain.configFile = extension.checkstyleDefaultConfig
checkstyleMain.ignoreFailures = extension.checkstyleIgnoreFailures

val checkstyleTest = project.tasks.findByName("checkstyleTest") as Checkstyle
checkstyleTest.configFile = extension.checkstyleDefaultConfig
checkstyleTest.ignoreFailures = extension.checkstyleIgnoreFailures
val checkstyleTest = project.tasks.findByName("checkstyleTest") as Checkstyle
checkstyleTest.configFile = extension.checkstyleDefaultConfig
checkstyleTest.ignoreFailures = extension.checkstyleIgnoreFailures

val sourceSets = project.the<JavaPluginConvention>().sourceSets
val main = sourceSets.findByName("main")
val test = sourceSets.findByName("test")
project.tasks.create("checkstyle", Checkstyle::class.java) { task ->
task.classpath = main.output + test.output
task.setSource(main.allSource + test.allSource)
val sourceSets = project.the<JavaPluginConvention>().sourceSets
val main = sourceSets.findByName("main")
val test = sourceSets.findByName("test")
project.tasks.create("checkstyle", Checkstyle::class.java) { task ->
task.classpath = main.output + test.output
task.setSource(main.allSource + test.allSource)
}
}
}

Expand All @@ -158,8 +179,8 @@ class EmbulkPlugin : Plugin<Project> {
if (jcenter == null) {
project.repositories.add(project.repositories.jcenter())
}
project.dependencies.add("compile", "org.embulk:embulk-core:${extension.embulkVersion}")
project.dependencies.add("provided", "org.embulk:embulk-core:${extension.embulkVersion}")
project.dependencies.add("compile", "org.embulk:embulk-core:${extension.version}")
project.dependencies.add("provided", "org.embulk:embulk-core:${extension.version}")
}
}

Expand All @@ -169,9 +190,9 @@ class EmbulkPlugin : Plugin<Project> {

task.doLast {
val binFile = extension.binFile
val embulkVersion = extension.embulkVersion
val embulkVersion = extension.version
if (!binFile.exists()) {
project.logger.debug("Setting Embulk version to $embulkVersion")
println("Setting Embulk version to $embulkVersion")

val url = URL("https://dl.bintray.com/embulk/maven/embulk-$embulkVersion.jar")
url.openStream().use { input ->
Expand All @@ -190,34 +211,7 @@ class EmbulkPlugin : Plugin<Project> {
}

fun embulkExecTask() {
project.tasks.addRule("""Pattern: "embulk_<command>": Executes an Embulk command.""") { taskName ->
if (taskName.startsWith("embulk_")) {
project.tasks.create(taskName, JavaExec::class.java) { task ->
task.dependsOn("embulkSetup")
task.main = "-jar"

val token = taskName.split("_".toRegex()).drop(1).toMutableList() // remove first 'embulk'
val args = mutableListOf(extension.binFile.absolutePath)

val command = token.removeAt(0)
args.add(command)
if (listOf("run", "cleanup", "preview", "guess").contains(command)) {
task.dependsOn("package")
args.add(extension.configYaml)

if (command == "guess") {
args.add("-o")
args.add(extension.outputYaml)
}
args.add("-L")
args.add(project.rootDir.absolutePath)
}
args.addAll(token)

task.args(args)
}
}
}
project.tasks.addRule(EmbulkExecRule(project, extension))
}

private fun generateAuthors(extension: EmbulkExtension): String {
Expand Down Expand Up @@ -249,4 +243,37 @@ class EmbulkPlugin : Plugin<Project> {
else -> extension.name
}
}

class EmbulkExecRule(val project: Project, val extension: EmbulkExtension) : Rule {
override fun getDescription() = """Pattern: "embulk_<command>": Executes an Embulk command."""

override fun apply(taskName: String) {
if (taskName.startsWith("embulk_")) {
project.tasks.create(taskName, JavaExec::class.java) { task ->
task.dependsOn("embulkSetup")
task.main = "-jar"

val token = taskName.split("_".toRegex()).drop(1).toMutableList() // remove first 'embulk'
val args = mutableListOf(extension.binFile.absolutePath)

val command = token.removeAt(0)
args.add(command)
if (listOf("run", "cleanup", "preview", "guess").contains(command)) {
task.dependsOn("package")
args.add(extension.configYaml)

if (command == "guess") {
args.add("-o")
args.add(extension.outputYaml)
}
args.add("-L")
args.add(project.rootDir.absolutePath)
}
args.addAll(token)

task.args(args)
}
}
}
}
}

0 comments on commit 9c854ac

Please sign in to comment.