1
1
package components
2
2
3
+ import StringCare
3
4
import StringCare.Configuration
4
5
import StringCare.Extension
5
6
import groovy.json.StringEscapeUtils
@@ -11,6 +12,8 @@ import org.slf4j.LoggerFactory
11
12
import org.w3c.dom.Document
12
13
import org.w3c.dom.Node
13
14
import org.xml.sax.InputSource
15
+ import task.SCPreview
16
+ import task.SCTestObfuscation
14
17
import java.io.*
15
18
import javax.xml.parsers.DocumentBuilderFactory
16
19
import javax.xml.transform.TransformerFactory
@@ -33,6 +36,28 @@ fun String.normalize(): String {
33
36
return com.joinToString(" " )
34
37
}
35
38
39
+ fun String.normalizePath (): String {
40
+ val unixPath = this .replace(" \\ " , " /" )
41
+ .replace(" \\\\ " , " /" )
42
+ .replace(" //" , " /" )
43
+ return when (getOs()) {
44
+ Os .OSX -> unixPath
45
+ Os .WINDOWS -> unixPath.replace(" /" , " \\ " )
46
+ }
47
+ }
48
+
49
+ fun String.uncapitalize (): String {
50
+ val original = this .trim()
51
+ if (original.isEmpty()) {
52
+ return " "
53
+ }
54
+ val char = original[0 ].toLowerCase()
55
+ return when {
56
+ original.length == 1 -> char.toString()
57
+ else -> char + original.substring(1 , original.length)
58
+ }
59
+ }
60
+
36
61
fun String.escape (): String = Regex .escape(this )
37
62
fun String.unescape (): String = StringEscapeUtils .unescapeJava(this )
38
63
fun String.removeNewLines (): String = this .replace(" \n " , " " )
@@ -49,7 +74,7 @@ fun String.androidTreatment(): String {
49
74
50
75
fun File.validForConfiguration (configuration : Configuration ): Boolean {
51
76
var valid = this .absolutePath.contains(" ${File .separator}${configuration.name}${File .separator} " )
52
-
77
+ && excluded(). not ()
53
78
if (valid) {
54
79
valid = false
55
80
configuration.srcFolders.forEach { folder ->
@@ -78,9 +103,28 @@ fun File.validForConfiguration(configuration: Configuration): Boolean {
78
103
}
79
104
}
80
105
}
106
+ if (configuration.debug && excluded().not ()) {
107
+ println (" ${if (valid) " ✔ " else " ❌ not" } valid file ${this .absolutePath} " )
108
+ }
81
109
return valid
82
110
}
83
111
112
+ fun File.excluded (): Boolean {
113
+ val exclude = listOf (
114
+ " /build/" ,
115
+ " /.git/" ,
116
+ " /.gradle/" ,
117
+ " /gradle/"
118
+ )
119
+ var valid = true
120
+ exclude.forEach { value ->
121
+ when {
122
+ this .absolutePath.contains(value.normalizePath()) -> valid = false
123
+ }
124
+ }
125
+ return (valid && this .isDirectory.not () && this .absolutePath.contains(" .xml" )).not ()
126
+ }
127
+
84
128
fun File.resourceFile (configuration : Configuration ): ResourceFile ? {
85
129
var sourceFolder = " "
86
130
var validFile: File ? = null
@@ -123,9 +167,32 @@ fun Project.absolutePath(): String = this.file(wrapperWindows).absolutePath.repl
123
167
fun Project.createExtension (): Extension {
124
168
val extension = this .extensions.create(extensionName, Extension ::class .java)
125
169
extension.modules = this .container<Configuration >(Configuration ::class .java)
170
+ StringCare .mainModule = extension.main_module
171
+ StringCare .debug = extension.debug
126
172
return extension
127
173
}
128
174
175
+ fun Project.registerTask () {
176
+ this .tasks.addRule(" Pattern: $gradleTaskNameObfuscate <variant>" ) { taskName ->
177
+ if (taskName.startsWith(gradleTaskNameObfuscate)) {
178
+ println (" taskname $taskName " )
179
+ task(taskName) {
180
+ it.doLast {
181
+ val variant = taskName.replace(gradleTaskNameObfuscate, " " ).uncapitalize()
182
+ println (" variant $variant " )
183
+ val task = this .tasks.getByName(gradleTaskNameObfuscate) as SCTestObfuscation
184
+ task.variant = variant
185
+ task.module = StringCare .mainModule
186
+ task.debug = StringCare .debug
187
+ task.greet()
188
+ }
189
+ }
190
+ }
191
+ }
192
+ this .tasks.register(gradleTaskNameDoctor, SCPreview ::class .java)
193
+ this .tasks.register(gradleTaskNameObfuscate, SCTestObfuscation ::class .java)
194
+ }
195
+
129
196
fun Process.outputString (): String {
130
197
val input = this .inputStream.bufferedReader().use { it.readText() }
131
198
val error = this .errorStream.bufferedReader().use { it.readText() }
@@ -135,13 +202,16 @@ fun Process.outputString(): String {
135
202
fun defaultConfig (): Configuration {
136
203
return Configuration (" app" ).apply {
137
204
stringFiles.add(" strings.xml" )
138
- srcFolders.add(" src${ File .separator} main" )
205
+ srcFolders.add(" src/ main" )
139
206
}
140
207
}
141
208
142
209
fun ResourceFile.backup (): File {
143
- val cleanPath = " ${StringCare .tempFolder}${File .separator}${this .module}${File .separator}${this .sourceFolder}${this .file.absolutePath.split(this .sourceFolder)[1 ]} "
144
- .replace(" ${File .separator}${File .separator} " , File .separator)
210
+ val cleanPath =
211
+ " ${StringCare .tempFolder}${File .separator}${this .module}${File .separator}${this .sourceFolder}${this .file.absolutePath.split(
212
+ this .sourceFolder
213
+ )[1 ]} "
214
+ .replace(" ${File .separator}${File .separator} " , File .separator)
145
215
146
216
val backupFile = File (cleanPath)
147
217
this .file.copyTo(backupFile, true )
@@ -294,3 +364,17 @@ fun Node.getType(): StringType {
294
364
else -> StringType .TEXT
295
365
}
296
366
}
367
+
368
+ fun Configuration.normalize (): Configuration {
369
+ val stringFiles = mutableListOf<String >()
370
+ val sourceFolders = mutableListOf<String >()
371
+ this .stringFiles.forEach { file ->
372
+ stringFiles.add(file.normalizePath())
373
+ }
374
+ this .srcFolders.forEach { folder ->
375
+ sourceFolders.add(folder.normalizePath())
376
+ }
377
+ this .stringFiles = stringFiles
378
+ this .srcFolders = sourceFolders
379
+ return this
380
+ }
0 commit comments