Skip to content

Commit 375f393

Browse files
committed
1、完善库
1 parent c05863f commit 375f393

File tree

4 files changed

+142
-126
lines changed

4 files changed

+142
-126
lines changed

module-communication-plugin/src/main/kotlin/com/flyjingfish/module_communication_plugin/Dom4jData.kt

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,15 @@ import org.dom4j.Document
55
import org.dom4j.Element
66
import org.dom4j.io.SAXReader
77
import org.dom4j.io.XMLWriter
8+
import java.io.BufferedReader
89
import java.io.File
10+
import java.io.FileInputStream
11+
import java.io.FileOutputStream
912
import java.io.FileWriter
13+
import java.io.IOException
14+
import java.io.InputStreamReader
15+
import java.io.OutputStreamWriter
16+
import java.io.PrintWriter
1017

1118

1219
object Dom4jData {
@@ -93,6 +100,11 @@ object Dom4jData {
93100
writer.close()
94101
} catch (e: Exception) {
95102
e.printStackTrace()
103+
} finally {
104+
try {
105+
deleteEmptyLine(xmlFile)
106+
} catch (_: Exception) {
107+
}
96108
}
97109
}
98110

@@ -127,4 +139,20 @@ object Dom4jData {
127139
}
128140
}
129141

142+
fun deleteEmptyLine(xmlFile:File) {
143+
val reader = BufferedReader(InputStreamReader(FileInputStream(xmlFile)))
144+
val sb = StringBuilder()
145+
var line: String?
146+
while (reader.readLine().also { line = it } != null) {
147+
if (line?.trim()?.isNotEmpty() == true) { // 判断当前行不为空白字符串或只包含空格时才添加到结果中
148+
sb.append(line).append("\n")
149+
}
150+
}
151+
reader.close()
152+
val writer = PrintWriter(OutputStreamWriter(FileOutputStream(xmlFile), "UTF-8"))
153+
writer.write(sb.toString())
154+
writer.flush()
155+
writer.close()
156+
}
157+
130158
}

module-communication-plugin/src/main/kotlin/com/flyjingfish/module_communication_plugin/ExportTask.kt

