Skip to content

Commit

Permalink
Polish API
Browse files Browse the repository at this point in the history
  • Loading branch information
natario1 committed Aug 25, 2024
1 parent 89494cd commit e550384
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 31 deletions.
38 changes: 17 additions & 21 deletions grease/src/main/kotlin/io/deepmedia/tools/grease/GreaseExtension.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,31 @@ package io.deepmedia.tools.grease
import com.github.jengelman.gradle.plugins.shadow.relocation.Relocator
import com.github.jengelman.gradle.plugins.shadow.relocation.SimpleRelocator
import com.github.jengelman.gradle.plugins.shadow.transformers.Transformer
import org.gradle.api.Action
import org.gradle.api.model.ObjectFactory
import org.gradle.kotlin.dsl.listProperty
import org.gradle.kotlin.dsl.newInstance
import org.gradle.kotlin.dsl.property
import javax.inject.Inject

abstract class GreaseExtension {
abstract class GreaseExtension @Inject constructor(objects: ObjectFactory) {

internal val relocators = mutableListOf<Relocator>()
internal val transformers = mutableListOf<Transformer>()
internal val prefix = objects.property<String>().convention("")
internal val relocators = objects.listProperty<Relocator>()
internal val transformers = objects.listProperty<Transformer>()

internal var isRelocationEnabled = false
var relocationPrefix: String = "shadow"
set(value) {
field = value
isRelocationEnabled = true
}
fun relocate(prefix: String = "grease") {
this.prefix.set(prefix)
}

fun relocate(
from: String,
to: String,
configure: (SimpleRelocator.() -> Unit)? = null
) {
fun relocate(from: String, to: String, configure: Action<SimpleRelocator> = Action { }) {
val relocator = SimpleRelocator(from, to, emptyList(), emptyList())
configure?.invoke(relocator)
configure.execute(relocator)
relocators.add(relocator)
}

fun <T : Transformer> transform(
transformer: T,
configure: (T.() -> Unit)? = null
) {
configure?.invoke(transformer)
fun <T : Transformer> transform(transformer: T, configure: Action<T> = Action { }) {
configure.execute(transformer)
transformers.add(transformer)
}

}
19 changes: 10 additions & 9 deletions grease/src/main/kotlin/io/deepmedia/tools/grease/GreasePlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -481,27 +481,28 @@ open class GreasePlugin : Plugin<Project> {
}
}

if (greaseExtension.isRelocationEnabled) {
val relocationPrefix = greaseExtension.prefix.get()
if (relocationPrefix.isNotEmpty()) {
greaseProcessTask.get().outputs.files
.asSequence()
.flatMap { inputFile -> inputFile.packageNames }
.distinct()
.forEach { packageName ->
val newPackageName = "${greaseExtension.relocationPrefix}.$packageName"
val newPackageName = "${relocationPrefix}.$packageName"
log.d { "Relocate package from $packageName to $newPackageName" }
relocate(packageName, newPackageName)
packagesToReplace[packageName] = newPackageName
}
}

greaseExtension.relocators.forEach { relocator ->
relocate(relocator)
if (relocator is SimpleRelocator) {
packagesToReplace[relocator.pattern] = relocator.shadedPattern
}
greaseExtension.relocators.get().forEach { relocator ->
relocate(relocator)
if (relocator is SimpleRelocator) {
packagesToReplace[relocator.pattern] = relocator.shadedPattern
}
greaseExtension.transformers.forEach(::transform)

}

greaseExtension.transformers.get().forEach(::transform)
}

doLast {
Expand Down
2 changes: 1 addition & 1 deletion tests/sample-library/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ plugins {
}

grease {
relocationPrefix = "temp"
relocate()
}

android {
Expand Down

0 comments on commit e550384

Please sign in to comment.