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

41 lines
1.3 KiB
Kotlin
Raw Normal View History

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