buildSrc/src/main/kotlin/RepositoryChangeset.kt
changeset 100 9ec927c219b0
parent 93 0f0e69edeafc
equal deleted inserted replaced
99:641b0bfe5292 100:9ec927c219b0
    40 
    40 
    41 internal fun ExecOperations.getGitSha1(projectDir: File): String? = runCommand("git rev-parse HEAD", workingDir = projectDir)?.trim()
    41 internal fun ExecOperations.getGitSha1(projectDir: File): String? = runCommand("git rev-parse HEAD", workingDir = projectDir)?.trim()
    42 
    42 
    43 internal fun ExecOperations.getHgSha1(projectDir: File): String? = runCommand("hg id --debug -i -r .", workingDir = projectDir)?.trim()
    43 internal fun ExecOperations.getHgSha1(projectDir: File): String? = runCommand("hg id --debug -i -r .", workingDir = projectDir)?.trim()
    44 
    44 
    45 internal fun ExecOperations.getHgLocalRevisionNumber(projectDir: File): String? = runCommand("hg id -n -r .", workingDir = projectDir)?.trim()
    45 internal fun ExecOperations.getHgLocalRevisionNumber(projectDir: File): String? {
       
    46     val hg = File(projectDir, ".hg")
       
    47     return if (hg.exists()) {
       
    48         runCommand("hg id -n -r .", workingDir = projectDir)?.trim()
       
    49     } else null
       
    50 }
    46 
    51 
    47 private fun ExecOperations.getChangeSet(projectDir: File): String {
    52 private fun ExecOperations.getChangeSet(projectDir: File): String {
    48     val git = File(projectDir, ".git")
    53     val git = File(projectDir, ".git")
    49     val hg = File(projectDir, ".hg")
    54     val hg = File(projectDir, ".hg")
    50     return when {
    55     return when {
   130         // are only available on the variant output.
   135         // are only available on the variant output.
   131         // Here gather the output when we are in single mode (ie no multi-apk)
   136         // Here gather the output when we are in single mode (ie no multi-apk)
   132         val mainOutput = it.outputs.single { it.outputType == OutputType.SINGLE }
   137         val mainOutput = it.outputs.single { it.outputType == OutputType.SINGLE }
   133 
   138 
   134         // create version Code generating task
   139         // create version Code generating task
   135         val versionCodeTask = project.tasks.register<VersionCodeTask>("computeVersionCodeFor${it.name.replaceFirstChar { it.titlecase() }}") {
   140         val versionCodeTask = project.tasks.register<VersionCodeTask>("computeVersionCodeFor${it.name.capitalized()}") {
   136             this.major.set(major)
   141             this.major.set(major)
   137             this.minor.set(minor)
   142             this.minor.set(minor)
   138             this.patch.set(patch)
   143             this.patch.set(patch)
   139             repositoryDirectory.set(project.rootDir.absolutePath)
   144             repositoryDirectory.set(project.rootDir.absolutePath)
   140             versionCodeOutputFile.set(project.layout.buildDirectory.file("intermediates/versionCode.txt"))
   145             versionCodeOutputFile.set(project.layout.buildDirectory.file("intermediates/versionCode.txt"))
   152         it.buildConfigFields?.put("REPOSITORY_CHANGESET", versionCodeTask.map {
   157         it.buildConfigFields?.put("REPOSITORY_CHANGESET", versionCodeTask.map {
   153             BuildConfigField("String", "\"${it.changesetOutputFile.get().asFile.readText()}\"", "Repository changeset")
   158             BuildConfigField("String", "\"${it.changesetOutputFile.get().asFile.readText()}\"", "Repository changeset")
   154         })
   159         })
   155     }
   160     }
   156 }
   161 }
       
   162 
       
   163 private fun String.capitalized() = replaceFirstChar { if (it.isLowerCase()) it.titlecase() else it.toString() }