package projectmanager import java io BufferedReader import java io Da

  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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package projectmanager;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.util.Map;
import java.util.HashMap;
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
public class Manager {
private final IniReader config = new IniReader("C:\\xuj.ini");
private BufferedReader cin = new BufferedReader(new InputStreamReader(System.in));
Map<String, String> path = new HashMap<>();
public Manager() {
System.out.println("---------------------------------------");
System.out.println("| Android Project Manager |");
System.out.println("| by Vladimir Shevchuk |");
System.out.println("---------------------------------------");
checkConfig();
System.out.println("Папка: '" + path.get("PROJECTS_HOME") + "'");
System.out.println("help - справка");
enterCommand();
}
private void enterCommand() {
while(true) {
System.out.print("> ");
String command = gets();
switch(command.toLowerCase()) {
case "help":
printHelp();
break;
case "create":
createProject();
break;
case "show":
showProjects();
break;
case "exit":
System.out.println("\nВсего хорошего! ;-)");
System.exit(0);
break;
default:
System.out.println("Неверная команда!");
}
}
}
private void showProjects() {
//System.out.println(getResource("Activity"));
}
private void createProject() {
System.out.print("Название проекта(android:label): ");
String project_name = gets();
System.out.print("Package: ");
String app_package = gets();
System.out.print("Activity name: ");
String activity = gets();
System.out.print("API Level: ");
String api = gets();
if(project_name.equals("") || !new File(path.get("PROJECTS_HOME") + "/" + project_name).mkdir()) {
System.out.println("Невозможно создать проект с таким названием, или запись в папку запрещена!");
return;
}
String project_path = path.get("PROJECTS_HOME") + "/" + project_name + "/";
writeFile(project_path + "AndroidManifest.xml", getResource("AndroidManifest").replace("{package}", app_package).replace("{api}", api).replace("{project}", project_name).replace("{activity}", activity));
}
private void writeFile(String fileName, String text) {
try {
PrintWriter out = new PrintWriter(new File(fileName).getAbsoluteFile());
try {
out.print(text);
} finally {
out.close();
}
} catch(IOException e) {
System.out.println("Системная ошибка!");
System.exit(0);
}
}
private String gets() {
try {
return cin.readLine();
}catch(IOException io) {
System.out.println("Системная ошибка!");
System.exit(0);
}
return null;
}
private String getResource(String file) {
DataInputStream dis = new DataInputStream(getClass().getResourceAsStream("/resources/" + file));
StringBuilder strBuff = new StringBuilder();
int ch = 0;
try {
while ((ch = dis.read()) != -1) {
strBuff.append((char) ((ch >= 0xc0 && ch <= 0xFF) ? (ch + 0x350) : ch));
}
dis.close();
} catch (Exception e) {
System.err.println("ERROR!");
System.exit(0);
}
return strBuff.toString();
}
private void printHelp() {
String[] commands = {
"help - справка",
"exit - выход",
"show - показать проекты",
"create - создать проект",
"remove - удалить проект",
"build - собрать проект"
};
for(String command : commands)
System.out.println("\t" + command);
}
private void checkConfig() {
boolean error = false;
if(!new File(config.get("Path", "JDK_HOME")).exists()) {
System.out.println("Папка с JDK не найдена!");
error = true;
}else
path.put("JDK_HOME", config.get("Path", "JDK_HOME"));
if(!new File(config.get("Path", "SDK_HOME")).exists()) {
System.out.println("Папка с Android SDK не найдена!");
error = true;
}else
path.put("SDK_HOME", config.get("Path", "SDK_HOME"));
if(!new File(config.get("Path", "PROJECTS_HOME")).exists()) {
System.out.println("Папка с проектами не найдена!");
error = true;
}else
path.put("PROJECTS_HOME", config.get("Path", "PROJECTS_HOME"));
if(!new File(config.get("Path", "SDK_HOME") + "/build-tools/" ).exists()) {
System.out.println("Папка build-tools в Android SDK не найдена!");
error = true;
}
if(error) {
System.out.println("\nПроверьте настройки в файле config.ini и запустите программу ещё раз!");
System.exit(0);
}
error = false;
String btv = getLatestBuildToolsVersion(new File(path.get("SDK_HOME") + "/build-tools/"));
if(btv != null)
path.put("BUILD_TOOLS_PATH", path.get("SDK_HOME") + "/build-tools/" + btv);
else {
System.out.println("Не найдено инструментов " + path.get("SDK_HOME") + "/build-tools/");
System.exit(0);
}
String[] checkFiles = {
path.get("BUILD_TOOLS_PATH") + "/aapt.exe",
path.get("BUILD_TOOLS_PATH") + "/dx.bat",
path.get("SDK_HOME") + "/platform-tools/adb.exe",
path.get("JDK_HOME") + "/bin/javac.exe",
path.get("JDK_HOME") + "/bin/keytool.exe",
path.get("JDK_HOME") + "/bin/jarsigner.exe"
};
for(String file : checkFiles)
if(!new File(file).exists()) {
System.out.println("Файл '" + file + "' не найден!");
error = true;
}
if(error)
System.exit(0);
}
private String getLatestBuildToolsVersion(File path) {
File[] dirs = path.listFiles(new FileFilter() {
public boolean accept(File file) {
return file.isDirectory();
}
});
for(int i = dirs.length - 1; i >= 0; i--)
try {
Integer.parseInt(dirs[i].getName().substring(0,1));
return dirs[i].getName();
}catch(NumberFormatException numex) {}
return null;
}
}