11package components
22
3+ import StringCare
34import StringCare.Configuration
45import StringCare.Extension
56import groovy.json.StringEscapeUtils
@@ -11,6 +12,8 @@ import org.slf4j.LoggerFactory
1112import org.w3c.dom.Document
1213import org.w3c.dom.Node
1314import org.xml.sax.InputSource
15+ import task.SCPreview
16+ import task.SCTestObfuscation
1417import java.io.*
1518import javax.xml.parsers.DocumentBuilderFactory
1619import javax.xml.transform.TransformerFactory
@@ -33,6 +36,28 @@ fun String.normalize(): String {
3336 return com.joinToString(" " )
3437}
3538
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+
3661fun String.escape (): String = Regex .escape(this )
3762fun String.unescape (): String = StringEscapeUtils .unescapeJava(this )
3863fun String.removeNewLines (): String = this .replace(" \n " , " " )
@@ -49,7 +74,7 @@ fun String.androidTreatment(): String {
4974
5075fun File.validForConfiguration (configuration : Configuration ): Boolean {
5176 var valid = this .absolutePath.contains(" ${File .separator}${configuration.name}${File .separator} " )
52-
77+ && excluded(). not ()
5378 if (valid) {
5479 valid = false
5580 configuration.srcFolders.forEach { folder ->
@@ -78,9 +103,28 @@ fun File.validForConfiguration(configuration: Configuration): Boolean {
78103 }
79104 }
80105 }
106+ if (configuration.debug && excluded().not ()) {
107+ println (" ${if (valid) " ✔ " else " ❌ not" } valid file ${this .absolutePath} " )
108+ }
81109 return valid
82110}
83111
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+
84128fun File.resourceFile (configuration : Configuration ): ResourceFile ? {
85129 var sourceFolder = " "
86130 var validFile: File ? = null
@@ -123,9 +167,32 @@ fun Project.absolutePath(): String = this.file(wrapperWindows).absolutePath.repl
123167fun Project.createExtension (): Extension {
124168 val extension = this .extensions.create(extensionName, Extension ::class .java)
125169 extension.modules = this .container<Configuration >(Configuration ::class .java)
170+ StringCare .mainModule = extension.main_module
171+ StringCare .debug = extension.debug
126172 return extension
127173}
128174
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+
129196fun Process.outputString (): String {
130197 val input = this .inputStream.bufferedReader().use { it.readText() }
131198 val error = this .errorStream.bufferedReader().use { it.readText() }
@@ -135,13 +202,16 @@ fun Process.outputString(): String {
135202fun defaultConfig (): Configuration {
136203 return Configuration (" app" ).apply {
137204 stringFiles.add(" strings.xml" )
138- srcFolders.add(" src${ File .separator} main" )
205+ srcFolders.add(" src/ main" )
139206 }
140207}
141208
142209fun 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)
145215
146216 val backupFile = File (cleanPath)
147217 this .file.copyTo(backupFile, true )
@@ -294,3 +364,17 @@ fun Node.getType(): StringType {
294364 else -> StringType .TEXT
295365 }
296366}
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