Magisk/buildSrc/src/main/java/BuildSource.kt

40 lines
1.4 KiB
Kotlin
Raw Normal View History

2020-12-24 12:46:31 +00:00
import org.eclipse.jgit.api.Git
import org.eclipse.jgit.internal.storage.file.FileRepository
2020-07-04 13:53:31 +00:00
import org.gradle.api.Plugin
import org.gradle.api.Project
2020-08-15 12:43:28 +00:00
import org.gradle.kotlin.dsl.provideDelegate
2020-07-04 13:53:31 +00:00
import java.io.File
import java.util.*
private val props = Properties()
2020-12-24 12:46:31 +00:00
private lateinit var commitHash: String
private var commitCount = 0
2020-07-04 13:53:31 +00:00
object Config {
2021-01-25 15:33:09 +00:00
operator fun get(key: String): String? {
2020-12-24 12:46:31 +00:00
val v = props[key] as? String ?: return null
return if (v.isBlank()) null else v
}
2021-01-25 15:33:09 +00:00
2020-12-24 12:46:31 +00:00
fun contains(key: String) = get(key) != null
2021-01-25 15:33:09 +00:00
val version: String get() = get("version") ?: commitHash
val versionCode: Int get() = get("magisk.versionCode")!!.toInt()
2021-01-23 04:45:37 +00:00
val stubVersion: String get() = get("magisk.stubVersion")!!
2020-07-04 13:53:31 +00:00
}
class MagiskPlugin : Plugin<Project> {
override fun apply(project: Project) {
project.rootProject.file("gradle.properties").inputStream().use { props.load(it) }
2020-08-15 12:43:28 +00:00
val configPath: String? by project
2020-12-25 21:03:25 +00:00
val config = configPath?.let { File(it) } ?: project.rootProject.file("config.prop")
if (config.exists())
config.inputStream().use { props.load(it) }
2020-12-25 13:34:15 +00:00
val repo = FileRepository(project.rootProject.file(".git"))
val refId = repo.refDatabase.exactRef("HEAD").objectId
commitHash = repo.newObjectReader().abbreviate(refId, 8).name()
commitCount = Git(repo).log().add(refId).call().count()
2020-07-04 13:53:31 +00:00
}
}