Created
November 30, 2019 08:26
-
-
Save hwaastad/3eea474d9e93db24f244f38e79490ece to your computer and use it in GitHub Desktop.
Transformer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.github.jengelman.gradle.plugins.shadow.transformers.Transformer | |
import com.github.jengelman.gradle.plugins.shadow.transformers.TransformerContext | |
import org.gradle.api.file.FileTreeElement | |
import shadow.org.apache.tools.zip.ZipEntry | |
import shadow.org.apache.tools.zip.ZipOutputStream | |
class OpenWebBeansPropertiesTransformer implements Transformer { | |
String resource | |
private final List<Properties> configurations = new ArrayList<>() | |
private String ordinalKey = "configuration.ordinal" | |
private int defaultOrdinal = 100 | |
private boolean reverseOrder | |
@Override | |
boolean canTransformResource(FileTreeElement element) { | |
def path = element.relativePath.pathString | |
if (resource != null && resource.equalsIgnoreCase(path)) { | |
return true | |
} | |
return false | |
} | |
@Override | |
void transform(TransformerContext context) { | |
Properties p = new Properties() | |
try { | |
p.load(context.is) | |
} catch (IOException e) { | |
throw new IllegalStateException(e); | |
} | |
configurations.add(p) | |
} | |
@Override | |
boolean hasTransformedResource() { | |
return !configurations.isEmpty() | |
} | |
@Override | |
void modifyOutputStream(ZipOutputStream jos, boolean preserveFileTimestamps) { | |
Properties out = mergeProperties(sortProperties(configurations)) | |
try { | |
jos.putNextEntry(new ZipEntry(resource)) | |
out.store(jos, "# gradle " + resource + " merge") | |
jos.closeEntry() | |
} catch (IOException e) { | |
throw new IllegalStateException(e); | |
} | |
} | |
private List<Properties> sortProperties(List<Properties> allProperties) { | |
List<Properties> sortedProperties = new ArrayList<>() | |
for (Properties p : allProperties) { | |
int configOrder = getConfigurationOrdinal(p) | |
int i | |
for (i = 0; i < sortedProperties.size(); i++) { | |
int listConfigOrder = getConfigurationOrdinal(sortedProperties.get(i)) | |
if ((!reverseOrder && listConfigOrder > configOrder) || (reverseOrder && listConfigOrder < configOrder)) { | |
break | |
} | |
} | |
sortedProperties.add(i, p) | |
} | |
return sortedProperties | |
} | |
private int getConfigurationOrdinal(Properties p) { | |
String configOrderString = p.getProperty(ordinalKey) | |
if (configOrderString != null && configOrderString.length() > 0) { | |
return Integer.parseInt(configOrderString) | |
} | |
return defaultOrdinal | |
} | |
private static Properties mergeProperties(List<Properties> sortedProperties) { | |
Properties mergedProperties = new Properties() | |
for (Properties p : sortedProperties) { | |
mergedProperties.putAll(p) | |
} | |
return mergedProperties | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment