Skip to content

Commit

Permalink
build: refactor build logic
Browse files Browse the repository at this point in the history
  • Loading branch information
WhiredPlanck committed Dec 23, 2023
1 parent cb76a43 commit 0e90c1b
Show file tree
Hide file tree
Showing 10 changed files with 258 additions and 158 deletions.
153 changes: 11 additions & 142 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,63 +1,18 @@
@file:Suppress("UnstableApiUsage")

import android.databinding.tool.ext.capitalizeUS
import com.android.build.gradle.internal.tasks.factory.dependsOn
import com.google.common.hash.Hashing
import com.google.common.io.Files
import groovy.json.JsonOutput
import groovy.json.JsonSlurper
import java.io.ByteArrayOutputStream
import java.nio.charset.Charset
import java.io.StringWriter
import java.util.Properties
import java.util.TimeZone
import java.util.Date
import java.text.SimpleDateFormat
import org.gradle.configurationcache.extensions.capitalized

plugins {
id("com.osfans.trime.data-checksums")
id("com.android.application")
kotlin("android")
kotlin("plugin.serialization") version Versions.kotlin
id("com.google.devtools.ksp") version Versions.ksp
id("com.mikepenz.aboutlibraries.plugin")
}

fun exec(cmd: String): String = ByteArrayOutputStream().use {
project.exec {
commandLine = cmd.split(" ")
standardOutput = it
}
it.toString().trim()
}
fun envOrDefault(env: String, default: () -> String): String {
val v = System.getenv(env)
return if (v.isNullOrBlank()) default() else v
}

val gitUserOrCIName = envOrDefault("CI_NAME") {
exec("git config user.name")
}
val gitVersionName = exec("git describe --tags --long --always")
val gitHashShort = exec("git rev-parse --short HEAD")
val gitRemoteUrl = exec("git remote get-url origin")
.replaceFirst("^git@github\\.com:", "https://github.com/")
.replaceFirst("\\.git\$", "")

fun buildInfo(): String {
val writer = StringWriter()
val time = SimpleDateFormat("yyyy-MM-dd HH:mm:ss").apply {
timeZone = TimeZone.getTimeZone("UTC")
}.format(Date(System.currentTimeMillis()))
writer.append("Builder: ${gitUserOrCIName}\\n")
writer.append("Build Time: $time UTC\\n")
writer.append("Build Version Name: ${gitVersionName}\\n")
writer.append("Git Hash: ${gitHashShort}\\n")
writer.append("Git Repo: $gitRemoteUrl")
val info = writer.toString()
println(info)
return info
}

