diff --git a/.github/workflows/build-pull-request.yaml b/.github/workflows/build-pull-request.yaml index a3fd160..20a828a 100644 --- a/.github/workflows/build-pull-request.yaml +++ b/.github/workflows/build-pull-request.yaml @@ -10,6 +10,8 @@ jobs: - uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 #v4.1.7 - uses: gradle/actions/setup-gradle@dbbdc275be76ac10734476cc723d82dfe7ec6eda #v3.4.2 - run: | - ./gradlew build + ./gradlew build + ./gradlew -p test-plugin build + ./gradlew -p test-plugin-isolated build ./gradlew -p test-app build - ./gradlew -p sample-app build + ./gradlew -p sample-app prepareIngredients diff --git a/build-logic/build.gradle.kts b/build-logic/build.gradle.kts index d9f1e22..1bdbd5b 100644 --- a/build-logic/build.gradle.kts +++ b/build-logic/build.gradle.kts @@ -20,6 +20,6 @@ group = "build-logic" /** * Ideally would use Runtime.version().feature() but the current Gradle still ships with Kotlin 1.9 - * that doesn't know about Java 22 🤷‍♂️ + * that doesn't know about Java 22 */ configureJavaCompatibility(17) diff --git a/build-logic/src/main/kotlin/main.kt b/build-logic/src/main/kotlin/main.kt new file mode 100644 index 0000000..d0e70e4 --- /dev/null +++ b/build-logic/src/main/kotlin/main.kt @@ -0,0 +1,16 @@ +import com.gradleup.librarian.gradle.librarianModule +import org.gradle.api.Project +import org.gradle.api.publish.PublishingExtension + +fun Project.module() { + librarianModule() + + val publishing = extensions.getByName("publishing") as PublishingExtension + + publishing.repositories { + it.maven { + it.name = "PluginTest" + it.url = uri(rootDir.resolve("build/m2")) + } + } +} \ No newline at end of file diff --git a/build.gradle.kts b/build.gradle.kts index 187ebb1..7db2a33 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -10,4 +10,10 @@ buildscript { } } -librarianRoot() \ No newline at end of file +librarianRoot() + +tasks.register("publishAllPublicationsToPluginTestRepository") { + subprojects { + dependsOn("$path:publishAllPublicationsToPluginTestRepository") + } +} \ No newline at end of file diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 9355b41..94113f2 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-8.10-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-8.11-bin.zip networkTimeout=10000 validateDistributionUrl=true zipStoreBase=GRADLE_USER_HOME diff --git a/gratatouille-core/build.gradle.kts b/gratatouille-core/build.gradle.kts index 84fd1bc..67381fd 100644 --- a/gratatouille-core/build.gradle.kts +++ b/gratatouille-core/build.gradle.kts @@ -1,11 +1,9 @@ -import com.gradleup.librarian.gradle.librarianModule - plugins { id("org.jetbrains.kotlin.jvm") id("org.jetbrains.kotlin.plugin.serialization") } -librarianModule() +module() dependencies { implementation(libs.kotlin.test) diff --git a/gratatouille-gradle-plugin/build.gradle.kts b/gratatouille-gradle-plugin/build.gradle.kts index 160a899..a4e78ee 100644 --- a/gratatouille-gradle-plugin/build.gradle.kts +++ b/gratatouille-gradle-plugin/build.gradle.kts @@ -1,5 +1,3 @@ -import com.gradleup.librarian.gradle.librarianModule - plugins { id("org.jetbrains.kotlin.jvm") id("org.jetbrains.kotlin.plugin.serialization") @@ -7,7 +5,7 @@ plugins { id("java-gradle-plugin") } -librarianModule() +module() dependencies { implementation(libs.kotlin.test) diff --git a/gratatouille-gradle-plugin/src/main/kotlin/gratatouille/gradle/GratatouilleApiPlugin.kt b/gratatouille-gradle-plugin/src/main/kotlin/gratatouille/gradle/GratatouilleApiPlugin.kt index dad49af..4d203c2 100644 --- a/gratatouille-gradle-plugin/src/main/kotlin/gratatouille/gradle/GratatouilleApiPlugin.kt +++ b/gratatouille-gradle-plugin/src/main/kotlin/gratatouille/gradle/GratatouilleApiPlugin.kt @@ -9,12 +9,14 @@ import javax.inject.Inject class GratatouilleApiPlugin : Plugin { override fun apply(target: Project) { + target.extensions.create("gratatouille", GratatouilleExtension::class.java, target) target.configurations.create("gratatouille") { it.isTransitive = false } - target.withPlugins("org.jetbrains.kotlin.jvm") { + target.withRequiredPlugins("org.jetbrains.kotlin.jvm") { target.configure() } + target.configureDefaultVersionsResolutionStrategy() } private fun Project.configure() { @@ -45,4 +47,4 @@ class GratatouilleApiPlugin : Plugin { abstract class ExtractGratatouilleSources: Copy() { @Inject abstract fun getArchiveOperations(): ArchiveOperations -} \ No newline at end of file +} diff --git a/gratatouille-gradle-plugin/src/main/kotlin/gratatouille/gradle/GratatouilleExtension.kt b/gratatouille-gradle-plugin/src/main/kotlin/gratatouille/gradle/GratatouilleExtension.kt index cd54215..e424699 100644 --- a/gratatouille-gradle-plugin/src/main/kotlin/gratatouille/gradle/GratatouilleExtension.kt +++ b/gratatouille-gradle-plugin/src/main/kotlin/gratatouille/gradle/GratatouilleExtension.kt @@ -24,7 +24,7 @@ class PluginSpec(private val id: String, private val project: Project) { // From https://github.com/gradle/gradle/blob/master/platforms/extensibility/plugin-development/src/main/java/org/gradle/plugin/devel/plugins/JavaGradlePluginPlugin.java#L253 project.tasks.named("processResources", Copy::class.java) { val copyPluginDescriptors: CopySpec = it.rootSpec.addChild() - copyPluginDescriptors.into("META-INF/gradle-plugins") + copyPluginDescriptors.into("/") copyPluginDescriptors.from(connection.directory) } } @@ -53,7 +53,7 @@ class PluginSpec(private val id: String, private val project: Project) { * * @param mainPublication the publication to redirect to. */ - fun marker(mainPublication: MavenPublication) { + fun mainPublication(mainPublication: MavenPublication) { project.withRequiredPlugins("maven-publish") { project.extensions.getByType(PublishingExtension::class.java).apply { // https://github.com/gradle/gradle/blob/master/platforms/extensibility/plugin-development/src/main/java/org/gradle/plugin/devel/plugins/MavenPluginPublishPlugin.java#L88 @@ -82,9 +82,25 @@ class PluginSpec(private val id: String, private val project: Project) { } } } + + fun mainPublication(mainPublication: String) { + project.withRequiredPlugins("maven-publish") { + project.extensions.getByType(PublishingExtension::class.java).apply { + val publication = publications.findByName(mainPublication) + check(publication != null) { + "Gratatouille: cannot find publication named '$mainPublication'" + } + check(publication is MavenPublication) { + "Gratatouille: publication '$mainPublication' is not an instance of MavenPublication" + } + mainPublication(publication) + } + } + } } -private fun String.displayName() = this.split(".").joinToString(separator = "") { it.replaceFirstChar { it.uppercase() } } +private fun String.displayName() = + this.split(".").joinToString(separator = "") { it.replaceFirstChar { it.uppercase() } } abstract class GratatouilleExtension(private val project: Project) { fun plugin(id: String, action: Action) { diff --git a/gratatouille-gradle-plugin/src/main/kotlin/gratatouille/gradle/GratatouilleImplementationPlugin.kt b/gratatouille-gradle-plugin/src/main/kotlin/gratatouille/gradle/GratatouilleImplementationPlugin.kt index b937da3..4de1c67 100644 --- a/gratatouille-gradle-plugin/src/main/kotlin/gratatouille/gradle/GratatouilleImplementationPlugin.kt +++ b/gratatouille-gradle-plugin/src/main/kotlin/gratatouille/gradle/GratatouilleImplementationPlugin.kt @@ -7,15 +7,14 @@ import org.gradle.api.Project class GratatouilleImplementationPlugin : Plugin { override fun apply(target: Project) { - target.withPlugins("org.jetbrains.kotlin.jvm", "com.google.devtools.ksp") { + target.withRequiredPlugins("org.jetbrains.kotlin.jvm", "com.google.devtools.ksp") { target.configurations.getByName("ksp").dependencies.add( - target.dependencies.create("${BuildConfig.group}:gratatouille-processor:${BuildConfig.version}") + target.dependencies.create("${BuildConfig.group}:gratatouille-processor") ) target.configurations.getByName("implementation").dependencies.add( - target.dependencies.create("${BuildConfig.group}:gratatouille-core:${BuildConfig.version}") + target.dependencies.create("${BuildConfig.group}:gratatouille-core") ) - target.afterEvaluate { target.extensions.getByName("ksp").apply { this as KspExtension @@ -23,5 +22,6 @@ class GratatouilleImplementationPlugin : Plugin { } } } + target.configureDefaultVersionsResolutionStrategy() } } diff --git a/gratatouille-gradle-plugin/src/main/kotlin/gratatouille/gradle/GratatouillePlugin.kt b/gratatouille-gradle-plugin/src/main/kotlin/gratatouille/gradle/GratatouillePlugin.kt index ebd9b35..a4f9db0 100644 --- a/gratatouille-gradle-plugin/src/main/kotlin/gratatouille/gradle/GratatouillePlugin.kt +++ b/gratatouille-gradle-plugin/src/main/kotlin/gratatouille/gradle/GratatouillePlugin.kt @@ -6,14 +6,16 @@ import org.gradle.api.Project class GratatouillePlugin : Plugin { override fun apply(target: Project) { - target.withPlugins("org.jetbrains.kotlin.jvm", "com.google.devtools.ksp") { + target.extensions.create("gratatouille", GratatouilleExtension::class.java, target) + target.withRequiredPlugins("org.jetbrains.kotlin.jvm", "com.google.devtools.ksp") { target.configurations.getByName("ksp").dependencies.add( - target.dependencies.create("${BuildConfig.group}:gratatouille-processor:${BuildConfig.version}") + target.dependencies.create("${BuildConfig.group}:gratatouille-processor") ) target.configurations.getByName("implementation").dependencies.add( - target.dependencies.create("${BuildConfig.group}:gratatouille-core:${BuildConfig.version}") + target.dependencies.create("${BuildConfig.group}:gratatouille-core") ) } + target.configureDefaultVersionsResolutionStrategy() } } diff --git a/gratatouille-gradle-plugin/src/main/kotlin/gratatouille/gradle/utils.kt b/gratatouille-gradle-plugin/src/main/kotlin/gratatouille/gradle/utils.kt index d4640c5..80f20ad 100644 --- a/gratatouille-gradle-plugin/src/main/kotlin/gratatouille/gradle/utils.kt +++ b/gratatouille-gradle-plugin/src/main/kotlin/gratatouille/gradle/utils.kt @@ -3,6 +3,7 @@ package gratatouille.gradle import com.gradleup.gratatouille.gradle.BuildConfig import org.gradle.api.Project import org.gradle.api.artifacts.ExternalDependency +import org.gradle.api.artifacts.component.ModuleComponentSelector internal fun Project.withRequiredPlugins(vararg ids: String, block: () -> Unit) { @@ -26,7 +27,6 @@ internal fun Project.withRequiredPlugins(vararg ids: String, block: () -> Unit) internal fun Project.configureDefaultVersionsResolutionStrategy() { configurations.configureEach { configuration -> - // Use the API introduced in Gradle 4.4 to modify the dependencies directly before they are resolved: configuration.withDependencies { dependencySet -> val pluginVersion = VERSION dependencySet.filterIsInstance() diff --git a/gratatouille-processor/build.gradle.kts b/gratatouille-processor/build.gradle.kts index 2a0365f..f5fd8a0 100644 --- a/gratatouille-processor/build.gradle.kts +++ b/gratatouille-processor/build.gradle.kts @@ -1,10 +1,8 @@ -import com.gradleup.librarian.gradle.librarianModule - plugins { id("org.jetbrains.kotlin.jvm") } -librarianModule() +module() dependencies { implementation(libs.kotlin.test) diff --git a/gratatouille-processor/src/main/kotlin/gratatouille/processor/GTaskAction.kt b/gratatouille-processor/src/main/kotlin/gratatouille/processor/GTaskAction.kt index 6d022af..b9fec55 100644 --- a/gratatouille-processor/src/main/kotlin/gratatouille/processor/GTaskAction.kt +++ b/gratatouille-processor/src/main/kotlin/gratatouille/processor/GTaskAction.kt @@ -12,7 +12,6 @@ import com.squareup.kotlinpoet.ClassName import com.squareup.kotlinpoet.ParameterizedTypeName import com.squareup.kotlinpoet.TypeName import com.squareup.kotlinpoet.ksp.toTypeName -import com.sun.org.apache.xpath.internal.operations.Bool internal class GTaskAction( val packageName: String, diff --git a/gratatouille-processor/src/main/kotlin/gratatouille/processor/Task.kt b/gratatouille-processor/src/main/kotlin/gratatouille/processor/Task.kt index 0ece0a5..278295d 100644 --- a/gratatouille-processor/src/main/kotlin/gratatouille/processor/Task.kt +++ b/gratatouille-processor/src/main/kotlin/gratatouille/processor/Task.kt @@ -9,11 +9,6 @@ internal fun GTaskAction.taskFile(): FileSpec { val fileSpec = FileSpec.builder(className) .addFunction(register()) - .apply { - if (!pure) { - addBodyComment("This task is impure and not cacheable") - } - } .addType(task()) .addType(workParameters()) .addType(workAction()) @@ -146,7 +141,8 @@ private fun GTaskAction.task(): TypeSpec { .build() ) } else { - addFunction(FunSpec.builder("init").addCode("outputs.upToDateWhen { false }").build()) + addKdoc("This task is impure and not cacheable") + addInitializerBlock(CodeBlock.of("outputs.upToDateWhen { false }\n")) } } .addModifiers(KModifier.ABSTRACT, KModifier.INTERNAL) diff --git a/librarian.properties b/librarian.properties index bf8bf28..d837acf 100644 --- a/librarian.properties +++ b/librarian.properties @@ -1,4 +1,4 @@ -java.compatibility=8 +java.compatibility=11 kotlin.compatibility=2.0.0 kdoc.olderVersions= diff --git a/libs.versions.toml b/libs.versions.toml index cae0a56..3c05fd4 100644 --- a/libs.versions.toml +++ b/libs.versions.toml @@ -1,6 +1,6 @@ [versions] -ksp = "2.0.20-1.0.24" -kotlin = "2.0.20" +ksp = "2.0.21-1.0.26" +kotlin = "2.0.21" coroutines = "1.8.0" kotlinpoet = "1.14.2" librarian = "0.0.6" @@ -22,5 +22,6 @@ librarian = { module = "com.gradleup.librarian:librarian-gradle-plugin", version okio = "com.squareup.okio:okio:3.9.0" cast = "net.mbonnin.cast:cast:0.0.1" + [plugins] librarian = { id = "com.gradleup.librarian", version.ref = "librarian"} \ No newline at end of file diff --git a/test-app/build.gradle.kts b/test-app/build.gradle.kts index bbe3260..f56d0b1 100644 --- a/test-app/build.gradle.kts +++ b/test-app/build.gradle.kts @@ -1,11 +1,22 @@ +import testplugin.TestExtension +import testplugin.isolated.TestExtensionIsolated import kotlin.test.assertFailsWith -plugins { - id("testplugin") - id("testplugin.isolated") +buildscript { + dependencies { + classpath("test-plugin:test-plugin") + classpath("test-plugin-isolated:api") + } + repositories { + mavenCentral() + } } -testExtension { +plugins.apply("testplugin") +plugins.apply("testplugin.isolated") + +extensions.getByName("testExtension").apply { + this as TestExtension stringInput.set("input") internalInput.set("internalInput") //optionalInput.set() @@ -26,7 +37,8 @@ testExtension { fileOutput3.set(file("build/output")) } -testExtensionIsolated { +extensions.getByName("testExtensionIsolated").apply { + this as TestExtensionIsolated stringInput.set("world") } diff --git a/test-app/settings.gradle.kts b/test-app/settings.gradle.kts index ed9b107..762c8df 100644 --- a/test-app/settings.gradle.kts +++ b/test-app/settings.gradle.kts @@ -3,6 +3,7 @@ pluginManagement { it.apply { mavenCentral() google() + maven("../build/m2") } } } diff --git a/test-plugin-isolated/api/build.gradle.kts b/test-plugin-isolated/api/build.gradle.kts index ff8d5de..3d176a4 100644 --- a/test-plugin-isolated/api/build.gradle.kts +++ b/test-plugin-isolated/api/build.gradle.kts @@ -3,18 +3,45 @@ plugins { id("org.jetbrains.kotlin.plugin.serialization") id("com.google.devtools.ksp") id("com.gradleup.gratatouille.api") - id("java-gradle-plugin") + id("maven-publish") +} + +version = "0.0.0" + +publishing { + publications { + create("default", MavenPublication::class.java) { + from(components["java"]) + } + } + repositories { + maven { + name = "pluginTest" + url = uri(rootDir.resolve("../build/m2")) + } + } } dependencies { gratatouille(project(":implementation")) } -gradlePlugin { - plugins { - create("testplugin.isolated") { - this.implementationClass = "testplugin.isolated.TestPluginIsolated" - this.id = "testplugin.isolated" - } +dependencies { + compileOnly(libs.gradle.api) + testImplementation(gradleTestKit()) + testImplementation(libs.kotlin.test) +} + + +gratatouille { + plugin("testplugin.isolated") { + implementationClass("testplugin.isolated.TestPluginIsolated") + mainPublication("default") } +} + +tasks.withType(Test::class.java) { + dependsOn("publishAllPublicationsToPluginTestRepository") + dependsOn(":implementation:publishAllPublicationsToPluginTestRepository") + dependsOn(gradle.includedBuild("gratatouille").task(":publishAllPublicationsToPluginTestRepository")) } \ No newline at end of file diff --git a/test-plugin-isolated/api/src/test/kotlin/MainTest.kt b/test-plugin-isolated/api/src/test/kotlin/MainTest.kt new file mode 100644 index 0000000..699e110 --- /dev/null +++ b/test-plugin-isolated/api/src/test/kotlin/MainTest.kt @@ -0,0 +1,29 @@ +import org.gradle.testkit.runner.BuildResult +import org.gradle.testkit.runner.GradleRunner +import org.gradle.testkit.runner.TaskOutcome +import java.io.File +import kotlin.test.Test +import kotlin.test.assertEquals + +class MainTest { + val projectDir = File("build/testProject") + + @Test + fun test() { + projectDir.deleteRecursively() + + File("testProjects/simple").copyRecursively(projectDir) + + build("taskActionIsolated").apply { + assertEquals(TaskOutcome.SUCCESS, task(":taskActionIsolated")!!.outcome) + } + } + + private fun build(task: String): BuildResult { + return GradleRunner.create() + .withDebug(true) + .withProjectDir(projectDir) + .withArguments(task) + .build()!! + } +} \ No newline at end of file diff --git a/test-plugin-isolated/api/testProjects/simple/build.gradle.kts b/test-plugin-isolated/api/testProjects/simple/build.gradle.kts new file mode 100644 index 0000000..c6c0293 --- /dev/null +++ b/test-plugin-isolated/api/testProjects/simple/build.gradle.kts @@ -0,0 +1,8 @@ +plugins { + id("testplugin.isolated").version("0.0.0") +} + +testExtensionIsolated { + this.stringInput.set("stringInput") +} + diff --git a/test-plugin-isolated/api/testProjects/simple/inputs/file0.txt b/test-plugin-isolated/api/testProjects/simple/inputs/file0.txt new file mode 100644 index 0000000..5ab2f8a --- /dev/null +++ b/test-plugin-isolated/api/testProjects/simple/inputs/file0.txt @@ -0,0 +1 @@ +Hello \ No newline at end of file diff --git a/test-plugin-isolated/api/testProjects/simple/inputs/input1.json b/test-plugin-isolated/api/testProjects/simple/inputs/input1.json new file mode 100644 index 0000000..ef90cbd --- /dev/null +++ b/test-plugin-isolated/api/testProjects/simple/inputs/input1.json @@ -0,0 +1 @@ +{"value": "value"} \ No newline at end of file diff --git a/test-plugin-isolated/api/testProjects/simple/inputs/subdir/file1.txt b/test-plugin-isolated/api/testProjects/simple/inputs/subdir/file1.txt new file mode 100644 index 0000000..50eb8b7 --- /dev/null +++ b/test-plugin-isolated/api/testProjects/simple/inputs/subdir/file1.txt @@ -0,0 +1 @@ +Hello file1.txt \ No newline at end of file diff --git a/test-plugin-isolated/api/testProjects/simple/settings.gradle.kts b/test-plugin-isolated/api/testProjects/simple/settings.gradle.kts new file mode 100644 index 0000000..fb760c5 --- /dev/null +++ b/test-plugin-isolated/api/testProjects/simple/settings.gradle.kts @@ -0,0 +1,6 @@ +pluginManagement { + listOf(repositories, dependencyResolutionManagement.repositories).forEach { + it.mavenCentral() + it.maven(rootDir.resolve("../../../../build/m2")) + } +} diff --git a/test-plugin-isolated/build.gradle.kts b/test-plugin-isolated/build.gradle.kts index 561988b..58e40c8 100644 --- a/test-plugin-isolated/build.gradle.kts +++ b/test-plugin-isolated/build.gradle.kts @@ -8,3 +8,4 @@ buildscript { classpath("com.gradleup.gratatouille:gratatouille-gradle-plugin") } } + diff --git a/test-plugin-isolated/implementation/build.gradle.kts b/test-plugin-isolated/implementation/build.gradle.kts index f886b6b..1f00476 100644 --- a/test-plugin-isolated/implementation/build.gradle.kts +++ b/test-plugin-isolated/implementation/build.gradle.kts @@ -3,6 +3,23 @@ plugins { id("org.jetbrains.kotlin.plugin.serialization") id("com.google.devtools.ksp") id("com.gradleup.gratatouille.implementation") + id("maven-publish") +} + +version = "0.0.0" + +publishing { + publications { + create("default", MavenPublication::class.java) { + from(components["java"]) + } + } + repositories { + maven { + name = "pluginTest" + url = uri(rootDir.resolve("../build/m2")) + } + } } dependencies { diff --git a/test-plugin/test-plugin/build.gradle.kts b/test-plugin/test-plugin/build.gradle.kts index 1a9d135..85a7022 100644 --- a/test-plugin/test-plugin/build.gradle.kts +++ b/test-plugin/test-plugin/build.gradle.kts @@ -1,22 +1,41 @@ plugins { - id("org.jetbrains.kotlin.jvm") - id("org.jetbrains.kotlin.plugin.serialization") - id("com.google.devtools.ksp") - id("com.gradleup.gratatouille") - id("java-gradle-plugin") + id("org.jetbrains.kotlin.jvm") + id("org.jetbrains.kotlin.plugin.serialization") + id("com.google.devtools.ksp") + id("com.gradleup.gratatouille") + id("maven-publish") } -gradlePlugin { - plugins { - create("testplugin") { - this.implementationClass = "testplugin.TestPlugin" - this.id = "testplugin" - } +version = "0.0.0" + +publishing { + publications { + create("default", MavenPublication::class.java) { + from(components["java"]) + } + } + repositories { + maven { + name = "pluginTest" + url = uri(rootDir.resolve("../build/m2")) } + } +} + +dependencies { + compileOnly(libs.gradle.api) + testImplementation(gradleTestKit()) + testImplementation(libs.kotlin.test) } gratatouille { - plugin("testplugin") { - implementationClass("testplugin.TestPlugin") - } + plugin("testplugin") { + implementationClass("testplugin.TestPlugin") + mainPublication("default") + } +} + +tasks.withType(Test::class.java) { + dependsOn("publishAllPublicationsToPluginTestRepository") + dependsOn(gradle.includedBuild("gratatouille").task(":publishAllPublicationsToPluginTestRepository")) } \ No newline at end of file diff --git a/test-plugin/test-plugin/src/main/kotlin/testplugin/TestPlugin.kt b/test-plugin/test-plugin/src/main/kotlin/testplugin/TestPlugin.kt index 413c406..0dc3a0d 100644 --- a/test-plugin/test-plugin/src/main/kotlin/testplugin/TestPlugin.kt +++ b/test-plugin/test-plugin/src/main/kotlin/testplugin/TestPlugin.kt @@ -37,6 +37,10 @@ class TestPlugin : Plugin { input = extension.stringInput3, outputFile = extension.fileOutput3 ) + + target.registerTaskAction4Task( + input = extension.stringInput + ) } } diff --git a/test-plugin/test-plugin/src/main/kotlin/testplugin/tasks.kt b/test-plugin/test-plugin/src/main/kotlin/testplugin/tasks.kt index 50a0756..78491b3 100644 --- a/test-plugin/test-plugin/src/main/kotlin/testplugin/tasks.kt +++ b/test-plugin/test-plugin/src/main/kotlin/testplugin/tasks.kt @@ -2,6 +2,7 @@ package testplugin import gratatouille.* import kotlinx.serialization.Serializable +import java.util.Date @Serializable data class Input1(val value: String) @@ -21,7 +22,8 @@ class TaskAction2Output(val output21: Output21, val output22: Output22) @GTaskAction internal fun taskAction1( stringInput: String, - @GInternal internalInput: String, + @GInternal + internalInput: String, optionalInput: String?, setInput: Set, listInput: List, @@ -64,9 +66,18 @@ internal fun taskAction2( @GTaskAction internal fun taskAction3( input: String, - @GManuallyWired outputFile: GOutputFile, + @GManuallyWired + outputFile: GOutputFile, outputDirectory: GOutputDirectory, ) { outputFile.writeText("$input in file") outputDirectory.resolve("output.txt").writeText("$input in file in dir") +} + +@GTaskAction(pure = false) +internal fun taskAction4( + input: String, + outputFile: GOutputFile, +) { + outputFile.writeText("${Date()}: $input in file") } \ No newline at end of file diff --git a/test-plugin/test-plugin/src/test/kotlin/MainTest.kt b/test-plugin/test-plugin/src/test/kotlin/MainTest.kt new file mode 100644 index 0000000..8eea8ee --- /dev/null +++ b/test-plugin/test-plugin/src/test/kotlin/MainTest.kt @@ -0,0 +1,43 @@ +import org.gradle.testkit.runner.BuildResult +import org.gradle.testkit.runner.GradleRunner +import org.gradle.testkit.runner.TaskOutcome +import java.io.File +import kotlin.test.Test +import kotlin.test.assertEquals + +class MainTest { + val projectDir = File("build/testProject") + + @Test + fun test() { + projectDir.deleteRecursively() + + File("testProjects/simple").copyRecursively(projectDir) + + build("taskAction1").apply { + assertEquals(TaskOutcome.SUCCESS, task(":taskAction1")!!.outcome) + } + build("taskAction2").apply { + assertEquals(TaskOutcome.UP_TO_DATE, task(":taskAction1")!!.outcome) + assertEquals(TaskOutcome.SUCCESS, task(":taskAction2")!!.outcome) + } + build("taskAction3").apply { + assertEquals(TaskOutcome.SUCCESS, task(":taskAction3")!!.outcome) + } + build("taskAction4").apply { + assertEquals(TaskOutcome.SUCCESS, task(":taskAction4")!!.outcome) + } + build("taskAction4").apply { + // taskAction4 is impure and never up-to-date + assertEquals(TaskOutcome.SUCCESS, task(":taskAction4")!!.outcome) + } + } + + private fun build(task: String): BuildResult { + return GradleRunner.create() + .withDebug(true) + .withProjectDir(projectDir) + .withArguments(task) + .build()!! + } +} \ No newline at end of file diff --git a/test-plugin/test-plugin/testProjects/simple/build.gradle.kts b/test-plugin/test-plugin/testProjects/simple/build.gradle.kts new file mode 100644 index 0000000..dda2c04 --- /dev/null +++ b/test-plugin/test-plugin/testProjects/simple/build.gradle.kts @@ -0,0 +1,19 @@ +plugins { + id("testplugin").version("0.0.0") +} + +testExtension { + this.mapInput.put("key", "value") + this.listInput.add(42) + this.setInput.add(43) + this.filesInput.from(file("inputs/file0.txt"), fileTree("inputs/subdir")) + this.fileInput.set(file("inputs/file0.txt")) + this.directoryInput.set(file("inputs/subdir")) + this.fileOutput3.set(file("build/file3.txt")) + this.internalInput.set("internalInput") + this.stringInput.set("stringInput") + this.stringInput2.set("stringInput2") + this.stringInput3.set("stringInput3") + this.serializableInput.set(file("inputs/input1.json")) +} + diff --git a/test-plugin/test-plugin/testProjects/simple/inputs/file0.txt b/test-plugin/test-plugin/testProjects/simple/inputs/file0.txt new file mode 100644 index 0000000..5ab2f8a --- /dev/null +++ b/test-plugin/test-plugin/testProjects/simple/inputs/file0.txt @@ -0,0 +1 @@ +Hello \ No newline at end of file diff --git a/test-plugin/test-plugin/testProjects/simple/inputs/input1.json b/test-plugin/test-plugin/testProjects/simple/inputs/input1.json new file mode 100644 index 0000000..ef90cbd --- /dev/null +++ b/test-plugin/test-plugin/testProjects/simple/inputs/input1.json @@ -0,0 +1 @@ +{"value": "value"} \ No newline at end of file diff --git a/test-plugin/test-plugin/testProjects/simple/inputs/subdir/file1.txt b/test-plugin/test-plugin/testProjects/simple/inputs/subdir/file1.txt new file mode 100644 index 0000000..50eb8b7 --- /dev/null +++ b/test-plugin/test-plugin/testProjects/simple/inputs/subdir/file1.txt @@ -0,0 +1 @@ +Hello file1.txt \ No newline at end of file diff --git a/test-plugin/test-plugin/testProjects/simple/settings.gradle.kts b/test-plugin/test-plugin/testProjects/simple/settings.gradle.kts new file mode 100644 index 0000000..fb760c5 --- /dev/null +++ b/test-plugin/test-plugin/testProjects/simple/settings.gradle.kts @@ -0,0 +1,6 @@ +pluginManagement { + listOf(repositories, dependencyResolutionManagement.repositories).forEach { + it.mavenCentral() + it.maven(rootDir.resolve("../../../../build/m2")) + } +}