Last active
July 18, 2022 12:32
-
-
Save kibotu/f363ed0e86b6656f279cb707d762e975 to your computer and use it in GitHub Desktop.
Generating build time variables into app/asset folder. Newly support for configuration caching.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Task to generate build.json and puts it into assets/meta folder within the app. | |
*/ | |
preBuild.dependsOn tasks.register("generateBuildJson") { | |
// note: file can't be named 'build.json' otherwise it will not be bundled into the app. | |
outputs.file(project.rootProject.file('app/src/main/assets/meta.json')) | |
// need to remember configuration variables for later task execution | |
def majorVersion = MAJOR_VERSION | |
def minorVersion = MINOR_VERSION | |
def hotfixVersion = HOTFIX_VERSION | |
def buildNumber = BUILD_NUMBER | |
def versioncode = VERSIONCODE | |
def versionName = VERSIONNAME | |
doLast { | |
description = "Generate //${outputs.files.singleFile.path}" | |
println(description) | |
def branchName = { | |
def branch = "" | |
def proc = "git rev-parse --abbrev-ref HEAD".execute() | |
proc.in.eachLine { line -> branch = line } | |
proc.err.eachLine { line -> println line } | |
proc.waitFor() | |
// fix detached head | |
if (branch.contains("release/")) { | |
branch = branch.replace("heads/", "") | |
} | |
branch.trim() | |
} | |
def commitHash = { | |
def hash = "" | |
def proc = "git log -n 1 --format='%h'".execute() | |
proc.in.eachLine { line -> hash = line } | |
proc.err.eachLine { line -> println line } | |
proc.waitFor() | |
hash.trim().replaceAll('\'', '') | |
} | |
def json = groovy.json.JsonOutput.toJson([ | |
'build-time' : new Date().getTime().toString(), | |
'branch-name' : branchName(), | |
'commit-hash' : commitHash(), | |
'major-version' : majorVersion.toString(), | |
'minor-version' : minorVersion.toString(), | |
'hotfix-version': hotfixVersion.toString(), | |
'build-number' : buildNumber.toString(), | |
'version-code' : versioncode.toString(), | |
'version-name' : versionName | |
]) | |
outputs.files.singleFile.text = groovy.json.JsonOutput.prettyPrint(json) | |
} | |
} | |
clean { | |
delete project.rootProject.file('app/src/main/assets/meta.json') | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment