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 {
java
`java-library`
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"
val apiVersion: String by project
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
java {
sourceCompatibility = JavaVersion.toVersion(javaTarget)
@ -68,8 +84,7 @@ tasks.processResources {
expand (
"version" to project.version,
"gitHash" to git.gitHashFull,
"gitClean" to git.isCleanTag
"gitHash" to gitHash + if (clean) "" else " (dirty)"
)
}
}

View File

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

View File

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