Skip to content

Commit

Permalink
Merge pull request #11 from StringCare/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
efraespada authored Jan 3, 2020
2 parents a84e0e0 + 07b38cd commit e817879
Show file tree
Hide file tree
Showing 15 changed files with 363 additions and 312 deletions.
5 changes: 4 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,22 @@ plugins {
}

group 'com.stringcare'
version '3.6.3'
version '4.0.0'

def siteUrl = 'https://github.com/StringCare/KotlinGradlePlugin'
def gitUrl = 'https://github.com/StringCare/KotlinGradlePlugin.git'

sourceCompatibility = 1.8

repositories {
google()
jcenter()
mavenCentral()
}

dependencies {
implementation gradleApi()
implementation group: 'com.android.tools.build', name: 'gradle', version: '3.5.3'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
implementation group: 'com.google.guava', name: 'guava', version: '28.0-jre'
testCompile group: 'junit', name: 'junit', version: '4.12'
Expand Down
261 changes: 93 additions & 168 deletions src/main/kotlin/StringCare.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@

import components.*
import org.gradle.api.NamedDomainObjectContainer
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.internal.plugins.DslObject

open class StringCare : Plugin<Project> {

Expand All @@ -23,16 +22,11 @@ open class StringCare : Plugin<Project> {
}

@JvmStatic
internal val moduleMap: MutableMap<String, Configuration> = mutableMapOf()

@JvmStatic
internal val variantMap: MutableMap<String, VariantApplicationId> = mutableMapOf()
internal var configuration: Configuration = defaultConfig()

@JvmStatic
internal var mainModule: String = defaultMainModule
internal var variantMap = mutableMapOf<String, String>()

@JvmStatic
internal var debug: Boolean = defaultDebug
}

private lateinit var project: Project
Expand All @@ -45,194 +39,125 @@ open class StringCare : Plugin<Project> {
absoluteProjectPath = project.absolutePath()

this.project.afterEvaluate {
extension.modules.forEach { module ->
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)
}
this.project.applicationVariants()?.forEach { variant ->
variantMap[variant.name] = variant.applicationId
}
extension.variants.forEach { variant ->
variantMap[variant.name] = VariantApplicationId(variant.name).apply {
applicationId = variant.applicationId
mockedFingerprint = variant.mockedFingerprint
skip = variant.skip
}
configuration = this.project.config(extension)

if (configuration.debug) {
PrintUtils.print("PATH", absoluteProjectPath)
}
this.project.registerTask()
this.project.registerTask(configuration)
}
this.project.gradle.addBuildListener(ExecutionListener(
debug = extension.debug,
dataFound = { _, _ ->
// nothing to do here
},
mergeResourcesStart = { module, variant ->
fingerPrint(variantMap, module, variant, extension.debug) { key ->
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
}
this.project.gradle.addBuildListener(
ExecutionListener(
debug = configuration.debug,
dataFound = { _, _ ->
// nothing to do here
},
mergeResourcesStart = { module, variant ->
configuration.name = module
if (variantMap.containsKey(variant)) {
configuration.applicationId = variantMap[variant] ?: ""
}
PrintUtils.print("", "ApplicationId: ${configuration.applicationId}", tab = true)
fingerPrint(module, variant, configuration) { key ->
if (configuration.skip) {
PrintUtils.print(module, "Skipping $variant")
return@fingerPrint
}

if ("none" == key || key.trim().isEmpty()) {
PrintUtils.print("No SHA1 key found for :$module:$variant")
return@fingerPrint
}
if ("none" == key || key.trim().isEmpty()) {
PrintUtils.print("No SHA1 key found for :$module:$variant")
return@fingerPrint
}

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

moduleMap[module]?.let { configuration ->
val files = locateResourceFiles(absoluteProjectPath, configuration)
files.forEach { file ->
modifyXML(
file.file, extension.main_module, key, extension.debug,
variantOrFlavor?.applicationId ?: ""
)
}
}
PrintUtils.print(module, obfuscateStringRes)
}
else -> {
val defaultConfiguration = defaultConfig().apply {
name = module
}
if ("none" == key || key.trim().isEmpty()) {
PrintUtils.print("No SHA1 key found for :$module:$variant")
return@fingerPrint
}
PrintUtils.print(module, "$variant:$key")
PrintUtils.print(module, backupStringRes)
backupResourceFiles(absoluteProjectPath, defaultConfiguration)
PrintUtils.print(module, obfuscateStringRes)
val files = locateResourceFiles(absoluteProjectPath, defaultConfiguration)
files.forEach { file ->
modifyXML(file.file, extension.main_module, key, extension.debug)
}

val files = locateResourceFiles(absoluteProjectPath, configuration)
files.forEach { file ->
modifyXML(file.file, key, configuration)
}
}
}
PrintUtils.print(module, obfuscateStringRes)
},
mergeResourcesFinish = { module, _ ->
if (configuration.skip) {
return@ExecutionListener
}
PrintUtils.print(module, restoreStringRes)
restoreResourceFiles(absoluteProjectPath, module)
},
mergeAssetsStart = { module, variant ->
configuration.name = module
if (variantMap.containsKey(variant)) {
configuration.applicationId = variantMap[variant] ?: ""
}
PrintUtils.print("", "ApplicationId: ${configuration.applicationId}", tab = true)
fingerPrint(module, variant, configuration) { key ->
if (configuration.skip) {
PrintUtils.print(module, "Skipping $variant")
return@fingerPrint
}

},
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(variantMap, module, variant, extension.debug) { key ->
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
}
if ("none" == key || key.trim().isEmpty()) {
PrintUtils.print("No SHA1 key found for :$module:$variant")
return@fingerPrint
}

if ("none" == key || key.trim().isEmpty()) {
PrintUtils.print("No SHA1 key found for :$module:$variant")
return@fingerPrint
}
PrintUtils.print(module, "$variant:$key")
PrintUtils.print(module, backupAssets)
backupAssetsFiles(absoluteProjectPath, configuration)

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())
}
}
val files = locateAssetsFiles(absoluteProjectPath, configuration)
files.forEach { file ->
if (configuration.debug) {
PrintUtils.print(null, file.file.getContent())
}
obfuscateFile(
key,
file.file,
configuration.applicationId
)
if (configuration.debug) {
PrintUtils.print(null, file.file.getContent())
}
PrintUtils.print(module, obfuscateAssets)
}
PrintUtils.print(module, obfuscateAssets)
}
},
mergeAssetsFinish = { module, _ ->
if (configuration.skip) {
return@ExecutionListener
}
PrintUtils.print(module, restoreAssets)
restoreAssetsFiles(absoluteProjectPath, module)
}

},
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
}
restoreAssetsFiles(absoluteProjectPath, module)
}

))
))
}

