ext.getBuildCode = {
    if (System.getenv('BUILD_BUILDID') != null) {
        return System.getenv('BUILD_BUILDID').trim().toInteger()
    } else {
        return 12
    }
}

ext.getBranchName = {
    def branch = System.getenv('CI_COMMIT_REF_NAME')
    if (branch != null) {
        return branch.trim()
    } else {
        return getGitBranch ?: "unknown"
    }
}

ext.getGitDescribe = {
    def stdout = new ByteArrayOutputStream()
    exec {
        commandLine 'git', 'describe', '--tags'
        standardOutput = stdout
    }
    return stdout.toString().trim()
}

ext.getGitBranch = {
    def stdout = new ByteArrayOutputStream()
    exec {
        commandLine 'git' 'rev-parse' '--abbrev-ref' 'HEAD'
        standardOutput = stdout
    }
    return stdout.toString().trim()
}

ext.getStorePassword = {
    return getPropertyByName("storePassword")
}

ext.getKeyPassword = {
    return getPropertyByName("keyPassword")
}

ext.getKeyAlias = {
    return getPropertyByName("keyAlias")
}

ext.getPropertyByName = { propertyName ->
    if (project.hasProperty(propertyName)) {
        return project.getProperty(propertyName)
    }
    return ""
}

Properties localProperties = new Properties()
File localPropertiesFile = project.rootProject.file('local.properties')
if (localPropertiesFile.exists()) {
    localProperties.load(localPropertiesFile.newDataInputStream())
}

ext.resolveProperty = { String key, String defaultValue ->
    System.getenv(key) ?: localProperties.getProperty(key, defaultValue)
}
