Commit f98a719d authored by pengjunjing's avatar pengjunjing

feat:完善字符串分析移动的功能

parent dd9cf2dd
package com.laihua.projecthelper.code
import java.io.File
/**
* 模块
* @param moduleFile 模块的文件夹路径
*/
class Module(val moduleName: String) {
class Module(private val moduleFile: File) {
//模块名字
val moduleName: String = moduleFile.name
/**
* 该模块下的代码
*/
......
package com.laihua.projecthelper.code
import com.laihua.projecthelper.implement.belongModule
import com.laihua.projecthelper.implement.belongModuleFile
import com.laihua.projecthelper.unuse.ReadUtil
import java.io.File
......@@ -30,7 +31,7 @@ class ProjectCode {
file.belongModule()?.let {
if (!moduleSet.containModule(it)) {
//添加不存在的模块
moduleSet.add(Module(it))
moduleSet.add(Module(file.belongModuleFile()!!))
}
//为该模块添加一份代码
......
......@@ -63,10 +63,17 @@ class CodeFileFindResult(val file: File, val codeLine: List<String>) {
* 返回的大小写不确定
*/
fun File.belongModule(): String? {
return this.belongModuleFile()?.name
}
/**
* 从一个文件开始,向上查找,直到查找到所在模块
*/
fun File.belongModuleFile(): File? {
var start = this
while (start.parentFile != null) {
if (hasModuleDir(start)) {
return start.name
return start
}
start = start.parentFile
}
......
......@@ -2,6 +2,7 @@ package com.laihua.projecthelper.implement
import com.laihua.projecthelper.unuse.ParseUtil
import com.laihua.projecthelper.unuse.ReadUtil
import com.laihua.projecthelper.util.exeCmd
import java.io.BufferedReader
import java.io.File
import java.io.InputStreamReader
......@@ -27,7 +28,8 @@ 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}")
println(baseFile.absolutePath + "\n")
......@@ -97,22 +99,7 @@ private fun getShellCmd(singleList: List<FileCalc>, projectPath: String): List<S
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("------------------")
}
/**
* 从资源文件中,返回只被一个模块依赖的文件.
......
......@@ -11,7 +11,7 @@ import java.io.File
* <string name="file_name">文件名称</string>
*/
data class StringElement(
// 文件
// 所属的文件
val file: File,
// 所属模块,指的是代码所属的模块
val belongModule: String,
......
......@@ -21,7 +21,8 @@ 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}")
println(baseFile.absolutePath + "\n")
......@@ -46,6 +47,7 @@ fun main(args: Array<String>) {
println("未被使用的字符串一共有${filter.size}个")
filter.forEach {
println("未被使用的字符串 id:${it.attributeName} 内容是: ${it.value}")
//删除未使用的string资源
StringXmlParse().deleteStringXmlElement(it)
}
}
......@@ -54,12 +56,14 @@ fun main(args: Array<String>) {
for (stringXmlResCode in stringXmlResReferList) {
val filter = stringXmlResCode.stringElementList.filter {
//过滤出只有一个模块依赖的string
it.refer.size == 1
}
println("${stringXmlResCode.stringXmlFile.absolutePath} 被一个模块依赖的字符串有:${filter.size}个")
filter.forEach { stringElement ->
val module = stringElement.refer.toList()[0]
println("模块 ${module.moduleName} 依赖了字符串:${stringElement.attributeName} , 内容是: ${stringElement.value}")
StringXmlParse().moveStringXmlElement(stringElement, module)
}
}
......
package com.laihua.projecthelper.stringxml
import com.laihua.projecthelper.code.Module
import com.laihua.projecthelper.implement.belongModule
import com.laihua.projecthelper.util.exeCmd
import org.jdom2.Document
import org.jdom2.Element
import org.jdom2.input.SAXBuilder
......@@ -21,6 +23,7 @@ class StringXmlParse {
const val resourcesStringDef = "resources"
const val nameStringDef = "name"
const val stringKeyDef = "string"
}
......@@ -36,7 +39,7 @@ class StringXmlParse {
if (belongModule != null && resourcesStringDef == rootElement.name) {
val children = rootElement.children
for (child in children) {
if (child.name == "string") {
if (child.name == stringKeyDef) {
//获取string节点的数据
val stringElement =
StringElement(
......@@ -70,7 +73,7 @@ class StringXmlParse {
saveXML(stringElement.file, document)
}
private fun saveXML(file: File, doc: Document) {
private fun saveXML(file: File, doc: Document): Boolean {
// 将doc对象输出到文件
try {
// 创建xml文件输出流
......@@ -86,8 +89,85 @@ class StringXmlParse {
// 将doc写入到指定的文件中
xmlOutPutter.output(doc, writer)
writer.close()
return true
} catch (e: Exception) {
e.printStackTrace()
return false
}
}
/**
* 移动一个string的定义到指定的模块下
*/
fun moveStringXmlElement(stringElement: StringElement, targetModule: Module) {
//目标xml的路径: module/src/main/res/values
val stringXmlPath = stringElement.file.absolutePath.replace(
stringElement.belongModule,
targetModule.moduleName
)
// val stringXmlPath = "${targetModule.moduleFile}" +
// "${File.pathSeparator}src${File.pathSeparator}main" +
// "${File.pathSeparator}res${File.pathSeparator}value" +
// "${File.pathSeparator}strings.xml"
val targetFile = File(stringXmlPath)
//判断创建文件
createFile(targetFile)
val document = SAXBuilder().build(targetFile)
if (!document.hasRootElement()) {
//创建根节点
document.rootElement = Element(resourcesStringDef)
}
//查找该变量存不存在
val findChild = document.rootElement.findChild(stringElement.attributeName)
if (findChild == null) {
//节点不存在,创建节点: <string name="recharge">充值</string>
val element = Element(stringKeyDef)
element.setAttribute(nameStringDef, stringElement.attributeName)
element.addContent(stringElement.value)
//添加一个字符串定义
document.rootElement.addContent(element)
//保存
if (saveXML(targetFile, document)) {
//保存成功,删除源文件的字符串
deleteStringXmlElement(stringElement)
}
} else {
//节点已存在,不移动
print("该值无法移动,目标位置已有同名字符串:${stringElement.attributeName} -> ${targetModule.moduleName}")
}
}
/**
* 判断一个文件存不存在,不存在则创建该文件
*/
private fun createFile(targetFile: File) {
//所在文件夹不存在则创建文件夹
if (!targetFile.parentFile.exists()) {
targetFile.parentFile.mkdirs()
}
if (!targetFile.exists()) {
if (targetFile.createNewFile()) {
//写入初始的xml文件
targetFile.writeText( "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
"<resources>\n" +
"" +
"</resources>")
//添加到git的版本控制
exeCmd("git add .")
}
}
}
private fun Element.findChild(nameAttributeValue: String): Element? {
for (child in this.children) {
for (attribute in child.attributes) {
if (attribute.name == nameStringDef && attribute.value == nameAttributeValue) {
return child
}
}
}
return null
}
}
\ No newline at end of file
package com.laihua.projecthelper.util
import java.io.BufferedReader
import java.io.InputStreamReader
class SysUtil {
}
/**
* 执行cmd命令
*/
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("------------------")
}
\ No newline at end of file
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