Change git hash detection

This commit is contained in:
Lukas Rieger (Blue) 2022-07-26 21:21:32 +02:00
parent b466fb4ed8
commit c40eb72d7c
No known key found for this signature in database
GPG Key ID: 2D09EC5ED2687FF2
3 changed files with 24 additions and 13 deletions

View File

@ -1,18 +1,34 @@
import java.io.IOException
plugins { plugins {
java java
`java-library` `java-library`
id("com.diffplug.spotless") version "6.1.2" id("com.diffplug.spotless") version "6.1.2"
id ("com.palantir.git-version") version "0.12.3"
} }
val versionDetails: groovy.lang.Closure<com.palantir.gradle.gitversion.VersionDetails> by extra
val git = versionDetails()
group = "de.bluecolored.bluemap.api" group = "de.bluecolored.bluemap.api"
val apiVersion: String by project val apiVersion: String by project
version = apiVersion version = apiVersion
fun String.runCommand(): String = ProcessBuilder(split("\\s(?=(?:[^'\"`]*(['\"`])[^'\"`]*\\1)*[^'\"`]*$)".toRegex()))
.directory(projectDir)
.redirectOutput(ProcessBuilder.Redirect.PIPE)
.redirectError(ProcessBuilder.Redirect.PIPE)
.start()
.apply { waitFor(60, TimeUnit.SECONDS) }
.run {
val error = errorStream.bufferedReader().readText().trim()
if (error.isNotEmpty()) {
throw IOException(error)
}
inputStream.bufferedReader().readText().trim()
}
val gitHash = "git rev-parse --verify HEAD".runCommand()
val clean = "git status --porcelain".runCommand().isEmpty()
println("Git hash: $gitHash" + if (clean) "" else " (dirty)")
val javaTarget = 11 val javaTarget = 11
java { java {
sourceCompatibility = JavaVersion.toVersion(javaTarget) sourceCompatibility = JavaVersion.toVersion(javaTarget)
@ -68,8 +84,7 @@ tasks.processResources {
expand ( expand (
"version" to project.version, "version" to project.version,
"gitHash" to git.gitHashFull, "gitHash" to gitHash + if (clean) "" else " (dirty)"
"gitClean" to git.isCleanTag
) )
} }
} }

View File

@ -45,9 +45,9 @@
public abstract class BlueMapAPI { public abstract class BlueMapAPI {
@SuppressWarnings("unused") @SuppressWarnings("unused")
private static final String VERSION, GIT_HASH, GIT_CLEAN; private static final String VERSION, GIT_HASH;
static { static {
String version = "DEV", gitHash = "DEV", gitClean = "DEV"; String version = "DEV", gitHash = "DEV";
URL url = BlueMapAPI.class.getResource("/de/bluecolored/bluemap/api/version.json"); URL url = BlueMapAPI.class.getResource("/de/bluecolored/bluemap/api/version.json");
if (url != null) { if (url != null) {
Gson gson = new Gson(); Gson gson = new Gson();
@ -55,7 +55,6 @@ public abstract class BlueMapAPI {
JsonObject element = gson.fromJson(reader, JsonElement.class).getAsJsonObject(); JsonObject element = gson.fromJson(reader, JsonElement.class).getAsJsonObject();
version = element.get("version").getAsString(); version = element.get("version").getAsString();
gitHash = element.get("git-hash").getAsString(); gitHash = element.get("git-hash").getAsString();
gitClean = element.get("git-clean").getAsString();
} catch (Exception ex) { } catch (Exception ex) {
System.err.println("Failed to load version from resources!"); System.err.println("Failed to load version from resources!");
ex.printStackTrace(); ex.printStackTrace();
@ -64,11 +63,9 @@ public abstract class BlueMapAPI {
if (version.equals("${version}")) version = "DEV"; if (version.equals("${version}")) version = "DEV";
if (gitHash.equals("${gitHash}")) version = "DEV"; if (gitHash.equals("${gitHash}")) version = "DEV";
if (gitClean.equals("${gitClean}")) version = "DEV";
VERSION = version; VERSION = version;
GIT_HASH = gitHash; GIT_HASH = gitHash;
GIT_CLEAN = gitClean;
} }
private static BlueMapAPI instance; private static BlueMapAPI instance;

View File

@ -1,5 +1,4 @@
{ {
"version": "${version}", "version": "${version}",
"git-hash": "${gitHash}", "git-hash": "${gitHash}"
"git-clean": "${gitClean}"
} }