Commit 197f0d6c authored by pengjunjing's avatar pengjunjing

feat:增加资源单一模块依赖的工具类,可运行版本

parent 7de33e41
......@@ -2,6 +2,11 @@ package com.laihua.projecthelper
import java.io.File
/**
* 是否会执行删除操作,确认要删除资源修改下一行代码,避免误删除
*/
const val realDelFile = false
/**
* 未使用资源删除工具
*/
......@@ -9,7 +14,7 @@ fun main(args: Array<String>) {
println("开始读取文件")
val projectRoot = File("")
val projectRootF = File(projectRoot.absolutePath)
//这里打印的应该会是as项目所处的路径
println(projectRoot.absolutePath)
//要检查资源的目录
val baseFile = File("${projectRoot.absolutePath}${File.separator}laihuaBase${File.separator}src${File.separator}")
......@@ -32,7 +37,9 @@ fun main(args: Array<String>) {
unusedSet.forEach {
println("未使用的资源: ${it.name}")
//确认要删除资源打开下一行代码,注释掉避免误删除
// it.deleteOnExit()
if (realDelFile) {
it.deleteOnExit()
}
}
//检查没使用的图片xml/布局xml文件
......@@ -42,7 +49,9 @@ fun main(args: Array<String>) {
startParseUnused.forEach {
println("未使用的xml资源: ${it.absolutePath}")
//确认要删除资源打开下一行代码,注释掉避免误删除
// it.deleteOnExit()
if (realDelFile) {
it.deleteOnExit()
}
}
}
......
package com.laihua.projecthelper
package com.laihua.projecthelper.implement
import com.laihua.projecthelper.implement.CodeFileFindResult
import com.laihua.projecthelper.implement.FileCalc
import com.laihua.projecthelper.ParseUtil
import com.laihua.projecthelper.ReadUtil
import java.io.BufferedReader
import java.io.File
import java.io.InputStreamReader
/**
* Author: pengjunjing
* Date: 2022/3/7
* Description:单文件引用工具类,用于抽取到对应模块的时候使用
*/
/**
* 是否真的会执行移动操作的变量,请对当前git的操作进行贮存,
* 确保项目没有其他操作后再执行,否则可能会操作记录混乱
*/
const val realExecuteCmd = false
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 projectPath = projectRoot.absolutePath
val projectRootF = File(projectPath)
//这里打印的应该会是as项目所处的路径
println(projectPath)
//要检查资源的目录,大小写敏感
val baseFile = File("$projectPath${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)
//查找单个依赖的文件
val singleList: List<FileCalc> = startFind(readImageRes, readCodeFile)
//这个单模块依赖的意思,包含本模块依赖的文件
println("一共有:${singleList.size}个文件是单模块依赖的,都需要进行移动")
val shellCmd = getShellCmd(singleList, projectPath)
println("------------------------------")
/**
* 当文件很多时,该遍历会导致一行一行执行命令,窗口一闪一闪的,效率比较低.
* 如果追求效率可以使用写入文件,然后使用bat或者shell的方式一次执行
*/
shellCmd.forEach { cmd ->
println("将要执行命令:$cmd")
if (realExecuteCmd) {
exeCmd(cmd)
}
}
//写入到文件
// writeToFile(shellCmd, projectPath)
}
fun startFind(readImageRes: MutableList<File>, readCodeFile: MutableList<File>) {
/**
* 获取到命令行
* 如果执行的命令结果提示:
* fatal: not under version control, source=m_kt_base/README.MD, destination=m_kt_account/README.MD
* 请检查文件夹大小是否一样
*/
private fun getShellCmd(singleList: List<FileCalc>, projectPath: String): List<String> {
val cmdList = mutableListOf<String>()
for (fileCalc in singleList) {
val implementModuleName = fileCalc.implementModule.toList()[0]
if (fileCalc.belongModule != null && implementModuleName.lowercase() != fileCalc.belongModule.lowercase()) {
println("模块:$implementModuleName,依赖了 ${fileCalc.belongModule} 模块下的文件: ${fileCalc.file.name}")
val srcPath = fileCalc.file.absolutePath
val targetPath =
srcPath.replace(fileCalc.belongModule!!, fileCalc.implementModule.toList()[0])
val targetFile = File(targetPath)
println("需要将:$srcPath 移动到: $targetPath")
if (realExecuteCmd && !targetFile.parentFile.exists()) {
//在确认需要执行命令的情况下 , 文件夹不存在则创建文件夹
val mkdirs = targetFile.parentFile.mkdirs()
println("创建文件夹:${targetFile.parentFile.absolutePath} $mkdirs")
}
if (targetFile.parentFile.exists()) {
//执行命令行
val form = srcPath.replace(projectPath, "")
val to = targetPath.replace(projectPath, "")
val cmd = "git mv .$form .$to"
cmdList.add(cmd)
}
}
}
return cmdList
}
/**
* 执行cmd命令
*/
private fun exeCmd(cmd: String) {
println(cmd)
val cmdHead = "cmd.exe /C start "
val process = Runtime.getRuntime().exec(cmdHead + cmd)
val status = process.waitFor()
val bufferedReader = BufferedReader(InputStreamReader(process.inputStream))
var readLine = bufferedReader.readLine()
while (readLine != null) {
println(readLine)
readLine = bufferedReader.readLine()
}
println("------------------")
}
/**
* 从资源文件中,返回只被一个模块依赖的文件.
*/
fun startFind(readImageRes: List<File>, readCodeFile: List<File>): List<FileCalc> {
//统计所有有调用到资源的代码文件
val codeFileList = mutableListOf<CodeFileFindResult>()
readCodeFile.forEach {
......@@ -73,12 +159,19 @@ fun startFind(readImageRes: MutableList<File>, readCodeFile: MutableList<File>)
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}")
}
}
return singleList
}
fun writeToFile(shellCmd: List<String>, projectPath: String) {
val batFile = File("$projectPath${File.separator}singleImplement.bat")
// batFile.deleteOnExit()
val createNewFile = batFile.createNewFile()
println("创建文件:${batFile.absolutePath} $createNewFile")
//清空内容
batFile.writeText("")
shellCmd.forEach {
batFile.appendText(it + "\n")
}
batFile.appendText("pause")
}
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