Skip to content

Commit f70f571

Browse files
committed
feat: generate version + commit ID at build time
1 parent f44c5ce commit f70f571

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

build.gradle.kts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,37 @@ tasks.register<Test>("semanticPreservationTest") {
8282
.map { (k, v) -> k.toString() to v.toString() }.toMap())
8383
}
8484

85+
tasks.register("generateVersionProperties") {
86+
val outputDir = layout.buildDirectory.dir("generated-resources/version")
87+
outputs.dir(outputDir)
88+
doLast {
89+
val dir = outputDir.get().asFile
90+
dir.mkdirs()
91+
val commitId = try {
92+
providers.exec {
93+
commandLine("git", "rev-parse", "--short", "HEAD")
94+
}.standardOutput.asText.get().trim()
95+
} catch (e: Exception) {
96+
"unknown"
97+
}
98+
File(dir, "version.properties").writeText(
99+
"version=${project.version}\ngit.commit=$commitId\n"
100+
)
101+
}
102+
}
103+
104+
sourceSets {
105+
getByName("main") {
106+
resources {
107+
srcDir(layout.buildDirectory.dir("generated-resources/version"))
108+
}
109+
}
110+
}
111+
112+
tasks.named("processResources") {
113+
dependsOn("generateVersionProperties")
114+
}
115+
85116
application {
86117
mainClass = "me.fponzi.tlaplusformatter.Main"
87118
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package me.fponzi.tlaplusformatter;
2+
3+
import java.io.IOException;
4+
import java.io.InputStream;
5+
import java.util.Properties;
6+
7+
public final class VersionInfo {
8+
private static final String VERSION;
9+
private static final String COMMIT_ID;
10+
11+
static {
12+
Properties props = new Properties();
13+
try (InputStream is = VersionInfo.class.getClassLoader().getResourceAsStream("version.properties")) {
14+
if (is != null) {
15+
props.load(is);
16+
}
17+
} catch (IOException e) {
18+
// ignore
19+
}
20+
VERSION = props.getProperty("version", "unknown");
21+
COMMIT_ID = props.getProperty("git.commit", "unknown");
22+
}
23+
24+
private VersionInfo() {}
25+
26+
public static String getVersion() {
27+
return VERSION;
28+
}
29+
30+
public static String getCommitId() {
31+
return COMMIT_ID;
32+
}
33+
34+
public static String getFullVersion() {
35+
return VERSION + " (commit " + COMMIT_ID + ")";
36+
}
37+
}

0 commit comments

Comments
 (0)