diff --git a/.travis.yml b/.travis.yml index df744f0..9b1403e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,7 @@ language: java jdk: - - openjdk8 + - openjdk11 before_install: - chmod +x gradlew diff --git a/CHANGELOG.md b/CHANGELOG.md index c28dc64..66fc3c0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +# 1.0.0 + +* [**#1** Kotlin Migration](https://github.com/Scalified/gradle-sourcegen-plugin/issues/1) + # 0.0.1 ## Features diff --git a/README.md b/README.md index eec097c..8b9aab8 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,7 @@ # Gradle SourceGen Plugin [![Build Status](https://travis-ci.org/Scalified/gradle-sourcegen-plugin.svg)](https://travis-ci.org/Scalified/gradle-sourcegen-plugin) +[![Maven Central](https://img.shields.io/maven-metadata/v?label=Plugin&metadataUrl=https%3A%2F%2Fplugins.gradle.org%2Fm2%2Fcom%2Fscalified%2Fplugins%2Fgradle%2Fsourcegen%2Fcom.scalified.plugins.gradle.sourcegen.gradle.plugin%2Fmaven-metadata.xml)](https://plugins.gradle.org/plugin/com.scalified.plugins.gradle.sourcegen) ## Description @@ -8,7 +9,6 @@ ## Requirements -* [JDK](http://www.oracle.com/technetwork/java/javase/downloads/index.html) * [Gradle 5+](https://gradle.org/) ## Changelog @@ -46,8 +46,9 @@ apply plugin: "com.scalified.plugins.gradle.sourcegen" After applying the plugin, the following takes place: -1. A directory, specified in **sourcegen.srcDir** property (**src/generated/java** by default) created (if missing): +1. A directory, specified in the **sourcegen.location** property (**src/generated/java** by default) created (if missing) 2. The created directory is marked as generated source root in IntelliJ IDEA +3. The created directory is configured to be used as an output for annotation processor generated sources ## Configuration @@ -55,7 +56,7 @@ Currently the following configuration parameters supported (default values are s ```gradle sourcegen { - srcDir = 'src/generated/java' // directory for generated source code + location = 'src/generated/java' // directory for generated source code } ``` diff --git a/build.gradle.kts b/build.gradle.kts index 27bf391..2b05252 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -26,16 +26,10 @@ allprojects { group = "com.scalified" - version = "0.0.1" + version = "1.0.0" repositories { mavenCentral() } - tasks.withType { - sourceCompatibility = "${JavaVersion.VERSION_1_8}" - targetCompatibility = "${JavaVersion.VERSION_1_8}" - options.encoding = Charsets.UTF_8.name() - } - } diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar index 27768f1..f3d88b1 100644 Binary files a/gradle/wrapper/gradle-wrapper.jar and b/gradle/wrapper/gradle-wrapper.jar differ diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 4c46317..4a6ebce 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,5 +1,5 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-6.2.1-bin.zip zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.4-bin.zip diff --git a/gradlew b/gradlew index cccdd3d..2fe81a7 100644 --- a/gradlew +++ b/gradlew @@ -1,5 +1,21 @@ #!/usr/bin/env sh +# +# Copyright 2015 the original author or authors. +# +# 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 +# +# https://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. +# + ############################################################################## ## ## Gradle start up script for UN*X @@ -28,7 +44,7 @@ APP_NAME="Gradle" APP_BASE_NAME=`basename "$0"` # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. -DEFAULT_JVM_OPTS="" +DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' # Use the maximum available, or set MAX_FD != -1 to use that value. MAX_FD="maximum" @@ -109,8 +125,8 @@ if $darwin; then GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" fi -# For Cygwin, switch paths to Windows format before running java -if $cygwin ; then +# For Cygwin or MSYS, switch paths to Windows format before running java +if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then APP_HOME=`cygpath --path --mixed "$APP_HOME"` CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` JAVACMD=`cygpath --unix "$JAVACMD"` @@ -138,19 +154,19 @@ if $cygwin ; then else eval `echo args$i`="\"$arg\"" fi - i=$((i+1)) + i=`expr $i + 1` done case $i in - (0) set -- ;; - (1) set -- "$args0" ;; - (2) set -- "$args0" "$args1" ;; - (3) set -- "$args0" "$args1" "$args2" ;; - (4) set -- "$args0" "$args1" "$args2" "$args3" ;; - (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; - (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; - (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; - (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; - (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; + 0) set -- ;; + 1) set -- "$args0" ;; + 2) set -- "$args0" "$args1" ;; + 3) set -- "$args0" "$args1" "$args2" ;; + 4) set -- "$args0" "$args1" "$args2" "$args3" ;; + 5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; + 6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; + 7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; + 8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; + 9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; esac fi @@ -159,14 +175,9 @@ save () { for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done echo " " } -APP_ARGS=$(save "$@") +APP_ARGS=`save "$@"` # Collect all arguments for the java command, following the shell quoting and substitution rules eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" -# by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong -if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then - cd "$(dirname "$0")" -fi - exec "$JAVACMD" "$@" diff --git a/settings.gradle.kts b/settings.gradle.kts index d1dd10c..3c6f77e 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -22,11 +22,6 @@ * SOFTWARE. * */ -pluginManagement { - repositories { - gradlePluginPortal() - } -} rootProject.name = "gradle-sourcegen-plugin" diff --git a/sourcegen/build.gradle.kts b/sourcegen/build.gradle.kts index 8feb21c..23315a1 100644 --- a/sourcegen/build.gradle.kts +++ b/sourcegen/build.gradle.kts @@ -23,13 +23,10 @@ * */ plugins { - groovy - `java-gradle-plugin` - id("com.gradle.plugin-publish") version "0.10.1" -} + kotlin("jvm") version "1.3.70" -repositories { - mavenCentral() + id("com.gradle.plugin-publish") version "0.10.1" + `java-gradle-plugin` } pluginBundle { diff --git a/sourcegen/src/main/kotlin/com/scalified/plugins/gradle/sourcegen/Extensions.kt b/sourcegen/src/main/kotlin/com/scalified/plugins/gradle/sourcegen/Extensions.kt new file mode 100644 index 0000000..e13bd14 --- /dev/null +++ b/sourcegen/src/main/kotlin/com/scalified/plugins/gradle/sourcegen/Extensions.kt @@ -0,0 +1,40 @@ +/* + * MIT License + * + * Copyright (c) 2020 Scalified + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + * + */ + +package com.scalified.plugins.gradle.sourcegen + +import org.gradle.api.Project +import org.gradle.api.plugins.ExtensionAware +import org.gradle.api.tasks.SourceSet +import org.gradle.api.tasks.SourceSetContainer + +/** + * @author shell + * @since 2020-03-10 + */ +internal val Project.sourceSets: SourceSetContainer + get() = (this as ExtensionAware).extensions.getByName("sourceSets") as SourceSetContainer + +internal fun Project.sourceSet(name: String): SourceSet = project.sourceSets.getByName(name) as SourceSet diff --git a/sourcegen/src/main/groovy/com/scalified/plugins/gradle/sourcegen/SourceGenExtension.groovy b/sourcegen/src/main/kotlin/com/scalified/plugins/gradle/sourcegen/SourceGenExtension.kt similarity index 83% rename from sourcegen/src/main/groovy/com/scalified/plugins/gradle/sourcegen/SourceGenExtension.groovy rename to sourcegen/src/main/kotlin/com/scalified/plugins/gradle/sourcegen/SourceGenExtension.kt index cceed0a..8a13fac 100644 --- a/sourcegen/src/main/groovy/com/scalified/plugins/gradle/sourcegen/SourceGenExtension.groovy +++ b/sourcegen/src/main/kotlin/com/scalified/plugins/gradle/sourcegen/SourceGenExtension.kt @@ -1,7 +1,7 @@ /* * MIT License * - * Copyright (c) 2019 Scalified + * Copyright (c) 2020 Scalified * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -20,20 +20,19 @@ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. + * */ package com.scalified.plugins.gradle.sourcegen -import groovy.transform.PackageScope - /** - * @author shell* @since 2019-12-13 + * @author shell + * @since 2020-03-10 */ -class SourceGenExtension { +internal const val SOURCE_GEN_EXTENSION_NAME = "sourcegen" - @PackageScope - static final String NAME = "sourcegen" +open class SourceGenExtension { - String generated = "src/generated/java" + var location = "src/generated/java" } diff --git a/sourcegen/src/main/kotlin/com/scalified/plugins/gradle/sourcegen/SourceGenPlugin.kt b/sourcegen/src/main/kotlin/com/scalified/plugins/gradle/sourcegen/SourceGenPlugin.kt new file mode 100644 index 0000000..6cefc62 --- /dev/null +++ b/sourcegen/src/main/kotlin/com/scalified/plugins/gradle/sourcegen/SourceGenPlugin.kt @@ -0,0 +1,117 @@ +/* + * MIT License + * + * Copyright (c) 2020 Scalified + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + * + */ + +package com.scalified.plugins.gradle.sourcegen + +import org.gradle.api.Plugin +import org.gradle.api.Project +import org.gradle.api.tasks.SourceSet +import org.gradle.api.tasks.compile.JavaCompile +import org.gradle.plugins.ide.idea.IdeaPlugin +import org.slf4j.LoggerFactory + +/** + * @author shell + * @since 2020-03-10 + */ +private const val SOURCE_SET_NAME = "generated" + +private const val JAVA_COMPILE_TASK_NAME = "compileJava" + +private const val CLEAN_TASK_NAME = "clean" + +private const val ANNOTATION_PROCESSOR_CONFIGURATION = "annotationProcessor" + +private const val JAXB_API_DEPENDENCY = "javax.xml.bind:jaxb-api:2.3.1" + +open class SourceGenPlugin : Plugin { + + private val logger = LoggerFactory.getLogger(SourceGenPlugin::class.java) + + override fun apply(project: Project) { + val extension = project.extensions.create(SOURCE_GEN_EXTENSION_NAME, SourceGenExtension::class.java) + logger.debug("Created $SOURCE_GEN_EXTENSION_NAME plugin extension") + + project.afterEvaluate { + configureTasks(project, extension) + configureSourceSets(project, extension) + createDirectories(project, extension) + configureDirectories(project, extension) + configureDependencies(project) + } + } + + private fun configureSourceSets(project: Project, extension: SourceGenExtension) { + val mainSourceSet = project.sourceSet(SourceSet.MAIN_SOURCE_SET_NAME) + val generatedSourceSet = project.sourceSets.create(SOURCE_SET_NAME) + logger.debug("Created $SOURCE_SET_NAME source set") + + val file = project.file(extension.location) + generatedSourceSet.java.srcDirs += file + generatedSourceSet.compileClasspath += (mainSourceSet.compileClasspath + mainSourceSet.output) + generatedSourceSet.runtimeClasspath += (mainSourceSet.runtimeClasspath + mainSourceSet.output) + logger.debug("Configured $SOURCE_SET_NAME source set") + } + + private fun createDirectories(project: Project, extension: SourceGenExtension) { + val file = project.file(extension.location) + if (!file.exists()) { + file.mkdirs() + logger.debug("Created ${file.absolutePath} directory") + } + } + + private fun configureDirectories(project: Project, extension: SourceGenExtension) { + if (!project.plugins.hasPlugin(IdeaPlugin::class.java)) { + project.plugins.apply(IdeaPlugin::class.java) + logger.debug("Idea Plugin applied") + } + val ideaModule = project.plugins.getPlugin(IdeaPlugin::class.java).model.module + ideaModule.generatedSourceDirs.add(project.file(extension.location)) + logger.debug("Marked ${extension.location} as IDEA generated sources directory") + } + + private fun configureDependencies(project: Project) { + project.dependencies.add(ANNOTATION_PROCESSOR_CONFIGURATION, JAXB_API_DEPENDENCY) + logger.debug("Added $JAXB_API_DEPENDENCY to $ANNOTATION_PROCESSOR_CONFIGURATION configuration") + } + + private fun configureTasks(project: Project, extension: SourceGenExtension) { + val file = project.file(extension.location) + val javaCompileTask = project.tasks.getByName(JAVA_COMPILE_TASK_NAME) as JavaCompile + javaCompileTask.options.compilerArgs.addAll(listOf("-s", file.absolutePath)) + logger.debug("Configured JavaCompile task") + + val cleanTask = project.tasks.getByName(CLEAN_TASK_NAME) + cleanTask.doFirst { + if (file.exists()) { + file.listFiles()?.forEach { + it.deleteRecursively() + } + } + } + } + +}