Skip to content

Commit 1485b57

Browse files
committed
windows bugfix, restoring check
1 parent d329786 commit 1485b57

File tree

5 files changed

+85
-28
lines changed

5 files changed

+85
-28
lines changed

src/main/kotlin/components/Execution.kt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,8 @@ package components
22

33
import com.google.common.io.Files
44
import models.ExecutionResult
5-
import java.io.File
65
import java.io.IOException
76

8-
fun executeWith(runtime: Runtime, command: String): ExecutionResult = execute(runtime, command)
9-
107
fun execute(command: String): ExecutionResult = execute(Runtime.getRuntime(), command)
118

129
private fun execute(runtime: Runtime, command: String): ExecutionResult {

src/main/kotlin/components/Extensions.kt

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ fun defaultConfig(): Configuration {
139139
}
140140
}
141141

142-
fun ResourceFile.backup(projectPath: String): File {
142+
fun ResourceFile.backup(): File {
143143
val cleanPath = "${StringCare.tempFolder}${File.separator}${this.module}${File.separator}${this.sourceFolder}${this.file.absolutePath.split(this.sourceFolder)[1]}"
144144
.replace("${File.separator}${File.separator}", File.separator)
145145

@@ -156,8 +156,14 @@ fun File.restore(projectPath: String): File {
156156
if (restore.exists()) {
157157
restore.delete()
158158
}
159-
FileWriter(restore.absolutePath).use { it.write(this.getContent()) }
160-
// this.copyTo(restore, true)
159+
160+
/*
161+
FileWriter(restore.absolutePath).use {
162+
it.write(this.getContent())
163+
}*/
164+
165+
File(restore.absolutePath).writeText(this.getContent())
166+
161167
return restore
162168
}
163169

src/main/kotlin/components/XParser.kt

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,21 @@ fun locateFiles(projectPath: String, configuration: Configuration): List<Resourc
1313
it.resourceFile(configuration)!!
1414
}.toList()
1515

16-
fun backupFiles(projectPath: String, configuration: Configuration): List<File> {
17-
val resourceFiles = mutableListOf<File>()
16+
fun backupFiles(projectPath: String, configuration: Configuration): List<ResourceFile> {
1817
val files = locateFiles(projectPath, configuration)
1918
files.forEach { resource ->
20-
resourceFiles.add(resource.backup(projectPath))
19+
resource.backup()
2120
}
22-
return resourceFiles
21+
return files
2322
}
2423

2524
fun restoreFiles(projectPath: String, module: String): List<File> {
26-
val resourceFiles = File("${StringCare.tempFolder}${File.separator}$module").walkTopDown().toList()
27-
resourceFiles.filter { file ->
28-
!file.isDirectory
29-
}.map {
30-
it.restore(projectPath)
31-
}
25+
val resourceFiles = File("${StringCare.tempFolder}${File.separator}$module")
26+
.walkTopDown().toList().filter { file ->
27+
!file.isDirectory
28+
}.map {
29+
it.restore(projectPath)
30+
}
3231
StringCare.resetFolder()
3332
return resourceFiles
3433
}
@@ -64,10 +63,14 @@ fun parseXML(file: File): List<StringEntity> {
6463
}
6564
}
6665
if (obfuscate) {
67-
entities.add(StringEntity(name, attributes, when {
68-
containsHtml -> node.extractHtml()
69-
else -> node.textContent
70-
}, "string", i, androidTreatment))
66+
entities.add(
67+
StringEntity(
68+
name, attributes, when {
69+
containsHtml -> node.extractHtml()
70+
else -> node.textContent
71+
}, "string", i, androidTreatment
72+
)
73+
)
7174
}
7275
}
7376
return entities
@@ -99,10 +102,12 @@ fun modifyXML(file: File, mainModule: String, key: String, debug: Boolean) {
99102
}
100103

101104
fun obfuscate(mainModule: String, key: String, entity: StringEntity): StringEntity {
102-
val obfuscation = Stark.obfuscate(mainModule, key, when(entity.androidTreatment) {
103-
true -> entity.value.androidTreatment()
104-
false -> entity.value.unescape()
105-
}.toByteArray()).toReadableString()
105+
val obfuscation = Stark.obfuscate(
106+
mainModule, key, when (entity.androidTreatment) {
107+
true -> entity.value.androidTreatment()
108+
false -> entity.value.unescape()
109+
}.toByteArray()
110+
).toReadableString()
106111
return StringEntity(entity.name, entity.attributes, obfuscation, entity.tag, entity.index, entity.androidTreatment)
107112
}
108113

src/main/kotlin/models/StringEntity.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package models
22

3-
class StringEntity(
3+
open class StringEntity(
44
var name: String,
55
var attributes: List<SAttribute>,
66
var value: String,

src/test/kotlin/SCTest.kt

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import components.*
2+
import models.StringEntity
23
import org.junit.Before
34
import org.junit.Test
45
import utils.modifyForTest
@@ -162,7 +163,55 @@ class SCTest {
162163
}
163164

164165
@Test
165-
fun `11 - (ANDROID COMPILATION) obfuscate xml and build (not real workflow)`() {
166+
fun `11 - (PLUGIN) obfuscate, restore and compare xml values with originals`() {
167+
val temp = tempPath()
168+
signingReportTask(temp).runCommand { _, report ->
169+
val configuration = defaultConfig().apply {
170+
stringFiles.add("strings_extra.xml")
171+
srcFolders.add("src${File.separator}other_source")
172+
}
173+
val files = backupFiles("$temp${File.separator}$testProjectName", configuration)
174+
assert(files.isNotEmpty())
175+
files.forEach { file ->
176+
val entities = parseXML(file.file)
177+
assert(entities.isNotEmpty())
178+
modifyXML(file.file, "$temp${File.separator}$mainModuleTest", report.extractFingerprint(), true)
179+
}
180+
val filesObfuscated = locateFiles("$temp${File.separator}$testProjectName", configuration)
181+
filesObfuscated.forEach { file ->
182+
val entities = parseXML(file.file)
183+
assert(entities.isEmpty())
184+
}
185+
186+
val restoredFiles = restoreFiles("$temp${File.separator}$testProjectName", defaultMainModule)
187+
assert(restoredFiles.isNotEmpty())
188+
189+
val originalEntities = mutableListOf<StringEntity>()
190+
files.forEach { file ->
191+
originalEntities.addAll(parseXML(file.file))
192+
}
193+
assert(originalEntities.isNotEmpty())
194+
195+
val restoredEntities = mutableListOf<StringEntity>()
196+
restoredFiles.forEach { file ->
197+
restoredEntities.addAll(parseXML(file))
198+
}
199+
assert(restoredEntities.isNotEmpty())
200+
201+
originalEntities.forEach { entity ->
202+
val eq = restoredEntities.find {
203+
it.name == entity.name
204+
}
205+
eq?.let {
206+
assert(entity.name == it.name)
207+
assert(entity.value == it.value)
208+
}
209+
}
210+
}
211+
}
212+
213+
@Test
214+
fun `12 - (ANDROID COMPILATION) obfuscate xml and build (not real workflow)`() {
166215
val temp = tempPath()
167216
signingReportTask(temp).runCommand { _, report ->
168217
val files = locateFiles("$temp${File.separator}$testProjectName", defaultConfig().apply {
@@ -194,14 +243,14 @@ class SCTest {
194243
}
195244

196245
@Test
197-
fun `12 - (PLUGIN COMPILATION) plugin with no test`() {
246+
fun `13 - (PLUGIN COMPILATION) plugin with no test`() {
198247
pluginBuildTask().runCommand { _, report ->
199248
assert(report.contains("BUILD SUCCESSFUL"))
200249
}
201250
}
202251

203252
@Test
204-
fun `13 - (ANDROID COMPILATION) plugin running on Android`() {
253+
fun `14 - (ANDROID COMPILATION) plugin running on Android`() {
205254
pluginBuildTask().runCommand { _, report ->
206255
assert(report.contains("BUILD SUCCESSFUL"))
207256
}

0 commit comments

Comments
 (0)