Commit b35a9f8c authored by pengjunjing's avatar pengjunjing

feat:增加修改gradle的脚本

parent 660e6a0c
package com.laihua.projecthelper
import com.laihua.projecthelper.unuse.ReadUtil
import java.io.File
/**
* Author: pengjunjing
* Date: 2024/12/11
* Description: 快速增加命名空间的脚本
*/
fun main() {
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}")
//从项目根目录开始查找
val baseFile = File("${projectRoot.absolutePath}")
println(baseFile.absolutePath + "\n")
val readGradleList = ReadUtil().readGradleFile(baseFile)
for (file: File in readGradleList) {
val pathGradle = file.absolutePath
if ((pathGradle.contains("mod_video")
|| pathGradle.contains("mod_kt")
|| pathGradle.contains("mod_design")
|| pathGradle.contains("mod_common"))
) {
val readText = file.readText()
var newText = ""
if (!readText.contains("namespace")) {
val nameSpace = getNameSpace(file)
println("没有定义命名空间:${file.parent} 包名是:$nameSpace")
for (line in readText.lines()) {
newText += line
newText += "\n"
if (line.contains("android {")) {
newText += " namespace '${nameSpace}'\n"
}
}
println("新结果:\n${newText.trim()}")
//写入结果
// file.writeText(newText.trim())
}
}
}
for (file: File in readGradleList) {
val pathGradle = file.absolutePath
if ((pathGradle.contains("mod_video")
|| pathGradle.contains("mod_kt")
|| pathGradle.contains("mod_design")
|| pathGradle.contains("mod_common"))
) {
val readText = file.readText()
println("去除前后空格:\n${readText.trim()}")
//写入结果
file.writeText(readText.trim())
}
}
}
//获取 AndroidManifest的包名
fun getNameSpace(file: File): String {
val module: String = file.parent
val androidManifest = File("$module/src/main/AndroidManifest.xml")
val readText = androidManifest.readText()
val split = readText.split("package=")
val s = split[1]
val substring = s.substring(1)
val substring1 = substring.substring(0, substring.indexOf("\""))
return substring1
}
......@@ -36,6 +36,22 @@ class ReadUtil {
return codeResult
}
/**
* 读取gradle文件
*/
fun readGradleFile(file: File): MutableList<File> {
val codeResult = mutableListOf<File>()
codeResult.clear()
if (file.exists() && file.isDirectory) {
readChildFile(file, ".gradle", resultList = codeResult)
} else {
println("资源目录不存在或者传入的不是目录:${file.absolutePath}")
}
return codeResult
}
val valuesDirName = "values"
private val excludeXml = arrayOf("values", "AndroidManifest", "res/xml", "res\\xml")
......
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