Commit 8ed90bde authored by pengjunjing's avatar pengjunjing

feat:增加白名单功能,增加文档说明

parent f98a719d
......@@ -3,10 +3,11 @@
## 工具列表
1. 删除没有被引用到的图片/xml文件
2. 将图片/xml文件移动到使用的模块
3. 将string.xml的字符串进行删除或者移动到对应模块
---
## 使用方式
## 工具的使用方式
* 在项目中️以模块的形式导入:
* 项目中根目录找到setting.gradle,加入刚刚复制的文件夹: include ':RemoveUnusedImg'
* 找到需要使用工具的入口,然后修改部分Java代码,以Java的形式运行即可
......@@ -48,9 +49,37 @@ UnuseRes.kt的main方法
SingleImplement.kt的main方法
### 原理:
跟第一个工具差不多,前三步跟第一个工具一样
4. 统计此文件被哪些代码文件依赖,存到列表中
5. 对每个资源文件的依赖列表进行判断,通过路径得到所属的模块,再通过set进行去重.
6. 第五步得到的结果数量为1的说明就是被一个模块依赖了,再通过代码执行git命令,将文件移动过去
### 使用方法
跟第一个工具的使用方法差不多,图片和xml都是分开来操作的. 所以需要循环操作几次,均为0的时候才算完全完成
* ***请在有GIT跟踪的目录下进行***
-------------------------
## 3. 将string.xml的字符串进行删除或者移动到对应模块
### 使用场景:
此代码跟第二个工具的作用差不多,只不过处理的对象是每个模块下的string.xml , 需要清除一些模块下string.xml文件没有用到的string内容 , 或者是将其内容移动到引用代码所对应的模块下 , 可以使用此工具来完成
### 入口
StringPull.kt的main方法
### 原理:
1. 给定一个路径,递归扫描该路径下的所有*.xml文件 , 并且判断是不是字符串的文件
2. 对扫描到的string.xml文件 , 获取所有定义的参数以及数值
3. 扫描所有代码
4. 对第一步的结果,进行判断,得到每个字符串引用的次数
5. 引用次数为0则解析xml , 并且删除该字符串内容
6. 引用次数为1的字符串 , 则视为只被一个模块调用 , 如果调用的模块不是本模块 , 则移动到对应的模块去
### 使用方法
* 找到主方法 , 检查realExecuteCmd是否为false , false为调试模式
* 主方法配置的核对路径是否为需要扫描的路径
* 调试模式下运行主方法 , 之后会打印日志
* 根据打印的日志 , 随机挑选几条数据进行人工校验 , 没问题之后直接调用主方法
### 注意事项
* 当某些模块的string.xml不存在时会自动创建 , 随后会用命令行添加到git中 , 由于使用的是"git add ." 可能会导致添加了一些不需要的文件 , 在提交的时候应该检查一下.如果有不需要add的可以使用 "git rm --cached xxx" 命令来移除添加的文件
* 由于有些字符串可能是在SDK调用的,这个工具时候并不一定能生效 , 可能会把有些有用的字符串给删除掉 , 这种情况可以添加到白名单解决 , 自定查找 isWhiteList 方法,进行手工添加
......@@ -12,6 +12,13 @@ import java.io.File
class StringPull {
}
/**
* 是否真的会执行移动操作的变量,请对当前git的操作进行贮存,
* 确保项目没有其他操作后再执行,否则可能会操作记录混乱
*/
const val realExecuteCmd = false
fun main(args: Array<String>) {
println("开始读取文件")
......@@ -21,10 +28,10 @@ fun main(args: Array<String>) {
//这里打印的应该会是as项目所处的路径
println(projectPath)
//要检查资源的目录,大小写敏感
val baseFile =
File("$projectPath${File.separator}LaiHuaBase${File.separator}src${File.separator}")
// val baseFile =
// File("$projectPath${File.separator}LaiHuaBase${File.separator}src${File.separator}")
//从项目根目录开始查找
// val baseFile = File("${projectRoot.absolutePath}")
val baseFile = File("${projectRoot.absolutePath}")
println(baseFile.absolutePath + "\n")
//读取代码文件
val readCode = ProjectCode().readCode(StringRefCodeReader())
......@@ -42,13 +49,15 @@ fun main(args: Array<String>) {
//未使用的string定义
for (stringXmlResCode in stringXmlResReferList) {
val filter = stringXmlResCode.stringElementList.filter {
it.refer.isEmpty()
it.refer.isEmpty() && (!isWhiteList(it))
}
println("未被使用的字符串一共有${filter.size}个")
filter.forEach {
println("未被使用的字符串 id:${it.attributeName} 内容是: ${it.value}")
//删除未使用的string资源
StringXmlParse().deleteStringXmlElement(it)
if (realExecuteCmd) {
StringXmlParse().deleteStringXmlElement(it)
}
}
}
......@@ -63,7 +72,9 @@ fun main(args: Array<String>) {
filter.forEach { stringElement ->
val module = stringElement.refer.toList()[0]
println("模块 ${module.moduleName} 依赖了字符串:${stringElement.attributeName} , 内容是: ${stringElement.value}")
StringXmlParse().moveStringXmlElement(stringElement, module)
if (realExecuteCmd) {
StringXmlParse().moveStringXmlElement(stringElement, module)
}
}
}
......@@ -108,3 +119,15 @@ private fun parseStringResFile(file: File, readCode: MutableSet<Module>): String
return stringXmlResCode
}
/**
* 白名单字符串,不能删除的字段可在此添加为白名单
*/
private fun isWhiteList(it: StringElement): Boolean {
return it.attributeName == "oppo_push_app_id"
|| it.attributeName == "oppo_push_key"
|| it.attributeName == "xiaomi_push_app_id"
|| it.attributeName == "xiaomi_push_key"
|| it.attributeName == "flyme_push_app_id"
|| it.attributeName == "flyme_push_key"
|| it.attributeName == "PORT_NUMBER"
}
......@@ -134,7 +134,7 @@ class StringXmlParse {
}
} else {
//节点已存在,不移动
print("该值无法移动,目标位置已有同名字符串:${stringElement.attributeName} -> ${targetModule.moduleName}")
println("该值无法移动,目标位置已有同名字符串:${stringElement.attributeName} -> ${targetModule.moduleName}")
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment