|
| 1 | +import java.io.File |
| 2 | + |
| 3 | +/** |
| 4 | + * Resolves the project version from the current git state. |
| 5 | + * |
| 6 | + * The resolution rules, in order: |
| 7 | + * 1. **Release** — if HEAD is tagged with `vX.Y.Z[...]`, use the tag with the `v` prefix stripped |
| 8 | + * (e.g. `v4.6.0` → `4.6.0`). Produces a non-SNAPSHOT version, which triggers artifact signing in |
| 9 | + * `cqf.publishing-conventions`. |
| 10 | + * 2. **main SNAPSHOT** — if on the `main` branch with no tag at HEAD, take the most recent `vX.Y.Z` |
| 11 | + * tag reachable from HEAD, bump the minor component, reset patch to 0, and append `-SNAPSHOT` |
| 12 | + * (e.g. last tag `v4.6.0` → `4.7.0-SNAPSHOT`). |
| 13 | + * 3. **Branch SNAPSHOT** — otherwise, append a branch identifier and the short SHA to the bumped |
| 14 | + * base (e.g. `4.7.0-feature-xyz-5ac419a5d-SNAPSHOT`). Branch name is taken from |
| 15 | + * `GITHUB_REF_NAME` if set (the GitHub Actions convention), else from `git rev-parse`; a |
| 16 | + * detached HEAD falls back to the literal `detached`. |
| 17 | + * |
| 18 | + * Fallbacks keep the build working in edge cases: no tags anywhere → base `0.0.0` (bumps to |
| 19 | + * `0.1.0`); `git` invocation failure → empty string, which the callers treat as "no data." |
| 20 | + * |
| 21 | + * The `-SNAPSHOT` suffix on non-release builds is load-bearing: the maven-publish convention checks |
| 22 | + * `version.endsWith("SNAPSHOT")` to decide whether to sign and which repository to target. |
| 23 | + */ |
| 24 | +fun gitVersion(rootDir: File): String { |
| 25 | + val exactTag = |
| 26 | + runGit(rootDir, "tag", "--points-at", "HEAD") |
| 27 | + .lineSequence() |
| 28 | + .map { it.trim() } |
| 29 | + .firstOrNull { it.matches(Regex("v\\d+\\.\\d+\\.\\d+.*")) } |
| 30 | + if (exactTag != null) { |
| 31 | + return exactTag.removePrefix("v") |
| 32 | + } |
| 33 | + |
| 34 | + val lastTag = |
| 35 | + runGit(rootDir, "describe", "--tags", "--abbrev=0", "--match=v*.*.*").trim().takeIf { |
| 36 | + it.isNotEmpty() |
| 37 | + } |
| 38 | + val baseVersion = lastTag?.removePrefix("v") ?: "0.0.0" |
| 39 | + val (major, minor, _) = parseVersion(baseVersion) |
| 40 | + val bumped = "$major.${minor + 1}.0" |
| 41 | + |
| 42 | + val branch = detectBranch(rootDir) |
| 43 | + if (branch == "main") { |
| 44 | + return "$bumped-SNAPSHOT" |
| 45 | + } |
| 46 | + |
| 47 | + val shortSha = runGit(rootDir, "rev-parse", "--short", "HEAD").trim().ifEmpty { "nosha" } |
| 48 | + val sanitized = sanitizeBranch(branch) |
| 49 | + return "$bumped-$sanitized-$shortSha-SNAPSHOT" |
| 50 | +} |
| 51 | + |
| 52 | +/** Parses `X.Y.Z` out of a version string, ignoring any `-suffix`. Missing parts default to 0. */ |
| 53 | +private fun parseVersion(v: String): Triple<Int, Int, Int> { |
| 54 | + val core = v.split("-", limit = 2)[0].split(".") |
| 55 | + return Triple( |
| 56 | + core.getOrNull(0)?.toIntOrNull() ?: 0, |
| 57 | + core.getOrNull(1)?.toIntOrNull() ?: 0, |
| 58 | + core.getOrNull(2)?.toIntOrNull() ?: 0, |
| 59 | + ) |
| 60 | +} |
| 61 | + |
| 62 | +/** |
| 63 | + * Picks the branch name. `GITHUB_REF_NAME` is preferred because GitHub Actions often checks out in |
| 64 | + * detached-HEAD mode where `git rev-parse --abbrev-ref HEAD` only yields `HEAD`. |
| 65 | + */ |
| 66 | +private fun detectBranch(rootDir: File): String { |
| 67 | + System.getenv("GITHUB_REF_NAME") |
| 68 | + ?.takeIf { it.isNotBlank() } |
| 69 | + ?.let { |
| 70 | + return it |
| 71 | + } |
| 72 | + val branch = runGit(rootDir, "rev-parse", "--abbrev-ref", "HEAD").trim() |
| 73 | + return if (branch.isBlank() || branch == "HEAD") "detached" else branch |
| 74 | +} |
| 75 | + |
| 76 | +/** |
| 77 | + * Makes a branch name safe to embed in a Maven version string: replaces runs of non-alphanumerics |
| 78 | + * with `-`, trims leading/trailing separators, and caps length at 40 chars. |
| 79 | + */ |
| 80 | +private fun sanitizeBranch(branch: String): String { |
| 81 | + val cleaned = branch.replace(Regex("[^A-Za-z0-9]+"), "-").trim('-') |
| 82 | + return cleaned.ifEmpty { "branch" }.take(40).trimEnd('-') |
| 83 | +} |
| 84 | + |
| 85 | +/** Runs `git <args>` in [workingDir]. Returns stdout on success, empty string on any failure. */ |
| 86 | +private fun runGit(workingDir: File, vararg args: String): String { |
| 87 | + return try { |
| 88 | + val proc = |
| 89 | + ProcessBuilder(listOf("git") + args.toList()) |
| 90 | + .directory(workingDir) |
| 91 | + .redirectErrorStream(false) |
| 92 | + .start() |
| 93 | + val out = proc.inputStream.bufferedReader().readText() |
| 94 | + proc.errorStream.bufferedReader().readText() |
| 95 | + if (proc.waitFor() == 0) out else "" |
| 96 | + } catch (_: Exception) { |
| 97 | + "" |
| 98 | + } |
| 99 | +} |
0 commit comments