android {
namespace = "com.osfans.trime"
compileSdk = 34
Expand All @@ -73,10 +28,11 @@ android {

multiDexEnabled = true
setProperty("archivesBaseName", "trime-$versionName")
buildConfigField("String", "BUILD_GIT_HASH", "\"${gitHashShort}\"")
buildConfigField("String", "BUILD_GIT_REPO", "\"${gitRemoteUrl}\"")
buildConfigField("String", "BUILD_VERSION_NAME", "\"${gitVersionName}\"")
buildConfigField("String", "BUILD_INFO", "\"${buildInfo()}\"")
buildConfigField("String", "BUILDER", "\"${project.builder}\"")
buildConfigField("long", "BUILD_TIMESTAMP", project.buildTimestamp)
buildConfigField("String", "BUILD_COMMIT_HASH", "\"${project.buildCommitHash}\"")
buildConfigField("String", "BUILD_GIT_REPO", "\"${project.buildGitRepo}\"")
buildConfigField("String", "BUILD_VERSION_NAME", "\"${project.buildVersionName}\"")
}

signingConfigs {
Expand Down Expand Up @@ -184,20 +140,13 @@ ksp {
arg("room.schemaLocation", "$projectDir/schemas")
}

val generateDataChecksum by tasks.register<DataChecksumsTask>("generateDataChecksum") {
inputDir.set(file("src/main/assets"))
outputFile.set(file("src/main/assets/checksums.json"))
}

android.applicationVariants.all {
val variantName = name.capitalizeUS()
tasks.findByName("merge${variantName}Assets")?.dependsOn(generateDataChecksum)
val variantName = name.capitalized()
tasks.findByName("generateDataChecksums")?.also {
tasks.getByName("merge${variantName}Assets").dependsOn(it)
}
}

tasks.register<Delete>("cleanGeneratedAssets") {
delete(file("src/main/assets/checksums.json"))
}.also { tasks.clean.dependsOn(it) }

tasks.register<Delete>("cleanCxxIntermediates") {
delete(file(".cxx"))
}.also { tasks.clean.dependsOn(it) }
Expand Down Expand Up @@ -236,83 +185,3 @@ dependencies {
testImplementation("junit:junit:4.13.2")
androidTestImplementation("junit:junit:4.13.2")
}

abstract class DataChecksumsTask : DefaultTask() {
@get:Incremental
@get:PathSensitive(PathSensitivity.NAME_ONLY)
@get:InputDirectory
abstract val inputDir: DirectoryProperty

@get:OutputFile
abstract val outputFile: RegularFileProperty

private val file by lazy { outputFile.get().asFile }

private fun serialize(map: Map<String, String>) {
file.deleteOnExit()
file.writeText(
JsonOutput.prettyPrint(
JsonOutput.toJson(
mapOf<Any, Any>(
"sha256" to Hashing.sha256()
.hashString(
map.entries.joinToString { it.key + it.value },
Charset.defaultCharset()
)
.toString(),
"files" to map
)
)
)
)
}

@Suppress("UNCHECKED_CAST")
private fun deserialize(): Map<String, String> =
((JsonSlurper().parseText(file.readText()) as Map<Any, Any>))["files"] as Map<String, String>

companion object {
fun sha256(file: File): String =
Files.asByteSource(file).hash(Hashing.sha256()).toString()
}

@TaskAction
fun execute(inputChanges: InputChanges) {
val map =
file.exists()
.takeIf { it }
?.runCatching {
deserialize()
// remove all old dirs
.filterValues { it.isNotBlank() }
.toMutableMap()
}
?.getOrNull()
?: mutableMapOf()

fun File.allParents(): List<File> =
if (parentFile == null || parentFile.path in map)
listOf()
else
listOf(parentFile) + parentFile.allParents()
inputChanges.getFileChanges(inputDir).forEach { change ->
if (change.file.name == file.name)
return@forEach
logger.log(LogLevel.DEBUG, "${change.changeType}: ${change.normalizedPath}")
val relativeFile = change.file.relativeTo(file.parentFile)
val key = relativeFile.path
if (change.changeType == ChangeType.REMOVED) {
map.remove(key)
} else {
map[key] = sha256(change.file)
}
}
// calculate dirs
inputDir.asFileTree.forEach {
it.relativeTo(file.parentFile).allParents().forEach { p ->
map[p.path] = ""
}
}
serialize(map.toSortedMap())
}
}
15 changes: 11 additions & 4 deletions app/src/main/java/com/osfans/trime/ui/fragments/AboutFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ import androidx.navigation.fragment.findNavController
import androidx.preference.Preference
import androidx.preference.get
import com.blankj.utilcode.util.ToastUtils
import com.osfans.trime.BuildConfig
import com.osfans.trime.R
import com.osfans.trime.core.Rime
import com.osfans.trime.data.opencc.OpenCCDictManager
import com.osfans.trime.ui.components.PaddingPreferenceFragment
import com.osfans.trime.ui.main.MainViewModel
import com.osfans.trime.util.Const
import com.osfans.trime.util.formatDateTime
import com.osfans.trime.util.optionalPreference
import com.osfans.trime.util.thirdPartySummary
import splitties.systemservices.clipboardManager
Expand All @@ -35,11 +35,18 @@ class AboutFragment : PaddingPreferenceFragment() {
intent =
Intent(
Intent.ACTION_VIEW,
Uri.parse("${Const.currentGitRepo}/commits/${Const.buildGitHash}"),
Uri.parse("${Const.currentGitRepo}/commits/${Const.buildCommitHash}"),
)
}
get<Preference>("about__buildinfo")?.apply {
summary = BuildConfig.BUILD_INFO
get<Preference>("about__build_info")?.apply {
summary =
requireContext().getString(
R.string.about__build_info_format,
Const.builder,
formatDateTime(Const.buildTimestamp),
Const.buildCommitHash,
Const.currentGitRepo,
)
setOnPreferenceClickListener {
val info = ClipData.newPlainText("BuildInfo", summary)
clipboardManager.setPrimaryClip(info)
Expand Down
4 changes: 3 additions & 1 deletion app/src/main/java/com/osfans/trime/util/Const.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package com.osfans.trime.util
import com.osfans.trime.BuildConfig

object Const {
val buildGitHash = BuildConfig.BUILD_GIT_HASH
val builder = BuildConfig.BUILDER
val buildTimestamp = BuildConfig.BUILD_TIMESTAMP
val buildCommitHash = BuildConfig.BUILD_COMMIT_HASH
val displayVersionName = "${BuildConfig.BUILD_VERSION_NAME}-${BuildConfig.BUILD_TYPE}"
val originalGitRepo = "https://github.com/osfans/trime"
val currentGitRepo = BuildConfig.BUILD_GIT_REPO
Expand Down
7 changes: 4 additions & 3 deletions app/src/main/res/values-zh-rCN/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
<string name="pref_credits">贡献代码</string>
<string name="about__product_prime">微软平台PRIME输入法</string>
<string name="about__privacy_policy">隐私策略</string>
<string name="about__build_info_format">构建者:%1$s\nGit 仓库: %2$s\n构建 Git 哈希: %3$s\n构建时间: %4$s</string>
<string name="pref_enable">启用</string>
<string name="pref_enable_summary">启用同文输入法</string>
<string name="pref_select">选取软键盘</string>
Expand Down Expand Up @@ -100,7 +101,7 @@
<string name="profile_sync_in_background">后台同步</string>
<string name="profile_enable_syncing_in_background">点击以开启后台同步</string>
<string name="profile_timing_sync">定时同步</string>
<string name="profile_timing_sync_trigger_time">已开启定时同步 下次同步时间: %s</string>
<string name="profile_timing_sync_trigger_time">已开启定时同步 下次同步时间: %1$s</string>
<string name="profile_enable_timing_sync">点击以开启定时同步</string>
<string name="pref_help__marketplace">应用市场</string>
<string name="pref_help__user_community">用户社区</string>
Expand Down Expand Up @@ -181,7 +182,7 @@
<string name="keyboard__swipe_enabled">允许触发按键的滑动手势</string>
<string name="keyboard__key_swipe_velocity">触发按键滑动手势的速度(距离/速度满足其一即可)</string>
<string name="keyboard__key_swipe_velocity_hi">连续击键时触发滑动手势的速度</string>
<string name="about__buildinfo">编译信息</string>
<string name="about__build_info">构建信息</string>
<string name="keyboard__use_mini_keyboard_title">连接实体键盘时,显示迷你软键盘</string>
<string name="pref_trime_custom_qq">修改版QQ群</string>
<string name="pref_keyboard__candidate">候选栏</string>
Expand Down Expand Up @@ -234,7 +235,7 @@
<string name="maintenance">维护</string>
<string name="loading">正在加载</string>
<string name="profile_reset">恢复默认设置</string>
<string name="profile_last_sync_in_background">上次后台同步时间:%s\n状态:%s</string>
<string name="profile_last_sync_in_background">上次后台同步时间:%1$s\n状态:%2$s</string>
<string name="success">成功</string>
<string name="failure">失败</string>
<string name="pref_profile_summary">设定存储位置和修改同步设置等</string>
Expand Down
7 changes: 4 additions & 3 deletions app/src/main/res/values-zh-rTW/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
<string name="pref_credits">貢獻代碼</string>
<string name="about__product_prime">Win10 PRIME輸入法平臺</string>
<string name="about__privacy_policy">隱私權政策</string>
<string name="about__build_info_format">建構者:%1$s\nGit 倉庫: %2$s\n建構 Git 雜湊值: %3$s\n建構時間: %4$s</string>
<string name="pref_enable">啓用</string>
<string name="pref_enable_summary">啓用同文輸入法平臺</string>
<string name="pref_select">選取軟鍵盤</string>
Expand Down Expand Up @@ -105,7 +106,7 @@
<string name="profile_never_sync_in_background">從未在後臺同步過</string>
<string name="profile_enable_syncing_in_background">點擊以啟用後台同步</string>
<string name="profile_timing_sync">定時同步</string>
<string name="profile_timing_sync_trigger_time">已啟用定時同步 下次同步時間: %s</string>
<string name="profile_timing_sync_trigger_time">已啟用定時同步 下次同步時間: %1$s</string>
<string name="profile_enable_timing_sync">點擊以啟用定時同步</string>
<string name="pref_help__marketplace">應用市場</string>
<string name="pref_help__user_community">使用者社群</string>
Expand Down Expand Up @@ -185,7 +186,7 @@
<string name="keyboard__swipe_enabled">允許觸發按鍵的滑動手勢</string>
<string name="keyboard__key_swipe_velocity">觸發按鍵滑動手勢的速度(距離/速度滿足其一即可)</string>
<string name="keyboard__key_swipe_velocity_hi">連續擊鍵時觸發滑動手勢的速度</string>
<string name="about__buildinfo">編譯信息</string>
<string name="about__build_info">建構資訊</string>
<string name="keyboard__use_mini_keyboard_title">連接實體鍵盤時,顯示迷你軟鍵盤</string>
<string name="pref_trime_custom_qq">修改版QQ羣</string>
<string name="pref_keyboard__candidate">候選欄</string>
Expand Down Expand Up @@ -238,7 +239,7 @@
<string name="maintenance">維護</string>
<string name="loading">正在載入</string>
<string name="profile_sync_in_background">後臺同步</string>
<string name="profile_last_sync_in_background">上次後臺同步時間:%s\n狀態:%s</string>
<string name="profile_last_sync_in_background">上次後臺同步時間:%1$s\n狀態:%2$s</string>
<string name="success">成功</string>
<string name="failure">失敗</string>
<string name="other_managed_clipboard">剪貼板內容</string>
Expand Down
7 changes: 4 additions & 3 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
<string name="pref_credits">Contribute</string>
<string name="about__product_prime">Win10 PRIME Platform</string>
<string name="about__privacy_policy">Privacy Policy</string>
<string name="about__build_info_format">Builder: %1$s\nGit Repo: %2$s\nBuild Git Hash: %3$s\nBuild Time: %4$s</string>
<string name="pref_enable">Enable</string>
<string name="pref_enable_summary">Enable Trime</string>
<string name="pref_select">Change Keyboard</string>
Expand Down Expand Up @@ -103,11 +104,11 @@
<string name="profile_shared_data_dir">Shared directory</string>
<string name="profile_user_data_dir">User directory</string>
<string name="profile_sync_in_background">Sync in background</string>
<string name="profile_last_sync_in_background">Last sync in background: %s\nStatus: %s</string>
<string name="profile_last_sync_in_background">Last sync in background: %1$s\nStatus: %2$s</string>
<string name="profile_never_sync_in_background">Never synced in the background</string>
<string name="profile_enable_syncing_in_background">Click to enable</string>
<string name="profile_timing_sync">Timing sync</string>
<string name="profile_timing_sync_trigger_time">Timing sync is enabled Next sync at time: %s</string>
<string name="profile_timing_sync_trigger_time">Timing sync is enabled Next sync at time: %1$s</string>
<string name="profile_enable_timing_sync">Click to enable</string>
<string name="pref_help__marketplace">Marketplace</string>
<string name="pref_help__user_community">User Community</string>
Expand Down Expand Up @@ -189,7 +190,7 @@
<string name="keyboard__swipe_enabled">Allow swipe gestures to trigger keys</string>
<string name="keyboard__key_swipe_velocity">The speed of triggering the button swipe gesture (the distance/velocity is sufficient)</string>
<string name="keyboard__key_swipe_velocity_hi">The velocity of the swipe gesture on consecutive keystrokes</string>
<string name="about__buildinfo">Build info</string>
<string name="about__build_info">Build Info</string>
<string name="keyboard__use_mini_keyboard_title">Show mini keyboard with real keyboard attached</string>
<string name="pref_trime_custom_qq">Custom QQ Group</string>
<string name="pref_trime_custom_qq_summary" translatable="false"> </string>
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/res/xml/about_preference.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
app:iconSpaceReserved="false">
</Preference>

<Preference app:key="about__buildinfo"
app:title="@string/about__buildinfo"
<Preference app:key="about__build_info"
app:title="@string/about__build_info"
app:iconSpaceReserved="false"> />
</Preference>

Expand Down
18 changes: 18 additions & 0 deletions buildSrc/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,7 +1,25 @@
plugins {
`kotlin-dsl`
kotlin("plugin.serialization") version embeddedKotlinVersion
}

repositories {
google()
mavenCentral()
gradlePluginPortal()
}

dependencies {
compileOnly("com.android.tools.build:gradle:8.2.0")
implementation("org.jetbrains.kotlin:kotlin-gradle-plugin:1.9.20")
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.6.1")
}

gradlePlugin {
plugins {
register("dataChecksums") {
id = "com.osfans.trime.data-checksums"
implementationClass = "DataChecksumsPlugin"
}
}
}
Loading

0 comments on commit 0e90c1b

Please sign in to comment.