├── .gitignore ├── README.md ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── settings.gradle └── src └── main └── kotlin └── com └── twiceyuan └── script └── android └── res └── prefix ├── Main.kt ├── bean ├── NameStyle.kt ├── RenameResult.kt ├── RenamedMapping.kt └── ResType.kt ├── files └── FileFinder.kt ├── handler ├── Handlers.kt └── ResTypeHandler.kt └── helper ├── AttrRenameHelper.kt ├── Commands.kt └── FileRenameHelper.kt /.gitignore: -------------------------------------------------------------------------------- 1 | /config.properties 2 | /module_paths.txt 3 | /ignore_res.txt 4 | /build 5 | /.idea 6 | /.gradle 7 | /*.iml -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # AndroidResPrefix 2 | 3 | 批量添加 Android 项目的资源文件前缀,并替换其在代码和其他资源文件中的引用,以解决配置了 `resourcePrefix` 后的 lint 检查的 `Resource with Wrong Prefix` 问题。在重命名文件时保留对原有文件的引用,需要环境安装了 git。 4 | 5 | 目前支持的资源类型:[ResType.kt](src/main/kotlin/com/twiceyuan/script/android/res/prefix/bean/ResType.kt) 6 | 7 | 暂不打算支持的: 8 | 9 | - declare-styleable 属性 10 | - 自定义配置 xml 文件 11 | 12 | ## 用法 13 | 14 | 1. clone 项目 15 | 2. 在项目根目录新建文件 `module_paths.txt` 配置想要替换资源的 module 目录,每个 module 占一行; 16 | 3. 在项目根目录新建文件 `config.properties` 添加属性 `prefix` 配置资源前缀; 17 | 4. 运行 Main 函数 18 | 19 | ## TODO 20 | 21 | - 支持 cli 方式使用 22 | - 优化过程输出 23 | -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | id 'java' 3 | id 'org.jetbrains.kotlin.jvm' version '1.4.10' 4 | } 5 | 6 | group 'com.twiceyuan.script' 7 | version '1.0-SNAPSHOT' 8 | 9 | repositories { 10 | mavenCentral() 11 | } 12 | 13 | dependencies { 14 | implementation "org.jetbrains.kotlin:kotlin-stdlib" 15 | testCompile group: 'junit', name: 'junit', version: '4.12' 16 | } 17 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | kotlin.code.style=official -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/twiceyuan/AndroidResPrefix/f4a4c27616360bbe444b595f78606f6a3e39c760/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionBase=GRADLE_USER_HOME 2 | distributionPath=wrapper/dists 3 | distributionUrl=https\://services.gradle.org/distributions/gradle-6.5-all.zip 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | # 4 | # Copyright 2015 the original author or authors. 5 | # 6 | # Licensed under the Apache License, Version 2.0 (the "License"); 7 | # you may not use this file except in compliance with the License. 8 | # You may obtain a copy of the License at 9 | # 10 | # https://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" BASIS, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | # See the License for the specific language governing permissions and 16 | # limitations under the License. 17 | # 18 | 19 | ############################################################################## 20 | ## 21 | ## Gradle start up script for UN*X 22 | ## 23 | ############################################################################## 24 | 25 | # Attempt to set APP_HOME 26 | # Resolve links: $0 may be a link 27 | PRG="$0" 28 | # Need this for relative symlinks. 29 | while [ -h "$PRG" ] ; do 30 | ls=`ls -ld "$PRG"` 31 | link=`expr "$ls" : '.*-> \(.*\)$'` 32 | if expr "$link" : '/.*' > /dev/null; then 33 | PRG="$link" 34 | else 35 | PRG=`dirname "$PRG"`"/$link" 36 | fi 37 | done 38 | SAVED="`pwd`" 39 | cd "`dirname \"$PRG\"`/" >/dev/null 40 | APP_HOME="`pwd -P`" 41 | cd "$SAVED" >/dev/null 42 | 43 | APP_NAME="Gradle" 44 | APP_BASE_NAME=`basename "$0"` 45 | 46 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 47 | DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' 48 | 49 | # Use the maximum available, or set MAX_FD != -1 to use that value. 50 | MAX_FD="maximum" 51 | 52 | warn () { 53 | echo "$*" 54 | } 55 | 56 | die () { 57 | echo 58 | echo "$*" 59 | echo 60 | exit 1 61 | } 62 | 63 | # OS specific support (must be 'true' or 'false'). 64 | cygwin=false 65 | msys=false 66 | darwin=false 67 | nonstop=false 68 | case "`uname`" in 69 | CYGWIN* ) 70 | cygwin=true 71 | ;; 72 | Darwin* ) 73 | darwin=true 74 | ;; 75 | MINGW* ) 76 | msys=true 77 | ;; 78 | NONSTOP* ) 79 | nonstop=true 80 | ;; 81 | esac 82 | 83 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 84 | 85 | 86 | # Determine the Java command to use to start the JVM. 87 | if [ -n "$JAVA_HOME" ] ; then 88 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 89 | # IBM's JDK on AIX uses strange locations for the executables 90 | JAVACMD="$JAVA_HOME/jre/sh/java" 91 | else 92 | JAVACMD="$JAVA_HOME/bin/java" 93 | fi 94 | if [ ! -x "$JAVACMD" ] ; then 95 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 96 | 97 | Please set the JAVA_HOME variable in your environment to match the 98 | location of your Java installation." 99 | fi 100 | else 101 | JAVACMD="java" 102 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 103 | 104 | Please set the JAVA_HOME variable in your environment to match the 105 | location of your Java installation." 106 | fi 107 | 108 | # Increase the maximum file descriptors if we can. 109 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then 110 | MAX_FD_LIMIT=`ulimit -H -n` 111 | if [ $? -eq 0 ] ; then 112 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 113 | MAX_FD="$MAX_FD_LIMIT" 114 | fi 115 | ulimit -n $MAX_FD 116 | if [ $? -ne 0 ] ; then 117 | warn "Could not set maximum file descriptor limit: $MAX_FD" 118 | fi 119 | else 120 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 121 | fi 122 | fi 123 | 124 | # For Darwin, add options to specify how the application appears in the dock 125 | if $darwin; then 126 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 127 | fi 128 | 129 | # For Cygwin or MSYS, switch paths to Windows format before running java 130 | if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then 131 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 132 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 133 | 134 | JAVACMD=`cygpath --unix "$JAVACMD"` 135 | 136 | # We build the pattern for arguments to be converted via cygpath 137 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 138 | SEP="" 139 | for dir in $ROOTDIRSRAW ; do 140 | ROOTDIRS="$ROOTDIRS$SEP$dir" 141 | SEP="|" 142 | done 143 | OURCYGPATTERN="(^($ROOTDIRS))" 144 | # Add a user-defined pattern to the cygpath arguments 145 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 146 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 147 | fi 148 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 149 | i=0 150 | for arg in "$@" ; do 151 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 152 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 153 | 154 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 155 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 156 | else 157 | eval `echo args$i`="\"$arg\"" 158 | fi 159 | i=`expr $i + 1` 160 | done 161 | case $i in 162 | 0) set -- ;; 163 | 1) set -- "$args0" ;; 164 | 2) set -- "$args0" "$args1" ;; 165 | 3) set -- "$args0" "$args1" "$args2" ;; 166 | 4) set -- "$args0" "$args1" "$args2" "$args3" ;; 167 | 5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 168 | 6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 169 | 7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 170 | 8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 171 | 9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 172 | esac 173 | fi 174 | 175 | # Escape application args 176 | save () { 177 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done 178 | echo " " 179 | } 180 | APP_ARGS=`save "$@"` 181 | 182 | # Collect all arguments for the java command, following the shell quoting and substitution rules 183 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" 184 | 185 | exec "$JAVACMD" "$@" 186 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @rem 2 | @rem Copyright 2015 the original author or authors. 3 | @rem 4 | @rem Licensed under the Apache License, Version 2.0 (the "License"); 5 | @rem you may not use this file except in compliance with the License. 6 | @rem You may obtain a copy of the License at 7 | @rem 8 | @rem https://www.apache.org/licenses/LICENSE-2.0 9 | @rem 10 | @rem Unless required by applicable law or agreed to in writing, software 11 | @rem distributed under the License is distributed on an "AS IS" BASIS, 12 | @rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | @rem See the License for the specific language governing permissions and 14 | @rem limitations under the License. 15 | @rem 16 | 17 | @if "%DEBUG%" == "" @echo off 18 | @rem ########################################################################## 19 | @rem 20 | @rem Gradle startup script for Windows 21 | @rem 22 | @rem ########################################################################## 23 | 24 | @rem Set local scope for the variables with windows NT shell 25 | if "%OS%"=="Windows_NT" setlocal 26 | 27 | set DIRNAME=%~dp0 28 | if "%DIRNAME%" == "" set DIRNAME=. 29 | set APP_BASE_NAME=%~n0 30 | set APP_HOME=%DIRNAME% 31 | 32 | @rem Resolve any "." and ".." in APP_HOME to make it shorter. 33 | for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi 34 | 35 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 36 | set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" 37 | 38 | @rem Find java.exe 39 | if defined JAVA_HOME goto findJavaFromJavaHome 40 | 41 | set JAVA_EXE=java.exe 42 | %JAVA_EXE% -version >NUL 2>&1 43 | if "%ERRORLEVEL%" == "0" goto init 44 | 45 | echo. 46 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 47 | echo. 48 | echo Please set the JAVA_HOME variable in your environment to match the 49 | echo location of your Java installation. 50 | 51 | goto fail 52 | 53 | :findJavaFromJavaHome 54 | set JAVA_HOME=%JAVA_HOME:"=% 55 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 56 | 57 | if exist "%JAVA_EXE%" goto init 58 | 59 | echo. 60 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 61 | echo. 62 | echo Please set the JAVA_HOME variable in your environment to match the 63 | echo location of your Java installation. 64 | 65 | goto fail 66 | 67 | :init 68 | @rem Get command-line arguments, handling Windows variants 69 | 70 | if not "%OS%" == "Windows_NT" goto win9xME_args 71 | 72 | :win9xME_args 73 | @rem Slurp the command line arguments. 74 | set CMD_LINE_ARGS= 75 | set _SKIP=2 76 | 77 | :win9xME_args_slurp 78 | if "x%~1" == "x" goto execute 79 | 80 | set CMD_LINE_ARGS=%* 81 | 82 | :execute 83 | @rem Setup the command line 84 | 85 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 86 | 87 | 88 | @rem Execute Gradle 89 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 90 | 91 | :end 92 | @rem End local scope for the variables with windows NT shell 93 | if "%ERRORLEVEL%"=="0" goto mainEnd 94 | 95 | :fail 96 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 97 | rem the _cmd.exe /c_ return code! 98 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 99 | exit /b 1 100 | 101 | :mainEnd 102 | if "%OS%"=="Windows_NT" endlocal 103 | 104 | :omega 105 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'AndroidResPrefix' 2 | 3 | -------------------------------------------------------------------------------- /src/main/kotlin/com/twiceyuan/script/android/res/prefix/Main.kt: -------------------------------------------------------------------------------- 1 | @file:Suppress("SameParameterValue") 2 | 3 | package com.twiceyuan.script.android.res.prefix 4 | 5 | import com.twiceyuan.script.android.res.prefix.bean.RenameResult 6 | import com.twiceyuan.script.android.res.prefix.bean.RenamedMapping 7 | import com.twiceyuan.script.android.res.prefix.bean.ResType 8 | import com.twiceyuan.script.android.res.prefix.bean.getNameStylePrefix 9 | import com.twiceyuan.script.android.res.prefix.files.getCodeByFlavor 10 | import com.twiceyuan.script.android.res.prefix.files.getXmlFiles 11 | import com.twiceyuan.script.android.res.prefix.handler.* 12 | import com.twiceyuan.script.android.res.prefix.helper.AttrRenameHelper 13 | import com.twiceyuan.script.android.res.prefix.helper.FileRenameHelper 14 | import java.io.File 15 | import java.util.* 16 | 17 | val modulePaths = File("module_paths.txt") 18 | .readText() 19 | .split("\n") 20 | .filter { it.isNotBlank() } 21 | 22 | val properties = Properties().apply { 23 | load(File("config.properties").inputStream()) 24 | } 25 | 26 | val prefix: String = properties["prefix"] as String 27 | 28 | val File.subFiles 29 | get() = listFiles()?.toList() ?: emptyList() 30 | 31 | private val renameMapping = hashSetOf() 32 | 33 | fun main() { 34 | renameResInModule(prefix, modulePaths) 35 | } 36 | 37 | private fun renameResInModule(prefix: String, modulePaths: List) { 38 | 39 | val modules = modulePaths.map { File(it) } 40 | 41 | ResType.values().forEach { 42 | renameResourceByType(prefix, it, modules) 43 | } 44 | } 45 | 46 | fun getFlavorDirs(modulePath: String): List { 47 | val srcPath = File(modulePath, "src").absolutePath 48 | return File(srcPath) 49 | .subFiles 50 | .filter { "test" !in it.name.toLowerCase() } 51 | } 52 | 53 | /** 54 | * 重命名一类资源 55 | */ 56 | private fun renameResourceByType(prefix: String, resType: ResType, modules: List) { 57 | val handler = getResTypeHandler(resType) 58 | 59 | // 重命名文件类型资源 60 | if (handler is FileResourceHandler) { 61 | for (module in modules) { 62 | // 获取所有的 drawable 文件 63 | for (file in handler.getResFiles(module.absolutePath)) { 64 | val resPrefix = handler.nameStyle().getNameStylePrefix(prefix) 65 | val result = FileRenameHelper.rename(file, resPrefix, module) 66 | if (result is RenameResult.Success) { 67 | val mapping = RenamedMapping(resType, result.oldResName, result.newResName) 68 | renameMapping.add(mapping) 69 | } 70 | } 71 | } 72 | } 73 | 74 | // 处理 attr 类型的资源 75 | if (handler is AttrResourceHandler) { 76 | for (module in modules) { 77 | for (file in handler.getAttrFiles(module.absolutePath)) { 78 | val resPrefix = handler.nameStyle().getNameStylePrefix(prefix) 79 | val results = AttrRenameHelper.renameAttrName( 80 | file, 81 | resPrefix, 82 | handler.tagMatcher() 83 | ) 84 | renameMapping.addAll(results 85 | .filterIsInstance() 86 | .map { 87 | RenamedMapping(handler.resType, it.oldResName, it.newResName) 88 | } 89 | ) 90 | } 91 | } 92 | } 93 | 94 | // 处理资源引用 95 | modules.forEach { renameReferences(handler, it) } 96 | } 97 | 98 | private fun renameReferences(handler: ResTypeHandler, moduleFile: File) { 99 | // 改变代码中的引用 100 | for (flavorDir in getFlavorDirs(moduleFile.absolutePath)) { 101 | for (codeFile in getCodeByFlavor(flavorDir.absolutePath)) { 102 | var content = codeFile.readText() 103 | var isChanged = false 104 | 105 | for (mapping in renameMapping) { 106 | val oldValue = handler.codeComposer(mapping.oldName) 107 | val newValue = handler.codeComposer(mapping.newName) 108 | // 避免 android.R.xxx 也被匹配并且替换,排除 R.xxx 前面有 . 的情况 109 | val oldValueMatcher = Regex("(? { 19 | prefix 20 | } 21 | NameStyle.UpperCamelStyle -> { 22 | upperCamelStylePrefix 23 | } 24 | } 25 | 26 | val upperCamelStylePrefix by lazy { 27 | prefix.toUpperCamelStyle() 28 | } 29 | 30 | private fun String.toUpperCamelStyle(): String { 31 | val words = split("_").filter { it.isNotBlank() } 32 | return words.joinToString("") { it.capitalize() } 33 | } 34 | -------------------------------------------------------------------------------- /src/main/kotlin/com/twiceyuan/script/android/res/prefix/bean/RenameResult.kt: -------------------------------------------------------------------------------- 1 | package com.twiceyuan.script.android.res.prefix.bean 2 | 3 | /** 4 | * 资源修改的结果 5 | */ 6 | sealed class RenameResult { 7 | class Success( 8 | val oldResName: String, 9 | val newResName: String 10 | ) : RenameResult() 11 | 12 | object Passed : RenameResult() 13 | object Failed : RenameResult() 14 | } 15 | 16 | -------------------------------------------------------------------------------- /src/main/kotlin/com/twiceyuan/script/android/res/prefix/bean/RenamedMapping.kt: -------------------------------------------------------------------------------- 1 | package com.twiceyuan.script.android.res.prefix.bean 2 | 3 | data class RenamedMapping( 4 | val resType: ResType, 5 | val oldName: String, 6 | val newName: String 7 | ) -------------------------------------------------------------------------------- /src/main/kotlin/com/twiceyuan/script/android/res/prefix/bean/ResType.kt: -------------------------------------------------------------------------------- 1 | package com.twiceyuan.script.android.res.prefix.bean 2 | 3 | enum class ResType { 4 | /** 5 | * Drawable 资源,包含图片文件、xml 类 drawable 文件和 values 中的 定义 6 | */ 7 | Drawable, 8 | 9 | /** 10 | * MipMap 资源,包含图片文件、xml 类 mipmap 文件 11 | */ 12 | MipMap, 13 | 14 | /** 15 | * 布局文件 16 | */ 17 | Layout, 18 | 19 | /** 20 | * 字符串资源 21 | */ 22 | String, 23 | 24 | /** 25 | * 字符数组资源 26 | */ 27 | StringArray, 28 | 29 | /** 30 | * 量词字符串资源 31 | */ 32 | StringPlurals, 33 | 34 | /** 35 | * dimen 定义 36 | */ 37 | Dimension, 38 | 39 | /** 40 | * Style 定义 41 | */ 42 | Style, 43 | 44 | /** 45 | * Anim 资源 46 | */ 47 | Animation, 48 | 49 | /** 50 | * Menu 资源 51 | */ 52 | Menu, 53 | 54 | /** 55 | * 颜色资源 56 | */ 57 | Color, 58 | } -------------------------------------------------------------------------------- /src/main/kotlin/com/twiceyuan/script/android/res/prefix/files/FileFinder.kt: -------------------------------------------------------------------------------- 1 | package com.twiceyuan.script.android.res.prefix.files 2 | 3 | import com.twiceyuan.script.android.res.prefix.getFlavorDirs 4 | import com.twiceyuan.script.android.res.prefix.modulePaths 5 | import com.twiceyuan.script.android.res.prefix.subFiles 6 | import java.io.File 7 | 8 | private fun getAllFiles(dir: File, fileMatcher: File.() -> Boolean): List { 9 | val codeFiles = mutableListOf() 10 | 11 | dir.subFiles.forEach { 12 | if (it.fileMatcher()) { 13 | codeFiles.add(it) 14 | } 15 | if (it.isDirectory) { 16 | codeFiles.addAll(getAllFiles(it, fileMatcher)) 17 | } 18 | } 19 | 20 | return codeFiles 21 | } 22 | 23 | /** 24 | * 获取 module 内的所有代码文件(仅包含 kotlin 和 java 文件) 25 | * [modulePath] Module 的根目录 26 | */ 27 | fun getCodeFiles(modulePath: String) = getFlavorDirs(modulePath) 28 | .flatMap { 29 | it.subFiles.filter { subFile -> 30 | subFile.name in arrayOf("java", "kotlin") 31 | } 32 | } 33 | .flatMap { getAllFiles(it, File::isCodeFile) } 34 | 35 | fun getCodeByFlavor(flavorPath: String) = File(flavorPath).subFiles 36 | .filter { subFile -> subFile.name in arrayOf("java", "kotlin") } 37 | .flatMap { getAllFiles(it, File::isCodeFile) } 38 | 39 | /** 40 | * 获取 module 下所有 xml 文件 41 | */ 42 | fun getXmlFiles(modulePath: String) = getFlavorDirs(modulePath) 43 | .flatMap { 44 | val mutableList = mutableListOf() 45 | mutableList.addAll(File(it, "res").subFiles) 46 | mutableList.add(it) // 添加 flavor 目录本身,下面有 AndroidManifest 文件 47 | mutableList 48 | } 49 | .flatMap { getAllFiles(it, File::isXmlFile) } 50 | 51 | fun File.isCodeFile(): Boolean = isFile && (name.endsWith(".kt") || name.endsWith(".java")) 52 | fun File.isXmlFile(): Boolean = isFile && name.endsWith(".xml") 53 | -------------------------------------------------------------------------------- /src/main/kotlin/com/twiceyuan/script/android/res/prefix/handler/Handlers.kt: -------------------------------------------------------------------------------- 1 | package com.twiceyuan.script.android.res.prefix.handler 2 | 3 | import com.twiceyuan.script.android.res.prefix.bean.NameStyle 4 | import com.twiceyuan.script.android.res.prefix.bean.ResType 5 | import com.twiceyuan.script.android.res.prefix.getFlavorDirs 6 | import com.twiceyuan.script.android.res.prefix.subFiles 7 | import java.io.File 8 | 9 | /** 10 | * res 类型映射到 handler 11 | */ 12 | fun getResTypeHandler(resType: ResType): ResTypeHandler { 13 | return when (resType) { 14 | ResType.Drawable -> DrawableHandler 15 | ResType.Layout -> LayoutHandler 16 | ResType.MipMap -> MipMapHandler 17 | ResType.String -> StringHandler 18 | ResType.StringArray -> StringArrayHandler 19 | ResType.StringPlurals -> StringPluralsHandler 20 | ResType.Dimension -> DimensionHandler 21 | ResType.Style -> StyleHandler 22 | ResType.Animation -> AnimationHandler 23 | ResType.Menu -> MenuHandler 24 | ResType.Color -> ColorHandler 25 | } 26 | } 27 | 28 | object DrawableHandler : ResTypeHandler, FileResourceHandler, AttrResourceHandler { 29 | override fun getAttrFiles(modulePath: String): List = getValuesDirs(modulePath) 30 | override fun tagMatcher(): Regex = tagMatcher("drawable") 31 | override val resType: ResType = ResType.Drawable 32 | override fun codeComposer(resName: String): String = "R.drawable.$resName" 33 | override fun xmlComposer(resName: String): String = "@drawable/$resName" 34 | 35 | override fun getResFiles(modulePath: String): List = getFlavorDirs(modulePath) 36 | .map { File(it, "res") } 37 | .flatMap { it.subFiles.filter { resDir -> resDir.name.startsWith("drawable") } } 38 | .flatMap { it.subFiles } 39 | } 40 | 41 | object MipMapHandler : ResTypeHandler, FileResourceHandler { 42 | 43 | override val resType: ResType = ResType.Drawable 44 | override fun codeComposer(resName: String): String = "R.mipmap.$resName" 45 | override fun xmlComposer(resName: String): String = "@mipmap/$resName" 46 | 47 | override fun getResFiles(modulePath: String): List = getFlavorDirs(modulePath) 48 | .map { File(it, "res") } 49 | .flatMap { it.subFiles.filter { resDir -> resDir.name.startsWith("mipmap") } } 50 | .flatMap { it.subFiles } 51 | } 52 | 53 | object LayoutHandler : ResTypeHandler, FileResourceHandler { 54 | 55 | override val resType: ResType = ResType.Layout 56 | override fun codeComposer(resName: String): String = "R.layout.$resName" 57 | override fun xmlComposer(resName: String): String = "@layout/$resName" 58 | 59 | override fun getResFiles(modulePath: String): List = getFlavorDirs(modulePath) 60 | .map { File(it, "res") } 61 | .flatMap { it.subFiles.filter { resDir -> resDir.name == "layout" } } 62 | .flatMap { it.subFiles } 63 | } 64 | 65 | object StringHandler : ResTypeHandler, AttrResourceHandler { 66 | 67 | override val resType: ResType = ResType.String 68 | override fun tagMatcher(): Regex = tagMatcher("string") 69 | override fun codeComposer(resName: String): String = "R.string.$resName" 70 | override fun xmlComposer(resName: String): String = "@string/$resName" 71 | 72 | override fun getAttrFiles(modulePath: String): List { 73 | return getValuesDirs(modulePath) 74 | } 75 | } 76 | 77 | object StringArrayHandler : ResTypeHandler, AttrResourceHandler { 78 | 79 | override val resType: ResType = ResType.StringArray 80 | override fun tagMatcher(): Regex = tagMatcher("string-array") 81 | override fun codeComposer(resName: String): String = "R.array.$resName" 82 | override fun xmlComposer(resName: String): String = "@array/$resName" 83 | 84 | override fun getAttrFiles(modulePath: String): List { 85 | return getValuesDirs(modulePath) 86 | } 87 | } 88 | 89 | object StringPluralsHandler : ResTypeHandler, AttrResourceHandler { 90 | 91 | override val resType: ResType = ResType.StringPlurals 92 | override fun tagMatcher(): Regex = tagMatcher("plurals") 93 | override fun codeComposer(resName: String): String = "R.plurals.$resName" 94 | override fun xmlComposer(resName: String): String = "@plurals/$resName" 95 | 96 | override fun getAttrFiles(modulePath: String): List { 97 | return getValuesDirs(modulePath) 98 | } 99 | } 100 | 101 | object DimensionHandler : ResTypeHandler, AttrResourceHandler { 102 | 103 | override val resType: ResType = ResType.Dimension 104 | override fun tagMatcher(): Regex = tagMatcher("dimen") 105 | override fun codeComposer(resName: String): String = "R.dimen.$resName" 106 | override fun xmlComposer(resName: String): String = "@dimen/$resName" 107 | 108 | override fun getAttrFiles(modulePath: String): List { 109 | return getValuesDirs(modulePath) 110 | } 111 | } 112 | 113 | object StyleHandler : ResTypeHandler, AttrResourceHandler { 114 | override val resType: ResType = ResType.Style 115 | override fun nameStyle(): NameStyle = NameStyle.UpperCamelStyle 116 | override fun tagMatcher(): Regex = tagMatcher("style") 117 | override fun codeComposer(resName: String): String = "R.style.$resName" 118 | override fun xmlComposer(resName: String): String = "@style/$resName" 119 | 120 | override fun getAttrFiles(modulePath: String): List { 121 | return getValuesDirs(modulePath) 122 | } 123 | } 124 | 125 | object AnimationHandler : ResTypeHandler, AttrResourceHandler { 126 | override val resType: ResType = ResType.Animation 127 | override fun tagMatcher(): Regex = tagMatcher("anim") 128 | override fun codeComposer(resName: String): String = "R.anim.$resName" 129 | override fun xmlComposer(resName: String): String = "@anim/$resName" 130 | 131 | override fun getAttrFiles(modulePath: String): List { 132 | return getFlavorDirs(modulePath) 133 | .map { File(it, "res") } 134 | .flatMap { it.subFiles.filter { resDir -> resDir.name.startsWith("anim") } } 135 | .flatMap { it.subFiles } 136 | .filter { it.name.endsWith(".xml") } 137 | } 138 | } 139 | 140 | object MenuHandler : ResTypeHandler, FileResourceHandler { 141 | override val resType: ResType = ResType.Menu 142 | override fun codeComposer(resName: String): String = "R.menu.$resName" 143 | override fun xmlComposer(resName: String): String = "@menu/$resName" 144 | 145 | override fun getResFiles(modulePath: String): List = getFlavorDirs(modulePath) 146 | .map { File(it, "res") } 147 | .flatMap { it.subFiles.filter { resDir -> resDir.name == "menu" } } 148 | .flatMap { it.subFiles } 149 | } 150 | 151 | object ColorHandler : ResTypeHandler, FileResourceHandler, AttrResourceHandler { 152 | 153 | override fun getAttrFiles(modulePath: String): List = getValuesDirs(modulePath) 154 | override fun tagMatcher(): Regex = tagMatcher("color") 155 | 156 | override val resType: ResType = ResType.Color 157 | override fun codeComposer(resName: String): String = "R.color.$resName" 158 | override fun xmlComposer(resName: String): String = "@color/$resName" 159 | 160 | override fun getResFiles(modulePath: String): List { 161 | return getFlavorDirs(modulePath) 162 | .map { File(it, "res") } 163 | .flatMap { it.subFiles.filter { resDir -> resDir.name == "color" } } 164 | .flatMap { it.subFiles } 165 | } 166 | } 167 | 168 | private fun getValuesDirs(modulePath: String): List { 169 | return getFlavorDirs(modulePath) 170 | .map { File(it, "res") } 171 | .flatMap { it.subFiles.filter { resDir -> resDir.name.startsWith("value") } } 172 | .flatMap { it.subFiles } 173 | .filter { it.name.endsWith(".xml") } 174 | } 175 | 176 | /** 177 | * tag 匹配器构造 178 | */ 179 | private fun tagMatcher(tag: String): Regex = Regex("<$tag [\\s\\S]+?>[\\s\\S]+?") 180 | 181 | object KotlinSyntheticRefHandlerCode : CodeRefExtHandler { 182 | 183 | override fun handle( 184 | flavorName: String, 185 | codeFile: File, 186 | content: String, 187 | resType: ResType, 188 | oldValue: String, 189 | newValue: String 190 | ): String { 191 | if (!codeFile.name.endsWith(".kt") || resType != ResType.Layout) { 192 | return content 193 | } 194 | var newContent = content 195 | val oldRefValue = "kotlinx.android.synthetic.$flavorName.$oldValue." 196 | val newRefValue = "kotlinx.android.synthetic.$flavorName.$newValue." 197 | 198 | if (oldValue in content) { 199 | newContent = newContent.replace(oldRefValue, newRefValue) 200 | } 201 | 202 | return newContent 203 | } 204 | } 205 | 206 | object StyleParentRefHandler : XmlRefExtHandler { 207 | 208 | override fun handle(file: File, content: String, resType: ResType, oldName: String, newName: String): String { 209 | if (resType != ResType.Style) { 210 | return content 211 | } 212 | 213 | val oldRefValue = "parent=\"$oldName\"" 214 | val newRefValue = "parent=\"$newName\"" 215 | return content.replace(oldRefValue, newRefValue) 216 | } 217 | } 218 | 219 | object ExternalHandlers { 220 | // 暂时写死,视情况需要方便后期扩展 221 | val extCodeHandler = listOf(KotlinSyntheticRefHandlerCode) 222 | val extXmlHandler = listOf(StyleParentRefHandler) 223 | } 224 | -------------------------------------------------------------------------------- /src/main/kotlin/com/twiceyuan/script/android/res/prefix/handler/ResTypeHandler.kt: -------------------------------------------------------------------------------- 1 | package com.twiceyuan.script.android.res.prefix.handler 2 | 3 | import com.twiceyuan.script.android.res.prefix.bean.NameStyle 4 | import com.twiceyuan.script.android.res.prefix.bean.ResType 5 | import java.io.File 6 | 7 | 8 | interface ResTypeHandler { 9 | 10 | /** 11 | * 资源类型 12 | */ 13 | val resType: ResType 14 | 15 | /** 16 | * 前缀的命名风格,默认是下划线分割命名 17 | */ 18 | fun nameStyle(): NameStyle = NameStyle.UnderScoreStyle 19 | 20 | /** 21 | * 代码中引用匹配器 22 | */ 23 | fun codeComposer(resName: String): String 24 | 25 | /** 26 | * XML 资源中引用匹配器 27 | */ 28 | fun xmlComposer(resName: String): String 29 | } 30 | 31 | interface FileResourceHandler { 32 | /** 33 | * 当资源是文件类型时(文件名代表资源名称),获取 module 所有该类型资源文件 34 | */ 35 | fun getResFiles(modulePath: String): List 36 | } 37 | 38 | interface AttrResourceHandler : ResTypeHandler { 39 | 40 | /** 41 | * 获取 module 下所有可能存储该 attr 的文件 42 | */ 43 | fun getAttrFiles(modulePath: String): List 44 | 45 | /** 46 | * 匹配 attr 在 xml 中定义的 tag 47 | */ 48 | fun tagMatcher(): Regex 49 | } 50 | 51 | /** 52 | * 引用修改,外部扩展。用于一些不符合常理的引用,例如 Kotlin Android Extension 生成的布局对象 53 | */ 54 | interface CodeRefExtHandler { 55 | fun handle( 56 | flavorName: String, 57 | codeFile: File, 58 | content: String, 59 | resType: ResType, 60 | oldValue: String, 61 | newValue: String 62 | ): String 63 | } 64 | 65 | interface XmlRefExtHandler { 66 | fun handle(file: File, content: String, resType: ResType, oldName: String, newName: String): String 67 | } -------------------------------------------------------------------------------- /src/main/kotlin/com/twiceyuan/script/android/res/prefix/helper/AttrRenameHelper.kt: -------------------------------------------------------------------------------- 1 | package com.twiceyuan.script.android.res.prefix.helper 2 | 3 | import com.twiceyuan.script.android.res.prefix.bean.RenameResult 4 | import java.io.File 5 | 6 | object AttrRenameHelper { 7 | 8 | fun renameAttrName(attrFile: File, prefix: String, tagMatcher: Regex): List { 9 | var content = attrFile.readText() 10 | var isChanged = false 11 | val results = mutableListOf() 12 | val nameMatcher = Regex("name=\"(.*?)\"") 13 | tagMatcher.findAll(content).forEach { tagResult -> 14 | val result = nameMatcher.find(tagResult.value)?.groups ?: return@forEach 15 | val nameDefinition = result[0]?.value ?: return@forEach 16 | val oldName = result[1]?.value ?: return@forEach 17 | // 命名符合规则的跳过 18 | if (oldName.startsWith(prefix)) { 19 | return@forEach 20 | } 21 | val newName = prefix + oldName 22 | val newNameDefinition = nameDefinition.replace(oldName, newName) 23 | content = content.replace(nameDefinition, newNameDefinition) 24 | results.add(RenameResult.Success(oldName, newName)) 25 | isChanged = true 26 | } 27 | 28 | if (isChanged) { 29 | attrFile.writeText(content) 30 | } 31 | 32 | return results 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/main/kotlin/com/twiceyuan/script/android/res/prefix/helper/Commands.kt: -------------------------------------------------------------------------------- 1 | package com.twiceyuan.script.android.res.prefix.helper 2 | 3 | import java.io.File 4 | 5 | 6 | /** 7 | * [commands] 执行命令 8 | * [dir] 执行路径 9 | * @return 是否成功 10 | */ 11 | fun runCommand(commands: String, dir: File? = null): Boolean { 12 | 13 | val process = Runtime.getRuntime().exec(commands.split(" ").toTypedArray(), null, dir) 14 | val bufferedReader = process.inputStream.bufferedReader() 15 | 16 | var line: String? 17 | while (bufferedReader.readLine().also { line = it } != null) { 18 | println(line) // stdout 19 | } 20 | 21 | // return process return code 22 | val returnCode = process.waitFor() 23 | return returnCode == 0 24 | } -------------------------------------------------------------------------------- /src/main/kotlin/com/twiceyuan/script/android/res/prefix/helper/FileRenameHelper.kt: -------------------------------------------------------------------------------- 1 | package com.twiceyuan.script.android.res.prefix.helper 2 | 3 | import com.twiceyuan.script.android.res.prefix.bean.RenameResult 4 | import java.io.File 5 | 6 | 7 | object FileRenameHelper { 8 | 9 | fun rename( 10 | oldFile: File, 11 | prefix: String, 12 | moduleFile: File 13 | ): RenameResult { 14 | // 如果文件已经以前缀命名,则跳过该文件 15 | if (oldFile.name.startsWith(prefix)) { 16 | return RenameResult.Passed 17 | } 18 | val newName = prefix + oldFile.name 19 | val newFile = File(oldFile.parent, newName) 20 | 21 | return if (renameFile(oldFile, newFile, moduleFile)) { 22 | val oldResName = oldFile.nameWithoutExtension 23 | val newResName = newFile.nameWithoutExtension 24 | RenameResult.Success(oldResName, newResName) 25 | } else { 26 | RenameResult.Failed 27 | } 28 | } 29 | 30 | /** 31 | * 使用 git mv 重命名文件,没有使用 File API 是因为需要保留 git 记录 32 | */ 33 | private fun renameFile(oldFile: File, newFile: File, dir: File): Boolean { 34 | val cmd = "git mv ${oldFile.absolutePath} ${newFile.absolutePath}" 35 | return runCommand(cmd, dir) 36 | } 37 | } 38 | 39 | --------------------------------------------------------------------------------