瀏覽代碼

refactor: [8/n] Extract git-based versioning logic

Peter Cai 2 年之前
父節點
當前提交
8845ceec9a
共有 2 個文件被更改,包括 31 次插入30 次删除
  1. 4 30
      app/build.gradle
  2. 27 0
      helpers.gradle

+ 4 - 30
app/build.gradle

@@ -3,33 +3,7 @@ plugins {
     id 'org.jetbrains.kotlin.android'
 }
 
-def getVersionCode = { ->
-    try {
-        def stdout = new ByteArrayOutputStream()
-        exec {
-            commandLine 'git', 'rev-list', '--first-parent', '--count', 'master'
-            standardOutput = stdout
-        }
-        return Integer.parseInt(stdout.toString().trim())
-    }
-    catch (ignored) {
-        return -1;
-    }
-}
-
-def getVersionName = { ->
-    try {
-        def stdout = new ByteArrayOutputStream()
-        exec {
-            commandLine 'git', 'describe', '--always', '--tags', '--dirty'
-            standardOutput = stdout
-        }
-        return stdout.toString().trim()
-    }
-    catch (ignored) {
-        return null;
-    }
-}
+apply from: '../helpers.gradle'
 
 // Signing config, mainly intended for debug builds
 def keystorePropertiesFile = rootProject.file("keystore.properties");
@@ -43,8 +17,8 @@ android {
         applicationId "im.angry.openeuicc"
         minSdk 30
         targetSdk 31
-        versionCode getVersionCode()
-        versionName getVersionName()
+        versionCode getGitVersionCode()
+        versionName getGitVersionName()
 
         testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
     }
@@ -67,7 +41,7 @@ android {
             signingConfig signingConfigs.config
         }
     }
-    applicationVariants.all { variant ->
+    applicationVariants.configureEach { variant ->
         if (variant.name == "debug") {
             variant.outputs.each { o -> o.versionCodeOverride = System.currentTimeSeconds() }
         }

+ 27 - 0
helpers.gradle

@@ -0,0 +1,27 @@
+ext.getGitVersionCode = { ->
+    try {
+        def stdout = new ByteArrayOutputStream()
+        exec {
+            commandLine 'git', 'rev-list', '--first-parent', '--count', 'master'
+            standardOutput = stdout
+        }
+        return Integer.parseInt(stdout.toString().trim())
+    }
+    catch (ignored) {
+        return -1;
+    }
+}
+
+ext.getGitVersionName = { ->
+    try {
+        def stdout = new ByteArrayOutputStream()
+        exec {
+            commandLine 'git', 'describe', '--always', '--tags', '--dirty'
+            standardOutput = stdout
+        }
+        return stdout.toString().trim()
+    }
+    catch (ignored) {
+        return null;
+    }
+}