diff --git a/release-plugin/src/main/kotlin/io/github/simonhauck/release/plugin/ReleaseExtension.kt b/release-plugin/src/main/kotlin/io/github/simonhauck/release/plugin/ReleaseExtension.kt index 4cbde78..6e78282 100644 --- a/release-plugin/src/main/kotlin/io/github/simonhauck/release/plugin/ReleaseExtension.kt +++ b/release-plugin/src/main/kotlin/io/github/simonhauck/release/plugin/ReleaseExtension.kt @@ -1,8 +1,10 @@ package io.github.simonhauck.release.plugin import org.gradle.api.file.ProjectLayout +import org.gradle.api.file.RegularFile import org.gradle.api.file.RegularFileProperty import org.gradle.api.model.ObjectFactory +import org.gradle.api.provider.ListProperty import org.gradle.api.provider.Property abstract class ReleaseExtension( @@ -15,6 +17,11 @@ abstract class ReleaseExtension( val versionPropertyFile: RegularFileProperty = objects.fileProperty().convention(layout.projectDirectory.file("version.properties")) + val gitAddFiles: ListProperty = + objects + .listProperty(RegularFile::class.java) + .convention(versionPropertyFile.map { listOf(it) }) + val releaseCommitMessage: Property = objects.property(String::class.java).convention("Release commit: v{version}") diff --git a/release-plugin/src/main/kotlin/io/github/simonhauck/release/plugin/ReleasePlugin.kt b/release-plugin/src/main/kotlin/io/github/simonhauck/release/plugin/ReleasePlugin.kt index 1cbc922..ecf7258 100644 --- a/release-plugin/src/main/kotlin/io/github/simonhauck/release/plugin/ReleasePlugin.kt +++ b/release-plugin/src/main/kotlin/io/github/simonhauck/release/plugin/ReleasePlugin.kt @@ -58,8 +58,11 @@ class ReleasePlugin : Plugin { it.commitMessagePrefix.set(extension.commitMessagePrefix) it.tagName.set(extension.tagName) it.stringTemplateVariables.set(extension.versionPropertyFile) - // TODO Simon.Hauck 2024-05-13 - solve this. Should be the file - it.gitAddFilePattern.set(".") + it.gitAddFilePattern.set( + extension.gitAddFiles.map { regularFiles -> + regularFiles.map { regularFile -> regularFile.asFile } + } + ) } } @@ -72,8 +75,9 @@ class ReleasePlugin : Plugin { it.commitMessage.set(extension.postReleaseCommitMessage) it.commitMessagePrefix.set(extension.commitMessagePrefix) it.stringTemplateVariables.set(extension.versionPropertyFile) - // TODO Simon.Hauck 2024-05-13 - solve this. Should be the file - it.gitAddFilePattern.set(".") + it.gitAddFilePattern.set( + extension.versionPropertyFile.map { file -> listOf(file.asFile) } + ) } } diff --git a/release-plugin/src/main/kotlin/io/github/simonhauck/release/tasks/CommitAndTagTask.kt b/release-plugin/src/main/kotlin/io/github/simonhauck/release/tasks/CommitAndTagTask.kt index 1ad42ff..e2dddc2 100644 --- a/release-plugin/src/main/kotlin/io/github/simonhauck/release/tasks/CommitAndTagTask.kt +++ b/release-plugin/src/main/kotlin/io/github/simonhauck/release/tasks/CommitAndTagTask.kt @@ -3,17 +3,16 @@ package io.github.simonhauck.release.tasks import arrow.core.fold import io.github.simonhauck.release.file.internal.PropertiesFileUtil import io.github.simonhauck.release.git.api.RevertCommand +import java.io.File import org.gradle.api.file.RegularFileProperty +import org.gradle.api.provider.ListProperty import org.gradle.api.provider.Property -import org.gradle.api.tasks.Input -import org.gradle.api.tasks.InputFile -import org.gradle.api.tasks.Optional -import org.gradle.api.tasks.TaskAction +import org.gradle.api.tasks.* abstract class CommitAndTagTask : BaseReleaseTask(), GitTask { @get:Input abstract val commitMessage: Property - @get:Input @get:Optional abstract val gitAddFilePattern: Property + @get:InputFiles abstract val gitAddFilePattern: ListProperty @get:Input @get:Optional abstract val commitMessagePrefix: Property @get:Input @get:Optional abstract val tagName: Property @get:Input @get:Optional abstract val tagPrefix: Property @@ -33,22 +32,24 @@ abstract class CommitAndTagTask : BaseReleaseTask(), GitTask { val commitMessage = commitMessage.get().replaceVariables(templateVariables) val commitPrefix = commitMessagePrefix.getOrElse("").replaceVariables(templateVariables) - val gitAddPattern = gitAddFilePattern.getOrElse(".").replaceVariables(templateVariables) val tagName = tagName.getOrElse("").replaceVariables(templateVariables) val tagPrefix = tagPrefix.getOrElse("").replaceVariables(templateVariables) val tagMessage = tagMessage.getOrElse(commitMessage).replaceVariables(templateVariables) val tagMessagePrefix = tagMessagePrefix.getOrElse("").replaceVariables(templateVariables) - gitCommandApi() - .add(gitAddPattern) - .onRight { - gitCommandHistoryApi - .get() - .registerRevertCommand(buildGitAddRevertCommand(gitAddPattern)) - } - .onLeft { gitCommandHistoryApi.get().revertAllCommands() } - .getOrThrowGradleException() + gitAddFilePattern.get().forEach { + val filePath = it.absolutePath + gitCommandApi() + .add(filePath) + .onRight { + gitCommandHistoryApi + .get() + .registerRevertCommand(buildGitAddRevertCommand(filePath)) + } + .onLeft { gitCommandHistoryApi.get().revertAllCommands() } + .getOrThrowGradleException() + } gitCommandApi() .commit("$commitPrefix$commitMessage") diff --git a/release-plugin/src/test/kotlin/io/github/simonhauck/release/tasks/CommitAndTagTaskTest.kt b/release-plugin/src/test/kotlin/io/github/simonhauck/release/tasks/CommitAndTagTaskTest.kt index aae114f..ac997b4 100644 --- a/release-plugin/src/test/kotlin/io/github/simonhauck/release/tasks/CommitAndTagTaskTest.kt +++ b/release-plugin/src/test/kotlin/io/github/simonhauck/release/tasks/CommitAndTagTaskTest.kt @@ -15,7 +15,7 @@ class CommitAndTagTaskTest { private val testDriver = ReleasePluginTestDriver() @Test - fun `should add, commit and tag the selected files`() = + fun `should add, commit and tag the selected file`() = testDriver(tmpDir) { createValidGitRepository() @@ -26,7 +26,7 @@ class CommitAndTagTaskTest { """ |tasks.register("commitAndTag") { | commitMessage.set("new commit") - | gitAddFilePattern.set("newFile.txt") + | gitAddFilePattern.set(listOf("newFile.txt")) | commitMessagePrefix.set("feat: ") | tagName.set("1.0.0") | tagPrefix.set("v") @@ -48,6 +48,33 @@ class CommitAndTagTaskTest { .contains("feat: new commit") } + @Test + fun `should add and commit multiple files`() { + testDriver(tmpDir) { + createValidGitRepository() + + File(tmpDir, "newFile.txt").writeText("Hello World") + File(tmpDir, "otherFile.txt").writeText("Hello World") + + appendContentToBuildGradle( + """ + |tasks.register("commitAndTag") { + | commitMessage.set("new commit") + | gitAddFilePattern.set(listOf(file("newFile.txt"), file("otherFile.txt")) + |} + """ + .trimMargin() + ) + + val runner = testKitRunner().withArguments("commitAndTag").build() + + val actual = runner.task(":commitAndTag")?.outcome + + assertThat(actual).isEqualTo(TaskOutcome.SUCCESS) + assertThat(gitCommandApi.status().assertIsOk().untracked).isEmpty() + } + } + @Test fun `should not tag the commit if tagName is not set`() = testDriver(tmpDir) { @@ -59,7 +86,7 @@ class CommitAndTagTaskTest { """ |tasks.register("commitAndTag") { | commitMessage.set("new commit") - | gitAddFilePattern.set("newFile.txt") + | gitAddFilePattern.set(listOf(file("newFile.txt"))) |} """ .trimMargin() @@ -85,7 +112,7 @@ class CommitAndTagTaskTest { """ |tasks.register("commitAndTag") { | commitMessage.set("new commit") - | gitAddFilePattern.set("newFile.txt") + | gitAddFilePattern.set(listOf("newFile.txt")) | tagName.set("v1.0.0") |} """