aboutsummaryrefslogtreecommitdiff
path: root/buildSrc/src/main/groovy/org/jetbrains/PluginXmlTransformer.groovy
blob: e711388f481b40aa99f276757f0f1bc87448fce8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package org.jetbrains

import com.github.jengelman.gradle.plugins.shadow.relocation.RelocateClassContext
import com.github.jengelman.gradle.plugins.shadow.relocation.Relocator
import com.github.jengelman.gradle.plugins.shadow.transformers.Transformer
import com.github.jengelman.gradle.plugins.shadow.transformers.TransformerContext
import groovy.xml.XmlUtil
import org.gradle.api.file.FileTreeElement
import shadow.org.apache.tools.zip.ZipEntry
import shadow.org.apache.tools.zip.ZipOutputStream

public class PluginXmlTransformer implements Transformer {
    private Map<String, Node> transformedPluginXmlFiles = new HashMap<>();

    @Override
    boolean canTransformResource(FileTreeElement fileTreeElement) {
        return fileTreeElement.relativePath.segments.contains("META-INF") && fileTreeElement.name.endsWith(".xml")
    }

    @Override
    void transform(TransformerContext context) {
        def path = context.path
        def inputStream = context.is
        System.out.println(path)
        Node node = new XmlParser().parse(inputStream)
        relocateXml(node, context)
        transformedPluginXmlFiles.put(path, node)
    }

    @Override
    boolean hasTransformedResource() {
        return !transformedPluginXmlFiles.isEmpty()
    }

    @Override
    void modifyOutputStream(ZipOutputStream zipOutputStream) {
        for (Map.Entry<String, Node> entry : transformedPluginXmlFiles.entrySet()) {
            zipOutputStream.putNextEntry(new ZipEntry(entry.key))
            XmlUtil.serialize(entry.value, zipOutputStream)
        }
    }

    private static void relocateXml(Node node, TransformerContext context) {
        Map attributes = node.attributes()
        RelocateClassContext relocateClassContext = new RelocateClassContext()
        relocateClassContext.stats = context.stats
        for (Map.Entry entry : attributes.entrySet()) {
            relocateClassContext.setClassName((String) entry.getValue())
            entry.setValue(relocateClassName(relocateClassContext, context))
        }
        List<String> localText = node.localText()
        if (localText.size() == 1) {
            relocateClassContext.setClassName(localText[0])
            node.setValue(relocateClassName(relocateClassContext, context))
        }
        node.children().each {
            if (it instanceof Node) {
                relocateXml((Node) it, context)
            }
        }
    }

    private static String relocateClassName(RelocateClassContext relocateContext, TransformerContext context) {
        for (Relocator relocator : context.relocators) {
            if (relocator.canRelocateClass(relocateContext)) {
                return relocator.relocateClass(relocateContext)
            }
        }
        return relocateContext.className
    }
}