open class Extension {
var debug: Boolean = false
var main_module: String = "app"
var modules: NamedDomainObjectContainer<Configuration>
@Suppress("UNCHECKED_CAST")
get() = DslObject(this).extensions.getByName("modules") as NamedDomainObjectContainer<Configuration>
internal set(value) {
DslObject(this).extensions.add("modules", value)
}
var variants: NamedDomainObjectContainer<VariantApplicationId>
@Suppress("UNCHECKED_CAST")
get() = DslObject(this).extensions.getByName("variants") as NamedDomainObjectContainer<VariantApplicationId>
internal set(value) {
DslObject(this).extensions.add("variants", value)
}
var assetsFiles = mutableListOf<String>()
var stringFiles = mutableListOf<String>()
var srcFolders = mutableListOf<String>()
var debug = false
var skip = false
var mockedFingerprint = ""
}

open class Configuration(var name: String) {
var assetsFiles = mutableListOf<String>()
var stringFiles = mutableListOf<String>()
var srcFolders = mutableListOf<String>()
var debug = false
}

open class VariantApplicationId(var name: String) {
var skip = false
var applicationId = ""
var mockedFingerprint = ""
var skip = false
}

}
Expand Down
11 changes: 5 additions & 6 deletions src/main/kotlin/components/AParser.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package components

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

fun locateAssetsFiles(projectPath: String, configuration: Configuration): List<AssetsFile> {
Expand Down Expand Up @@ -36,16 +36,15 @@ fun restoreAssetsFiles(projectPath: String, module: String): List<File> {
return resourceFiles
}

fun obfuscateFile(mainModule: String, key: String, file: File, mockId: String = "") {
fun obfuscateFile(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))
fun revealFile(key: String, file: File, mockId: String = "") {
file.writeBytes(Stark.reveal(key, file.readBytes(), mockId))
}
1 change: 1 addition & 0 deletions src/main/kotlin/components/Execution.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package components

import StringCare
import com.google.common.io.Files
import models.ExecutionResult
import java.io.IOException
Expand Down
Loading

0 comments on commit e817879

Please sign in to comment.