Skip to content

Commit

Permalink
build: implement the gradle task to calculate native cache hash
Browse files Browse the repository at this point in the history
  • Loading branch information
WhiredPlanck committed Jan 15, 2024
1 parent 82e82b2 commit 521ea1f
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 6 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/commit-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ jobs:
- name: Calculate JNI cache hash
id: cache-hash
shell: bash
run: script/cache-hash.sh
run: |
./gradlew :app:calculateNativeCacheHash
- name: Fetch JNI cache
uses: actions/cache@v3
id: jni-cache
with:
path: "app/prebuilt"
key: ${{ matrix.os }}-trime-jni-debug-${{ steps.cache-hash.outputs.hash }}
key: ${{ matrix.os }}-trime-jni-debug-${{ steps.cache-hash.outputs.native-cache-hash }}

- name: Fetch submodules
if: ${{ !steps.jni-cache.outputs.cache-hit }}
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/pull-request-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@ jobs:
- name: Calculate JNI cache hash
id: cache-hash
shell: bash
run: script/cache-hash.sh
run: |
./gradlew :app:calculateNativeCacheHash
- name: Fetch JNI cache
uses: actions/cache@v3
id: jni-cache
with:
path: "app/prebuilt"
key: ${{ matrix.os }}-trime-jni-debug-${{ steps.cache-hash.outputs.hash }}
key: ${{ matrix.os }}-trime-jni-debug-${{ steps.cache-hash.outputs.native-cache-hash }}

- name: Fetch submodules
if: ${{ !steps.jni-cache.outputs.cache-hit }}
Expand Down
1 change: 1 addition & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import org.gradle.configurationcache.extensions.capitalized
plugins {
id("com.osfans.trime.native-app-convention")
id("com.osfans.trime.data-checksums")
id("com.osfans.trime.native-cache-hash")
alias(libs.plugins.aboutlibraries)
alias(libs.plugins.kotlin.android)
alias(libs.plugins.kotlin.serialization)
Expand Down
4 changes: 4 additions & 0 deletions build-logic/convention/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,9 @@ gradlePlugin {
id = "com.osfans.trime.native-app-convention"
implementationClass = "NativeAppConventionPlugin"
}
register("nativeCacheHash") {
id = "com.osfans.trime.native-cache-hash"
implementationClass = "NativeCacheHashPlugin"
}
}
}
53 changes: 53 additions & 0 deletions build-logic/convention/src/main/kotlin/NativeCacheHashPlugin.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import ApkRelease.buildApkRelease
import Versions.cmakeVersion
import Versions.ndkVersion
import org.gradle.api.DefaultTask
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.logging.LogLevel
import org.gradle.api.tasks.TaskAction
import org.gradle.kotlin.dsl.task
import org.jetbrains.kotlin.com.google.common.hash.Hashing
import org.jetbrains.kotlin.com.google.common.io.ByteSource
import java.io.File

class NativeCacheHashPlugin : Plugin<Project> {
override fun apply(target: Project) {
target.task<NativeCacheHashCalcTask>("calculateNativeCacheHash")
}

abstract class NativeCacheHashCalcTask : DefaultTask() {
companion object {
fun sha256(file: File): String = ByteSource.wrap(file.readBytes()).hash(Hashing.sha256()).toString()

fun sha256(string: String) = ByteSource.wrap(string.encodeToByteArray()).hash(Hashing.sha256()).toString()
}

@TaskAction
fun execute() {
with(project) {
val magic =
buildString {
appendLine(cmakeVersion)
appendLine(ndkVersion)
if (!buildApkRelease) {
appendLine(buildABI)
}
appendLine(runCmd("git submodule status"))
fileTree("src/main/jni/cmake").forEach { module ->
appendLine(sha256(module))
}
fileTree("src/main/jni/librime_jni").forEach { source ->
appendLine(sha256(source))
}
appendLine(sha256(file("src/main/jni/CMakeLists.txt")))
}
val hash = sha256(magic)
logger.log(LogLevel.DEBUG, "Native Cache Hash: $hash")
System.getenv("GITHUB_OUTPUT")?.takeIf { it.isNotBlank() }?.let {
File(it).appendText("native-cache-hash=$hash")
}
}
}
}
}

0 comments on commit 521ea1f

Please sign in to comment.