11@file:Suppress(" UnstableApiUsage" )
22
3- import android.databinding.tool.ext.capitalizeUS
43import com.android.build.gradle.internal.tasks.factory.dependsOn
5- import com.google.common.hash.Hashing
6- import com.google.common.io.Files
7- import groovy.json.JsonOutput
8- import groovy.json.JsonSlurper
9- import java.io.ByteArrayOutputStream
10- import java.nio.charset.Charset
11- import java.io.StringWriter
124import java.util.Properties
13- import java.util.TimeZone
14- import java.util.Date
15- import java.text.SimpleDateFormat
5+ import org.gradle.configurationcache.extensions.capitalized
166
177plugins {
8+ id(" com.osfans.trime.data-checksums" )
189 id(" com.android.application" )
1910 kotlin(" android" )
2011 kotlin(" plugin.serialization" ) version Versions .kotlin
2112 id(" com.google.devtools.ksp" ) version Versions .ksp
2213 id(" com.mikepenz.aboutlibraries.plugin" )
2314}
2415
25- fun exec (cmd : String ): String = ByteArrayOutputStream ().use {
26- project.exec {
27- commandLine = cmd.split(" " )
28- standardOutput = it
29- }
30- it.toString().trim()
31- }
32- fun envOrDefault (env : String , default : () -> String ): String {
33- val v = System .getenv(env)
34- return if (v.isNullOrBlank()) default() else v
35- }
36-
37- val gitUserOrCIName = envOrDefault(" CI_NAME" ) {
38- exec(" git config user.name" )
39- }
40- val gitVersionName = exec(" git describe --tags --long --always" )
41- val gitHashShort = exec(" git rev-parse --short HEAD" )
42- val gitRemoteUrl = exec(" git remote get-url origin" )
43- .replaceFirst(" ^git@github\\ .com:" , " https://github.com/" )
44- .replaceFirst(" \\ .git\$ " , " " )
45-
46- fun buildInfo (): String {
47- val writer = StringWriter ()
48- val time = SimpleDateFormat (" yyyy-MM-dd HH:mm:ss" ).apply {
49- timeZone = TimeZone .getTimeZone(" UTC" )
50- }.format(Date (System .currentTimeMillis()))
51- writer.append(" Builder: ${gitUserOrCIName} \\ n" )
52- writer.append(" Build Time: $time UTC\\ n" )
53- writer.append(" Build Version Name: ${gitVersionName} \\ n" )
54- writer.append(" Git Hash: ${gitHashShort} \\ n" )
55- writer.append(" Git Repo: $gitRemoteUrl " )
56- val info = writer.toString()
57- println (info)
58- return info
59- }
60-
6116android {
6217 namespace = " com.osfans.trime"
6318 compileSdk = 34
@@ -73,10 +28,11 @@ android {
7328
7429 multiDexEnabled = true
7530 setProperty(" archivesBaseName" , " trime-$versionName " )
76- buildConfigField(" String" , " BUILD_GIT_HASH" , " \" ${gitHashShort} \" " )
77- buildConfigField(" String" , " BUILD_GIT_REPO" , " \" ${gitRemoteUrl} \" " )
78- buildConfigField(" String" , " BUILD_VERSION_NAME" , " \" ${gitVersionName} \" " )
79- buildConfigField(" String" , " BUILD_INFO" , " \" ${buildInfo()} \" " )
31+ buildConfigField(" String" , " BUILDER" , " \" ${project.builder} \" " )
32+ buildConfigField(" long" , " BUILD_TIMESTAMP" , project.buildTimestamp)
33+ buildConfigField(" String" , " BUILD_COMMIT_HASH" , " \" ${project.buildCommitHash} \" " )
34+ buildConfigField(" String" , " BUILD_GIT_REPO" , " \" ${project.buildGitRepo} \" " )
35+ buildConfigField(" String" , " BUILD_VERSION_NAME" , " \" ${project.buildVersionName} \" " )
8036 }
8137
8238 signingConfigs {
@@ -184,20 +140,13 @@ ksp {
184140 arg(" room.schemaLocation" , " $projectDir /schemas" )
185141}
186142
187- val generateDataChecksum by tasks.register<DataChecksumsTask >(" generateDataChecksum" ) {
188- inputDir.set(file(" src/main/assets" ))
189- outputFile.set(file(" src/main/assets/checksums.json" ))
190- }
191-
192143android.applicationVariants.all {
193- val variantName = name.capitalizeUS()
194- tasks.findByName(" merge${variantName} Assets" )?.dependsOn(generateDataChecksum)
144+ val variantName = name.capitalized()
145+ tasks.findByName(" generateDataChecksums" )?.also {
146+ tasks.getByName(" merge${variantName} Assets" ).dependsOn(it)
147+ }
195148}
196149
197- tasks.register<Delete >(" cleanGeneratedAssets" ) {
198- delete(file(" src/main/assets/checksums.json" ))
199- }.also { tasks.clean.dependsOn(it) }
200-
201150tasks.register<Delete >(" cleanCxxIntermediates" ) {
202151 delete(file(" .cxx" ))
203152}.also { tasks.clean.dependsOn(it) }
@@ -236,83 +185,3 @@ dependencies {
236185 testImplementation(" junit:junit:4.13.2" )
237186 androidTestImplementation(" junit:junit:4.13.2" )
238187}
239-
240- abstract class DataChecksumsTask : DefaultTask () {
241- @get:Incremental
242- @get:PathSensitive(PathSensitivity .NAME_ONLY )
243- @get:InputDirectory
244- abstract val inputDir: DirectoryProperty
245-
246- @get:OutputFile
247- abstract val outputFile: RegularFileProperty
248-
249- private val file by lazy { outputFile.get().asFile }
250-
251- private fun serialize (map : Map <String , String >) {
252- file.deleteOnExit()
253- file.writeText(
254- JsonOutput .prettyPrint(
255- JsonOutput .toJson(
256- mapOf<Any , Any >(
257- " sha256" to Hashing .sha256()
258- .hashString(
259- map.entries.joinToString { it.key + it.value },
260- Charset .defaultCharset()
261- )
262- .toString(),
263- " files" to map
264- )
265- )
266- )
267- )
268- }
269-
270- @Suppress(" UNCHECKED_CAST" )
271- private fun deserialize (): Map <String , String > =
272- ((JsonSlurper ().parseText(file.readText()) as Map <Any , Any >))[" files" ] as Map <String , String >
273-
274- companion object {
275- fun sha256 (file : File ): String =
276- Files .asByteSource(file).hash(Hashing .sha256()).toString()
277- }
278-
279- @TaskAction
280- fun execute (inputChanges : InputChanges ) {
281- val map =
282- file.exists()
283- .takeIf { it }
284- ?.runCatching {
285- deserialize()
286- // remove all old dirs
287- .filterValues { it.isNotBlank() }
288- .toMutableMap()
289- }
290- ?.getOrNull()
291- ? : mutableMapOf ()
292-
293- fun File.allParents (): List <File > =
294- if (parentFile == null || parentFile.path in map)
295- listOf ()
296- else
297- listOf (parentFile) + parentFile.allParents()
298- inputChanges.getFileChanges(inputDir).forEach { change ->
299- if (change.file.name == file.name)
300- return @forEach
301- logger.log(LogLevel .DEBUG , " ${change.changeType} : ${change.normalizedPath} " )
302- val relativeFile = change.file.relativeTo(file.parentFile)
303- val key = relativeFile.path
304- if (change.changeType == ChangeType .REMOVED ) {
305- map.remove(key)
306- } else {
307- map[key] = sha256(change.file)
308- }
309- }
310- // calculate dirs
311- inputDir.asFileTree.forEach {
312- it.relativeTo(file.parentFile).allParents().forEach { p ->
313- map[p.path] = " "
314- }
315- }
316- serialize(map.toSortedMap())
317- }
318- }
0 commit comments