Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in / Register
Toggle navigation
R
RemoveUnusedImg
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
pengjunjing
RemoveUnusedImg
Commits
f98a719d
Commit
f98a719d
authored
Mar 14, 2022
by
pengjunjing
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat:完善字符串分析移动的功能
parent
dd9cf2dd
Show whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
133 additions
and
24 deletions
+133
-24
Module.kt
src/main/java/com/laihua/projecthelper/code/Module.kt
+7
-1
ProjectCode.kt
src/main/java/com/laihua/projecthelper/code/ProjectCode.kt
+2
-1
FileCalc.kt
src/main/java/com/laihua/projecthelper/implement/FileCalc.kt
+8
-1
SingleImplement.kt
...ava/com/laihua/projecthelper/implement/SingleImplement.kt
+4
-17
StringElement.kt
.../java/com/laihua/projecthelper/stringxml/StringElement.kt
+1
-1
StringPull.kt
...ain/java/com/laihua/projecthelper/stringxml/StringPull.kt
+5
-1
StringXmlParse.kt
...java/com/laihua/projecthelper/stringxml/StringXmlParse.kt
+82
-2
SysUtil.kt
src/main/java/com/laihua/projecthelper/util/SysUtil.kt
+24
-0
No files found.
src/main/java/com/laihua/projecthelper/code/Module.kt
View file @
f98a719d
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
/**
* 该模块下的代码
*/
...
...
src/main/java/com/laihua/projecthelper/code/ProjectCode.kt
View file @
f98a719d
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
()
!!
))
}
//为该模块添加一份代码
...
...
src/main/java/com/laihua/projecthelper/implement/FileCalc.kt
View file @
f98a719d
...
...
@@ -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
}
...
...
src/main/java/com/laihua/projecthelper/implement/SingleImplement.kt
View file @
f98a719d
...
...
@@ -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
(
"------------------"
)
}
/**
* 从资源文件中,返回只被一个模块依赖的文件.
...
...
src/main/java/com/laihua/projecthelper/stringxml/StringElement.kt
View file @
f98a719d
...
...
@@ -11,7 +11,7 @@ import java.io.File
* <string name="file_name">文件名称</string>
*/
data class
StringElement
(
// 文件
//
所属的
文件
val
file
:
File
,
// 所属模块,指的是代码所属的模块
val
belongModule
:
String
,
...
...
src/main/java/com/laihua/projecthelper/stringxml/StringPull.kt
View file @
f98a719d
...
...
@@ -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
)
}
}
...
...
src/main/java/com/laihua/projecthelper/stringxml/StringXmlParse.kt
View file @
f98a719d
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
src/main/java/com/laihua/projecthelper/util/SysUtil.kt
0 → 100644
View file @
f98a719d
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
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment