Skip to content

Commit b03b170

Browse files
committed
kotlin plugin working
1 parent f1c6b0e commit b03b170

File tree

8 files changed

+123
-49
lines changed

8 files changed

+123
-49
lines changed

src/main/kotlin/StringCare.kt

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
import components.*
2-
import models.Configuration
3-
import models.Extension
2+
import org.gradle.api.NamedDomainObjectContainer
43
import org.gradle.api.Plugin
54
import org.gradle.api.Project
5+
import org.gradle.api.internal.plugins.DslObject
66

7-
class StringCare : Plugin<Project> {
7+
open class StringCare : Plugin<Project> {
8+
9+
companion object {
10+
@JvmStatic
11+
private lateinit var absoluteProjectPath: String
12+
}
813

9-
private lateinit var absoluteProjectPath: String
1014
private lateinit var project: Project
1115
private lateinit var extension: Extension
1216
private val moduleMap: MutableMap<String, Configuration> = mutableMapOf()
@@ -19,7 +23,7 @@ class StringCare : Plugin<Project> {
1923
absoluteProjectPath = project.absolutePath()
2024

2125
this.project.afterEvaluate {
22-
extension.modules.forEach { module ->
26+
extension.modules?.forEach { module ->
2327
when {
2428
module.stringFiles.isNotEmpty() && module.srcFolders.isNotEmpty() -> {
2529
moduleMap[module.name!!] = Configuration(module.name).apply {
@@ -87,5 +91,21 @@ class StringCare : Plugin<Project> {
8791
}
8892
))
8993
}
94+
open class Extension {
95+
var debug: Boolean = false
96+
var main_module: String = "app"
97+
var modules: NamedDomainObjectContainer<Configuration>
98+
@Suppress("UNCHECKED_CAST")
99+
get() = DslObject(this).extensions.getByName("modules") as NamedDomainObjectContainer<Configuration>
100+
internal set(value) {
101+
DslObject(this).extensions.add("modules", value)
102+
}
103+
}
90104

105+
open class Configuration(var name: String?) {
106+
var stringFiles = mutableListOf<String>()
107+
var srcFolders = mutableListOf<String>()
108+
}
91109
}
110+
111+

src/main/kotlin/components/Extensions.kt

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

3+
import StringCare.*
34
import groovy.json.StringEscapeUtils
4-
import models.Configuration
5-
import models.Extension
65
import models.ResourceFile
76
import org.gradle.api.Project
87
import org.gradle.api.Task
8+
import org.gradle.api.internal.plugins.DslObject
99
import org.w3c.dom.Document
1010
import org.xml.sax.InputSource
1111
import java.io.*

src/main/kotlin/components/Stark.kt

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,20 @@ package components
22

33
import java.io.File
44
import java.io.FileOutputStream
5+
import java.util.zip.ZipFile
56

6-
class Stark {
7+
open class Stark {
78

89
companion object {
910
init {
1011
when (getOs()) {
11-
Os.WINDOWS -> loadLib("..\\$winLib")
12-
Os.OSX -> loadLib("../$osxLib")
12+
Os.WINDOWS -> loadLib(winLib)
13+
Os.OSX -> loadLib(osxLib)
1314
}
1415
}
1516

1617
private fun loadLib(name: String) {
17-
// val path = (Stark::class.java.protectionDomain.codeSource.location.toURI()).path
18-
val inputStream = Stark::class.java.getResourceAsStream(name)
18+
val inputStream = getLibFromFolder(name)?.inputStream()?: return
1919
val buffer = ByteArray(1024)
2020
val temp = File.createTempFile(name, "")
2121
val fos = FileOutputStream(temp)
@@ -31,9 +31,44 @@ class Stark {
3131
System.load(temp.absolutePath)
3232
}
3333

34-
@JvmStatic external fun obfuscate(mainModule: String, key: String, value: ByteArray): ByteArray
34+
private fun getLibFromFolder(fileName: String): File? {
35+
var lib: File? = null
36+
val dir = Stark::class.java.protectionDomain.codeSource.location.toURI()
37+
when {
38+
dir.toString().endsWith(".jar") -> {
39+
val jar = File(dir)
40+
val zip = File(jar.absolutePath.replace(".jar", ".zip"))
3541

36-
@JvmStatic external fun reveal(mainModule: String, key: String, value: ByteArray): ByteArray
42+
jar.copyTo(zip, true)
43+
44+
ZipFile(zip.absolutePath).use { zip ->
45+
zip.entries().asSequence().forEach { entry ->
46+
zip.getInputStream(entry).use { input ->
47+
if (entry.name == fileName) {
48+
lib = File(entry.name).apply {
49+
this.outputStream().use { output ->
50+
input.copyTo(output)
51+
}
52+
}
53+
}
54+
}
55+
}
56+
}
57+
}
58+
else -> {
59+
lib = File(dir).walkTopDown().find { file ->
60+
file.name == fileName
61+
}
62+
}
63+
}
64+
return lib
65+
}
66+
67+
@JvmStatic
68+
external fun obfuscate(mainModule: String, key: String, value: ByteArray): ByteArray
69+
70+
@JvmStatic
71+
external fun reveal(mainModule: String, key: String, value: ByteArray): ByteArray
3772
}
3873

3974
}

src/main/kotlin/components/XParser.kt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package components
22

3-
import models.Configuration
3+
import StringCare.*
44
import models.ResourceFile
55
import models.SAttribute
66
import models.StringEntity
@@ -30,10 +30,7 @@ fun restoreFiles(projectPath: String, module: String): List<File> {
3030
return resourceFiles
3131
}
3232

33-
fun parseXML(file: File, debug: Boolean): List<StringEntity> {
34-
if (debug) {
35-
PrintUtils.print(null, file.getContent(), true)
36-
}
33+
fun parseXML(file: File): List<StringEntity> {
3734
val entities = mutableListOf<StringEntity>()
3835

3936
val doc = file.getXML()
@@ -63,7 +60,7 @@ fun parseXML(file: File, debug: Boolean): List<StringEntity> {
6360
}
6461

6562
fun modifyXML(file: File, mainModule: String, key: String, debug: Boolean) {
66-
val stringEntities = parseXML(file, debug)
63+
val stringEntities = parseXML(file)
6764
if (debug) {
6865
PrintUtils.print(null, file.getContent(), true)
6966
}
@@ -82,10 +79,13 @@ fun modifyXML(file: File, mainModule: String, key: String, debug: Boolean) {
8279

8380
file.updateXML(doc)
8481
file.removeHiddenAttributes()
82+
if (debug) {
83+
PrintUtils.print(null, file.getContent(), true)
84+
}
8585
}
8686

8787
fun obfuscate(mainModule: String, key: String, entity: StringEntity): StringEntity {
88-
val obfuscation = Stark.obfuscate(mainModule, key, entity.value.toByteArray()).toReadableString()
88+
val obfuscation = Stark.obfuscate(mainModule, key, entity.value.unescape().toByteArray()).toReadableString()
8989
return StringEntity(entity.name, entity.attributes, obfuscation, entity.tag, entity.index)
9090
}
9191

src/main/kotlin/models/Configuration.kt

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

src/main/kotlin/models/Extension.kt

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

src/test/kotlin/SCTest.kt

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import components.*
22
import org.junit.Before
33
import org.junit.Test
4+
import utils.modifyForTest
45
import java.io.File
56

67

@@ -89,18 +90,17 @@ class SCTest {
8990
prepareTask.runCommand { _, _ ->
9091
val files = locateFiles(projectName, defaultConfig())
9192
files.forEach {
92-
assert(parseXML(it.file, true).isNotEmpty())
93+
assert(parseXML(it.file).isNotEmpty())
9394
}
9495
}
9596
}
9697

97-
9898
@Test
9999
fun `08 - (PLUGIN) obfuscate string values`() {
100100
signingReportTask.runCommand { _, report ->
101101
val files = locateFiles(projectName, defaultConfig())
102102
files.forEach { file ->
103-
val entities = parseXML(file.file, true)
103+
val entities = parseXML(file.file)
104104
entities.forEach { entity ->
105105
val obfuscated = obfuscate(
106106
mainModuleTest,
@@ -118,7 +118,7 @@ class SCTest {
118118
signingReportTask.runCommand { _, report ->
119119
val files = locateFiles(projectName, defaultConfig())
120120
files.forEach { file ->
121-
val entities = parseXML(file.file, true)
121+
val entities = parseXML(file.file)
122122
entities.forEach { entity ->
123123
val obfuscated = obfuscate(
124124
mainModuleTest,
@@ -145,13 +145,13 @@ class SCTest {
145145
signingReportTask.runCommand { _, report ->
146146
val files = locateFiles(projectName, defaultConfig())
147147
files.forEach { file ->
148-
val entities = parseXML(file.file, true)
148+
val entities = parseXML(file.file)
149149
assert(entities.isNotEmpty())
150150
modifyXML(file.file, mainModuleTest, report.extractFingerprint(), true)
151151
}
152152
val filesObfuscated = locateFiles(projectName, defaultConfig())
153153
filesObfuscated.forEach { file ->
154-
val entities = parseXML(file.file, true)
154+
val entities = parseXML(file.file)
155155
assert(entities.isEmpty())
156156
}
157157
}
@@ -160,28 +160,46 @@ class SCTest {
160160
@Test
161161
fun `11 - (ANDROID COMPILATION) obfuscate xml and build (not real workflow)`() {
162162
signingReportTask.runCommand { _, report ->
163-
val files = locateFiles(projectName, defaultConfig())
163+
val files = locateFiles(projectName, defaultConfig().apply {
164+
stringFiles.add("strings_extra.xml")
165+
srcFolders.add("src/other_source")
166+
})
164167
files.forEach { file ->
165-
val entities = parseXML(file.file, true)
168+
val entities = parseXML(file.file)
166169
assert(entities.isNotEmpty())
167170
modifyXML(file.file, mainModuleTest, report.extractFingerprint(), true)
168171
}
169-
val filesObfuscated = locateFiles(projectName, defaultConfig())
172+
val filesObfuscated = locateFiles(projectName, defaultConfig().apply {
173+
stringFiles.add("strings_extra.xml")
174+
srcFolders.add("src/other_source")
175+
})
170176
filesObfuscated.forEach { file ->
171-
val entities = parseXML(file.file, true)
177+
val entities = parseXML(file.file)
172178
assert(entities.isEmpty())
173179
}
174-
buildTask.runCommand { _, androidReport ->
175-
assert(androidReport.contains("BUILD SUCCESSFUL"))
176-
}
177180
}
178181
}
179182

183+
184+
180185
@Test
181186
fun `12 - (PLUGIN COMPILATION) plugin with no test`() {
182187
pluginBuildTask().runCommand { _, report ->
183188
assert(report.contains("BUILD SUCCESSFUL"))
184189
}
185190
}
186191

192+
@Test
193+
fun `13 - (ANDROID COMPILATION) plugin running on Android`() {
194+
pluginBuildTask().runCommand { _, report ->
195+
assert(report.contains("BUILD SUCCESSFUL"))
196+
}
197+
prepareTask.runCommand { _, _ ->
198+
modifyForTest(projectName)
199+
buildTask.runCommand { _, androidReport ->
200+
assert(androidReport.contains("BUILD SUCCESSFUL"))
201+
}
202+
}
203+
}
204+
187205
}

src/test/kotlin/utils/Helper.kt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package utils
2+
3+
import components.extensionName
4+
import components.getContent
5+
import java.io.File
6+
import java.io.FileWriter
7+
8+
fun modifyForTest(projectPath: String) {
9+
val file = File("$projectPath${File.separator}build.gradle")
10+
val content = file.getContent().replace(
11+
"classpath \"com.stringcare:plugin:\$stringcare_version\"",
12+
"\nclasspath files(\"..${File.separator}build${File.separator}libs${File.separator}plugin-2.3-SNAPSHOT.jar\")\n"
13+
)
14+
FileWriter(file.absolutePath).use { it.write(content) }
15+
16+
}

0 commit comments

Comments
 (0)