Skip to content

Commit

Permalink
Migrate MergeStrategy to enum
Browse files Browse the repository at this point in the history
  • Loading branch information
Goooler committed Nov 26, 2024
1 parent 2e904bc commit a039e01
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 11 deletions.
15 changes: 15 additions & 0 deletions api/shadow.api
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,21 @@ public class com/github/jengelman/gradle/plugins/shadow/transformers/PropertiesF
public fun transform (Lcom/github/jengelman/gradle/plugins/shadow/transformers/TransformerContext;)V
}

public final class com/github/jengelman/gradle/plugins/shadow/transformers/PropertiesFileTransformer$MergeStrategy : java/lang/Enum {
public static final field Append Lcom/github/jengelman/gradle/plugins/shadow/transformers/PropertiesFileTransformer$MergeStrategy;
public static final field Companion Lcom/github/jengelman/gradle/plugins/shadow/transformers/PropertiesFileTransformer$MergeStrategy$Companion;
public static final field First Lcom/github/jengelman/gradle/plugins/shadow/transformers/PropertiesFileTransformer$MergeStrategy;
public static final field Latest Lcom/github/jengelman/gradle/plugins/shadow/transformers/PropertiesFileTransformer$MergeStrategy;
public static final fun from (Ljava/lang/String;)Lcom/github/jengelman/gradle/plugins/shadow/transformers/PropertiesFileTransformer$MergeStrategy;
public static fun getEntries ()Lkotlin/enums/EnumEntries;
public static fun valueOf (Ljava/lang/String;)Lcom/github/jengelman/gradle/plugins/shadow/transformers/PropertiesFileTransformer$MergeStrategy;
public static fun values ()[Lcom/github/jengelman/gradle/plugins/shadow/transformers/PropertiesFileTransformer$MergeStrategy;
}

public final class com/github/jengelman/gradle/plugins/shadow/transformers/PropertiesFileTransformer$MergeStrategy$Companion {
public final fun from (Ljava/lang/String;)Lcom/github/jengelman/gradle/plugins/shadow/transformers/PropertiesFileTransformer$MergeStrategy;
}

public class com/github/jengelman/gradle/plugins/shadow/transformers/ServiceFileTransformer : com/github/jengelman/gradle/plugins/shadow/transformers/Transformer, org/gradle/api/tasks/util/PatternFilterable {
public fun <init> ()V
public fun <init> (Lorg/gradle/api/tasks/util/PatternSet;)V
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.github.jengelman.gradle.plugins.shadow.transformers

import com.github.jengelman.gradle.plugins.shadow.internal.CleanProperties
import com.github.jengelman.gradle.plugins.shadow.internal.property
import com.github.jengelman.gradle.plugins.shadow.transformers.PropertiesFileTransformer.MergeStrategy.values
import groovy.lang.Closure
import groovy.lang.Closure.IDENTITY
import java.io.ByteArrayOutputStream
Expand Down Expand Up @@ -112,11 +113,8 @@ public open class PropertiesFileTransformer @Inject constructor(
public open val mappings: MapProperty<String, Map<String, String>> =
objectFactory.mapProperty(String::class.java, Map::class.java) as MapProperty<String, Map<String, String>>

/**
* Optional values: first, latest, append.
*/
@get:Input
public open val mergeStrategy: Property<String> = objectFactory.property("first")
public open val mergeStrategy: Property<MergeStrategy> = objectFactory.property(MergeStrategy.First)

@get:Input
public open val mergeSeparator: Property<String> = objectFactory.property(",")
Expand Down Expand Up @@ -156,11 +154,10 @@ public open class PropertiesFileTransformer @Inject constructor(
} else {
for ((key, value) in incoming) {
if (props.containsKey(key)) {
when (mergeStrategyFor(context.path).lowercase()) {
"latest" -> props[key] = value
"append" -> props[key] = props.getProperty(key as String) + mergeSeparatorFor(context.path) + value
"first" -> Unit
else -> Unit
when (MergeStrategy.from(mergeStrategyFor(context.path))) {
MergeStrategy.Latest -> props[key] = value
MergeStrategy.Append -> props[key] = props.getProperty(key as String) + mergeSeparatorFor(context.path) + value
MergeStrategy.First -> Unit
}
} else {
props[key] = value
Expand Down Expand Up @@ -189,7 +186,7 @@ public open class PropertiesFileTransformer @Inject constructor(

private fun mergeStrategyFor(path: String): String {
val mappings = mappings.get()
val mergeStrategy = mergeStrategy.get()
val mergeStrategy = mergeStrategy.get().name

mappings[path]?.let {
return it["mergeStrategy"] ?: mergeStrategy
Expand Down Expand Up @@ -244,6 +241,21 @@ public open class PropertiesFileTransformer @Inject constructor(
return os.toByteArray().inputStream().reader(_charset)
}

public enum class MergeStrategy {
First,
Latest,
Append,
;

public companion object {
@JvmStatic
public fun from(value: String): MergeStrategy {
return values().find { it.name.equals(value, ignoreCase = true) }
?: throw IllegalArgumentException("Unknown merge strategy: $value")
}
}
}

private companion object {
private const val PROPERTIES_SUFFIX = ".properties"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class PropertiesFileTransformerSpec extends TransformerSpecSupport {
def element = getFileElement(path)
Transformer transformer = new PropertiesFileTransformer(objectFactory)
transformer.paths.set(paths)
transformer.mergeStrategy.set('first')
transformer.mergeStrategy.set(PropertiesFileTransformer.MergeStrategy.from('first'))

when:
if (transformer.canTransformResource(element)) {
Expand Down

0 comments on commit a039e01

Please sign in to comment.