Skip to content

Commit 3e3abdd

Browse files
committed
refactor: bundle core native lib version name into BuildConfig
1 parent 4001d78 commit 3e3abdd

File tree

10 files changed

+56
-72
lines changed

10 files changed

+56
-72
lines changed

app/src/main/java/com/osfans/trime/core/Rime.kt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -397,9 +397,6 @@ class Rime :
397397
@JvmStatic
398398
external fun forgetRimeCandidate(index: Int): Boolean
399399

400-
@JvmStatic
401-
external fun getLibrimeVersion(): String
402-
403400
// module
404401
@JvmStatic
405402
external fun runRimeTask(taskName: String?): Boolean

app/src/main/java/com/osfans/trime/data/opencc/OpenCCDictManager.kt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,6 @@ object OpenCCDictManager {
115115
configFileName: String,
116116
): String
117117

118-
@JvmStatic
119-
external fun getOpenCCVersion(): String
120-
121118
const val MODE_BIN_TO_TXT = true // OCD(2) to TXT
122119
const val MODE_TXT_TO_BIN = false // TXT to OCD2
123120
}

app/src/main/java/com/osfans/trime/ui/fragments/AboutFragment.kt

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,18 @@
44

55
package com.osfans.trime.ui.fragments
66

7-
import android.content.ClipData
87
import android.content.Intent
98
import android.net.Uri
109
import android.os.Bundle
11-
import android.widget.Toast
1210
import androidx.fragment.app.activityViewModels
1311
import androidx.navigation.fragment.findNavController
1412
import androidx.preference.Preference
1513
import androidx.preference.get
1614
import com.osfans.trime.R
17-
import com.osfans.trime.core.Rime
18-
import com.osfans.trime.data.opencc.OpenCCDictManager
1915
import com.osfans.trime.ui.components.PaddingPreferenceFragment
2016
import com.osfans.trime.ui.main.MainViewModel
2117
import com.osfans.trime.util.Const
2218
import com.osfans.trime.util.formatDateTime
23-
import com.osfans.trime.util.toast
24-
import splitties.systemservices.clipboardManager
2519

2620
class AboutFragment : PaddingPreferenceFragment() {
2721
private val viewModel: MainViewModel by activityViewModels()
@@ -33,40 +27,35 @@ class AboutFragment : PaddingPreferenceFragment() {
3327
setPreferencesFromResource(R.xml.about_preference, rootKey)
3428
with(preferenceScreen) {
3529
get<Preference>("about__changelog")?.apply {
36-
summary = Const.displayVersionName
30+
summary = Const.VERSION_NAME
3731
isCopyingEnabled = true
3832
intent =
3933
Intent(
4034
Intent.ACTION_VIEW,
41-
Uri.parse("${Const.currentGitRepo}/commits/${Const.buildCommitHash}"),
35+
Uri.parse("${Const.GIT_REPO}/commits/${Const.BUILD_COMMIT_HASH}"),
4236
)
4337
}
4438
get<Preference>("about__build_info")?.apply {
4539
summary =
4640
requireContext().getString(
4741
R.string.about__build_info_format,
48-
Const.builder,
49-
Const.currentGitRepo,
50-
Const.buildCommitHash,
51-
formatDateTime(Const.buildTimestamp),
42+
Const.BUILDER,
43+
Const.GIT_REPO,
44+
Const.BUILD_COMMIT_HASH,
45+
formatDateTime(Const.BUILD_TIMESTAMP),
5246
)
53-
setOnPreferenceClickListener {
54-
val info = ClipData.newPlainText("BuildInfo", summary)
55-
clipboardManager.setPrimaryClip(info)
56-
context.toast(R.string.copy_done, Toast.LENGTH_LONG)
57-
true
58-
}
47+
isCopyingEnabled = true
5948
}
6049
get<Preference>("about__librime_version")?.apply {
61-
val code = Rime.getLibrimeVersion()
50+
val code = Const.LIBRIME_VERSION
6251
val hash = extractCommitHash(code)
6352
summary = code
6453
intent?.data?.also {
6554
intent!!.data = Uri.withAppendedPath(it, "commit/$hash")
6655
}
6756
}
6857
get<Preference>("about__opencc_version")?.apply {
69-
val code = OpenCCDictManager.getOpenCCVersion()
58+
val code = Const.OPENCC_VERSION
7059
val hash = extractCommitHash(code)
7160
summary = code
7261
intent?.data?.also {
@@ -92,7 +81,7 @@ class AboutFragment : PaddingPreferenceFragment() {
9281
private val COMMON_PATTERN = Regex("^([^-]*)(-.*)$")
9382

9483
private fun extractCommitHash(versionCode: String): String =
95-
DASH_G_PATTERN.find(versionCode)?.groupValues?.get(1)
84+
DASH_G_PATTERN.find(versionCode)?.groupValues?.get(2)
9685
?: COMMON_PATTERN.find(versionCode)?.groupValues?.get(1)
9786
?: versionCode
9887
}

app/src/main/java/com/osfans/trime/util/Const.kt

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@ package com.osfans.trime.util
77
import com.osfans.trime.BuildConfig
88

99
object Const {
10-
val builder = BuildConfig.BUILDER
11-
val buildTimestamp = BuildConfig.BUILD_TIMESTAMP
12-
val buildCommitHash = BuildConfig.BUILD_COMMIT_HASH
13-
val displayVersionName = "${BuildConfig.BUILD_VERSION_NAME}-${BuildConfig.BUILD_TYPE}"
14-
val originalGitRepo = "https://github.com/osfans/trime"
15-
val currentGitRepo = BuildConfig.BUILD_GIT_REPO
10+
const val BUILDER = BuildConfig.BUILDER
11+
const val BUILD_TIMESTAMP = BuildConfig.BUILD_TIMESTAMP
12+
const val BUILD_COMMIT_HASH = BuildConfig.BUILD_COMMIT_HASH
13+
const val VERSION_NAME = "${BuildConfig.BUILD_VERSION_NAME}-${BuildConfig.BUILD_TYPE}"
14+
const val GIT_REPO = BuildConfig.BUILD_GIT_REPO
15+
16+
const val LIBRIME_VERSION = BuildConfig.LIBRIME_VERSION
17+
const val OPENCC_VERSION = BuildConfig.OPENCC_VERSION
1618
}

app/src/main/java/com/osfans/trime/util/DeviceInfo.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ object DeviceInfo {
3939
)
4040
appendLine("--------- Build Info")
4141
appendLine("Package Name: ${BuildConfig.APPLICATION_ID}")
42-
appendLine("Builder: ${Const.builder}")
42+
appendLine("Builder: ${Const.BUILDER}")
4343
appendLine("Version Code: ${BuildConfig.VERSION_CODE}")
44-
appendLine("Version Name: ${Const.displayVersionName}")
45-
appendLine("Build Time: ${iso8601UTCDateTime(Const.buildTimestamp)}")
46-
appendLine("Build Git Hash: ${Const.buildCommitHash}")
44+
appendLine("Version Name: ${Const.VERSION_NAME}")
45+
appendLine("Build Time: ${iso8601UTCDateTime(Const.BUILD_TIMESTAMP)}")
46+
appendLine("Build Git Hash: ${Const.BUILD_COMMIT_HASH}")
4747
}
4848
}

app/src/main/jni/librime_jni/CMakeLists.txt

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,6 @@ execute_process(
2626
)
2727
string(STRIP ${TRIME_VERSION} TRIME_VERSION)
2828

29-
execute_process(
30-
COMMAND git --git-dir ${CMAKE_SOURCE_DIR}/OpenCC/.git describe --tags
31-
OUTPUT_VARIABLE OPENCC_VERSION
32-
)
33-
string(STRIP ${OPENCC_VERSION} OPENCC_VERSION)
34-
35-
execute_process(
36-
COMMAND git --git-dir ${CMAKE_SOURCE_DIR}/librime/.git describe --tags --exclude "latest"
37-
OUTPUT_VARIABLE LIBRIME_VERSION
38-
)
39-
string(STRIP ${LIBRIME_VERSION} LIBRIME_VERSION)
40-
4129
target_compile_definitions(rime_jni PRIVATE
4230
TRIME_VERSION="${TRIME_VERSION}"
43-
OPENCC_VERSION="${OPENCC_VERSION}"
44-
LIBRIME_VERSION="${LIBRIME_VERSION}"
4531
)

app/src/main/jni/librime_jni/opencc.cc

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,6 @@
1212

1313
// opencc
1414

15-
extern "C" JNIEXPORT jstring JNICALL
16-
Java_com_osfans_trime_data_opencc_OpenCCDictManager_getOpenCCVersion(
17-
JNIEnv *env, jclass clazz) {
18-
return env->NewStringUTF(OPENCC_VERSION);
19-
}
20-
2115
extern "C" JNIEXPORT jstring JNICALL
2216
Java_com_osfans_trime_data_opencc_OpenCCDictManager_openCCLineConv(
2317
JNIEnv *env, jclass clazz, jstring input, jstring config_file_name) {

app/src/main/jni/librime_jni/rime_jni.cc

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -474,12 +474,6 @@ Java_com_osfans_trime_core_Rime_forgetRimeCandidate(JNIEnv *env,
474474
return Rime::Instance().forgetCandidate(index);
475475
}
476476

477-
extern "C" JNIEXPORT jstring JNICALL
478-
Java_com_osfans_trime_core_Rime_getLibrimeVersion(JNIEnv *env,
479-
jclass /* thiz */) {
480-
return env->NewStringUTF(LIBRIME_VERSION);
481-
}
482-
483477
extern "C" JNIEXPORT jboolean JNICALL
484478
Java_com_osfans_trime_core_Rime_runRimeTask(JNIEnv *env, jclass /* thiz */,
485479
jstring task_name) {

build-logic/convention/src/main/kotlin/NativeAppConventionPlugin.kt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,20 @@ import org.gradle.api.Project
77
import org.gradle.kotlin.dsl.configure
88

99
class NativeAppConventionPlugin : NativeBaseConventionPlugin() {
10+
private val Project.librimeVersion: String
11+
get() =
12+
runCmd(
13+
"git describe --tags --long --always --exclude 'latest'",
14+
workingDir = file("src/main/jni/librime"),
15+
)
16+
17+
private val Project.openccVersion: String
18+
get() =
19+
runCmd(
20+
"git describe --tags --long --always",
21+
workingDir = file("src/main/jni/OpenCC"),
22+
)
23+
1024
override fun apply(target: Project) {
1125
super.apply(target)
1226

@@ -16,6 +30,10 @@ class NativeAppConventionPlugin : NativeBaseConventionPlugin() {
1630
useLegacyPackaging = true
1731
}
1832
}
33+
defaultConfig {
34+
buildConfigField("String", "LIBRIME_VERSION", "\"${target.librimeVersion}\"")
35+
buildConfigField("String", "OPENCC_VERSION", "\"${target.openccVersion}\"")
36+
}
1937
}
2038
}
2139
}

build-logic/convention/src/main/kotlin/ProjectExtensions.kt

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,22 @@ import java.io.File
1010
import kotlin.io.encoding.Base64
1111
import kotlin.io.encoding.ExperimentalEncodingApi
1212

13-
fun Project.runCmd(cmd: String): String =
14-
ByteArrayOutputStream().use {
15-
project.exec {
16-
commandLine = cmd.split(" ")
17-
standardOutput = it
13+
fun Project.runCmd(
14+
cmd: String,
15+
default: String = "",
16+
workingDir: File? = null,
17+
): String {
18+
val stdout = ByteArrayOutputStream()
19+
val result =
20+
stdout.use {
21+
project.exec {
22+
commandLine = cmd.split(" ")
23+
standardOutput = stdout
24+
workingDir?.let { this.workingDir = it }
25+
}
1826
}
19-
it.toString().trim()
20-
}
27+
return if (result.exitValue == 0) stdout.toString().trim() else default
28+
}
2129

2230
val Project.assetsDir: File
2331
get() = file("src/main/assets").also { it.mkdirs() }
@@ -44,8 +52,7 @@ val Project.buildGitRepo
4452
get() =
4553
envOrProp("BUILD_GIT_REPO", "buildGitRepo") {
4654
runCmd("git remote get-url origin")
47-
.replaceFirst("^git@github\\.com:", "https://github.com/")
48-
.replaceFirst("\\.git\$", "")
55+
.replace("git@([^:]+):(.+)/(.+)\\.git".toRegex(), "https://$1/$2/$3")
4956
}
5057

5158
val Project.buildVersionName

0 commit comments

Comments
 (0)