From 5dfe07f58630edd5d7086cc773c204ae28bbcc61 Mon Sep 17 00:00:00 2001 From: Eyre_S Date: Sun, 6 Nov 2022 21:31:24 +0800 Subject: [PATCH] =?UTF-8?q?=E7=89=88=E6=9C=AC=E5=8F=B7=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=20git=20commit=20=E6=A0=87=E7=AD=BE=E6=94=AF=E6=8C=81=EF=BC=8C?= =?UTF-8?q?gradle=20build=20=E7=9A=84=20git=20=E7=8A=B6=E6=80=81=E8=BE=93?= =?UTF-8?q?=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 58 +++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 53 insertions(+), 5 deletions(-) diff --git a/build.gradle b/build.gradle index d721443..ea2f560 100644 --- a/build.gradle +++ b/build.gradle @@ -1,3 +1,5 @@ +import org.ajoberstar.grgit.Status + import java.nio.charset.Charset import java.nio.charset.StandardCharsets @@ -8,6 +10,27 @@ plugins { id 'application' id 'com.github.johnrengelman.shadow' version '7.1.0' id 'com.github.gmazzo.buildconfig' version '3.1.0' + id 'org.ajoberstar.grgit' version '5.0.0' +} + +final boolean proj_git = grgit!=null +final String proj_commit = proj_git ? grgit.head().id : null +final boolean proj_clean = isCleanBuild() +if (!proj_git) + print "[MornyBuild] git repository not available for current working space! git version tag will be disabled." +else if (isCleanBuild()) { + print "git: clean build at ${grgit.head().id}" +} else { + final Status status = grgit.status() + println "git: non-clean-build" + if (!status.unstaged.allChanges.empty) { + println "git: unstaged changes" + listChanges(status.unstaged) + } + if (!status.staged.allChanges.empty) { + println "[MornyBuild] git: staged changes" + listChanges(status.staged) + } } final String proj_group = 'cc.sukazyo' @@ -17,7 +40,9 @@ final String proj_archive_name = MORNY_ARCHIVE_NAME final String proj_version_base = VERSION final String proj_version_delta = VERSION_DELTA final String proj_version_use_delta = Boolean.parseBoolean(VERSION_DELTA) -final String proj_version = proj_version_base + (proj_version_use_delta ? "-δ${proj_version_delta}" : "") +String proj_version = proj_version_base +if (proj_version_use_delta) proj_version += "-δ${proj_version_delta}" +if (proj_git) proj_version += "+git.${proj_commit.substring(0, 8)}" + (proj_clean?"":".δ") final String proj_version_codename = CODENAME final JavaVersion proj_java = JavaVersion.VERSION_17 @@ -79,10 +104,12 @@ buildConfig { packageName(proj_package) - buildConfigField('String', 'VERSION', "\"${proj_version}\"") - buildConfigField('String', 'VERSION_DELTA', "\"${proj_version_delta}\"") - buildConfigField('String', 'CODENAME', "\"${proj_version_codename}\"") - buildConfigField('long', 'COMPILE_TIMESTAMP', "${System.currentTimeMillis()}L") + buildConfigField('String', 'VERSION', "\"${proj_version}\"") + buildConfigField('String', 'VERSION_DELTA', "\"${proj_version_delta}\"") + buildConfigField('String', 'CODENAME', "\"${proj_version_codename}\"") + buildConfigField('long', 'COMPILE_TIMESTAMP', "${System.currentTimeMillis()}L") + buildConfigField('String', 'COMMIT', proj_git ? "\"${proj_commit}\"" : "null") + buildConfigField('boolean', 'CLEAN_BUILD', "${proj_clean}") } @@ -92,6 +119,27 @@ shadowJar { archiveClassifier.set("fat") } +@SuppressWarnings("all") +boolean isCleanBuild () { + if (grgit == null) return false + Set changes = grgit.status().unstaged.allChanges + grgit.status().staged.allChanges + for (String file in changes) { + if (file.startsWith("src/")) return false + if (file == "build.gradle") return false + if (file == "gradle.properties") return false + } + return true +} + +void listChanges (Status.Changes listing) { + for (String file in listing.added) + println " add: ${file}" + for (String file in listing.modified) + println " mod: ${file}" + for (String file in listing.removed) + println " del: ${file}" +} + publishing { repositories{ maven {