-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathAbstractTask.java
188 lines (168 loc) · 5.88 KB
/
AbstractTask.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package com.chrisney.enigma.tasks;
import com.chrisney.enigma.utils.Utils;
import org.apache.commons.io.FileUtils;
import org.gradle.api.DefaultTask;
import org.gradle.api.tasks.Internal;
import org.gradle.internal.Pair;
import java.io.File;
import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
/**
* Abstract Enigma Gradle Task
* @author Christopher Ney
*/
public class AbstractTask extends DefaultTask {
private static final String BACKUP_DIR = "enigma-backup";
private static final String SVN_FOLDER = ".svn";
private static final String MERCURIAL_FOLDER = ".hg";
private static final String GIT_FOLDER = ".git";
private static final String GIT_IGNORE = "*\n" +
"*/\n" +
"!.gitignore";
public boolean enabled = true;
public boolean debug = false;
public String rootProject;
public String pathSrc;
public String[] pathSrcs;
public AbstractTask() {
this.setGroup("enigma");
}
/**
* Return the collection of all JAVA files (*.java) found in the app source folder
* @return Collection of all JAVA files (*.java)
*/
@Internal
protected Collection<Pair<Integer, File>> getAllJavaFiles() {
System.out.println("Enigma pathSrcs:[");
for(String src : pathSrcs) {
System.out.println("\tEnigma src: " + src);
}
System.out.println("Enigma ]");
Collection<Pair<Integer, File>> ret = new java.util.ArrayList<>(Collections.emptyList());
if(pathSrcs.length <= 0) {
Collection<File> files = Utils.listFileTree(new File(pathSrc), ".java");
for(File file : files) {
ret.add(Pair.of(0, file));
}
return ret;
}
else {
int index = 0;
for(String src : pathSrcs) {
Collection<File> files = Utils.listFileTree(new File(src), ".java");
for(File file : files) {
ret.add(Pair.of(index, file));
}
index++;
}
return ret;
}
}
/**
*
* Return the collection of all XML files (*.xml) found in the app source folder
* @return Collection of all XML files (*.xml)
*/
@Internal
protected Collection<File> getAllXmlFiles() {
if(pathSrcs.length <= 0) {
Utils.listFileTree(new File(pathSrc), ".xml");
}
Collection<File> ret = new java.util.ArrayList<>(Collections.emptyList());
for(String src : pathSrcs) {
ret.addAll(Utils.listFileTree(new File(src), ".xml"));
}
return ret;
}
/**
* Check if an SCM (Source Code Management) tool is setup or not not.
* @return True if an SCM tool is found
*/
protected boolean checkSCM() {
boolean result = hasGit() || hasSubversion() || hasMercurial();
if (!result) {
System.out.println("⚠️ The project has no Source Code Management. Please setup one (Git, SVN, Mercurial) before use Enigma plugin!");
}
return result;
}
/**
* Check if an Subversion tool is setup or not not.
* @return True if an Subversion tool is found
*/
private boolean hasSubversion() {
return new File(rootProject + File.separator + SVN_FOLDER).exists();
}
/**
* Check if an Mercurial tool is setup or not not.
* @return True if an Mercurial tool is found
*/
private boolean hasMercurial() {
return new File(rootProject + File.separator + MERCURIAL_FOLDER).exists();
}
/**
* Check if an Git tool is setup or not not.
* @return True if an Git tool is found
*/
private boolean hasGit() {
return new File(rootProject + File.separator + GIT_FOLDER).exists();
}
/**
* Return the file path of 'backup' directory of the current project
* @return File path of 'backup' directory
*/
protected String backupDir() {
return rootProject + File.separator + BACKUP_DIR + File.separator;
}
/**
* Check if 'backup' directory exists or not
* @return True if exists
*/
protected boolean backupDirExists() {
return new File(backupDir()).exists();
}
/**
* Remove the existing 'backup' directory
* @throws IOException If an I/O exception
*/
protected void removeBackupDir() throws IOException {
File backup = new File(backupDir());
FileUtils.deleteDirectory(backup);
}
/**
* Create the 'backup' directory (with .gitignore config file)
* @return True if success
* @throws IOException If an I/O exception
*/
protected boolean createBackupDir() throws IOException {
File backupDir = new File(backupDir());
File gitIgnore = new File(backupDir() + File.separator + ".gitignore");
if (!backupDir.exists()) {
if (backupDir.mkdir())
FileUtils.writeStringToFile(gitIgnore, GIT_IGNORE, "UTF-8");
} else if (!gitIgnore.exists()) {
FileUtils.writeStringToFile(gitIgnore, GIT_IGNORE, "UTF-8");
}
return true;
}
/**
* Check if file if Enigma file
* @param srcFile JAVA file to test
* @return True if file is com.chrisney.enigma file
*/
protected boolean isEnigmaFile(File srcFile) {
return srcFile.getName().endsWith(InjectCodeTask.CLASS_NAME + ".java");
}
/**
* Check if the JAVA file contains Enigma code
* @param srcFile JAVA file to test
* @throws IOException If an I/O exception
* @return True if the file contains Enigma code
*/
protected boolean isEnigmatized(File srcFile) throws IOException {
String contents = FileUtils.readFileToString(srcFile, "UTF-8");
return contents.contains(InjectCodeTask.IMPORT_NAME) || contents.contains(InjectCodeTask.FUNCTION_NAME);
}
}