Skip to content

Commit 12d677e

Browse files
committed
chore: Replace shipkit-auto-version by custom version plugin
1 parent ac35199 commit 12d677e

File tree

4 files changed

+170
-4
lines changed

4 files changed

+170
-4
lines changed

build.gradle

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ plugins {
66
id 'com.github.spotbugs' version '5.0.14'
77
id 'de.thetaphi.forbiddenapis' version '3.8'
88

9-
id 'org.shipkit.shipkit-auto-version' version '2.1.2'
9+
id 'tracer-version'
1010
id 'io.github.gradle-nexus.publish-plugin' version '2.0.0'
1111

1212
id 'com.gradleup.shadow' version '8.3.6' apply false
@@ -54,11 +54,8 @@ apply from: "$rootDir/gradle/spotless.gradle"
5454

5555
def compileTask = tasks.register("compile")
5656

57-
def repoVersion = version
58-
5957
allprojects {
6058
group = 'com.datadoghq'
61-
version = repoVersion
6259

6360
if (isCI) {
6461
buildDir = "$rootDir/workspace/${projectDir.path.replace(rootDir.path, '')}/build/"

buildSrc/build.gradle.kts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ gradlePlugin {
2525
id = "call-site-instrumentation"
2626
implementationClass = "datadog.gradle.plugin.CallSiteInstrumentationPlugin"
2727
}
28+
create("tracer-version-plugin") {
29+
id = "tracer-version"
30+
implementationClass = "datadog.gradle.plugin.version.TracerVersionPlugin"
31+
}
2832
}
2933
}
3034

@@ -42,6 +46,8 @@ dependencies {
4246
implementation("org.eclipse.aether", "aether-transport-http", "1.1.0")
4347
implementation("org.apache.maven", "maven-aether-provider", "3.3.9")
4448

49+
implementation("com.github.zafarkhaja:java-semver:0.10.2")
50+
4551
implementation("com.google.guava", "guava", "20.0")
4652
implementation("org.ow2.asm", "asm", "9.8")
4753
implementation("org.ow2.asm", "asm-tree", "9.8")
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package datadog.gradle.plugin.version
2+
3+
import org.gradle.api.GradleException
4+
import org.gradle.api.file.DirectoryProperty
5+
import org.gradle.api.provider.Property
6+
import org.gradle.api.provider.ValueSource
7+
import org.gradle.api.provider.ValueSourceParameters
8+
import org.gradle.process.ExecOperations
9+
import org.gradle.process.ExecResult
10+
import java.io.ByteArrayOutputStream
11+
import java.nio.charset.Charset
12+
import javax.inject.Inject
13+
14+
abstract class GitDescribeValueSource @Inject constructor(
15+
private val execOperations: ExecOperations
16+
) : ValueSource<String, GitDescribeValueSource.Parameters> {
17+
override fun obtain(): String {
18+
val workDir = parameters.workingDirectory.get()
19+
val tagPrefix = parameters.tagVersionPrefix.get()
20+
21+
val commandsArray = mutableListOf(
22+
"git",
23+
"describe",
24+
"--abbrev=8",
25+
"--tags",
26+
"--first-parent",
27+
"--match=$tagPrefix[0-9].[0-9]*.[0-9]",
28+
)
29+
if (parameters.showDirty.get()) {
30+
commandsArray.add("--dirty")
31+
}
32+
33+
val outputStream = ByteArrayOutputStream()
34+
val result = try {
35+
execOperations.exec {
36+
commandLine(commandsArray)
37+
workingDir(workDir)
38+
standardOutput = outputStream
39+
errorOutput = outputStream
40+
}
41+
} catch (e: Exception) {
42+
throw GradleException("Failed to run: ${commandsArray.joinToString(" ")}", e)
43+
}
44+
45+
val output = outputStream.toString(Charset.defaultCharset().name())
46+
result.exitValue.let { exitValue ->
47+
if (exitValue != 0) {
48+
throw GradleException(
49+
"""
50+
Failed to run: ${commandsArray.joinToString(" ")}
51+
(exit code: $exitValue)
52+
Output:
53+
$output
54+
""".trimIndent()
55+
)
56+
}
57+
}
58+
59+
return output
60+
}
61+
62+
interface Parameters : ValueSourceParameters {
63+
val tagVersionPrefix: Property<String>
64+
val showDirty: Property<Boolean>
65+
val workingDirectory: DirectoryProperty
66+
}
67+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package datadog.gradle.plugin.version
2+
3+
import com.github.zafarkhaja.semver.Version
4+
import org.gradle.api.Plugin
5+
import org.gradle.api.Project
6+
import org.gradle.api.logging.Logging
7+
import org.gradle.api.model.ObjectFactory
8+
import org.gradle.api.provider.ProviderFactory
9+
import org.gradle.kotlin.dsl.property
10+
import javax.inject.Inject
11+
12+
class TracerVersionPlugin @Inject constructor(
13+
private val providerFactory: ProviderFactory,
14+
) : Plugin<Project> {
15+
private val logger = Logging.getLogger(TracerVersionPlugin::class.java)
16+
17+
override fun apply(targetProject: Project) {
18+
if (targetProject.rootProject != targetProject) {
19+
throw IllegalStateException("Only root project can apply plugin")
20+
}
21+
22+
val extension = targetProject.extensions.create("tracerVersion", TracerVersionExtension::class.java)
23+
val versionProvider = versionProvider(targetProject, extension)
24+
25+
targetProject.allprojects {
26+
version = versionProvider
27+
}
28+
}
29+
30+
private fun versionProvider(
31+
targetProject: Project,
32+
extension: TracerVersionExtension
33+
): String {
34+
val workingDirectory = targetProject.projectDir
35+
36+
val buildVersion: String = if (!workingDirectory.resolve(".git").exists()) {
37+
extension.defaultVersion.get()
38+
} else {
39+
providerFactory.of(GitDescribeValueSource::class.java) {
40+
parameters {
41+
this.tagVersionPrefix.set(extension.tagVersionPrefix)
42+
this.showDirty.set(extension.detectDirty)
43+
this.workingDirectory.set(workingDirectory)
44+
}
45+
}.map {
46+
toTracerVersion(it.trim(), extension)
47+
}.get()
48+
}
49+
50+
logger.lifecycle("Tracer build version: {}", buildVersion)
51+
return buildVersion
52+
}
53+
54+
private fun toTracerVersion(describeString: String, extension: TracerVersionExtension): String {
55+
logger.info("Git describe output: {}", describeString)
56+
57+
val tagPrefix = extension.tagVersionPrefix.get()
58+
val tagRegex = Regex("$tagPrefix(\\d+\\.\\d+\\.\\d+)(.*)")
59+
val matchResult = tagRegex.find(describeString)
60+
?: return extension.defaultVersion.get()
61+
62+
val (lastTagVersion, describeTrailer) = matchResult.destructured
63+
val hasLaterCommits = describeTrailer.isNotBlank()
64+
val version = Version.parse(lastTagVersion).let {
65+
if (hasLaterCommits) {
66+
it.nextMinorVersion()
67+
} else {
68+
it
69+
}
70+
}
71+
72+
return buildString {
73+
append(tagPrefix)
74+
append(version.toString())
75+
76+
if (hasLaterCommits) {
77+
append(if (extension.useSnapshot.get()) "-SNAPSHOT" else describeTrailer)
78+
}
79+
80+
if (describeTrailer.endsWith("-dirty")) {
81+
append("-dirty")
82+
}
83+
}
84+
}
85+
86+
open class TracerVersionExtension @Inject constructor(objectFactory: ObjectFactory) {
87+
val defaultVersion = objectFactory.property(String::class)
88+
.convention("0.1.0-SNAPSHOT")
89+
val tagVersionPrefix = objectFactory.property(String::class)
90+
.convention("v")
91+
val useSnapshot = objectFactory.property(Boolean::class)
92+
.convention(true)
93+
val detectDirty = objectFactory.property(Boolean::class)
94+
.convention(false)
95+
}
96+
}

0 commit comments

Comments
 (0)