buildSrc/src/main/kotlin/RepositoryChangeset.kt
changeset 0 0a13dcabf7d3
child 34 ce299aacc068
equal deleted inserted replaced
-1:000000000000 0:0a13dcabf7d3
       
     1 /*
       
     2  * AboutOss is a utility library to retrieve and display
       
     3  * opensource licenses in Android applications.
       
     4  *
       
     5  * Copyright (C) 2023 by Frederic-Charles Barthelery.
       
     6  *
       
     7  * This file is part of AboutOss.
       
     8  *
       
     9  * AboutOss is free software: you can redistribute it and/or modify
       
    10  * it under the terms of the GNU General Public License as published by
       
    11  * the Free Software Foundation, either version 3 of the License, or
       
    12  * (at your option) any later version.
       
    13  *
       
    14  * AboutOss is distributed in the hope that it will be useful,
       
    15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    17  * GNU General Public License for more details.
       
    18  *
       
    19  * You should have received a copy of the GNU General Public License
       
    20  * along with AboutOss.  If not, see <http://www.gnu.org/licenses/>.
       
    21  */
       
    22 package com.geekorum.build
       
    23 
       
    24 import com.android.build.api.variant.ApplicationAndroidComponentsExtension
       
    25 import com.android.build.api.variant.BuildConfigField
       
    26 import com.android.build.api.variant.VariantOutputConfiguration.OutputType
       
    27 import org.gradle.api.DefaultTask
       
    28 import org.gradle.api.Project
       
    29 import org.gradle.api.file.RegularFileProperty
       
    30 import org.gradle.api.provider.Property
       
    31 import org.gradle.api.tasks.Input
       
    32 import org.gradle.api.tasks.OutputFile
       
    33 import org.gradle.api.tasks.TaskAction
       
    34 import org.gradle.configurationcache.extensions.capitalized
       
    35 import org.gradle.kotlin.dsl.register
       
    36 import org.gradle.process.ExecOperations
       
    37 import java.io.ByteArrayOutputStream
       
    38 import java.io.File
       
    39 import javax.inject.Inject
       
    40 
       
    41 internal fun ExecOperations.getGitSha1(projectDir: File): String? = runCommand("git rev-parse HEAD", workingDir = projectDir)?.trim()
       
    42 
       
    43 internal fun ExecOperations.getHgSha1(projectDir: File): String? = runCommand("hg id --debug -i -r .", workingDir = projectDir)?.trim()
       
    44 
       
    45 internal fun ExecOperations.getHgLocalRevisionNumber(projectDir: File): String? = runCommand("hg id -n -r .", workingDir = projectDir)?.trim()
       
    46 
       
    47 private fun ExecOperations.getChangeSet(projectDir: File): String {
       
    48     val git = File(projectDir, ".git")
       
    49     val hg = File(projectDir, ".hg")
       
    50     return when {
       
    51         git.exists() -> "git:${getGitSha1(projectDir)}"
       
    52         hg.exists() -> "hg:${getHgSha1(projectDir)}"
       
    53         else -> "unknown"
       
    54     }
       
    55 }
       
    56 
       
    57 /**
       
    58  * Compute a version code following this format : MmmPBBB
       
    59  * M is major, mm is minor, P is patch
       
    60  * BBB is build version number from hg
       
    61  */
       
    62 private fun ExecOperations.computeChangesetVersionCode(projectDir: File, major: Int = 0, minor: Int = 0, patch: Int = 0): Int {
       
    63     val base = (major * 1000000) + (minor * 10000) + (patch * 1000)
       
    64     return base + (getHgLocalRevisionNumber(projectDir)?.trim()?.toIntOrNull() ?: 0)
       
    65 }
       
    66 
       
    67 private fun ExecOperations.runCommand(
       
    68     command: String,
       
    69     workingDir: File = File(".")
       
    70 ): String? {
       
    71     val output = ByteArrayOutputStream()
       
    72     val result = exec {
       
    73         commandLine(command.split("\\s".toRegex()))
       
    74         setWorkingDir(workingDir)
       
    75         setStandardOutput(output)
       
    76         setErrorOutput(output)
       
    77     }
       
    78     result.rethrowFailure()
       
    79     return output.toString(Charsets.UTF_8)
       
    80 }
       
    81 
       
    82 abstract class VersionCodeTask : DefaultTask() {
       
    83 
       
    84     @get:OutputFile
       
    85     abstract val versionCodeOutputFile: RegularFileProperty
       
    86 
       
    87     @get:OutputFile
       
    88     abstract val changesetOutputFile: RegularFileProperty
       
    89 
       
    90     @get:Input
       
    91     abstract val repositoryDirectory: Property<String>
       
    92 
       
    93     @get:Input
       
    94     abstract val major: Property<Int>
       
    95 
       
    96     @get:Input
       
    97     abstract val minor: Property<Int>
       
    98 
       
    99     @get:Input
       
   100     abstract val patch: Property<Int>
       
   101 
       
   102     @get:Inject
       
   103     abstract val exec: ExecOperations
       
   104 
       
   105     @TaskAction
       
   106     fun computeVersionCode() {
       
   107         val projectDir = File(repositoryDirectory.get())
       
   108         val versionCode = exec.computeChangesetVersionCode(projectDir, major.getOrElse(0), minor.getOrElse(0), patch.getOrElse(0))
       
   109         versionCodeOutputFile.get().asFile.writeText("$versionCode")
       
   110     }
       
   111 
       
   112     @TaskAction
       
   113     fun computeChangeset() {
       
   114         val projectDir = File(repositoryDirectory.get())
       
   115         val changeset = exec.getChangeSet(projectDir)
       
   116         changesetOutputFile.get().asFile.writeText(changeset)
       
   117     }
       
   118 }
       
   119 
       
   120 /**
       
   121  * @param versionNameSuffix extra string to add to version name
       
   122  */
       
   123 fun ApplicationAndroidComponentsExtension.configureVersionChangeset(project: Project, major: Int, minor: Int, patch: Int, versionNameSuffix: String = "") {
       
   124     // Note: Everything in there is incubating.
       
   125 
       
   126     // onVariantProperties registers an action that configures variant properties during
       
   127     // variant computation (which happens during afterEvaluate)
       
   128     onVariants {
       
   129         // Because app module can have multiple output when using mutli-APK, versionCode/Name
       
   130         // are only available on the variant output.
       
   131         // Here gather the output when we are in single mode (ie no multi-apk)
       
   132         val mainOutput = it.outputs.single { it.outputType == OutputType.SINGLE }
       
   133 
       
   134         // create version Code generating task
       
   135         val versionCodeTask = project.tasks.register<VersionCodeTask>("computeVersionCodeFor${it.name.capitalized()}") {
       
   136             this.major.set(major)
       
   137             this.minor.set(minor)
       
   138             this.patch.set(patch)
       
   139             repositoryDirectory.set(project.rootDir.absolutePath)
       
   140             versionCodeOutputFile.set(project.layout.buildDirectory.file("intermediates/versionCode.txt"))
       
   141             changesetOutputFile.set(project.layout.buildDirectory.file("intermediates/changeset.txt"))
       
   142         }
       
   143 
       
   144         // wire version code from the task output
       
   145         // map will create a lazy Provider that
       
   146         // 1. runs just before the consumer(s), ensuring that the producer (VersionCodeTask) has run
       
   147         //    and therefore the file is created.
       
   148         // 2. contains task dependency information so that the consumer(s) run after the producer.
       
   149         mainOutput.versionCode.set(versionCodeTask.map { it.versionCodeOutputFile.get().asFile.readText().toInt() })
       
   150         mainOutput.versionName.set("$major.$minor.$patch$versionNameSuffix")
       
   151 
       
   152         it.buildConfigFields.put("REPOSITORY_CHANGESET", versionCodeTask.map {
       
   153             BuildConfigField("String", "\"${it.changesetOutputFile.get().asFile.readText()}\"", "Repository changeset")
       
   154         })
       
   155     }
       
   156 }