Skip to content

Commit

Permalink
#1 Kotlin Migration
Browse files Browse the repository at this point in the history
  • Loading branch information
vbaidak committed Mar 11, 2020
1 parent e5d561d commit 19ba33e
Show file tree
Hide file tree
Showing 12 changed files with 209 additions and 51 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
language: java

jdk:
- openjdk8
- openjdk11

before_install:
- chmod +x gradlew
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 1.0.0

* [**#1** Kotlin Migration](https://github.com/Scalified/gradle-sourcegen-plugin/issues/1)

# 0.0.1

## Features
Expand Down
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# 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

[Gradle SourceGen Plugin](https://plugins.gradle.org/plugin/com.scalified.plugins.gradle.sourcegen) configures directory for generated sources

## Requirements

* [JDK](http://www.oracle.com/technetwork/java/javase/downloads/index.html)
* [Gradle 5+](https://gradle.org/)

## Changelog
Expand Down Expand Up @@ -46,16 +46,17 @@ 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

Currently the following configuration parameters supported (default values are shown):

```gradle
sourcegen {
srcDir = 'src/generated/java' // directory for generated source code
location = 'src/generated/java' // directory for generated source code
}
```

Expand Down
8 changes: 1 addition & 7 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,10 @@
allprojects {

group = "com.scalified"
version = "0.0.1"
version = "1.0.0"

repositories {
mavenCentral()
}

tasks.withType<JavaCompile> {
sourceCompatibility = "${JavaVersion.VERSION_1_8}"
targetCompatibility = "${JavaVersion.VERSION_1_8}"
options.encoding = Charsets.UTF_8.name()
}

}
Binary file modified gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -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
51 changes: 31 additions & 20 deletions gradlew
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"`
Expand Down Expand Up @@ -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

Expand All @@ -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" "$@"
5 changes: 0 additions & 5 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,6 @@
* SOFTWARE.
*
*/
pluginManagement {
repositories {
gradlePluginPortal()
}
}

rootProject.name = "gradle-sourcegen-plugin"

Expand Down
9 changes: 3 additions & 6 deletions sourcegen/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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"

}
Original file line number Diff line number Diff line change
@@ -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<Project> {

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()
}
}
}
}

}

0 comments on commit 19ba33e

Please sign in to comment.