Skip to content

Commit 813e0e2

Browse files
committed
allow overriding the project version with -Pversion=1.2.3, and add a fallback if there's no git repo
1 parent 0e2829d commit 813e0e2

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

settings.gradle.kts

+15-2
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,17 @@ val currentCommitHash: Provider<String> =
9696
isIgnoreExitValue = true
9797
}.standardOutput.asText.map { it.trim() }
9898

99+
/**
100+
* The standard Gradle way of setting the version, which can be set on the CLI with
101+
*
102+
* ```shell
103+
* ./gradlew -Pversion=1.2.3
104+
* ```
105+
*
106+
* This can be used to override [gitVersion].
107+
*/
108+
val standardVersion: Provider<String> = providers.gradleProperty("version")
109+
99110
/** Match simple SemVer tags. The first group is the `major.minor.patch` digits. */
100111
val semverRegex = Regex("""v((?:0|[1-9][0-9]*)\.(?:0|[1-9][0-9]*)\.(?:0|[1-9][0-9]*))""")
101112

@@ -110,11 +121,13 @@ val gitVersion: Provider<String> =
110121
val head = descriptions.singleOrNull() ?: ""
111122
// drop the leading `v`, try to find the `major.minor.patch` digits group
112123
val headVersion = semverRegex.matchEntire(head)?.groupValues?.last()
113-
headVersion ?: currentCommitHash.get() // fall back to using the git commit hash
124+
headVersion
125+
?: currentCommitHash.orNull // fall back to using the git commit hash
126+
?: "unknown" // just in case there's no git repo, e.g. someone downloaded a zip archive
114127
}
115128
}
116129

117130
gradle.allprojects {
118-
extensions.add<Provider<String>>("gitVersion", gitVersion)
131+
extensions.add<Provider<String>>("gitVersion", standardVersion.orElse(gitVersion))
119132
}
120133
//endregion

0 commit comments

Comments
 (0)