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
7de33e41
Commit
7de33e41
authored
Mar 07, 2022
by
pengjunjing
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat:增加资源单一模块依赖的工具类
parent
e7cbf11f
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
177 additions
and
1 deletion
+177
-1
ParseUtil.kt
src/main/java/com/laihua/projecthelper/ParseUtil.kt
+1
-1
UnuseRes.kt
src/main/java/com/laihua/projecthelper/UnuseRes.kt
+3
-0
FileCalc.kt
src/main/java/com/laihua/projecthelper/implement/FileCalc.kt
+89
-0
SingleImplement.kt
...ava/com/laihua/projecthelper/implement/SingleImplement.kt
+84
-0
No files found.
src/main/java/com/laihua/projecthelper/ParseUtil.kt
View file @
7de33e41
...
@@ -14,7 +14,7 @@ class ParseUtil {
...
@@ -14,7 +14,7 @@ class ParseUtil {
/**
/**
* 返回引用了图片资源的该行文本内容
* 返回引用了图片资源的该行文本内容
*/
*/
private
fun
parseFile
(
file
:
File
):
MutableList
<
String
>
{
fun
parseFile
(
file
:
File
):
MutableList
<
String
>
{
val
reader
=
FileReader
(
file
)
val
reader
=
FileReader
(
file
)
val
readLines
=
reader
.
readLines
()
val
readLines
=
reader
.
readLines
()
val
result
=
mutableListOf
<
String
>()
val
result
=
mutableListOf
<
String
>()
...
...
src/main/java/com/laihua/projecthelper/UnuseRes.kt
View file @
7de33e41
...
@@ -2,6 +2,9 @@ package com.laihua.projecthelper
...
@@ -2,6 +2,9 @@ package com.laihua.projecthelper
import
java.io.File
import
java.io.File
/**
* 未使用资源删除工具
*/
fun
main
(
args
:
Array
<
String
>)
{
fun
main
(
args
:
Array
<
String
>)
{
println
(
"开始读取文件"
)
println
(
"开始读取文件"
)
val
projectRoot
=
File
(
""
)
val
projectRoot
=
File
(
""
)
...
...
src/main/java/com/laihua/projecthelper/implement/FileCalc.kt
0 → 100644
View file @
7de33e41
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
}
src/main/java/com/laihua/projecthelper/implement/SingleImplement.kt
0 → 100644
View file @
7de33e41
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}"
)
}
}
}
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