Skip to content

Commit

Permalink
Generator improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
johnsonlee committed May 4, 2022
1 parent 3f98feb commit f36db66
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
26 changes: 16 additions & 10 deletions src/main/kotlin/io/johnsonlee/buildprops/BuildGenerator.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.johnsonlee.buildprops

import org.gradle.api.DefaultTask
import org.gradle.api.Project
import org.gradle.api.plugins.JavaPluginConvention
import org.gradle.api.tasks.OutputDirectory
import org.gradle.api.tasks.SourceSet
Expand All @@ -14,11 +15,16 @@ import java.util.StringTokenizer
open class BuildGenerator : DefaultTask() {

@get:OutputDirectory
val output: File = project.getGeneratedSourceDir(project.convention.getPlugin(JavaPluginConvention::class.java).sourceSets.getByName(SourceSet.MAIN_SOURCE_SET_NAME))
val output: File = project.getGeneratedSourceDir(
project.convention.getPlugin(JavaPluginConvention::class.java).sourceSets.getByName(SourceSet.MAIN_SOURCE_SET_NAME)
)

@TaskAction
fun generate() {
val pkg = mkpkg("${project.group}.${project.name}")
val group = project.group ?: project.rootProject.group
val name = project.name
val version = project.version.takeIf { it == Project.DEFAULT_VERSION } ?: project.rootProject.version
val pkg = mkpkg("${group}.${name}")
val path = "${pkg.replace(".", File.separator)}${File.separator}Build.java"
val revision = File(project.rootProject.projectDir, GIT_LOG_HEAD).takeIf {
it.exists() && it.canRead() && it.length() > 0
Expand All @@ -29,22 +35,22 @@ open class BuildGenerator : DefaultTask() {
File(output, path).also {
it.parentFile.mkdirs()
it.createNewFile()
}.printWriter().use {
it.apply {
it.println("""
}.printWriter().use { out ->
out.println(
"""
/**
* DO NOT MODIFY! This file is generated automatically.
*/
package $pkg;
public interface Build {
String GROUP = "${project.group}";
String ARTIFACT = "${project.name}";
String VERSION = "${project.version}";
String GROUP = "$group";
String ARTIFACT = "$name";
String VERSION = "$version";
String REVISION = "$revision";
}
""".trimIndent())
}
""".trimIndent()
)
}
}

Expand Down
10 changes: 6 additions & 4 deletions src/main/kotlin/io/johnsonlee/buildprops/BuildPropsPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@ class BuildPropsPlugin : Plugin<Project> {

sourceSets.filter {
it.name == SourceSet.MAIN_SOURCE_SET_NAME
}.forEach { sourceSet ->
(project.tasks.findByName(sourceSet.getCompileTaskName("java"))?.dependsOn(buildProps) as? SourceTask)?.source(buildProps.output)
(project.tasks.findByName(sourceSet.getCompileTaskName("groovy"))?.dependsOn(buildProps) as? SourceTask)?.source(buildProps.output)
(project.tasks.findByName(sourceSet.getCompileTaskName("kotlin"))?.dependsOn(buildProps) as? SourceTask)?.source(buildProps.output)
}.map { sourceSet ->
listOf("java", "kotlin", "groovy").mapNotNull { lang ->
project.tasks.findByName(sourceSet.getCompileTaskName(lang))
}
}.flatten().forEach {
(it.dependsOn(buildProps) as? SourceTask)?.source(buildProps.output)
}
}

Expand Down

0 comments on commit f36db66

Please sign in to comment.