From 68af3cd067381e079f15721e397e42c510c9e912 Mon Sep 17 00:00:00 2001 From: Konrad Malawski Date: Thu, 18 Jun 2026 12:53:22 +0900 Subject: [PATCH] Derive Maven version from `git describe --tags --always` SwiftKitCore and SwiftKitFFM compute their Maven version directly from the current checkout via `git describe --tags --always`. A checkout on an exact SemVer tag (e.g. `0.4.2`) publishes that version; off-tag working trees publish a tag-prefixed identifier such as `0.4.1-52-g8f0d0794`. This replaces the previous hardcoded `1.0-SNAPSHOT` so Maven coordinates always reflect the upstream commit a consumer pinned. --- .gitignore | 3 ++ BuildLogic/.gitignore | 2 +- .../org/swift/build/utils/gitVersion.kt | 47 +++++++++++++++++++ SwiftKitCore/build.gradle.kts | 8 ++-- SwiftKitFFM/build.gradle.kts | 7 +-- 5 files changed, 60 insertions(+), 7 deletions(-) create mode 100644 BuildLogic/src/main/kotlin/org/swift/build/utils/gitVersion.kt diff --git a/.gitignore b/.gitignore index 01dc9f697..4a1ca5a72 100644 --- a/.gitignore +++ b/.gitignore @@ -25,6 +25,9 @@ BuildLogic/out/ **/build/ lib/ +# Though make sure we include actual sources, even if they are ignored because e.g. /build/ in the path +!**/src/main/kotlin/** + # Ignore JVM crash logs **/*.log diff --git a/BuildLogic/.gitignore b/BuildLogic/.gitignore index 91ea7410d..605e05406 100644 --- a/BuildLogic/.gitignore +++ b/BuildLogic/.gitignore @@ -1,2 +1,2 @@ -build +/build/ .gradle diff --git a/BuildLogic/src/main/kotlin/org/swift/build/utils/gitVersion.kt b/BuildLogic/src/main/kotlin/org/swift/build/utils/gitVersion.kt new file mode 100644 index 000000000..e4279d2c4 --- /dev/null +++ b/BuildLogic/src/main/kotlin/org/swift/build/utils/gitVersion.kt @@ -0,0 +1,47 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the Swift.org open source project +// +// Copyright (c) 2026 Apple Inc. and the Swift.org project authors +// Licensed under Apache License v2.0 +// +// See LICENSE.txt for license information +// See CONTRIBUTORS.txt for the list of Swift.org project authors +// +// SPDX-License-Identifier: Apache-2.0 +// +//===----------------------------------------------------------------------===// + +package org.swift.build.utils + +import org.gradle.api.Project + +/** + * Resolve the publication version for swift-java's Java artifacts. + * + * Resolution order: + * 1. `-PswiftkitVersion=` project property, for CI/release overrides. + * 2. `git describe --tags --always`, for normal local builds with full git history. + * 3. `GITHUB_SHA` (truncated) — useful in shallow CI checkouts where git history is not available. + * 4. `0.0.0-SNAPSHOT` fallback. + * + * `git describe` is allowed to fail without breaking the build, because some CI + * environments do not have a full checkout (e.g. `fetch-depth: 1`) or any tags + * reachable, in which case the command exits with code 128. + */ +fun Project.resolveSwiftKitVersion(): String { + findProperty("swiftkitVersion")?.toString()?.takeIf { it.isNotBlank() }?.let { return it } + + val gitVersion = runCatching { + providers.exec { + commandLine("git", "describe", "--tags", "--always") + workingDir = rootDir + isIgnoreExitValue = true + }.standardOutput.asText.get().trim() + }.getOrNull() + if (!gitVersion.isNullOrBlank()) return gitVersion + + System.getenv("GITHUB_SHA")?.take(12)?.takeIf { it.isNotBlank() }?.let { return it } + + return "0.0.0-SNAPSHOT" +} diff --git a/SwiftKitCore/build.gradle.kts b/SwiftKitCore/build.gradle.kts index 89d8f5c46..fcf0590bc 100644 --- a/SwiftKitCore/build.gradle.kts +++ b/SwiftKitCore/build.gradle.kts @@ -12,6 +12,8 @@ // //===----------------------------------------------------------------------===// +import org.swift.build.utils.resolveSwiftKitVersion + plugins { id("build-logic.java-application-conventions") id("me.champeau.jmh") version "0.7.2" @@ -19,7 +21,9 @@ plugins { } group = "org.swift.swiftkit" -version = "1.0-SNAPSHOT" + +version = resolveSwiftKitVersion() + base { archivesName = "swiftkit-core" } @@ -34,8 +38,6 @@ publishing { create("maven") { groupId = group as? String artifactId = "swiftkit-core" - version = "1.0-SNAPSHOT" - from(components["java"]) } } diff --git a/SwiftKitFFM/build.gradle.kts b/SwiftKitFFM/build.gradle.kts index 98a988900..a56f9df1c 100644 --- a/SwiftKitFFM/build.gradle.kts +++ b/SwiftKitFFM/build.gradle.kts @@ -12,13 +12,16 @@ // //===----------------------------------------------------------------------===// +import org.swift.build.utils.resolveSwiftKitVersion + plugins { id("build-logic.java-application-conventions") `maven-publish` } group = "org.swift.swiftkit" -version = "1.0-SNAPSHOT" + +version = resolveSwiftKitVersion() base { archivesName = "swiftkit-ffm" @@ -34,8 +37,6 @@ publishing { create("maven") { groupId = group as? String artifactId = "swiftkit-ffm" - version = "1.0-SNAPSHOT" - from(components["java"]) } }