|
| 1 | +package com.github.jengelman.gradle.plugins.shadow.relocation |
| 2 | + |
| 3 | +import java.util.regex.Pattern |
| 4 | +import org.codehaus.plexus.util.SelectorUtils |
| 5 | +import org.gradle.api.tasks.Input |
| 6 | +import org.gradle.api.tasks.Optional |
| 7 | + |
| 8 | +/** |
| 9 | + * Modified from `org.apache.maven.plugins.shade.relocation.SimpleRelocator.java` |
| 10 | + * |
| 11 | + * @author Jason van Zyl |
| 12 | + * @author Mauro Talevi |
| 13 | + * @author John Engelman |
| 14 | + */ |
| 15 | +@CacheableRelocator |
| 16 | +open class SimpleRelocator @JvmOverloads constructor( |
| 17 | + pattern: String?, |
| 18 | + shadedPattern: String?, |
| 19 | + includes: List<String>?, |
| 20 | + excludes: List<String>?, |
| 21 | + private val _isRawString: Boolean = false, |
| 22 | +) : Relocator { |
| 23 | + private val _pattern: String? |
| 24 | + private val _pathPattern: String |
| 25 | + private val _shadedPattern: String? |
| 26 | + private val _shadedPathPattern: String |
| 27 | + private val _includes = mutableSetOf<String>() |
| 28 | + private val _excludes = mutableSetOf<String>() |
| 29 | + |
| 30 | + init { |
| 31 | + if (_isRawString) { |
| 32 | + _pathPattern = pattern.orEmpty() |
| 33 | + _shadedPathPattern = shadedPattern.orEmpty() |
| 34 | + _pattern = null // not used for raw string relocator |
| 35 | + _shadedPattern = null // not used for raw string relocator |
| 36 | + } else { |
| 37 | + if (pattern == null) { |
| 38 | + _pattern = "" |
| 39 | + _pathPattern = "" |
| 40 | + } else { |
| 41 | + _pattern = pattern.replace('/', '.') |
| 42 | + _pathPattern = pattern.replace('.', '/') |
| 43 | + } |
| 44 | + if (shadedPattern == null) { |
| 45 | + _shadedPattern = "hidden.${_pattern}" |
| 46 | + _shadedPathPattern = "hidden/$_pathPattern" |
| 47 | + } else { |
| 48 | + _shadedPattern = shadedPattern.replace('/', '.') |
| 49 | + _shadedPathPattern = shadedPattern.replace('.', '/') |
| 50 | + } |
| 51 | + } |
| 52 | + _includes += normalizePatterns(includes) |
| 53 | + _excludes += normalizePatterns(excludes) |
| 54 | + } |
| 55 | + |
| 56 | + @get:Input |
| 57 | + @get:Optional |
| 58 | + open val pattern: String? get() = _pattern |
| 59 | + |
| 60 | + @get:Input |
| 61 | + open val pathPattern: String get() = _pathPattern |
| 62 | + |
| 63 | + @get:Input |
| 64 | + @get:Optional |
| 65 | + open val shadedPattern: String? get() = _shadedPattern |
| 66 | + |
| 67 | + @get:Input |
| 68 | + open val shadedPathPattern: String get() = _shadedPathPattern |
| 69 | + |
| 70 | + @get:Input |
| 71 | + open val isRawString: Boolean get() = _isRawString |
| 72 | + |
| 73 | + @get:Input |
| 74 | + open val includes: Set<String> get() = _includes |
| 75 | + |
| 76 | + @get:Input |
| 77 | + open val excludes: Set<String> get() = _excludes |
| 78 | + |
| 79 | + open fun include(pattern: String): SimpleRelocator = apply { |
| 80 | + _includes += normalizePatterns(listOf(pattern)) |
| 81 | + } |
| 82 | + |
| 83 | + open fun exclude(pattern: String): SimpleRelocator = apply { |
| 84 | + _excludes += normalizePatterns(listOf(pattern)) |
| 85 | + } |
| 86 | + |
| 87 | + override fun canRelocatePath(path: String): Boolean { |
| 88 | + if (_isRawString) return Pattern.compile(_pathPattern).matcher(path).find() |
| 89 | + // If string is too short - no need to perform expensive string operations |
| 90 | + if (path.length < _pathPattern.length) return false |
| 91 | + val adjustedPath = if (path.endsWith(".class")) { |
| 92 | + // Safeguard against strings containing only ".class" |
| 93 | + if (path.length == 6) return false |
| 94 | + path.dropLast(6) |
| 95 | + } else { |
| 96 | + path |
| 97 | + } |
| 98 | + // Allow for annoying option of an extra / on the front of a path. See MSHADE-119 comes from getClass().getResource("/a/b/c.properties"). |
| 99 | + val startIndex = if (adjustedPath.startsWith("/")) 1 else 0 |
| 100 | + val pathStartsWithPattern = adjustedPath.startsWith(_pathPattern, startIndex) |
| 101 | + return pathStartsWithPattern && isIncluded(adjustedPath) && !isExcluded(adjustedPath) |
| 102 | + } |
| 103 | + |
| 104 | + override fun canRelocateClass(className: String): Boolean { |
| 105 | + return !_isRawString && !className.contains('/') && canRelocatePath(className.replace('.', '/')) |
| 106 | + } |
| 107 | + |
| 108 | + override fun relocatePath(context: RelocatePathContext): String { |
| 109 | + val path = context.path |
| 110 | + context.stats.relocate(_pathPattern, _shadedPathPattern) |
| 111 | + return if (_isRawString) { |
| 112 | + path.replace(_pathPattern.toRegex(), _shadedPathPattern) |
| 113 | + } else { |
| 114 | + path.replaceFirst(_pathPattern, _shadedPathPattern) |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + override fun relocateClass(context: RelocateClassContext): String { |
| 119 | + context.stats.relocate(_pathPattern, _shadedPathPattern) |
| 120 | + return context.className.replaceFirst(_pattern.orEmpty(), _shadedPattern.orEmpty()) |
| 121 | + } |
| 122 | + |
| 123 | + override fun applyToSourceContent(sourceContent: String): String { |
| 124 | + return if (_isRawString) { |
| 125 | + sourceContent |
| 126 | + } else { |
| 127 | + sourceContent.replace("\\b$_pattern".toRegex(), _shadedPattern.orEmpty()) |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + private fun normalizePatterns(patterns: Collection<String>?) = buildSet { |
| 132 | + for (pattern in patterns.orEmpty()) { |
| 133 | + // Regex patterns don't need to be normalized and stay as is |
| 134 | + if (pattern.startsWith(SelectorUtils.REGEX_HANDLER_PREFIX)) { |
| 135 | + add(pattern) |
| 136 | + continue |
| 137 | + } |
| 138 | + |
| 139 | + val classPattern = pattern.replace('.', '/') |
| 140 | + add(classPattern) |
| 141 | + |
| 142 | + if (classPattern.endsWith("/*")) { |
| 143 | + val packagePattern = classPattern.substring(0, classPattern.lastIndexOf('/')) |
| 144 | + add(packagePattern) |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + private fun isIncluded(path: String): Boolean { |
| 150 | + return _includes.isEmpty() || _includes.any { SelectorUtils.matchPath(it, path, "/", true) } |
| 151 | + } |
| 152 | + |
| 153 | + private fun isExcluded(path: String): Boolean { |
| 154 | + return _excludes.any { SelectorUtils.matchPath(it, path, "/", true) } |
| 155 | + } |
| 156 | +} |
0 commit comments