|
| 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