Skip to content

Commit

Permalink
fix: Refactoring
Browse files Browse the repository at this point in the history
closes #9
  • Loading branch information
vbaidak committed Nov 15, 2024
1 parent 7408ba9 commit 554da9e
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 56 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 2.6.0

* [**#9** Refactoring](https://github.com/Scalified/gradle-sourcegen-plugin/issues/9)

# 2.4.0

* [**#8** Gradle Upgrade](https://github.com/Scalified/gradle-sourcegen-plugin/issues/8)
Expand Down
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,18 @@

```kotlin
plugins {
id "com.scalified.plugins.gradle.sourcegen" version "$VERSION"
id("com.scalified.plugins.gradle.sourcegen") version "$VERSION"
}
```

## Usage

After applying the plugin, the following takes place:

1. A directory, specified in the `sourcegen.location` property (`src/main/generated` by default) created (if missing)
2. The created directory is configured:
1. JavaPlugin applied if not yet applied
2. IdeaPlugin applied if not yet applied
3. A directory, specified in the `sourcegen.location` property (`src/main/generated` by default) created (if missing)
4. The created directory is configured:
* as an output for annotation processor generated sources
* for clean up during `clean` task
* as marked generated sources root in IntelliJ IDEA (only if it is resided within `src/main`)
Expand Down
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[versions]
project = "2.4.0"
project = "2.6.0"

gradle-publish = "1.3.0"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,38 +25,52 @@
package com.scalified.plugins.gradle.sourcegen

import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.Task
import org.gradle.api.plugins.ExtensionContainer
import org.gradle.api.plugins.JavaPluginExtension
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.TaskProvider
import org.gradle.api.tasks.compile.JavaCompile
import org.gradle.kotlin.dsl.getByType
import org.gradle.kotlin.dsl.getPlugin
import org.gradle.kotlin.dsl.named
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.kaptOptional: Plugin<*>?
get() = findPlugin("org.jetbrains.kotlin.kapt")

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

internal val ExtensionContainer.java: JavaPluginExtension?
get() = findByType(JavaPluginExtension::class.java)
internal val ExtensionContainer.java: JavaPluginExtension
get() = getByType<JavaPluginExtension>()

internal val TaskContainer.clean: Task
get() = getByName("clean")
internal val TaskContainer.clean: TaskProvider<Task>
get() = named("clean")

internal val TaskContainer.javaCompile: JavaCompile
get() = getByName("compileJava") as JavaCompile
internal val TaskContainer.javaCompile: TaskProvider<JavaCompile>
get() = named<JavaCompile>("compileJava")

internal val TaskContainer.kapt: KaptTask
get() = getByName("kaptKotlin") as KaptTask
internal val TaskContainer.kapt: TaskProvider<KaptTask>
get() = named<KaptTask>("kaptKotlin")

internal val SourceSetContainer.main: SourceSet
get() = getByName("main")
get() = getByName("main")

internal fun Project.createMissingDirectories(paths: Set<String>) {
paths.map(this::file).forEach { file ->
if (!file.exists()) {
file.mkdirs()
logger.debug("Created '${file.absolutePath}' directory")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,13 @@ package com.scalified.plugins.gradle.sourcegen

import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.plugins.JavaPlugin
import org.gradle.kotlin.dsl.apply
import org.gradle.kotlin.dsl.create
import org.gradle.kotlin.dsl.hasPlugin
import org.gradle.plugins.ide.idea.IdeaPlugin
import org.slf4j.LoggerFactory
import java.io.File

/**
* @author shell
Expand All @@ -40,55 +44,43 @@ open class SourceGenPlugin : Plugin<Project> {
private val logger = LoggerFactory.getLogger(SourceGenPlugin::class.java)

override fun apply(project: Project) {
listOf(JavaPlugin::class, IdeaPlugin::class).filterNot(project.plugins::hasPlugin).forEach { type ->
project.plugins.apply(type)
logger.info("Applied '${type.simpleName}' plugin")
}

val extension = project.extensions.create<SourceGenPluginExtension>(SOURCE_GEN).apply {
logger.debug("'$SOURCE_GEN' extension created")
logger.debug("Extension '$SOURCE_GEN' created")
location.convention(LOCATION)
logger.debug("'{}' extension configured: {}", SOURCE_GEN, this)
logger.debug("Extension '{}' configured: {}", SOURCE_GEN, this)
}

project.afterEvaluate {
createDirectories(project, extension)
configureDirectories(project, extension)
configureTasks(project, extension)
}
}
val location = extension.location.get()
val locationFile = file(location)

private fun createDirectories(project: Project, extension: SourceGenPluginExtension) {
val file = project.file(extension.location.get())
if (!file.exists()) {
file.mkdirs()
logger.debug("Created ${file.absolutePath} directory")
}
}
createMissingDirectories(setOf(location))

private fun configureDirectories(project: Project, extension: SourceGenPluginExtension) {
val file = project.file(extension.location.get())
project.extensions.java?.sourceSets?.main?.java?.srcDir(file)
project.extensions.java.sourceSets.main.java.srcDir(locationFile)
logger.debug("Added '$location' generated sources as java sources")

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.get()))
logger.debug("Marked '{}' as IDEA generated sources directory", extension.location.get())
}
project.plugins.idea.model.module.generatedSourceDirs.add(locationFile)
logger.debug("Marked '$location' as an IDEA generated sources directory")

private fun configureTasks(project: Project, extension: SourceGenPluginExtension) {
val file = project.file(extension.location.get())
val javaCompileTask = project.tasks.javaCompile
javaCompileTask.options.generatedSourceOutputDirectory.set(file)
logger.debug("Configured JavaCompile task")
tasks.javaCompile.get().options.generatedSourceOutputDirectory.set(locationFile)
logger.debug("Added '$location' generated sources as a java compile generated source output directory")

project.plugins.kapt?.let {
val kaptTask = project.tasks.kapt
kaptTask.destinationDir.set(file)
kaptTask.kotlinSourcesDestinationDir.set(file)
logger.debug("Configured Kapt task")
}
plugins.kaptOptional?.let { tasks.kapt.get() }?.apply {
destinationDir.set(locationFile)
kotlinSourcesDestinationDir.set(locationFile)
logger.debug("Added '$location' generated sources as kapt sources destination directory")
}

project.tasks.clean.doFirst {
if (file.exists()) {
file.listFiles()?.forEach { it.deleteRecursively() }
tasks.clean.get().doFirst {
if (locationFile.exists()) {
locationFile.listFiles()?.forEach(File::deleteRecursively)
logger.debug("Location '$location' cleaned")
}
}
}
}
Expand Down

0 comments on commit 554da9e

Please sign in to comment.