Skip to content

Commit

Permalink
#6 Kotlin Generated Sources missing in SourceSet
Browse files Browse the repository at this point in the history
  • Loading branch information
vbaidak committed Dec 5, 2020
1 parent 0fc5b28 commit 72b638a
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 46 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 2.2.0

* [**#6** Kotlin Generated Sources missing in SourceSet](https://github.com/Scalified/gradle-sourcegen-plugin/issues/6)

# 2.0.0

* [**#5** Kotlin KAPT Support](https://github.com/Scalified/gradle-sourcegen-plugin/issues/5)
Expand Down
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
allprojects {

group = "com.scalified"
version = "2.0.0"
version = "2.2.0"

repositories {
mavenCentral()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* 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.Task
import org.gradle.api.plugins.Convention
import org.gradle.api.plugins.JavaPluginConvention
import org.gradle.api.plugins.PluginContainer
import org.gradle.api.tasks.SourceSet
import org.gradle.api.tasks.SourceSetContainer
import org.gradle.api.tasks.TaskContainer
import org.gradle.api.tasks.compile.JavaCompile
import org.gradle.plugins.ide.idea.IdeaPlugin
import org.jetbrains.kotlin.gradle.internal.KaptTask

/**
* @author shell
* @since 2020-12-05
*/
internal val PluginContainer.kapt: Plugin<*>?
get() = findPlugin("org.jetbrains.kotlin.kapt")

internal val PluginContainer.idea: IdeaPlugin?
get() = findPlugin(IdeaPlugin::class.java)

internal val Convention.java: JavaPluginConvention?
get() = findPlugin(JavaPluginConvention::class.java)

internal val TaskContainer.clean: Task
get() = getByName("clean")

internal val TaskContainer.javaCompile: JavaCompile
get() = getByName("compileJava") as JavaCompile

internal val TaskContainer.kapt: KaptTask
get() = getByName("kaptKotlin") as KaptTask

internal val SourceSetContainer.main: SourceSet
get() = getByName("main")
Original file line number Diff line number Diff line change
Expand Up @@ -27,27 +27,13 @@ package com.scalified.plugins.gradle.sourcegen

import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.tasks.compile.JavaCompile
import org.gradle.plugins.ide.idea.IdeaPlugin
import org.jetbrains.kotlin.gradle.internal.KaptTask
import org.slf4j.LoggerFactory

/**
* @author shell
* @since 2020-03-10
*/
private const val JAVA_COMPILE_TASK_NAME = "compileJava"

private const val KAPT_PLUGIN = "org.jetbrains.kotlin.kapt"

private const val KAPT_TASK = "kaptKotlin"

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)
Expand All @@ -56,12 +42,9 @@ open class SourceGenPlugin : Plugin<Project> {
val extension = project.extensions.create(SOURCE_GEN_EXTENSION_NAME, SourceGenExtension::class.java)
logger.debug("Created $SOURCE_GEN_EXTENSION_NAME plugin extension")

project.afterEvaluate {
createDirectories(project, extension)
configureDependencies(project)
configureTasks(project, extension)
configureDirectories(project, extension)
}
createDirectories(project, extension)
configureDirectories(project, extension)
configureTasks(project, extension)
}

private fun createDirectories(project: Project, extension: SourceGenExtension) {
Expand All @@ -72,40 +55,40 @@ open class SourceGenPlugin : Plugin<Project> {
}
}

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 configureDirectories(project: Project, extension: SourceGenExtension) {
val file = project.file(extension.location)
project.convention.java?.sourceSets?.main?.java?.srcDir(file)

if (!project.plugins.hasPlugin(IdeaPlugin::class.java)) {
project.plugins.apply(IdeaPlugin::class.java)
logger.debug("Idea Plugin applied")
}
val ideaModule = project.plugins.idea?.model?.module
ideaModule?.generatedSourceDirs?.add(project.file(extension.location))
logger.debug("Marked ${extension.location} as IDEA generated sources directory")
}

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.annotationProcessorGeneratedSourcesDirectory = file
logger.debug("Configured JavaCompile task")

if (project.plugins.hasPlugin(KAPT_PLUGIN)) {
val kaptTask = project.tasks.getByName(KAPT_TASK) as KaptTask
kaptTask.destinationDir = file
}
project.afterEvaluate {
val javaCompileTask = project.tasks.javaCompile
javaCompileTask.options.annotationProcessorGeneratedSourcesDirectory = file
logger.debug("Configured JavaCompile task")

project.plugins.kapt?.let {
val kaptTask = project.tasks.kapt
kaptTask.destinationDir = file
kaptTask.kotlinSourcesDestinationDir = file
logger.debug("Configured Kapt task")
}

val cleanTask = project.tasks.getByName(CLEAN_TASK_NAME)
cleanTask.doFirst {
if (file.exists()) {
file.listFiles()?.forEach {
it.deleteRecursively()
project.tasks.clean.doFirst {
if (file.exists()) {
file.listFiles()?.forEach { it.deleteRecursively() }
}
}
}
}

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

}

0 comments on commit 72b638a

Please sign in to comment.