diff --git a/.gitignore b/.gitignore index 45241b570473cd75ba69cb425c00868b06f3db49..5cb5f08b49eb9f5c737a0ff6c3d1747743d2cc86 100644 --- a/.gitignore +++ b/.gitignore @@ -13,3 +13,4 @@ DisaggCalc.java node_modules package-lock.json package.json +*version.json diff --git a/build.gradle b/build.gradle index e7b4c104068a92f9c462274d8b2f7b617e63846a..ad7f391ada7659143b6c158e3415285b47aa5645 100644 --- a/build.gradle +++ b/build.gradle @@ -2,6 +2,7 @@ plugins { id "com.diffplug.spotless" id "com.github.spotbugs" id "com.github.node-gradle.node" + id 'com.palantir.git-version' id "com.star-zero.gradle.githook" id "eclipse-wtp" id "jacoco" @@ -13,6 +14,7 @@ compileJava.options.encoding = "UTF-8" compileTestJava.options.encoding = "UTF-8" sourceCompatibility = JavaVersion.VERSION_11 +apply from: "${projectDir}/gradle/app-version.gradle" apply from: "${projectDir}/gradle/git-hooks.gradle" apply from: "${projectDir}/gradle/node.gradle" apply from: "${projectDir}/gradle/repositories.gradle" diff --git a/gradle.properties b/gradle.properties index 45e311a76b85e163296c3b69e097ce417fc639f9..da768a73ede34ed1c47d5b33ae970fd146fd46f8 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,5 +1,6 @@ junitVersion = 5.8.2 githooksVersion = 1.2.0 +gitVersionVersion = 0.15.0 gsonVersion = 2.8.9 guavaVersion = 31.1-jre nodePluginVersion = 3.1.1 diff --git a/gradle/app-version.gradle b/gradle/app-version.gradle new file mode 100644 index 0000000000000000000000000000000000000000..c44e69cc10dd4bdf0cb254c6f9cafaeb9c335a22 --- /dev/null +++ b/gradle/app-version.gradle @@ -0,0 +1,22 @@ +apply plugin: "com.palantir.git-version" + +tasks.withType(JavaCompile) { + doFirst { + def versionFile = new File("${projectDir}/src/main/resources/version/nshmp-lib-version.json") + new File(versionFile.getParent()).mkdirs() + def details = versionDetails() + def versionInfo = [ + branchName: details.branchName, + commitDistance: details.commitDistance, + gitHash: details.gitHash, + gitHashFull: details.gitHashFull, + isCleanTag: details.isCleanTag, + lastTag: details.lastTag, + projectName: project.name, + url: 'git config --get remote.origin.url'.execute().text.replace('\n', ''), + version: details.version, + ] + def json = groovy.json.JsonOutput.toJson(versionInfo) + versionFile.write(groovy.json.JsonOutput.prettyPrint(json)) + } +} diff --git a/settings.gradle b/settings.gradle index 989a33fe857fe5c381894845d6c7c1925be86cc5..3162174a0fab248aae9c8083fb02755e2eb560ad 100644 --- a/settings.gradle +++ b/settings.gradle @@ -5,6 +5,7 @@ pluginManagement { id "com.github.spotbugs" version "${spotbugsVersion}" id "com.github.node-gradle.node" version "${nodePluginVersion}" id "com.star-zero.gradle.githook" version "${githooksVersion}" + id "com.palantir.git-version" version "${gitVersionVersion}" } } diff --git a/src/main/java/gov/usgs/earthquake/nshmp/internal/AppVersion.java b/src/main/java/gov/usgs/earthquake/nshmp/internal/AppVersion.java new file mode 100644 index 0000000000000000000000000000000000000000..c4d262e5eca7520634a4597b9539eed4a5b5d880 --- /dev/null +++ b/src/main/java/gov/usgs/earthquake/nshmp/internal/AppVersion.java @@ -0,0 +1,43 @@ +package gov.usgs.earthquake.nshmp.internal; + +import java.net.URL; + +import com.google.common.base.Charsets; +import com.google.common.io.Resources; +import com.google.gson.Gson; + +/** + * Interface for reading in version info JSON files. + */ +public interface AppVersion { + + /** + * Returns the version info for the repo. + */ + public VersionInfo getVersionInfo(); + + static VersionInfo versionInfo(URL resource) { + try { + return new Gson().fromJson( + Resources.toString(resource, Charsets.UTF_8), + VersionInfo.class); + } catch (Exception e) { + throw new RuntimeException(e.getMessage()); + } + } + + /** + * The version info of a repository. + */ + public static class VersionInfo { + public String projectName; + public String url; + public String version; + public String branchName; + public int commitDistance; + public String gitHash; + public String gitHashFull; + public boolean isCleanTag; + public String lastTag; + } +} diff --git a/src/main/java/gov/usgs/earthquake/nshmp/internal/LibVersion.java b/src/main/java/gov/usgs/earthquake/nshmp/internal/LibVersion.java new file mode 100644 index 0000000000000000000000000000000000000000..5ec804e5fbb207705da8efd2ba417dd1e25a68d9 --- /dev/null +++ b/src/main/java/gov/usgs/earthquake/nshmp/internal/LibVersion.java @@ -0,0 +1,16 @@ +package gov.usgs.earthquake.nshmp.internal; + +import com.google.common.io.Resources; + +/** + * Read in and return nshmp-lib-version.json. + * + * File is create from Gradle on build. + */ +public class LibVersion implements AppVersion { + + public VersionInfo getVersionInfo() { + var resource = Resources.getResource("version/nshmp-lib-version.json"); + return AppVersion.versionInfo(resource); + } +}