diff --git a/README.md b/README.md index 0a13bba..be5964a 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,8 @@

- # String Care Android Plugin +Source code of plugin used in Android Studio at compilation time for strings encryption. Gradle implementation ------------ @@ -14,7 +14,7 @@ root_project/build.gradle buildscript { ext { - stringcare_version = '0.3' + stringcare_version = '0.4' } repositories { @@ -31,7 +31,7 @@ apply plugin: StringCare stringcare { - debug true // prints detail build variant info + debug true // prints details modules { @@ -58,4 +58,22 @@ stringcare { } } -``` \ No newline at end of file +``` + + +License +------- + Copyright 2017 EfraĆ­n Espada + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + diff --git a/build.gradle b/build.gradle index de54807..771bda3 100644 --- a/build.gradle +++ b/build.gradle @@ -24,7 +24,7 @@ apply plugin: 'java' apply plugin: 'com.jfrog.bintray' group 'com.stringcare' -version '0.3' +version '0.4' sourceCompatibility = 1.8 diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 53239d4..2c2bbe5 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,5 @@ -#Sun Jan 21 18:39:53 CET 2018 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-4.4.1-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-4.4.1-bin.zip diff --git a/src/main/groovy/CredentialUtils.java b/src/main/groovy/CredentialUtils.java index aa72140..f39dc5a 100644 --- a/src/main/groovy/CredentialUtils.java +++ b/src/main/groovy/CredentialUtils.java @@ -66,6 +66,18 @@ private static void parseTrace(String module, String variant, String line, boole if (debug) { PrintUtils.print(module, line, debug); } + } else if (line.toLowerCase().contains("config:") && moduleLocated && variantLocated) { + boolean valid = !line.split(": ")[1].trim().equalsIgnoreCase("none"); + if (!valid) { + key = line.split(": ")[1].trim(); + PrintUtils.print(module, "\uD83E\uDD2F no config defined for variant " + variant, true); + if (debug) { + until = key; + } + } else if (debug){ + PrintUtils.print(module, "Variant:" + variant, true); + } + } else if (line.toLowerCase().contains("sha") && moduleLocated && variantLocated) { key = line.split(" ")[1]; if (debug) { @@ -86,9 +98,6 @@ private static void parseTrace(String module, String variant, String line, boole String locV = line.split(" ")[1]; if (locV.equals(variant)) { variantLocated = true; - if (debug) { - PrintUtils.print(module, line, debug); - } } } else if (line.toLowerCase().contains(":" + module)) { moduleLocated = true; diff --git a/src/main/groovy/FileUtils.java b/src/main/groovy/FileUtils.java index 83287bf..abfae68 100644 --- a/src/main/groovy/FileUtils.java +++ b/src/main/groovy/FileUtils.java @@ -58,10 +58,10 @@ public static String getString(BufferedReader br) { } // detect multiple sourceSet res.srcDirs - public static void backupStringResources(String module, Config config) { - String currentPath = getCurrentPath(module); + public static void backupStringResources(String module, Config config, boolean debug) { + String path = getCurrentPath(module); for (String folder : config.getSrcFolders()) { - currentPath += module + File.separator + folder + File.separator + "res" + File.separator; + String currentPath = path + 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) { @@ -77,8 +77,11 @@ public static void backupStringResources(String module, Config config) { toCheck.delete(); } if (toCopy.exists()) { - PrintUtils.print("- " + toCopy.getParentFile().getName() + File.separator + toCopy.getName(), true); + PrintUtils.print(module, "- " + toCopy.getParentFile().getName() + File.separator + toCopy.getName(), true); copyFile(toCopy, toCheck); + if (debug) { + PrintUtils.print(module, "backuping file: " + toCopy.getPath(), true); + } } } catch (IOException e) { e.printStackTrace(); @@ -87,15 +90,18 @@ public static void backupStringResources(String module, Config config) { } } } else { - PrintUtils.print(module, "source folder not found: " + folder, true); - } + if (debug) { + PrintUtils.print(module, "source folder not found: " + currentPath, true); + } else { + PrintUtils.print(module, "source folder not found: " + folder, true); + } } } } - public static void encryptStringResources(String module, Config config, String key) { - String currentPath = getCurrentPath(module); + public static void encryptStringResources(String module, Config config, String key, boolean debug) { + String path = getCurrentPath(module); for (String folder : config.getSrcFolders()) { - currentPath += module + File.separator + folder + File.separator + "res" + File.separator; + String currentPath = path + 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) { @@ -104,19 +110,28 @@ public static void encryptStringResources(String module, Config config, String k 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()), key); + PrintUtils.print(module, "- " + toEncrypt.getParentFile().getName() + File.separator + toEncrypt.getName(), true); + String encrypted = find(module, getTextFromFilePath(toEncrypt.getAbsolutePath()), key, debug); writeFile(toEncrypt, encrypted); + if (debug) { + PrintUtils.print(module, "writing file: " + toEncrypt.getPath(), true); + } + } else if (debug) { + PrintUtils.print(module, "source file not exist: " + pathToEncrypt + sFile, true); } } } } else { - PrintUtils.print(module, "source folder not found: " + folder, true); + if (debug) { + PrintUtils.print(module, "source folder not found: " + currentPath, true); + } else { + PrintUtils.print(module, "source folder not found: " + folder, true); + } } } } - public static void restoreStringResources(String module, Config config) { + public static void restoreStringResources(String module, Config config, boolean debug) { String currentPath = getCurrentPath(module) + module + File.separator + "resbackup" + File.separator; File file = new File(currentPath); String[] directories = file.list((current, name) -> new File(current, name).isDirectory()); @@ -132,8 +147,11 @@ public static void restoreStringResources(String module, Config config) { File toCheck = new File(pathRes + sFile); if (toRestore.exists()) { try { - PrintUtils.print("- " + toCheck.getParentFile().getName() + File.separator + toCheck.getName(), true); + PrintUtils.print(module,"- " + toCheck.getParentFile().getName() + File.separator + toCheck.getName(), true); copyFile(toRestore, toCheck); + if (debug) { + PrintUtils.print(module, "restoring: " + toRestore.getPath(), true); + } } catch (IOException e) { e.printStackTrace(); } @@ -146,9 +164,16 @@ public static void restoreStringResources(String module, Config config) { } if (file.isDirectory()) { file.delete(); + if (debug) { + PrintUtils.print(module, "temp source folder removed: " + file.getPath(), true); + } } } else { - PrintUtils.print(module, "restore folder not found", true); + if (debug) { + PrintUtils.print(module, "restore folder not found: " + currentPath, true); + } else { + PrintUtils.print(module, "restore folder not found", true); + } } } @@ -168,7 +193,7 @@ public static boolean isEncrypted(String value, String key) { return encrypted; } - public static String find(String xmlO, String key) { + public static String find(String module, String xmlO, String key, boolean debug) { String content = xmlO; String toFind1 = "hidden=\"true\""; @@ -198,9 +223,9 @@ public static String find(String xmlO, String key) { toShow = toShow.length() > maxToShow ? toShow.substring(0, maxToShow) + ".." : toShow; encrypted = encrypted.length() > maxToShow ? encrypted.substring(0, maxToShow) + ".." : encrypted; - PrintUtils.print("\t[" + toShow + "] - [" + encrypted + "]" + (hasExtra ? extra : ""), true); + PrintUtils.print(module, "\t[" + toShow + "] - [" + encrypted + "]" + (hasExtra ? extra : ""), true); } catch (Exception e) { - PrintUtils.print("error on " + result); + PrintUtils.print(module, "error on " + result, true); e.printStackTrace(); } diff --git a/src/main/groovy/StringCarePlugin.groovy b/src/main/groovy/StringCarePlugin.groovy index 10d2c43..9036ea3 100644 --- a/src/main/groovy/StringCarePlugin.groovy +++ b/src/main/groovy/StringCarePlugin.groovy @@ -7,7 +7,7 @@ class StringCare implements Plugin { private static final float VERSION = 0.3; private Project project; - private static String key = null; + private String key; private static boolean debug; private static Map moduleMap = new HashMap<>(); @@ -67,48 +67,52 @@ class StringCare implements Plugin { @Override void onMergeResourcesStarts(String module, String variant) { - String key = CredentialUtils.getKey(module, variant, debug); - - if (moduleMap.containsKey(module)) { - PrintUtils.print(module, variant + ":" + key) - PrintUtils.print(module, "backupStringResources") - FileUtils.backupStringResources(module, moduleMap.get(module)) - PrintUtils.print(module, "encryptStringResources") - - FileUtils.encryptStringResources(module, moduleMap.get(module), key) - } 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) - - PrintUtils.print(module, variant + ":" + key) - PrintUtils.print(module, "backupStringResources") - FileUtils.backupStringResources(module, config) - PrintUtils.print(module, "encryptStringResources") - FileUtils.encryptStringResources(module, config, key) + key = CredentialUtils.getKey(module, variant, debug); + if (!"none".equals(key)) { + if (moduleMap.containsKey(module)) { + PrintUtils.print(module, variant + ":" + key) + PrintUtils.print(module, "backupStringResources") + FileUtils.backupStringResources(module, moduleMap.get(module), debug) + PrintUtils.print(module, "encryptStringResources") + + FileUtils.encryptStringResources(module, moduleMap.get(module), key, debug) + } 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) + + PrintUtils.print(module, variant + ":" + key) + PrintUtils.print(module, "backupStringResources") + FileUtils.backupStringResources(module, config, debug) + PrintUtils.print(module, "encryptStringResources") + FileUtils.encryptStringResources(module, config, key, debug) + } } + } @Override void onMergeResourcesFinish(String module, String variant) { - if (moduleMap.containsKey(module)) { - PrintUtils.print(module, "restoreStringResources") - FileUtils.restoreStringResources(module, 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) - - PrintUtils.print(module, "restoreStringResources") - FileUtils.restoreStringResources(module, config) + if (!"none".equals(key)) { + if (moduleMap.containsKey(module)) { + PrintUtils.print(module, "restoreStringResources") + FileUtils.restoreStringResources(module, moduleMap.get(module), debug) + } 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) + + PrintUtils.print(module, "restoreStringResources") + FileUtils.restoreStringResources(module, config, debug) + } } } })) diff --git a/src/test/groovy/LibraryTest.groovy b/src/test/groovy/LibraryTest.groovy new file mode 100644 index 0000000..e61c659 --- /dev/null +++ b/src/test/groovy/LibraryTest.groovy @@ -0,0 +1,18 @@ +/* + * This Spock specification was generated by the Gradle 'init' task. + */ +import spock.lang.Specification + +class LibraryTest extends Specification { + /* + def "someLibraryMethod returns true"() { + setup: + def lib = new Library() + + when: + def result = lib.someLibraryMethod() + + then: + result == true + }*/ +}