From 7a9cb9c641d49b7e51e519f9716cb66dc1533e50 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javier=20Segovia=20C=C3=B3rdoba?= Date: Wed, 6 Dec 2023 15:54:36 +0100 Subject: [PATCH] Add `mapVersion` which expose `GitData` to `semver` extension --- CHANGELOG.md | 2 + .../api/semver-project-gradle-plugin.api | 64 +++++++++++- .../project/gradle/plugin/SemverExtension.kt | 98 +++++++++++++++++++ .../gradle/plugin/VersionMappingTest.kt | 14 +++ .../ARGUMENTS.txt | 1 + .../build.gradle.kts | 10 ++ .../expect-version.txt | 2 + .../last-tag.txt | 1 + .../settings.gradle.kts | 1 + 9 files changed, 192 insertions(+), 1 deletion(-) create mode 100644 semver-project-gradle-plugin/testFunctional/resources/version-mapping/v1_0_0 to v1_0_0+2_0_0-develop/ARGUMENTS.txt create mode 100644 semver-project-gradle-plugin/testFunctional/resources/version-mapping/v1_0_0 to v1_0_0+2_0_0-develop/build.gradle.kts create mode 100644 semver-project-gradle-plugin/testFunctional/resources/version-mapping/v1_0_0 to v1_0_0+2_0_0-develop/expect-version.txt create mode 100644 semver-project-gradle-plugin/testFunctional/resources/version-mapping/v1_0_0 to v1_0_0+2_0_0-develop/last-tag.txt create mode 100644 semver-project-gradle-plugin/testFunctional/resources/version-mapping/v1_0_0 to v1_0_0+2_0_0-develop/settings.gradle.kts diff --git a/CHANGELOG.md b/CHANGELOG.md index 5f1837b3..44731a5e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ ### Added +- `mapVersion` which expose `GitData` to `semver` extension + ### Changed ### Deprecated diff --git a/semver-project-gradle-plugin/api/semver-project-gradle-plugin.api b/semver-project-gradle-plugin/api/semver-project-gradle-plugin.api index 091e2bcc..72d8f82c 100644 --- a/semver-project-gradle-plugin/api/semver-project-gradle-plugin.api +++ b/semver-project-gradle-plugin/api/semver-project-gradle-plugin.api @@ -28,11 +28,74 @@ public abstract class com/javiersc/semver/project/gradle/plugin/SemverExtension public final fun getVersion ()Lorg/gradle/api/provider/Property; public final fun isEnabled ()Lorg/gradle/api/provider/Property; public final fun mapVersion (Lkotlin/jvm/functions/Function1;)V + public final fun mapVersion (Lkotlin/jvm/functions/Function2;)V } public final class com/javiersc/semver/project/gradle/plugin/SemverExtension$Companion { } +public final class com/javiersc/semver/project/gradle/plugin/SemverExtension$GitData { + public fun (Lcom/javiersc/semver/project/gradle/plugin/SemverExtension$GitData$Commit;Lcom/javiersc/semver/project/gradle/plugin/SemverExtension$GitData$Tag;Lcom/javiersc/semver/project/gradle/plugin/SemverExtension$GitData$Branch;)V + public final fun component1 ()Lcom/javiersc/semver/project/gradle/plugin/SemverExtension$GitData$Commit; + public final fun component2 ()Lcom/javiersc/semver/project/gradle/plugin/SemverExtension$GitData$Tag; + public final fun component3 ()Lcom/javiersc/semver/project/gradle/plugin/SemverExtension$GitData$Branch; + public final fun copy (Lcom/javiersc/semver/project/gradle/plugin/SemverExtension$GitData$Commit;Lcom/javiersc/semver/project/gradle/plugin/SemverExtension$GitData$Tag;Lcom/javiersc/semver/project/gradle/plugin/SemverExtension$GitData$Branch;)Lcom/javiersc/semver/project/gradle/plugin/SemverExtension$GitData; + public static synthetic fun copy$default (Lcom/javiersc/semver/project/gradle/plugin/SemverExtension$GitData;Lcom/javiersc/semver/project/gradle/plugin/SemverExtension$GitData$Commit;Lcom/javiersc/semver/project/gradle/plugin/SemverExtension$GitData$Tag;Lcom/javiersc/semver/project/gradle/plugin/SemverExtension$GitData$Branch;ILjava/lang/Object;)Lcom/javiersc/semver/project/gradle/plugin/SemverExtension$GitData; + public fun equals (Ljava/lang/Object;)Z + public final fun getBranch ()Lcom/javiersc/semver/project/gradle/plugin/SemverExtension$GitData$Branch; + public final fun getCommit ()Lcom/javiersc/semver/project/gradle/plugin/SemverExtension$GitData$Commit; + public final fun getTag ()Lcom/javiersc/semver/project/gradle/plugin/SemverExtension$GitData$Tag; + public fun hashCode ()I + public fun toString ()Ljava/lang/String; +} + +public final class com/javiersc/semver/project/gradle/plugin/SemverExtension$GitData$Branch { + public fun (Ljava/lang/String;Ljava/lang/String;Ljava/util/List;Ljava/util/List;)V + public final fun component1 ()Ljava/lang/String; + public final fun component2 ()Ljava/lang/String; + public final fun component3 ()Ljava/util/List; + public final fun component4 ()Ljava/util/List; + public final fun copy (Ljava/lang/String;Ljava/lang/String;Ljava/util/List;Ljava/util/List;)Lcom/javiersc/semver/project/gradle/plugin/SemverExtension$GitData$Branch; + public static synthetic fun copy$default (Lcom/javiersc/semver/project/gradle/plugin/SemverExtension$GitData$Branch;Ljava/lang/String;Ljava/lang/String;Ljava/util/List;Ljava/util/List;ILjava/lang/Object;)Lcom/javiersc/semver/project/gradle/plugin/SemverExtension$GitData$Branch; + public fun equals (Ljava/lang/Object;)Z + public final fun getCommits ()Ljava/util/List; + public final fun getName ()Ljava/lang/String; + public final fun getRefName ()Ljava/lang/String; + public final fun getTags ()Ljava/util/List; + public fun hashCode ()I + public fun toString ()Ljava/lang/String; +} + +public final class com/javiersc/semver/project/gradle/plugin/SemverExtension$GitData$Commit { + public fun (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V + public final fun component1 ()Ljava/lang/String; + public final fun component2 ()Ljava/lang/String; + public final fun component3 ()Ljava/lang/String; + public final fun copy (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)Lcom/javiersc/semver/project/gradle/plugin/SemverExtension$GitData$Commit; + public static synthetic fun copy$default (Lcom/javiersc/semver/project/gradle/plugin/SemverExtension$GitData$Commit;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;ILjava/lang/Object;)Lcom/javiersc/semver/project/gradle/plugin/SemverExtension$GitData$Commit; + public fun equals (Ljava/lang/Object;)Z + public final fun getFullMessage ()Ljava/lang/String; + public final fun getHash ()Ljava/lang/String; + public final fun getMessage ()Ljava/lang/String; + public fun hashCode ()I + public fun toString ()Ljava/lang/String; +} + +public final class com/javiersc/semver/project/gradle/plugin/SemverExtension$GitData$Tag { + public fun (Ljava/lang/String;Ljava/lang/String;Lcom/javiersc/semver/project/gradle/plugin/SemverExtension$GitData$Commit;)V + public final fun component1 ()Ljava/lang/String; + public final fun component2 ()Ljava/lang/String; + public final fun component3 ()Lcom/javiersc/semver/project/gradle/plugin/SemverExtension$GitData$Commit; + public final fun copy (Ljava/lang/String;Ljava/lang/String;Lcom/javiersc/semver/project/gradle/plugin/SemverExtension$GitData$Commit;)Lcom/javiersc/semver/project/gradle/plugin/SemverExtension$GitData$Tag; + public static synthetic fun copy$default (Lcom/javiersc/semver/project/gradle/plugin/SemverExtension$GitData$Tag;Ljava/lang/String;Ljava/lang/String;Lcom/javiersc/semver/project/gradle/plugin/SemverExtension$GitData$Commit;ILjava/lang/Object;)Lcom/javiersc/semver/project/gradle/plugin/SemverExtension$GitData$Tag; + public fun equals (Ljava/lang/Object;)Z + public final fun getCommit ()Lcom/javiersc/semver/project/gradle/plugin/SemverExtension$GitData$Commit; + public final fun getName ()Ljava/lang/String; + public final fun getRefName ()Ljava/lang/String; + public fun hashCode ()I + public fun toString ()Ljava/lang/String; +} + public final class com/javiersc/semver/project/gradle/plugin/SemverProjectPlugin : org/gradle/api/Plugin { public fun ()V public synthetic fun apply (Ljava/lang/Object;)V @@ -151,4 +214,3 @@ public final class com/javiersc/semver/project/gradle/plugin/tasks/WriteSemverTa public fun (Lkotlin/jvm/functions/Function1;)V public final synthetic fun isSatisfiedBy (Ljava/lang/Object;)Z } - diff --git a/semver-project-gradle-plugin/main/kotlin/com/javiersc/semver/project/gradle/plugin/SemverExtension.kt b/semver-project-gradle-plugin/main/kotlin/com/javiersc/semver/project/gradle/plugin/SemverExtension.kt index 470cd5af..483fe1bb 100644 --- a/semver-project-gradle-plugin/main/kotlin/com/javiersc/semver/project/gradle/plugin/SemverExtension.kt +++ b/semver-project-gradle-plugin/main/kotlin/com/javiersc/semver/project/gradle/plugin/SemverExtension.kt @@ -3,7 +3,11 @@ package com.javiersc.semver.project.gradle.plugin import com.javiersc.gradle.version.GradleVersion import com.javiersc.semver.project.gradle.plugin.internal.DefaultTagPrefix import com.javiersc.semver.project.gradle.plugin.internal.git.GitCache +import com.javiersc.semver.project.gradle.plugin.internal.git.currentBranch +import com.javiersc.semver.project.gradle.plugin.internal.git.headCommit +import com.javiersc.semver.project.gradle.plugin.internal.git.lastVersionTagInCurrentBranch import javax.inject.Inject +import org.eclipse.jgit.api.Git import org.gradle.api.Action import org.gradle.api.Project import org.gradle.api.file.RegularFileProperty @@ -54,10 +58,54 @@ constructor( onMapVersion?.execute(Unit) } + public fun mapVersion(transform: (version: GradleVersion, git: GitData) -> String) { + val cache = GitCache(gitDir = gitDir.get().asFile, maxCount = commitsMaxCount) + val gitData = cache.git.buildGitData(tagPrefix) + val mappedVersion: Provider = + calculatedVersion.map { version -> transform(version, gitData) } + version.set(mappedVersion) + onMapVersion?.execute(Unit) + } + internal fun onMapVersion(action: Action) { onMapVersion = action } + /** + * @property commit The commit information. + * @property tag The tag information, if available. + * @property branch The branch information. + */ + public data class GitData(val commit: Commit, val tag: Tag?, val branch: Branch) { + + /** + * @property message The commit message. + * @property fullMessage The full commit message, including any additional details. + * @property hash The unique hash that identifies the commit. + */ + public data class Commit(val message: String, val fullMessage: String, val hash: String) + + /** + * @property name The name of the tag. + * @property refName The reference name of the tag. + * @property commit The commit associated with the tag. + */ + public data class Tag(val name: String, val refName: String, val commit: Commit) + + /** + * @property name The name of the branch. Example: `main`. + * @property refName The reference name of the branch. Example: `refs/heads/main`. + * @property commits The list of commits in the branch. + * @property tags The list of tags in the branch. + */ + public data class Branch( + val name: String, + val refName: String, + val commits: List, + val tags: List, + ) + } + public companion object { public const val ExtensionName: String = "semver" @@ -72,3 +120,53 @@ constructor( internal val Project.semverExtension: SemverExtension get() = extensions.getByType() + +private fun Git.buildGitData(tagPrefix: Property): SemverExtension.GitData { + return SemverExtension.GitData( + tag = + lastVersionTagInCurrentBranch(tagPrefix.get())?.let { tag -> + SemverExtension.GitData.Tag( + name = tag.name, + refName = tag.refName, + commit = + SemverExtension.GitData.Commit( + message = tag.commit.message, + fullMessage = tag.commit.fullMessage, + hash = tag.commit.hash, + ), + ) + }, + commit = + SemverExtension.GitData.Commit( + message = headCommit.commit.message, + fullMessage = headCommit.commit.fullMessage, + hash = headCommit.commit.hash, + ), + branch = + SemverExtension.GitData.Branch( + name = currentBranch.name, + refName = currentBranch.refName, + commits = + currentBranch.commits.map { commit -> + SemverExtension.GitData.Commit( + message = commit.message, + fullMessage = commit.fullMessage, + hash = commit.hash, + ) + }, + tags = + currentBranch.tags.map { tag -> + SemverExtension.GitData.Tag( + name = tag.name, + refName = tag.refName, + commit = + SemverExtension.GitData.Commit( + message = tag.commit.message, + fullMessage = tag.commit.fullMessage, + hash = tag.commit.hash, + ), + ) + }, + ), + ) +} diff --git a/semver-project-gradle-plugin/testFunctional/kotlin/com/javiersc/semver/project/gradle/plugin/VersionMappingTest.kt b/semver-project-gradle-plugin/testFunctional/kotlin/com/javiersc/semver/project/gradle/plugin/VersionMappingTest.kt index bad2e9ab..9c222808 100644 --- a/semver-project-gradle-plugin/testFunctional/kotlin/com/javiersc/semver/project/gradle/plugin/VersionMappingTest.kt +++ b/semver-project-gradle-plugin/testFunctional/kotlin/com/javiersc/semver/project/gradle/plugin/VersionMappingTest.kt @@ -17,4 +17,18 @@ internal class VersionMappingTest : GradleTestKitTest() { projectDir.assertVersionFromExpectVersionFiles() } } + + @Test + fun `v1_0_0 to v1_0_0+2_0_0-develop`() { + gradleTestKitTest("version-mapping/v1_0_0 to v1_0_0+2_0_0-develop") { + projectDir.generateInitialCommitAddVersionTagAndAddNewCommit() + git.branchCreate().setName("develop").call() + git.checkout().setName("develop").call() + git.tag().setObjectId(git.headRevCommitInBranch).setName("v1.0.0").call() + + withArgumentsFromTXT() + build() + projectDir.assertVersionFromExpectVersionFiles() + } + } } diff --git a/semver-project-gradle-plugin/testFunctional/resources/version-mapping/v1_0_0 to v1_0_0+2_0_0-develop/ARGUMENTS.txt b/semver-project-gradle-plugin/testFunctional/resources/version-mapping/v1_0_0 to v1_0_0+2_0_0-develop/ARGUMENTS.txt new file mode 100644 index 00000000..a638d59a --- /dev/null +++ b/semver-project-gradle-plugin/testFunctional/resources/version-mapping/v1_0_0 to v1_0_0+2_0_0-develop/ARGUMENTS.txt @@ -0,0 +1 @@ +build -Psemver.tagPrefix=v diff --git a/semver-project-gradle-plugin/testFunctional/resources/version-mapping/v1_0_0 to v1_0_0+2_0_0-develop/build.gradle.kts b/semver-project-gradle-plugin/testFunctional/resources/version-mapping/v1_0_0 to v1_0_0+2_0_0-develop/build.gradle.kts new file mode 100644 index 00000000..c105203c --- /dev/null +++ b/semver-project-gradle-plugin/testFunctional/resources/version-mapping/v1_0_0 to v1_0_0+2_0_0-develop/build.gradle.kts @@ -0,0 +1,10 @@ +plugins { + id("com.javiersc.semver.project") +} + +semver { + tagPrefix.set("v") + mapVersion { gradleVersion, git -> + "${gradleVersion.copy(metadata = "2.0.0-${git.branch.name}")}" + } +} diff --git a/semver-project-gradle-plugin/testFunctional/resources/version-mapping/v1_0_0 to v1_0_0+2_0_0-develop/expect-version.txt b/semver-project-gradle-plugin/testFunctional/resources/version-mapping/v1_0_0 to v1_0_0+2_0_0-develop/expect-version.txt new file mode 100644 index 00000000..c5e485de --- /dev/null +++ b/semver-project-gradle-plugin/testFunctional/resources/version-mapping/v1_0_0 to v1_0_0+2_0_0-develop/expect-version.txt @@ -0,0 +1,2 @@ +1.0.0+2.0.0-develop +v1.0.0+2.0.0-develop diff --git a/semver-project-gradle-plugin/testFunctional/resources/version-mapping/v1_0_0 to v1_0_0+2_0_0-develop/last-tag.txt b/semver-project-gradle-plugin/testFunctional/resources/version-mapping/v1_0_0 to v1_0_0+2_0_0-develop/last-tag.txt new file mode 100644 index 00000000..f979adec --- /dev/null +++ b/semver-project-gradle-plugin/testFunctional/resources/version-mapping/v1_0_0 to v1_0_0+2_0_0-develop/last-tag.txt @@ -0,0 +1 @@ +v0.9.0 diff --git a/semver-project-gradle-plugin/testFunctional/resources/version-mapping/v1_0_0 to v1_0_0+2_0_0-develop/settings.gradle.kts b/semver-project-gradle-plugin/testFunctional/resources/version-mapping/v1_0_0 to v1_0_0+2_0_0-develop/settings.gradle.kts new file mode 100644 index 00000000..dc691831 --- /dev/null +++ b/semver-project-gradle-plugin/testFunctional/resources/version-mapping/v1_0_0 to v1_0_0+2_0_0-develop/settings.gradle.kts @@ -0,0 +1 @@ +rootProject.name = "sandbox-project"