Skip to content

Commit

Permalink
Merge pull request #11 from kamatama41/add-tests
Browse files Browse the repository at this point in the history
Add tests
  • Loading branch information
kamatama41 authored Mar 19, 2017
2 parents 09d00ee + 4a1171b commit 23a77b6
Show file tree
Hide file tree
Showing 7 changed files with 194 additions and 94 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@ gradle-app.setting
# gradle/wrapper/gradle-wrapper.properties

.idea

sandbox
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ apply plugin: "com.github.kamatama41.embulk"
embulk {
version = "0.8.18"
category = "filter"
name = "myfilter"
homepage = "https://github.com/someuser/embulk-filter-myfilter"
category = "file-input"
name = "xlsx"
authors = ["A User"]
email = "[email protected]"
homepage = "https://github.com/a.user/embulk-input-xlsx"
}
```

Expand Down
7 changes: 6 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ buildscript {

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

repositories {
Expand All @@ -24,9 +25,9 @@ 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 gradleTestKit()
testCompile "junit:junit:4.12"
}

Expand All @@ -36,3 +37,7 @@ gitRelease {
repoUri = "[email protected]:kamatama41/maven-repository.git"
repoDir = file("${System.getProperty("user.home")}/gh-maven-repository")
}

gradlePlugin {
automatedPublishing = false
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,29 @@ open class EmbulkExtension(project: Project) {
lateinit var version: String
lateinit var category: String
lateinit var name: String
var authors: Array<String> = emptyArray()
lateinit var authors: Array<String>
lateinit var email: String
var summary: String? = null
var description: String? = null
var email: String? = null
var licenses = arrayOf("MIT")
var homepage = ""
var jrubyVersion = "1.7.19" // TODO: update (currently v9.1.x)

var checkstyleVersion = "6.14.1"
var checkstyleConfig = project.resources.text.fromFile("config/checkstyle/checkstyle.xml").asFile()
var checkstyleDefaultConfig = project.resources.text.fromFile("config/checkstyle/default.xml").asFile()
var checkstyleConfig : File? = null
var checkstyleDefaultConfig : File? = null
var checkstyleIgnoreFailures = true

var workDir = project.file("${project.projectDir.absolutePath}/.gradle/embulk")
val binFile get() = File("${workDir.absolutePath}/$version/embulk")
var configYaml = "config.yml"
var outputYaml = "output.yml"

val displayName get() = name.toUpperCamel()
val displayCategory get() = category.replace("-", " ")
val embulkCategory get() = when(category) {
"file-input" -> "input"
"file-output" -> "output"
else -> category
}
}
68 changes: 38 additions & 30 deletions src/main/kotlin/com/github/kamatama41/gradle/embulk/EmbulkPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import org.gradle.api.plugins.JavaPlugin
import org.gradle.api.plugins.JavaPluginConvention
import org.gradle.api.plugins.quality.Checkstyle
import org.gradle.api.plugins.quality.CheckstyleExtension
import org.gradle.api.plugins.quality.CheckstylePlugin
import org.gradle.api.tasks.Copy
import org.gradle.api.tasks.JavaExec
import org.gradle.api.tasks.bundling.Jar
Expand All @@ -20,7 +21,6 @@ import java.net.URL
class EmbulkPlugin : Plugin<Project> {
lateinit var project: Project
lateinit var extension: EmbulkExtension
lateinit var git: Git
val extensionName = "embulk"
val classpathDir = "classpath"
val gemspecFile: File by lazy { project.file("${project.name}.gemspec") }
Expand All @@ -33,9 +33,9 @@ class EmbulkPlugin : Plugin<Project> {
override fun apply(project: Project) {
this.project = project
this.extension = project.extensions.create(extensionName, EmbulkExtension::class.java, project)
this.git = Git.new(project.rootProject.rootDir)
project.plugins.apply(JavaPlugin::class.java)
project.plugins.apply(JRubyPlugin::class.java)
project.plugins.apply(CheckstylePlugin::class.java)
project.configurations.maybeCreate("provided")

newPluginTask()
Expand All @@ -56,14 +56,16 @@ class EmbulkPlugin : Plugin<Project> {
task.group = groupName
project.afterEvaluate {
task.dependsOn("embulk_new_java-${extension.category}_${extension.name}")
task.from("embulk-${extension.category}-${extension.name}") { spec ->

val pluginName = "embulk-${extension.embulkCategory}-${extension.name}"
task.from(pluginName) { spec ->
DirectoryScanner.removeDefaultExclude("**/.gitignore")
spec.include("lib/**", "src/**", "README.md", "LICENSE.txt", ".gitignore")
}
task.into(".")

task.doLast {
project.delete("embulk-${extension.category}-${extension.name}")
project.delete(pluginName)
}
}
}
Expand Down Expand Up @@ -98,17 +100,16 @@ class EmbulkPlugin : Plugin<Project> {

task.inputs.file("build.gradle")
task.outputs.file(gemspecFile)
git.config()

task.doLast {
gemspecFile.writeText("""
|Gem::Specification.new do |spec|
| spec.name = "${project.name}"
| spec.version = "${project.version}"
| spec.authors = ["${generateAuthors(extension)}"]
| spec.summary = %[${generateSummary(extension)}]
| spec.description = %[${generateDescription(extension)}]
| spec.email = ["${generateEmail(extension)}"]
| spec.authors = ["${extension.authors.joinToString("\", \"")}"]
| spec.summary = %[${generateSummary()}]
| spec.description = %[${generateDescription()}]
| spec.email = ["${extension.email}"]
| spec.licenses = ["${extension.licenses.joinToString()}"]
| spec.homepage = "${extension.homepage}"
|
Expand Down Expand Up @@ -148,19 +149,21 @@ class EmbulkPlugin : Plugin<Project> {
}

fun checkstyleTask() {
project.plugins.apply("checkstyle")
project.afterEvaluate {
val config = extension.checkstyleConfig ?: getConfigFromResources("checkstyle/checkstyle.xml")
val defaultConfig = extension.checkstyleDefaultConfig ?: getConfigFromResources("checkstyle/default.xml")

project.extensions.configure(CheckstyleExtension::class.java) { checkstyle ->
checkstyle.configFile = extension.checkstyleConfig
checkstyle.configFile = config
checkstyle.toolVersion = extension.checkstyleVersion
}

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

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

val sourceSets = project.the<JavaPluginConvention>().sourceSets
Expand Down Expand Up @@ -214,36 +217,41 @@ class EmbulkPlugin : Plugin<Project> {
project.tasks.addRule(EmbulkExecRule(project, extension))
}

private fun generateAuthors(extension: EmbulkExtension): String {
return if (extension.authors.isEmpty()) {
this.git.config().getString("user", null, "name")
} else {
extension.authors.joinToString()
}
}

private fun generateEmail(extension: EmbulkExtension): String
= extension.email ?: this.git.config().getString("user", null, "email")

private fun generateSummary(extension: EmbulkExtension): String {
return extension.summary ?: "${extension.name.toUpperCamel()} ${extension.category} plugin for Embulk"
}
private fun generateSummary(): String
= extension.summary ?: "${extension.displayName} ${extension.displayCategory} plugin for Embulk"

private fun generateDescription(extension: EmbulkExtension): String {
private fun generateDescription(): String {
return extension.description ?: when (extension.category) {
"input" -> "Loads records from ${extension.name}."
"file_input" -> "Reads files stored on ${extension.name}."
"file-input" -> "Reads files stored on ${extension.name}."
"parser" -> "Parses ${extension.name} files read by other file input plugins."
"decoder" -> "Decodes ${extension.name}}-encoded files read by other file input plugins."
"output" -> "Dumps records to ${extension.name}."
"file_output" -> "Stores files on ${extension.name}."
"file-output" -> "Stores files on ${extension.name}."
"formatter" -> "Formats ${extension.name} files for other file output plugins."
"encoder" -> "Encodes files using ${extension.name} for other file output plugins."
"filter" -> extension.name
else -> extension.name
}
}

private fun getConfigFromResources(path: String): File {
val tmpConfig = project.file("${project.buildDir.absolutePath}/embulk/config/$path")
if (!tmpConfig.parentFile.exists()) {
tmpConfig.parentFile.mkdirs()
}
tmpConfig.delete()
tmpConfig.createNewFile()

this.javaClass.classLoader.getResourceAsStream("config/$path").use { input ->
FileOutputStream(tmpConfig).use { output ->
input.copyTo(output)
}
}

return tmpConfig
}

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

Expand Down
43 changes: 0 additions & 43 deletions src/main/kotlin/com/github/kamatama41/gradle/embulk/Git.kt

This file was deleted.

Loading

0 comments on commit 23a77b6

Please sign in to comment.