Skip to content

Commit

Permalink
Merge pull request #4 from StringCare/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
efraespada authored Aug 17, 2019
2 parents 05ea4ae + 3fe7b45 commit 57c0428
Show file tree
Hide file tree
Showing 15 changed files with 445 additions and 81 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {
}

group 'com.stringcare'
version '3.3'
version '3.4'

def siteUrl = 'https://github.com/StringCare/KotlinGradlePlugin'
def gitUrl = 'https://github.com/StringCare/KotlinGradlePlugin.git'
Expand Down
113 changes: 82 additions & 31 deletions src/main/kotlin/StringCare.kt
Original file line number Diff line number Diff line change
Expand Up @@ -43,32 +43,25 @@ open class StringCare : Plugin<Project> {

this.project.afterEvaluate {
extension.modules.forEach { module ->
when {
module.stringFiles.isNotEmpty() && module.srcFolders.isNotEmpty() -> {
moduleMap[module.name] = Configuration(module.name).apply {
stringFiles.addAll(module.stringFiles)
srcFolders.addAll(module.srcFolders)
debug = extension.debug
}
}
module.srcFolders.isNotEmpty() -> {
moduleMap[module.name] = Configuration(module.name).apply {
stringFiles.addAll(defaultConfig().stringFiles)
srcFolders.addAll(module.srcFolders)
debug = extension.debug
}
}
module.stringFiles.isNotEmpty() -> {
moduleMap[module.name] = Configuration(module.name).apply {
stringFiles.addAll(module.stringFiles)
srcFolders.addAll(defaultConfig().srcFolders)
debug = extension.debug
}
}
moduleMap[module.name] = Configuration(module.name).apply {
debug = extension.debug
}
if (module.srcFolders.isNotEmpty()) {
moduleMap[module.name]!!.srcFolders.addAll(module.srcFolders)
}
if (module.stringFiles.isNotEmpty()) {
moduleMap[module.name]!!.stringFiles.addAll(module.stringFiles)
}
if (module.assetsFiles.isNotEmpty()) {
moduleMap[module.name]!!.assetsFiles.addAll(module.assetsFiles)
}

if (moduleMap[module.name]!!.srcFolders.isEmpty()) {
moduleMap[module.name]!!.srcFolders.addAll(defaultConfig().srcFolders)
}
if (moduleMap[module.name]!!.stringFiles.isEmpty()) {
moduleMap[module.name]!!.stringFiles.addAll(defaultConfig().stringFiles)
}
}
if (moduleMap.isEmpty()) {
moduleMap[defaultMainModule] = defaultConfig().normalize()
}
this.project.registerTask()
}
Expand All @@ -95,13 +88,14 @@ open class StringCare : Plugin<Project> {
PrintUtils.print(module, "$variant:$key")
PrintUtils.print(module, backupStringRes)
moduleMap[module]?.let { configuration ->
backupFiles(absoluteProjectPath, configuration)
backupResourceFiles(absoluteProjectPath, configuration)
}

moduleMap[module]?.let { configuration ->
val files = locateFiles(absoluteProjectPath, configuration)
val files = locateResourceFiles(absoluteProjectPath, configuration)
files.forEach { file ->
modifyXML(file.file, extension.main_module, key, extension.debug,
modifyXML(
file.file, extension.main_module, key, extension.debug,
variantOrFlavor?.applicationId ?: ""
)
}
Expand All @@ -114,9 +108,9 @@ open class StringCare : Plugin<Project> {
}
PrintUtils.print(module, "$variant:$key")
PrintUtils.print(module, backupStringRes)
backupFiles(absoluteProjectPath, defaultConfiguration)
backupResourceFiles(absoluteProjectPath, defaultConfiguration)
PrintUtils.print(module, obfuscateStringRes)
val files = locateFiles(absoluteProjectPath, defaultConfiguration)
val files = locateResourceFiles(absoluteProjectPath, defaultConfiguration)
files.forEach { file ->
modifyXML(file.file, extension.main_module, key, extension.debug)
}
Expand All @@ -126,14 +120,70 @@ open class StringCare : Plugin<Project> {

},
mergeResourcesFinish = { module, variant ->
PrintUtils.print(module, restoreStringRes)
val variantOrFlavor = extension.variants.find {
variant.toLowerCase().contains(it.name.toLowerCase())
}
if (variantOrFlavor != null && variantOrFlavor.skip) {
return@ExecutionListener
}
restoreResourceFiles(absoluteProjectPath, module)
},
mergeAssetsStart = { module, variant ->
fingerPrint(module, variant, extension.debug) { key ->
if ("none" == key) {
return@fingerPrint
}
when {
moduleMap.containsKey(module) -> {
val variantOrFlavor = extension.variants.find {
variant.toLowerCase().contains(it.name.toLowerCase())
}
if (variantOrFlavor != null && variantOrFlavor.skip) {
PrintUtils.print(module, "Skipping $variant")
return@fingerPrint
}

PrintUtils.print(module, "$variant:$key")
PrintUtils.print(module, backupAssets)
moduleMap[module]?.let { configuration ->
backupAssetsFiles(absoluteProjectPath, configuration)
}

moduleMap[module]?.let { configuration ->
val files = locateAssetsFiles(absoluteProjectPath, configuration)
files.forEach { file ->
if (extension.debug) {
PrintUtils.print(null, file.file.getContent())
}
obfuscateFile(
extension.main_module,
key,
file.file,
variantOrFlavor?.applicationId ?: ""
)
if (extension.debug) {
PrintUtils.print(null, file.file.getContent())
}
}
}
PrintUtils.print(module, obfuscateAssets)
}
}
}

},
mergeAssetsFinish = { module, variant ->
PrintUtils.print(module, restoreAssets)
val variantOrFlavor = extension.variants.find {
variant.toLowerCase().contains(it.name.toLowerCase())
}
if (variantOrFlavor != null && variantOrFlavor.skip) {
return@ExecutionListener
}
restoreFiles(absoluteProjectPath, module)
restoreAssetsFiles(absoluteProjectPath, module)
}

))
}

Expand All @@ -155,6 +205,7 @@ open class StringCare : Plugin<Project> {
}

open class Configuration(var name: String) {
var assetsFiles = mutableListOf<String>()
var stringFiles = mutableListOf<String>()
var srcFolders = mutableListOf<String>()
var debug = false
Expand Down
51 changes: 51 additions & 0 deletions src/main/kotlin/components/AParser.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package components

import StringCare.*
import models.AssetsFile
import models.ResourceFile
import java.io.File

fun locateAssetsFiles(projectPath: String, configuration: Configuration): List<AssetsFile> {
if (configuration.debug) {
println("== ASSETS FILES FOUND ======================================")
}
return File(projectPath).walkTopDown()
.filterIndexed { _, file ->
file.validForAssetsConfiguration(configuration.normalize())
}.map {
it.assetsFile(configuration.normalize())!!
}.toList()
}

fun backupAssetsFiles(projectPath: String, configuration: Configuration): List<AssetsFile> {
val files = locateAssetsFiles(projectPath, configuration.normalize())
files.forEach { resource ->
resource.backup()
}
return files
}

fun restoreAssetsFiles(projectPath: String, module: String): List<File> {
val resourceFiles = File("${StringCare.tempFolder}${File.separator}$module")
.walkTopDown().toList().filter { file ->
!file.isDirectory
}.map {
it.restore(projectPath)
}
StringCare.resetFolder()
return resourceFiles
}

fun obfuscateFile(mainModule: String, key: String, file: File, mockId: String = "") {
val obfuscation = Stark.obfuscate(
mainModule,
key,
file.readBytes(),
mockId
)
file.writeBytes(obfuscation)
}

fun revealFile(mainModule: String, key: String, file: File, mockId: String = "") {
file.writeBytes(Stark.reveal(mainModule, key, file.readBytes(), mockId))
}
13 changes: 12 additions & 1 deletion src/main/kotlin/components/ExecutionListener.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ class ExecutionListener(
private val debug: Boolean = false,
val dataFound: (module: String, variant: String) -> Unit,
val mergeResourcesStart: (module: String, variant: String) -> Unit,
val mergeResourcesFinish: (module: String, variant: String) -> Unit
val mergeResourcesFinish: (module: String, variant: String) -> Unit,
val mergeAssetsStart: (module: String, variant: String) -> Unit,
val mergeAssetsFinish: (module: String, variant: String) -> Unit
) : BuildListener,
TaskExecutionListener {

Expand All @@ -27,6 +29,12 @@ class ExecutionListener(
}
mergeResourcesStart(it, PrintUtils.uncapitalize(task.onMergeResourcesStartsVariant()))
}
task.onMergeAssetsStarts() -> task.getModuleName()?.let {
if (debug) {
PrintUtils.print(it, "Module: $it", true)
}
mergeAssetsStart(it, PrintUtils.uncapitalize(task.onMergeAssetsStartsVariant()))
}
}
}

Expand All @@ -35,6 +43,9 @@ class ExecutionListener(
task.onMergeResourcesFinish() -> task.getModuleName()?.let {
mergeResourcesFinish(it, PrintUtils.uncapitalize(task.onMergeResourcesFinishVariant()))
}
task.onMergeAssetsFinish() -> task.getModuleName()?.let {
mergeAssetsFinish(it, PrintUtils.uncapitalize(task.onMergeAssetsFinishVariant()))
}
}
}

Expand Down
Loading

0 comments on commit 57c0428

Please sign in to comment.