Commit 7de33e41 authored by pengjunjing's avatar pengjunjing

feat:增加资源单一模块依赖的工具类

parent e7cbf11f
......@@ -14,7 +14,7 @@ class ParseUtil {
/**
* 返回引用了图片资源的该行文本内容
*/
private fun parseFile(file: File): MutableList<String> {
fun parseFile(file: File): MutableList<String> {
val reader = FileReader(file)
val readLines = reader.readLines()
val result = mutableListOf<String>()
......
......@@ -2,6 +2,9 @@ package com.laihua.projecthelper
import java.io.File
/**
* 未使用资源删除工具
*/
fun main(args: Array<String>) {
println("开始读取文件")
val projectRoot = File("")
......
package com.laihua.projecthelper.implement
import java.io.File
/**
* Author: pengjunjing
* Date: 2022/3/7
* Description:
*/
class FileCalc(val file: File) {
/**
* 去除后缀的名字,代码依赖的时候,不需要使用后缀的
*/
val implementName: String
//依赖的代码
val implementCode = mutableListOf<CodeFileFindResult>()
val belongModule = this.file.belongModule()
init {
//文件名 ,带后缀,去除后缀
val name = file.name
implementName = if (name.contains(".")) {
name.substring(0, name.indexOf("."))
} else {
name
}
}
val implementModule: MutableSet<String> = mutableSetOf()
/**
* 统计该资源被哪些模块使用了
*/
fun calcImplementModule() {
implementModule.clear()
implementCode.forEach {
it.belongModule?.let { moduleName ->
implementModule.add(moduleName)
}
}
}
}
/**
* @param file 文件
* @param codeLine 调用的代码
*/
class CodeFileFindResult(val file: File, val codeLine: List<String>) {
val belongModule = this.file.belongModule()
}
/**
* 判断一个文件是属于哪个模块的
* 返回的大小写不确定
*/
fun File.belongModule(): String? {
var start = this
while (start.parentFile != null) {
if (hasModuleDir(start)) {
return start.name
}
start = start.parentFile
}
return null
}
/**
* 判断一个目录是否为模块的根目录
*/
fun hasModuleDir(file: File): Boolean {
val listFiles = file.listFiles()
if (!listFiles.isNullOrEmpty()) {
for (listFile in listFiles) {
if (listFile.name.lowercase() == "build.gradle") {
return true
}
}
}
return false
}
package com.laihua.projecthelper
import com.laihua.projecthelper.implement.CodeFileFindResult
import com.laihua.projecthelper.implement.FileCalc
import java.io.File
/**
* Author: pengjunjing
* Date: 2022/3/7
* Description:单文件引用工具类,用于抽取到对应模块的时候使用
*/
fun main(args: Array<String>) {
println("开始读取文件")
val projectRoot = File("")
val projectRootF = File(projectRoot.absolutePath)
println(projectRoot.absolutePath)
//要检查资源的目录
val baseFile = File("${projectRoot.absolutePath}${File.separator}laihuaBase${File.separator}src${File.separator}")
//从项目根目录开始查找
// val baseFile = File("${projectRoot.absolutePath}")
println(baseFile.absolutePath + "\n")
val readImageRes = ReadUtil().readImageRes(baseFile)
val readCodeFile = ReadUtil().readCodeFile(projectRootF)
startFind(readImageRes, readCodeFile)
}
fun startFind(readImageRes: MutableList<File>, readCodeFile: MutableList<File>) {
//统计所有有调用到资源的代码文件
val codeFileList = mutableListOf<CodeFileFindResult>()
readCodeFile.forEach {
val parseFile = ParseUtil().parseFile(it)
if (parseFile.isNotEmpty()) {
codeFileList.add(CodeFileFindResult(it, parseFile))
}
}
val resList: List<FileCalc> = readImageRes.map {
FileCalc(it)
}
//统计引用次数
codeFileList.forEach { codeFile: CodeFileFindResult ->//代码文件
codeFile.codeLine.forEach { codeLine: String ->//一行代码
resList.forEach { resFile: FileCalc ->//资源文件
if (codeLine.contains(resFile.implementName, ignoreCase = true)) {
//引用次数+1
resFile.implementCode.add(codeFile)
}
}
}
}
resList.forEach { fileCalc ->
//计算所有依赖是属于哪个模块的
fileCalc.calcImplementModule()
}
//只有一个依赖的列表
val singleList = mutableListOf<FileCalc>()
resList.forEach { fileCalc ->
if (fileCalc.implementModule.size == 1) {
//只有一个模块依赖的该文件,移动到
singleList.add(fileCalc)
}
}
//按模块排序
singleList.sortBy {
it.implementModule.toList()[0]
}
singleList.forEach {
val implementModuleName = it.implementModule.toList()[0]
if (it.belongModule != null && implementModuleName.lowercase() != it.belongModule.lowercase()) {
println("模块:$implementModuleName,依赖了 ${it.belongModule} 模块下的文件: ${it.file.name}")
}
}
}
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