Skip to content

Commit a4d3567

Browse files
authored
Merge pull request #3 from StringCare/develop
Develop
2 parents ba1b272 + ee1f39d commit a4d3567

File tree

7 files changed

+139
-66
lines changed

7 files changed

+139
-66
lines changed

README.md

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<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>
22

33

4-
54
# String Care Android Plugin
5+
Source code of plugin used in Android Studio at compilation time for strings encryption.
66

77
Gradle implementation
88
------------
@@ -14,7 +14,7 @@ root_project/build.gradle
1414
buildscript {
1515
1616
ext {
17-
stringcare_version = '0.3'
17+
stringcare_version = '0.4'
1818
}
1919
2020
repositories {
@@ -31,7 +31,7 @@ apply plugin: StringCare
3131
3232
stringcare {
3333
34-
debug true // prints detail build variant info
34+
debug true // prints details
3535
3636
modules {
3737
@@ -58,4 +58,22 @@ stringcare {
5858
}
5959
6060
}
61-
```
61+
```
62+
63+
64+
License
65+
-------
66+
Copyright 2017 Efraín Espada
67+
68+
Licensed under the Apache License, Version 2.0 (the "License");
69+
you may not use this file except in compliance with the License.
70+
You may obtain a copy of the License at
71+
72+
http://www.apache.org/licenses/LICENSE-2.0
73+
74+
Unless required by applicable law or agreed to in writing, software
75+
distributed under the License is distributed on an "AS IS" BASIS,
76+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
77+
See the License for the specific language governing permissions and
78+
limitations under the License.
79+

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ apply plugin: 'java'
2424
apply plugin: 'com.jfrog.bintray'
2525

2626
group 'com.stringcare'
27-
version '0.3'
27+
version '0.4'
2828

2929
sourceCompatibility = 1.8
3030

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
#Sun Jan 21 18:39:53 CET 2018
21
distributionBase=GRADLE_USER_HOME
32
distributionPath=wrapper/dists
43
zipStoreBase=GRADLE_USER_HOME
54
zipStorePath=wrapper/dists
6-
distributionUrl=https\://services.gradle.org/distributions/gradle-4.4.1-all.zip
5+
distributionUrl=https\://services.gradle.org/distributions/gradle-4.4.1-bin.zip

src/main/groovy/CredentialUtils.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,18 @@ private static void parseTrace(String module, String variant, String line, boole
6666
if (debug) {
6767
PrintUtils.print(module, line, debug);
6868
}
69+
} else if (line.toLowerCase().contains("config:") && moduleLocated && variantLocated) {
70+
boolean valid = !line.split(": ")[1].trim().equalsIgnoreCase("none");
71+
if (!valid) {
72+
key = line.split(": ")[1].trim();
73+
PrintUtils.print(module, "\uD83E\uDD2F no config defined for variant " + variant, true);
74+
if (debug) {
75+
until = key;
76+
}
77+
} else if (debug){
78+
PrintUtils.print(module, "Variant:" + variant, true);
79+
}
80+
6981
} else if (line.toLowerCase().contains("sha") && moduleLocated && variantLocated) {
7082
key = line.split(" ")[1];
7183
if (debug) {
@@ -86,9 +98,6 @@ private static void parseTrace(String module, String variant, String line, boole
8698
String locV = line.split(" ")[1];
8799
if (locV.equals(variant)) {
88100
variantLocated = true;
89-
if (debug) {
90-
PrintUtils.print(module, line, debug);
91-
}
92101
}
93102
} else if (line.toLowerCase().contains(":" + module)) {
94103
moduleLocated = true;

src/main/groovy/FileUtils.java

Lines changed: 43 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,10 @@ public static String getString(BufferedReader br) {
5858
}
5959

6060
// detect multiple sourceSet res.srcDirs
61-
public static void backupStringResources(String module, Config config) {
62-
String currentPath = getCurrentPath(module);
61+
public static void backupStringResources(String module, Config config, boolean debug) {
62+
String path = getCurrentPath(module);
6363
for (String folder : config.getSrcFolders()) {
64-
currentPath += module + File.separator + folder + File.separator + "res" + File.separator;
64+
String currentPath = path + module + File.separator + folder + File.separator + "res" + File.separator;
6565
File file = new File(currentPath);
6666
String[] directories = file.list((current, name) -> new File(current, name).isDirectory());
6767
if (directories != null) {
@@ -77,8 +77,11 @@ public static void backupStringResources(String module, Config config) {
7777
toCheck.delete();
7878
}
7979
if (toCopy.exists()) {
80-
PrintUtils.print("- " + toCopy.getParentFile().getName() + File.separator + toCopy.getName(), true);
80+
PrintUtils.print(module, "- " + toCopy.getParentFile().getName() + File.separator + toCopy.getName(), true);
8181
copyFile(toCopy, toCheck);
82+
if (debug) {
83+
PrintUtils.print(module, "backuping file: " + toCopy.getPath(), true);
84+
}
8285
}
8386
} catch (IOException e) {
8487
e.printStackTrace();
@@ -87,15 +90,18 @@ public static void backupStringResources(String module, Config config) {
8790
}
8891
}
8992
} else {
90-
PrintUtils.print(module, "source folder not found: " + folder, true);
91-
}
93+
if (debug) {
94+
PrintUtils.print(module, "source folder not found: " + currentPath, true);
95+
} else {
96+
PrintUtils.print(module, "source folder not found: " + folder, true);
97+
} }
9298
}
9399
}
94100

95-
public static void encryptStringResources(String module, Config config, String key) {
96-
String currentPath = getCurrentPath(module);
101+
public static void encryptStringResources(String module, Config config, String key, boolean debug) {
102+
String path = getCurrentPath(module);
97103
for (String folder : config.getSrcFolders()) {
98-
currentPath += module + File.separator + folder + File.separator + "res" + File.separator;
104+
String currentPath = path + module + File.separator + folder + File.separator + "res" + File.separator;
99105
File file = new File(currentPath);
100106
String[] directories = file.list((current, name) -> new File(current, name).isDirectory());
101107
if (directories != null) {
@@ -104,19 +110,28 @@ public static void encryptStringResources(String module, Config config, String k
104110
for (String sFile : config.getStringFiles()) {
105111
File toEncrypt = new File(pathToEncrypt + sFile);
106112
if (toEncrypt.exists()) {
107-
PrintUtils.print("- " + toEncrypt.getParentFile().getName() + File.separator + toEncrypt.getName(), true);
108-
String encrypted = find(getTextFromFilePath(toEncrypt.getAbsolutePath()), key);
113+
PrintUtils.print(module, "- " + toEncrypt.getParentFile().getName() + File.separator + toEncrypt.getName(), true);
114+
String encrypted = find(module, getTextFromFilePath(toEncrypt.getAbsolutePath()), key, debug);
109115
writeFile(toEncrypt, encrypted);
116+
if (debug) {
117+
PrintUtils.print(module, "writing file: " + toEncrypt.getPath(), true);
118+
}
119+
} else if (debug) {
120+
PrintUtils.print(module, "source file not exist: " + pathToEncrypt + sFile, true);
110121
}
111122
}
112123
}
113124
} else {
114-
PrintUtils.print(module, "source folder not found: " + folder, true);
125+
if (debug) {
126+
PrintUtils.print(module, "source folder not found: " + currentPath, true);
127+
} else {
128+
PrintUtils.print(module, "source folder not found: " + folder, true);
129+
}
115130
}
116131
}
117132
}
118133

119-
public static void restoreStringResources(String module, Config config) {
134+
public static void restoreStringResources(String module, Config config, boolean debug) {
120135
String currentPath = getCurrentPath(module) + module + File.separator + "resbackup" + File.separator;
121136
File file = new File(currentPath);
122137
String[] directories = file.list((current, name) -> new File(current, name).isDirectory());
@@ -132,8 +147,11 @@ public static void restoreStringResources(String module, Config config) {
132147
File toCheck = new File(pathRes + sFile);
133148
if (toRestore.exists()) {
134149
try {
135-
PrintUtils.print("- " + toCheck.getParentFile().getName() + File.separator + toCheck.getName(), true);
150+
PrintUtils.print(module,"- " + toCheck.getParentFile().getName() + File.separator + toCheck.getName(), true);
136151
copyFile(toRestore, toCheck);
152+
if (debug) {
153+
PrintUtils.print(module, "restoring: " + toRestore.getPath(), true);
154+
}
137155
} catch (IOException e) {
138156
e.printStackTrace();
139157
}
@@ -146,9 +164,16 @@ public static void restoreStringResources(String module, Config config) {
146164
}
147165
if (file.isDirectory()) {
148166
file.delete();
167+
if (debug) {
168+
PrintUtils.print(module, "temp source folder removed: " + file.getPath(), true);
169+
}
149170
}
150171
} else {
151-
PrintUtils.print(module, "restore folder not found", true);
172+
if (debug) {
173+
PrintUtils.print(module, "restore folder not found: " + currentPath, true);
174+
} else {
175+
PrintUtils.print(module, "restore folder not found", true);
176+
}
152177
}
153178
}
154179

@@ -168,7 +193,7 @@ public static boolean isEncrypted(String value, String key) {
168193
return encrypted;
169194
}
170195

171-
public static String find(String xmlO, String key) {
196+
public static String find(String module, String xmlO, String key, boolean debug) {
172197
String content = xmlO;
173198
String toFind1 = "hidden=\"true\"";
174199

@@ -198,9 +223,9 @@ public static String find(String xmlO, String key) {
198223

199224
toShow = toShow.length() > maxToShow ? toShow.substring(0, maxToShow) + ".." : toShow;
200225
encrypted = encrypted.length() > maxToShow ? encrypted.substring(0, maxToShow) + ".." : encrypted;
201-
PrintUtils.print("\t[" + toShow + "] - [" + encrypted + "]" + (hasExtra ? extra : ""), true);
226+
PrintUtils.print(module, "\t[" + toShow + "] - [" + encrypted + "]" + (hasExtra ? extra : ""), true);
202227
} catch (Exception e) {
203-
PrintUtils.print("error on " + result);
228+
PrintUtils.print(module, "error on " + result, true);
204229
e.printStackTrace();
205230
}
206231

src/main/groovy/StringCarePlugin.groovy

Lines changed: 42 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class StringCare implements Plugin<Project> {
77

88
private static final float VERSION = 0.3;
99
private Project project;
10-
private static String key = null;
10+
private String key;
1111
private static boolean debug;
1212
private static Map<String, Config> moduleMap = new HashMap<>();
1313

@@ -67,48 +67,52 @@ class StringCare implements Plugin<Project> {
6767

6868
@Override
6969
void onMergeResourcesStarts(String module, String variant) {
70-
String key = CredentialUtils.getKey(module, variant, debug);
71-
72-
if (moduleMap.containsKey(module)) {
73-
PrintUtils.print(module, variant + ":" + key)
74-
PrintUtils.print(module, "backupStringResources")
75-
FileUtils.backupStringResources(module, moduleMap.get(module))
76-
PrintUtils.print(module, "encryptStringResources")
77-
78-
FileUtils.encryptStringResources(module, moduleMap.get(module), key)
79-
} else {
80-
Config config = new Config();
81-
List<String> stg = new ArrayList<>();
82-
stg.add("strings.xml")
83-
List<String> src = new ArrayList<>();
84-
src.add("src/main")
85-
config.setStringFiles(stg)
86-
config.setSrcFolders(src)
87-
88-
PrintUtils.print(module, variant + ":" + key)
89-
PrintUtils.print(module, "backupStringResources")
90-
FileUtils.backupStringResources(module, config)
91-
PrintUtils.print(module, "encryptStringResources")
92-
FileUtils.encryptStringResources(module, config, key)
70+
key = CredentialUtils.getKey(module, variant, debug);
71+
if (!"none".equals(key)) {
72+
if (moduleMap.containsKey(module)) {
73+
PrintUtils.print(module, variant + ":" + key)
74+
PrintUtils.print(module, "backupStringResources")
75+
FileUtils.backupStringResources(module, moduleMap.get(module), debug)
76+
PrintUtils.print(module, "encryptStringResources")
77+
78+
FileUtils.encryptStringResources(module, moduleMap.get(module), key, debug)
79+
} else {
80+
Config config = new Config();
81+
List<String> stg = new ArrayList<>();
82+
stg.add("strings.xml")
83+
List<String> src = new ArrayList<>();
84+
src.add("src/main")
85+
config.setStringFiles(stg)
86+
config.setSrcFolders(src)
87+
88+
PrintUtils.print(module, variant + ":" + key)
89+
PrintUtils.print(module, "backupStringResources")
90+
FileUtils.backupStringResources(module, config, debug)
91+
PrintUtils.print(module, "encryptStringResources")
92+
FileUtils.encryptStringResources(module, config, key, debug)
93+
}
9394
}
95+
9496
}
9597

9698
@Override
9799
void onMergeResourcesFinish(String module, String variant) {
98-
if (moduleMap.containsKey(module)) {
99-
PrintUtils.print(module, "restoreStringResources")
100-
FileUtils.restoreStringResources(module, moduleMap.get(module))
101-
} else {
102-
Config config = new Config();
103-
List<String> stg = new ArrayList<>();
104-
stg.add("strings.xml")
105-
List<String> src = new ArrayList<>();
106-
src.add("src/main")
107-
config.setStringFiles(stg)
108-
config.setSrcFolders(src)
109-
110-
PrintUtils.print(module, "restoreStringResources")
111-
FileUtils.restoreStringResources(module, config)
100+
if (!"none".equals(key)) {
101+
if (moduleMap.containsKey(module)) {
102+
PrintUtils.print(module, "restoreStringResources")
103+
FileUtils.restoreStringResources(module, moduleMap.get(module), debug)
104+
} else {
105+
Config config = new Config();
106+
List<String> stg = new ArrayList<>();
107+
stg.add("strings.xml")
108+
List<String> src = new ArrayList<>();
109+
src.add("src/main")
110+
config.setStringFiles(stg)
111+
config.setSrcFolders(src)
112+
113+
PrintUtils.print(module, "restoreStringResources")
114+
FileUtils.restoreStringResources(module, config, debug)
115+
}
112116
}
113117
}
114118
}))

src/test/groovy/LibraryTest.groovy

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
* This Spock specification was generated by the Gradle 'init' task.
3+
*/
4+
import spock.lang.Specification
5+
6+
class LibraryTest extends Specification {
7+
/*
8+
def "someLibraryMethod returns true"() {
9+
setup:
10+
def lib = new Library()
11+
12+
when:
13+
def result = lib.someLibraryMethod()
14+
15+
then:
16+
result == true
17+
}*/
18+
}

0 commit comments

Comments
 (0)