42 lines
1.4 KiB
Kotlin
Raw Normal View History

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