Lines changed: 18 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ abstract class ExportTask : DefaultTask() {
6161
val buildFile = File(dir, path)
6262

6363
val resValuesDel = mutableListOf<String>()
64-
for (resId in PackageRecordUtils.getExposeResAssets()) {
64+
for (resId in IncrementalRecordUtils.getExposeAssets()) {
6565
resValuesDel.add(resId)
6666
}
6767
if (resValuesDel.isNotEmpty()){
@@ -70,9 +70,9 @@ abstract class ExportTask : DefaultTask() {
7070
for (srcDir in assets.srcDirs) {
7171
if (srcDir.exists()){
7272
for (resValue in resValuesDel) {
73-
val file = File("${srcDir.absolutePath}/$resValue")
74-
if (file.exists()){
75-
file.deleteRecursively()
73+
val targetFile = File("${buildFile.absolutePath}/$resValue")
74+
if (targetFile.exists()){
75+
targetFile.deleteRecursively()
7676
}
7777
}
7878
}
@@ -94,14 +94,16 @@ abstract class ExportTask : DefaultTask() {
9494
for (resValue in resValues) {
9595
val targetFile = File("${buildFile.absolutePath}/$resValue")
9696
val file = File("${srcDir.absolutePath}/$resValue")
97-
file.copyRecursively(targetFile,true)
97+
if (file.exists()){
98+
file.copyRecursively(targetFile,true)
99+
}
98100
}
99101

100102

101103
}
102104
}
103105
}
104-
PackageRecordUtils.recordExposeAssets(communicationConfig.exposeResIds)
106+
IncrementalRecordUtils.recordExposeAssets(communicationConfig.exposeAssets)
105107
}
106108

107109
fun searchResFileAndCopy(curProject: Project){
@@ -114,70 +116,8 @@ abstract class ExportTask : DefaultTask() {
114116
val path = "build$codePath"
115117
val buildFile = File(dir, path)
116118

117-
val resValuesPreDel = mutableListOf<ResValue>()
118-
for (resId in PackageRecordUtils.getExposeResIds()) {
119-
val res = ResValue(resId,resId.substring(resId.indexOf(".")+1,resId.lastIndexOf(".")),resId.substring(resId.lastIndexOf(".")+1))
120-
resValuesPreDel.add(res)
121-
}
122-
val resValuesDel = resValuesPreDel.filter {
123-
it.id.startsWith("R.")
124-
}
125-
if (resValuesDel.isNotEmpty()){
126-
for (name in variantNames) {
127-
val res = libraryExtension.sourceSets.getByName(name).res
128-
for (srcDir in res.srcDirs) {
129-
if (srcDir.exists()){
130-
val genFile = srcDir.listFiles()
131-
for (resValue in resValuesDel) {
132-
if (Dom4jData.fileRes.contains(resValue.dir)){//复制文件的
133-
val dirs = genFile.filter {
134-
it.name.startsWith(resValue.dir)
135-
}
136-
val collection = curProject.files(dirs).asFileTree.filter { it.name.startsWith(resValue.fileName) }
137-
138-
for (file in collection.files) {
139-
val copyPath = "${file.parentFile.name}/${file.name}"
140-
val targetFile = File("${buildFile.absolutePath}/$copyPath")
141-
if (targetFile.exists()){
142-
targetFile.delete()
143-
}
144-
}
145-
}else{//复制xml里边的值
146-
val dirs = genFile.filter {
147-
it.name.startsWith("values")
148-
}
149-
val collection = curProject.files(dirs).asFileTree.filter { it.name.endsWith(".xml") }
150-
151-
for (file in collection.files) {
152-
val elements = Dom4jData.getXmlFileElements(file) ?: continue
153-
for (element in elements) {
154-
val nodeName: String = element.name
155-
val name: String = element.attribute("name").value
156-
if (name == resValue.fileName && nodeName != "item"){
157-
val targetFile = File("${buildFile.absolutePath}/${file.parentFile.name}",file.name)
158-
if (!targetFile.exists()){
159-
targetFile.parentFile?.mkdirs()
160-
targetFile.createNewFile()
161-
targetFile.writeText("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
162-
"<resources xmlns:tools=\"http://schemas.android.com/tools\" xmlns:android=\"http://schemas.android.com/apk/res/android\" xmlns:app=\"http://schemas.android.com/apk/res-auto\">\n" +
163-
"</resources>", Charset.forName("utf-8"))
164-
}
165-
val resMapValue = Dom4jData.resMap[resValue.dir]
166-
if ((resMapValue != null && nodeName == resMapValue)||resValue.dir == nodeName){
167-
Dom4jData.deleteElementLabel(targetFile,resValue)
168-
}
169-
}
170-
}
171-
}
172-
}
173-
}
174-
175-
176-
177-
}
178-
}
179-
}
180-
}
119+
IncrementalRecordUtils.clearResFile(moduleKey, buildFile)
120+
IncrementalRecordUtils.clearResValue(moduleKey, buildFile)
181121

182122
val resValuesPre = mutableListOf<ResValue>()
183123
for (resId in communicationConfig.exposeResIds) {
@@ -206,6 +146,7 @@ abstract class ExportTask : DefaultTask() {
206146
val copyPath = "${file.parentFile.name}/${file.name}"
207147
val targetFile = File("${buildFile.absolutePath}/$copyPath")
208148
file.copyTo(targetFile,true)
149+
IncrementalRecordUtils.recordResFile(moduleKey,copyPath)
209150
}
210151
}else{//复制xml里边的值
211152
val dirs = genFile.filter {
@@ -229,8 +170,12 @@ abstract class ExportTask : DefaultTask() {
229170
}
230171
val resMapValue = Dom4jData.resMap[resValue.dir]
231172
if ((resMapValue != null && nodeName == resMapValue)||resValue.dir == nodeName){
232-
val resValueRecord = ResValueRecord(targetFile,resValue)
233173
Dom4jData.addElementLabel(targetFile,element,resValue.fileName)
174+
val resValueRecord = ResValueRecord(targetFile,resValue)
175+
IncrementalRecordUtils.recordResValue(moduleKey, resValueRecord)
176+
177+
val text = targetFile.readText(Charset.forName("utf-8")).replace("\\s*[\r\n]+", "");
178+
targetFile.writeText(text,Charset.forName("utf-8"))
234179
}
235180
}
236181
}
@@ -243,7 +188,6 @@ abstract class ExportTask : DefaultTask() {
243188
}
244189
}
245190
}
246-
PackageRecordUtils.recordExposeResIds(communicationConfig.exposeResIds)
247191
}
248192

249193
private fun searchApiFileAndCopy(curProject: Project){
@@ -257,7 +201,7 @@ abstract class ExportTask : DefaultTask() {
257201
val buildFile = File(dir, path)
258202

259203
val moduleKey = curProject.buildDir.absolutePath
260-
val isClear = PackageRecordUtils.clearCodeFile(moduleKey,buildFile)
204+
val isClear = IncrementalRecordUtils.clearCodeFile(moduleKey,buildFile)
261205
if (isClear){
262206
val recordPackageSet = mutableSetOf<String>()
263207
for (file in collection.files) {
@@ -276,7 +220,7 @@ abstract class ExportTask : DefaultTask() {
276220
for (file in collection.files) {
277221
val packageName = getPackageName(file)
278222
packageName?.let {
279-
PackageRecordUtils.recordCodeFile(moduleKey,it)
223+
IncrementalRecordUtils.recordCodeFile(moduleKey,it)
280224
val packagePath = buildFile.absolutePath +"/"+ it.replace(".","/")
281225
val targetFile = File(packagePath,file.name.replace(".api",""))
282226
file.copyTo(targetFile,true)
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package com.flyjingfish.module_communication_plugin
2+
3+
import java.io.File
4+
5+
object IncrementalRecordUtils {
6+
private val lastRecordPackageMap = mutableMapOf<String,MutableSet<String>>()
7+
private val lastExposeResFileMap = mutableMapOf<String,MutableSet<String>>()
8+
private val lastExposeResValueMap = mutableMapOf<String,MutableList<ResValueRecord>>()
9+
// private val exposeResIds = mutableListOf<String>()
10+
private val exposeAssets = mutableListOf<String>()
11+
12+
// fun recordExposeResIds(ids : MutableList<String>){
13+
// exposeResIds.clear()
14+
// exposeResIds.addAll(ids)
15+
// }
16+
//
17+
// fun getExposeResIds():MutableList<String>{
18+
// return exposeResIds
19+
// }
20+
fun recordResFile(moduleKey :String, filePath :String){
21+
var lastRecordPackageSet = lastExposeResFileMap[moduleKey]
22+
if (lastRecordPackageSet == null){
23+
lastRecordPackageSet = mutableSetOf()
24+
lastExposeResFileMap[moduleKey] = lastRecordPackageSet
25+
}
26+
lastRecordPackageSet.add(filePath)
27+
}
28+
fun clearResFile(moduleKey :String, buildFile : File):Boolean{
29+
val lastRecordPackageSet = lastExposeResFileMap[moduleKey]
30+
lastRecordPackageSet?.let {
31+
// println("lastRecordPackageSet-size="+it.size);
32+
33+
for (filePath in it) {
34+
val packageFile = File("${buildFile.absolutePath}/${filePath}")
35+
packageFile.deleteRecursively()
36+
}
37+
38+
it.clear()
39+
}
40+
return lastRecordPackageSet.isNullOrEmpty()
41+
}
42+
fun recordResValue(moduleKey :String, resValueRecord: ResValueRecord){
43+
var lastRecordPackageSet = lastExposeResValueMap[moduleKey]
44+
if (lastRecordPackageSet == null){
45+
lastRecordPackageSet = mutableListOf()
46+
lastExposeResValueMap[moduleKey] = lastRecordPackageSet
47+
}
48+
lastRecordPackageSet.add(resValueRecord)
49+
}
50+
fun clearResValue(moduleKey :String, buildFile : File):Boolean{
51+
val lastRecordPackageSet = lastExposeResValueMap[moduleKey]
52+
lastRecordPackageSet?.let {
53+
for (resValueRecord in it) {
54+
val xmlFile = resValueRecord.xmlFile
55+
Dom4jData.deleteElementLabel(xmlFile,resValueRecord.resValue)
56+
}
57+
58+
it.clear()
59+
}
60+
return lastRecordPackageSet.isNullOrEmpty()
61+
}
62+
63+
fun recordExposeAssets(ids : MutableList<String>){
64+
exposeAssets.clear()
65+
exposeAssets.addAll(ids)
66+
}
67+
68+
fun getExposeAssets():MutableList<String>{
69+
return exposeAssets
70+
}
71+
72+
fun recordCodeFile(moduleKey :String, packageName :String){
73+
var lastRecordPackageSet = lastRecordPackageMap[moduleKey]
74+
if (lastRecordPackageSet == null){
75+
lastRecordPackageSet = mutableSetOf()
76+
lastRecordPackageMap[moduleKey] = lastRecordPackageSet
77+
}
78+
lastRecordPackageSet.add(packageName)
79+
}
80+
81+
fun clearCodeFile(moduleKey :String, buildFile : File):Boolean{
82+
val lastRecordPackageSet = lastRecordPackageMap[moduleKey]
83+
lastRecordPackageSet?.let {
84+
// println("lastRecordPackageSet-size="+it.size);
85+
86+
for (packageName in it) {
87+
val packageFile = File("${buildFile.absolutePath}/${packageName.replace(".","/")}")
88+
packageFile.deleteRecursively()
89+
}
90+
91+
it.clear()
92+
}
93+
return lastRecordPackageSet.isNullOrEmpty()
94+
}
95+
96+
}

module-communication-plugin/src/main/kotlin/com/flyjingfish/module_communication_plugin/PackageRecordUtils.kt

Lines changed: 0 additions & 52 deletions
This file was deleted.

0 commit comments

Comments
 (0)