Skip to content

Commit

Permalink
Add mapVersion which expose GitData to semver extension
Browse files Browse the repository at this point in the history
  • Loading branch information
JavierSegoviaCordoba committed Dec 6, 2023
1 parent a64fe21 commit 7a9cb9c
Show file tree
Hide file tree
Showing 9 changed files with 192 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

### Added

- `mapVersion` which expose `GitData` to `semver` extension

### Changed

### Deprecated
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 <init> (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 <init> (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 <init> (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 <init> (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 <init> ()V
public synthetic fun apply (Ljava/lang/Object;)V
Expand Down Expand Up @@ -151,4 +214,3 @@ public final class com/javiersc/semver/project/gradle/plugin/tasks/WriteSemverTa
public fun <init> (Lkotlin/jvm/functions/Function1;)V
public final synthetic fun isSatisfiedBy (Ljava/lang/Object;)Z
}

Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<String> =
calculatedVersion.map { version -> transform(version, gitData) }
version.set(mappedVersion)
onMapVersion?.execute(Unit)
}

internal fun onMapVersion(action: Action<Unit>) {
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<Commit>,
val tags: List<Tag>,
)
}

public companion object {

public const val ExtensionName: String = "semver"
Expand All @@ -72,3 +120,53 @@ constructor(

internal val Project.semverExtension: SemverExtension
get() = extensions.getByType()

private fun Git.buildGitData(tagPrefix: Property<String>): 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,
),
)
},
),
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
build -Psemver.tagPrefix=v
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
plugins {

Check notice on line 1 in semver-project-gradle-plugin/testFunctional/resources/version-mapping/v1_0_0 to v1_0_0+2_0_0-develop/build.gradle.kts

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Package name does not match containing directory

Package directive does not match the file location
id("com.javiersc.semver.project")
}

semver {
tagPrefix.set("v")
mapVersion { gradleVersion, git ->
"${gradleVersion.copy(metadata = "2.0.0-${git.branch.name}")}"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
1.0.0+2.0.0-develop
v1.0.0+2.0.0-develop
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v0.9.0
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rootProject.name = "sandbox-project"

Check notice on line 1 in semver-project-gradle-plugin/testFunctional/resources/version-mapping/v1_0_0 to v1_0_0+2_0_0-develop/settings.gradle.kts

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Package name does not match containing directory

Package directive does not match the file location

0 comments on commit 7a9cb9c

Please sign in to comment.