From 17c2795bc8bb10a0abbf91611f147edec11673c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Efrai=CC=81n=20Espada?= Date: Fri, 15 Dec 2017 19:30:43 +0100 Subject: [PATCH] v0.1 --- README.md | 48 +++++- build.gradle | 6 +- .../com/stringcare}/AES.java | 2 +- src/main/groovy/com/stringcare/Config.java | 30 ++++ .../com/stringcare}/CredentialUtils.java | 4 +- .../com/stringcare}/FileUtils.java | 141 ++++++++++-------- .../stringcare}/GradleHandlerCallback.java | 2 +- .../com/stringcare}/PrintUtils.java | 12 +- .../groovy/com/stringcare/StringCare.groovy | 96 ++++++++---- .../com/stringcare/TimingRecorder.groovy | 4 +- 10 files changed, 236 insertions(+), 109 deletions(-) rename src/main/{java/com/efraespada/stringobfuscatorplugin => groovy/com/stringcare}/AES.java (95%) create mode 100644 src/main/groovy/com/stringcare/Config.java rename src/main/{java/com/efraespada/stringobfuscatorplugin => groovy/com/stringcare}/CredentialUtils.java (96%) rename src/main/{java/com/efraespada/stringobfuscatorplugin => groovy/com/stringcare}/FileUtils.java (57%) rename src/main/{java/com/efraespada/stringobfuscatorplugin/interfaces => groovy/com/stringcare}/GradleHandlerCallback.java (79%) rename src/main/{java/com/efraespada/stringobfuscatorplugin => groovy/com/stringcare}/PrintUtils.java (70%) diff --git a/README.md b/README.md index d398a1b..d0bc79d 100644 --- a/README.md +++ b/README.md @@ -1,23 +1,55 @@ -repo under construction, sorry - -# AndroidObfuscatorPlugin +# String Care Android Plugin Gradle implementation ------------ root_project/build.gradle ```groovy -apply plugin: com.efraespada.stringobfuscatorplugin.StringObfuscatorPlugin +// root_project/build.gradle + +apply plugin: com.stringcare.SCPlugin buildscript { + + ext { + stringcare_version = '0.1' + } + repositories { - mavenLocal() + jcenter() } + dependencies { - // ... - classpath files('../AndroidLibrary/build/libs/stringobfuscatorplugin-1.0-SNAPSHOT.jar') - // ... + classpath "com.stringcare:plugin:$stringcare_version" } + } +stringcare { + + modules { + + sample { + stringFiles = ['strings.xml',"other_file.xml"] + srcFolders = ['src/main', "other_folder"] + } + + // root_folder/sample/src/main/res/.../strings.xml + // root_folder/sample/src/main/res/.../other_file.xml + // root_folder/sample/other_folder/res/.../strings.xml + // root_folder/sample/other_folder/res/.../other_file.xml + + other_module { + srcFolders = ['src/moduleB'] + } + + // root_folder/other_module/src/moduleB/res/.../strings.xml + + other_module_ {} // + + // root_folder/other_module_/src/main/res/.../strings.xml + + } + +} ``` \ No newline at end of file diff --git a/build.gradle b/build.gradle index b9ca3e2..d06f824 100644 --- a/build.gradle +++ b/build.gradle @@ -20,7 +20,7 @@ apply plugin: 'idea' apply plugin: 'java' apply plugin: 'com.jfrog.bintray' -group 'com.efraespada' +group 'com.stringcare' version '0.1' @@ -41,7 +41,7 @@ install { pom { project { packaging 'aar' - name 'SAndroid' + name 'StringCareAndroidPlugin' url siteUrl // Set your license licenses { @@ -75,7 +75,7 @@ bintray { configurations = ['archives'] pkg { repo = "maven" - name = "stringcare:plugin" + name = "StringCareAndroidPlugin" websiteUrl = siteUrl vcsUrl = gitUrl licenses = ["Apache-2.0"] diff --git a/src/main/java/com/efraespada/stringobfuscatorplugin/AES.java b/src/main/groovy/com/stringcare/AES.java similarity index 95% rename from src/main/java/com/efraespada/stringobfuscatorplugin/AES.java rename to src/main/groovy/com/stringcare/AES.java index ab86de9..6328991 100755 --- a/src/main/java/com/efraespada/stringobfuscatorplugin/AES.java +++ b/src/main/groovy/com/stringcare/AES.java @@ -1,4 +1,4 @@ -package com.efraespada.stringobfuscatorplugin; +package com.stringcare; import javax.crypto.Cipher; import javax.crypto.IllegalBlockSizeException; diff --git a/src/main/groovy/com/stringcare/Config.java b/src/main/groovy/com/stringcare/Config.java new file mode 100644 index 0000000..92ec638 --- /dev/null +++ b/src/main/groovy/com/stringcare/Config.java @@ -0,0 +1,30 @@ +package com.stringcare; + +import java.util.List; + +public class Config { + + List stringFiles; + List srcFolders; + + Config() { + // nothing to do here + } + + List getStringFiles() { + return stringFiles; + } + + void setStringFiles(List stringFiles) { + this.stringFiles = stringFiles; + } + + List getSrcFolders() { + return srcFolders; + } + + void setSrcFolders(List srcFolders) { + this.srcFolders = srcFolders; + } + +} diff --git a/src/main/java/com/efraespada/stringobfuscatorplugin/CredentialUtils.java b/src/main/groovy/com/stringcare/CredentialUtils.java similarity index 96% rename from src/main/java/com/efraespada/stringobfuscatorplugin/CredentialUtils.java rename to src/main/groovy/com/stringcare/CredentialUtils.java index a6ac25f..e493d2a 100644 --- a/src/main/java/com/efraespada/stringobfuscatorplugin/CredentialUtils.java +++ b/src/main/groovy/com/stringcare/CredentialUtils.java @@ -1,11 +1,11 @@ -package com.efraespada.stringobfuscatorplugin; +package com.stringcare; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; -import static com.efraespada.stringobfuscatorplugin.PrintUtils.print; +import static com.stringcare.PrintUtils.print; public class CredentialUtils { diff --git a/src/main/java/com/efraespada/stringobfuscatorplugin/FileUtils.java b/src/main/groovy/com/stringcare/FileUtils.java similarity index 57% rename from src/main/java/com/efraespada/stringobfuscatorplugin/FileUtils.java rename to src/main/groovy/com/stringcare/FileUtils.java index 576bc4c..7a6b017 100644 --- a/src/main/java/com/efraespada/stringobfuscatorplugin/FileUtils.java +++ b/src/main/groovy/com/stringcare/FileUtils.java @@ -1,33 +1,25 @@ -package com.efraespada.stringobfuscatorplugin; +package com.stringcare; import java.io.*; -import java.nio.channels.FileChannel; -import java.nio.file.Files; -import java.util.HashMap; -import java.util.Map; -import static com.efraespada.stringobfuscatorplugin.PrintUtils.print; +import static com.stringcare.PrintUtils.print; public class FileUtils { - private static String variant; private static String module; private static String key; + private static Config config; final static int maxToShow = 15; - - private static final Map files = new HashMap<>(); - private FileUtils() { // nothing to do here } - public static void init(String key, String module, String variant) { - files.clear(); + public static void init(String key, String module, String variant, Config config) { FileUtils.key = key; FileUtils.module = module; - FileUtils.variant = variant; + FileUtils.config = config; } public static String getTextFromFilePath(String path) { @@ -37,8 +29,6 @@ public static String getTextFromFilePath(String path) { String xml = ""; String inputFilePath = path; - if (true) print("reading " + inputFilePath); - String message = ""; File file = new File(inputFilePath); @@ -83,41 +73,58 @@ public static String getString(BufferedReader br) { // detect multiple sourceSet res.srcDirs public static void backupStringResources() { String currentPath = getCurrentPath(); - currentPath += module + File.separator + "src" + File.separator + "main" + File.separator + "res" + File.separator; - File file = new File(currentPath); - String[] directories = file.list((current, name) -> new File(current, name).isDirectory()); - for (String dir : directories) { - String pathToCopy = currentPath + dir + File.separator; - String pathToCheck = getCurrentPath() + module + File.separator + "resbackup" + File.separator + dir + File.separator; - - try { - File toCopy = new File(pathToCopy + "strings.xml"); - File toCheck = new File(pathToCheck + "strings.xml"); - if (toCheck.exists()) { - toCheck.delete(); - } - if (toCopy.exists()) { - copyFile(toCopy, toCheck); + for (String folder : config.getSrcFolders()) { + currentPath += module + File.separator + folder + File.separator + "res" + File.separator; + File file = new File(currentPath); + String[] directories = file.list((current, name) -> new File(current, name).isDirectory()); + if (directories != null) { + for (String dir : directories) { + String pathToCopy = currentPath + dir + File.separator; + String pathToCheck = getCurrentPath() + module + File.separator + "resbackup" + File.separator + dir + File.separator; + + for (String sFile : config.getStringFiles()) { + try { + File toCopy = new File(pathToCopy + sFile); + File toCheck = new File(pathToCheck + sFile); + if (toCheck.exists()) { + toCheck.delete(); + } + if (toCopy.exists()) { + print("- " + toCopy.getParentFile().getName() + File.separator + toCopy.getName(), true); + copyFile(toCopy, toCheck); + } + } catch (IOException e) { + e.printStackTrace(); + // TODO HANDLE ERROR + } + } } - } catch (IOException e) { - e.printStackTrace(); - // TODO HANDLE ERROR + } else { + PrintUtils.print("source folder not found: " + folder, true); } } } public static void encryptStringResources() { String currentPath = getCurrentPath(); - currentPath += module + File.separator + "src" + File.separator + "main" + File.separator + "res" + File.separator; - File file = new File(currentPath); - String[] directories = file.list((current, name) -> new File(current, name).isDirectory()); - for (String dir : directories) { - String pathToEncrypt = currentPath + dir + File.separator; - - File toEncrypt = new File(pathToEncrypt + "strings.xml"); - if (toEncrypt.exists()) { - String encrypted = find(getTextFromFilePath(toEncrypt.getAbsolutePath())); - writeFile(toEncrypt, encrypted); + for (String folder : config.getSrcFolders()) { + currentPath += module + File.separator + folder + File.separator + "res" + File.separator; + File file = new File(currentPath); + String[] directories = file.list((current, name) -> new File(current, name).isDirectory()); + if (directories != null) { + for (String dir : directories) { + String pathToEncrypt = currentPath + dir + File.separator; + for (String sFile : config.getStringFiles()) { + File toEncrypt = new File(pathToEncrypt + sFile); + if (toEncrypt.exists()) { + PrintUtils.print("- " + toEncrypt.getParentFile().getName() + File.separator + toEncrypt.getName(), true); + String encrypted = find(getTextFromFilePath(toEncrypt.getAbsolutePath())); + writeFile(toEncrypt, encrypted); + } + } + } + } else { + PrintUtils.print("source folder not found: " + folder, true); } } } @@ -126,23 +133,35 @@ public static void restoreStringResources() { String currentPath = getCurrentPath() + module + File.separator + "resbackup" + File.separator; File file = new File(currentPath); String[] directories = file.list((current, name) -> new File(current, name).isDirectory()); - for (String dir : directories) { - String pathToEncrypt = currentPath + dir + File.separator; - String pathRes = getCurrentPath() + module + File.separator + "src" + File.separator + "main" + File.separator + "res" + File.separator + dir + File.separator; - - File toRestore = new File(pathToEncrypt + "strings.xml"); - File toCheck = new File(pathRes + "strings.xml"); - - try { - copyFile(toRestore, toCheck); - } catch (IOException e) { - e.printStackTrace(); + if (directories != null) { + File toRestore; + for (String dir : directories) { + String pathToRestore = currentPath + dir + File.separator; + for (String folder : config.getSrcFolders()) { + String pathRes = getCurrentPath() + module + File.separator + folder + File.separator + "res" + File.separator + dir + File.separator; + + for (String sFile : config.getStringFiles()) { + toRestore = new File(pathToRestore + sFile); + File toCheck = new File(pathRes + sFile); + if (toRestore.exists()) { + try { + PrintUtils.print("- " + toCheck.getParentFile().getName() + File.separator + toCheck.getName(), true); + copyFile(toRestore, toCheck); + } catch (IOException e) { + e.printStackTrace(); + } + + toRestore.delete(); + toRestore.getParentFile().delete(); + } + } + } } - - toRestore.delete(); - } - if (file.isDirectory()) { - file.delete(); + if (file.isDirectory()) { + file.delete(); + } + } else { + PrintUtils.print("restore folder not found"); } } @@ -192,7 +211,7 @@ public static String find(String xmlO) { toShow = toShow.length() > maxToShow ? toShow.substring(0, maxToShow) + ".." : toShow; encrypted = encrypted.length() > maxToShow ? encrypted.substring(0, maxToShow) + ".." : encrypted; - print("[" + toShow + "] - [" + encrypted + "]" + (hasExtra ? extra : "")); + print("\t[" + toShow + "] - [" + encrypted + "]" + (hasExtra ? extra : ""), true); } catch (Exception e) { print("error on " + result); e.printStackTrace(); @@ -204,8 +223,6 @@ public static String find(String xmlO) { if (xml1.indexOf(toFind1) <= 0) break; } - print(content); - return content; } diff --git a/src/main/java/com/efraespada/stringobfuscatorplugin/interfaces/GradleHandlerCallback.java b/src/main/groovy/com/stringcare/GradleHandlerCallback.java similarity index 79% rename from src/main/java/com/efraespada/stringobfuscatorplugin/interfaces/GradleHandlerCallback.java rename to src/main/groovy/com/stringcare/GradleHandlerCallback.java index d1220f0..b791722 100644 --- a/src/main/java/com/efraespada/stringobfuscatorplugin/interfaces/GradleHandlerCallback.java +++ b/src/main/groovy/com/stringcare/GradleHandlerCallback.java @@ -1,4 +1,4 @@ -package com.efraespada.stringobfuscatorplugin.interfaces; +package com.stringcare; public interface GradleHandlerCallback { diff --git a/src/main/java/com/efraespada/stringobfuscatorplugin/PrintUtils.java b/src/main/groovy/com/stringcare/PrintUtils.java similarity index 70% rename from src/main/java/com/efraespada/stringobfuscatorplugin/PrintUtils.java rename to src/main/groovy/com/stringcare/PrintUtils.java index eec5bd7..a674d5b 100644 --- a/src/main/java/com/efraespada/stringobfuscatorplugin/PrintUtils.java +++ b/src/main/groovy/com/stringcare/PrintUtils.java @@ -1,8 +1,7 @@ -package com.efraespada.stringobfuscatorplugin; +package com.stringcare; public class PrintUtils { - private final static String TAG = "stringobfuscatorplugin"; private static String variant; private static String module; @@ -20,8 +19,15 @@ public static void init(String module, String variant) { * @param message */ public static void print(String message) { + print(message, false); + } + public static void print(String message, boolean tab) { if (variant != null && module != null) { - System.out.println(":" + module + ":" + TAG + " - " + message); + if (!tab) { + System.out.println(":" + module + ":" + message); + } else { + System.out.println("\t" + message); + } } else { System.out.println(message); } diff --git a/src/main/groovy/com/stringcare/StringCare.groovy b/src/main/groovy/com/stringcare/StringCare.groovy index aa0b772..d1be1d5 100644 --- a/src/main/groovy/com/stringcare/StringCare.groovy +++ b/src/main/groovy/com/stringcare/StringCare.groovy @@ -1,69 +1,121 @@ package com.stringcare -import com.efraespada.stringobfuscatorplugin.CredentialUtils -import com.efraespada.stringobfuscatorplugin.FileUtils -import com.efraespada.stringobfuscatorplugin.PrintUtils -import com.efraespada.stringobfuscatorplugin.interfaces.GradleHandlerCallback -import org.gradle.api.NamedDomainObjectCollection import org.gradle.api.Plugin import org.gradle.api.Project import org.gradle.api.logging.Logger class SCPlugin implements Plugin { + private static final float VERSION = 0.3; private Project project; private static String key = null; + private static Map moduleMap = new HashMap<>(); Logger logger - NamedDomainObjectCollection reporterExtensions - @Override void apply(Project project) { this.project = project; - this.project.task('hello') { + createExtensions() + + this.project.task('stop') { doLast { - println 'Hello from the GreetingPlugin' + String cmd = ""; + if (System.getProperty("os.name").toLowerCase().contains("windows")) { + cmd = "gradlew.bat"; + } else { + cmd = "gradlew"; + Runtime.getRuntime().exec("chmod +x ./" + cmd); + } + + Runtime.getRuntime().exec("./" + cmd + " --stop"); + Runtime.getRuntime().exec("./" + cmd + " clean"); } } this.logger = this.project.logger - this.project.extensions.create("stringobfuscator", StringCareExtension) - reporterExtensions = this.project.extensions.reporters = project.container(ReporterExtension) + this.project.afterEvaluate { + this.project.stringcare.modules.all { mod -> + Config config = new Config() + if (mod.stringFiles != null && mod.srcFolders != null) { + config.setStringFiles(mod.stringFiles) + config.setSrcFolders(mod.srcFolders) + moduleMap.put(mod.name, config) + } else if (mod.srcFolders != null) { + List stg = new ArrayList<>(); + stg.add("strings.xml") + config.setStringFiles(stg) + config.setSrcFolders(mod.srcFolders) + moduleMap.put(mod.name, config) + } else if (mod.stringFiles != null) { + List src = new ArrayList<>(); + src.add("src/main") + config.setStringFiles(mod.stringFiles) + config.setSrcFolders(src) + moduleMap.put(mod.name, config) + } + } + } this.project.gradle.addBuildListener(new TimingRecorder(this, new GradleHandlerCallback() { @Override void onDataFound(String module, String variant) { PrintUtils.init(module, variant) CredentialUtils.init(module, variant, true) key = CredentialUtils.getKey() - FileUtils.init(key, module, variant) + if (moduleMap.containsKey(module)) { + FileUtils.init(key, module, variant, moduleMap.get(module)) + } else { + Config config = new Config(); + List stg = new ArrayList<>(); + stg.add("strings.xml") + List src = new ArrayList<>(); + src.add("src/main") + config.setStringFiles(stg) + config.setSrcFolders(src) + FileUtils.init(key, module, variant, config) + } } @Override void onMergeResourcesStarts(String module, String variant) { - println ":" + module + ":mergeResources:" + variant + ":" + key + PrintUtils.print(variant + ":" + key) + PrintUtils.print("backupStringResources") FileUtils.backupStringResources() + PrintUtils.print("encryptStringResources") FileUtils.encryptStringResources() } @Override void onMergeResourcesFinish(String module, String variant) { + PrintUtils.print("restoreStringResources") FileUtils.restoreStringResources() } })) } + + private void createExtensions() { + project.extensions.create('stringcare', Extension ) + project.stringcare.extensions.modules = project.container(Conf) + } } -class StringCareExtension { - // Not in use at the moment. +class Extension { + + Extension() { + + } + } -class ReporterExtension { +class Conf { + final String name - final Map options = [:] + String lala + List stringFiles + List srcFolders - ReporterExtension(String name) { + Conf(String name) { this.name = name } @@ -72,12 +124,4 @@ class ReporterExtension { return name } - def methodMissing(String name, args) { - // I'm feeling really, really naughty. - if (args.length == 1) { - options[name] = args[0].toString() - } else { - throw new MissingMethodException(name, this.class, args) - } - } } diff --git a/src/main/groovy/com/stringcare/TimingRecorder.groovy b/src/main/groovy/com/stringcare/TimingRecorder.groovy index 7296b85..47fc72a 100644 --- a/src/main/groovy/com/stringcare/TimingRecorder.groovy +++ b/src/main/groovy/com/stringcare/TimingRecorder.groovy @@ -1,7 +1,5 @@ package com.stringcare -import com.efraespada.stringobfuscatorplugin.PrintUtils -import com.efraespada.stringobfuscatorplugin.interfaces.GradleHandlerCallback import com.stringcare.util.Clock import org.gradle.BuildResult import org.gradle.api.Task @@ -67,7 +65,7 @@ class TimingRecorder extends BuildAndTaskExecutionListenerAdapter implements Tas @Override void buildFinished(BuildResult result) { - plugin.reporterExtensions.each { it.run timings; it.onBuildResult result } + // plugin.reporterExtensions.each { it.run timings; it.onBuildResult result } } List getTasks() {