diff --git a/app/build.gradle b/app/build.gradle index 283a8374..ab77fe0a 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -5,19 +5,9 @@ plugins { alias(libs.plugins.kotlin.parcelize) } -// For constructing gitSha only -def getGitSha = { - try { // Try-catch is necessary for build to work on non-git distributions - providers.exec { - commandLine 'git', 'rev-parse', 'HEAD' - executionResult.rethrowFailure() // Without this, sometimes it just stops immediately instead of throwing - }.standardOutput.asText.get().trim() - } catch (Exception e) { - "unknown" - } -} +apply from: 'getGitSha.gradle' -final def gitSha = getGitSha() +final def gitSha = ext.getGitSha() // The app name final def APP_NAME = "Tusky" diff --git a/app/getGitSha.gradle b/app/getGitSha.gradle new file mode 100644 index 00000000..53315b2b --- /dev/null +++ b/app/getGitSha.gradle @@ -0,0 +1,27 @@ +import org.gradle.api.provider.ValueSourceParameters +import javax.inject.Inject + +// Must wrap this in a ValueSource in order to get well-defined fail behavior without confusing Gradle on repeat builds. +abstract class GitShaValueSource implements ValueSource { + @Inject abstract ExecOperations getExecOperations() + + @Override String obtain() { + try { + def output = new ByteArrayOutputStream() + + execOperations.exec { + it.commandLine 'git', 'rev-parse', '--short=8', 'HEAD' + it.standardOutput = output + } + return output.toString().trim() + } catch (GradleException ignore) { + // Git executable unavailable, or we are not building in a git repo. Fall through: + } + return "unknown" + } +} + +// Export closure +ext.getGitSha = { + providers.of(GitShaValueSource) {}.get() +}