From c84d53c827914e1985b319bcc59a167730d9f246 Mon Sep 17 00:00:00 2001 From: Konicai <71294714+Konicai@users.noreply.github.com> Date: Mon, 19 Sep 2022 11:22:09 -0400 Subject: [PATCH] Re-add git.properties (#3287) Co-authored-by: Camotoy <20743703+Camotoy@users.noreply.github.com> --- core/build.gradle.kts | 68 +++++++++++++++++++++----- core/src/main/resources/git.properties | 7 +++ 2 files changed, 62 insertions(+), 13 deletions(-) create mode 100644 core/src/main/resources/git.properties diff --git a/core/build.gradle.kts b/core/build.gradle.kts index f5c41ed12..2825ead1f 100644 --- a/core/build.gradle.kts +++ b/core/build.gradle.kts @@ -79,23 +79,65 @@ configurations.api { exclude(group = "io.netty.incubator", module = "netty-incubator-transport-native-io_uring") } +tasks.processResources { + // This is solely for backwards compatibility for other programs that used this file before the switch to gradle. + // It used to be generated by the maven Git-Commit-Id-Plugin + filesMatching("git.properties") { + val info = GitInfo() + expand( + "branch" to info.branch, + "buildNumber" to info.buildNumber, + "projectVersion" to project.version, + "commit" to info.commit, + "commitAbbrev" to info.commitAbbrev, + "commitMessage" to info.commitMessage, + "repository" to info.repository + ) + } +} + configure { - val indra = the() - val mainFile = "src/main/java/org/geysermc/geyser/GeyserImpl.java" - // On Jenkins, a detached head is checked out, so indra cannot determine the branch. Fortunately, this environment variable is available. - val branchName = indra.branchName() ?: System.getenv("BRANCH_NAME") ?: "DEV" - val commit = indra.commit() - val git = indra.git() - val gitVersion = "git-${branchName}-${commit?.name?.substring(0, 7) ?: "0000000"}" + val info = GitInfo() - replaceToken("\${version}", "${project.version} ($gitVersion)", mainFile) - replaceToken("\${gitVersion}", gitVersion, mainFile) - replaceToken("\${buildNumber}", buildNumber(), mainFile) - replaceToken("\${branch}", branchName, mainFile) - if (commit != null && commit.name != null) replaceToken("\${commit}", commit.name, mainFile) - if (git != null) replaceToken("\${repository}", git.repository.config.getString("remote", "origin", "url")) + replaceToken("\${version}", "${project.version} (${info.gitVersion})", mainFile) + replaceToken("\${gitVersion}", info.gitVersion, mainFile) + replaceToken("\${buildNumber}", info.buildNumber, mainFile) + replaceToken("\${branch}", info.branch, mainFile) + replaceToken("\${commit}", info.commit, mainFile) + replaceToken("\${repository}", info.repository, mainFile) } fun Project.buildNumber(): Int = Integer.parseInt(System.getenv("BUILD_NUMBER") ?: "-1") + +inner class GitInfo { + val branch: String + val commit: String + val commitAbbrev: String + + val gitVersion: String + val version: String + val buildNumber: Int + + val commitMessage: String + val repository: String + + init { + // On Jenkins, a detached head is checked out, so indra cannot determine the branch. + // Fortunately, this environment variable is available. + branch = indraGit.branchName() ?: System.getenv("BRANCH_NAME") ?: "DEV" + + val commit = indraGit.commit() + this.commit = commit?.name ?: "0".repeat(40) + commitAbbrev = commit?.name?.substring(0, 7) ?: "0".repeat(7) + + gitVersion = "git-${branch}-${commitAbbrev}" + version = "${project.version} ($gitVersion)" + buildNumber = buildNumber() + + val git = indraGit.git() + commitMessage = git?.commit()?.message ?: "" + repository = git?.repository?.config?.getString("remote", "origin", "url") ?: "" + } +} diff --git a/core/src/main/resources/git.properties b/core/src/main/resources/git.properties new file mode 100644 index 000000000..f14e55623 --- /dev/null +++ b/core/src/main/resources/git.properties @@ -0,0 +1,7 @@ +git.branch=${branch} +git.build.number=${buildNumber} +git.build.version=${projectVersion} +git.commit.id=${commit} +git.commit.id.abbrev=${commitAbbrev} +git.commit.message.full=${commitMessage} +git.remote.origin.url=${repository}