Skip to content

Commit

Permalink
Merge pull request #3 from StringCare/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
efraespada authored Jan 23, 2018
2 parents ba1b272 + ee1f39d commit a4d3567
Show file tree
Hide file tree
Showing 7 changed files with 139 additions and 66 deletions.
26 changes: 22 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<p align="center"><img width="10%" vspace="20" src="https://raw.githubusercontent.com/StringCare/AndroidLibrary/develop/sample/src/main/res/mipmap-xxxhdpi/ic_launcher.png"></p>



# String Care Android Plugin
Source code of plugin used in Android Studio at compilation time for strings encryption.

Gradle implementation
------------
Expand All @@ -14,7 +14,7 @@ root_project/build.gradle
buildscript {
ext {
stringcare_version = '0.3'
stringcare_version = '0.4'
}
repositories {
Expand All @@ -31,7 +31,7 @@ apply plugin: StringCare
stringcare {
debug true // prints detail build variant info
debug true // prints details
modules {
Expand All @@ -58,4 +58,22 @@ stringcare {
}
}
```
```


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.

2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ apply plugin: 'java'
apply plugin: 'com.jfrog.bintray'

group 'com.stringcare'
version '0.3'
version '0.4'

sourceCompatibility = 1.8

Expand Down
3 changes: 1 addition & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -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
15 changes: 12 additions & 3 deletions src/main/groovy/CredentialUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
Expand Down
61 changes: 43 additions & 18 deletions src/main/groovy/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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();
Expand All @@ -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) {
Expand All @@ -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());
Expand All @@ -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();
}
Expand All @@ -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);
}
}
}

Expand All @@ -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\"";

Expand Down Expand Up @@ -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();
}

Expand Down
80 changes: 42 additions & 38 deletions src/main/groovy/StringCarePlugin.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class StringCare implements Plugin<Project> {

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<String, Config> moduleMap = new HashMap<>();

Expand Down Expand Up @@ -67,48 +67,52 @@ class StringCare implements Plugin<Project> {

@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<String> stg = new ArrayList<>();
stg.add("strings.xml")
List<String> 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<String> stg = new ArrayList<>();
stg.add("strings.xml")
List<String> 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<String> stg = new ArrayList<>();
stg.add("strings.xml")
List<String> 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<String> stg = new ArrayList<>();
stg.add("strings.xml")
List<String> src = new ArrayList<>();
src.add("src/main")
config.setStringFiles(stg)
config.setSrcFolders(src)

PrintUtils.print(module, "restoreStringResources")
FileUtils.restoreStringResources(module, config, debug)
}
}
}
}))
Expand Down
18 changes: 18 additions & 0 deletions src/test/groovy/LibraryTest.groovy
Original file line number Diff line number Diff line change
@@ -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
}*/
}

0 comments on commit a4d3567

Please sign in to comment.