-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.java
240 lines (192 loc) · 7.5 KB
/
Main.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
import java.io.File;
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
private static final String FIGMA_RES = "C:\\Users\\Artem\\Desktop\\figma_res";
private static final String APP_RES = "C:\\GitProjects\\NadymProjects\\EG_Kids\\app\\src\\main\\res";
private static final String DRAWABLE = "\\drawable";
private static final String SW_600 = "-sw600dp";
private static final String NODPI = "-nodpi";
//x1, x1.5, x2, x3, x4
private String[] figma = {"", "@2x", "@2x-1", "@3x", "-1"};
private String[] destination = {"-mdpi", "-hdpi", "-xhdpi", "-xxhdpi", "-xxxhdpi"};
//all figma schemes, etc. ingonred, move everything from FIGMA_RES to drawable-nodpi
private boolean goNodpi = false;
//no need to change it to false if tablet folder is empty, but optionally you can
private boolean includeTablets = true;
//starting with 0.5 from original, e.g. x0.5, x0.75, x1, x1.5, x2
private boolean useFigmaV2Scheme = false;
private String[] figmaV2 = {"", "-1", "-2", "@2x", "@2x-1"};
private String[] scheme;
/*
Script flow:
1) changes names to newNames, if there's any specified
2) replaces all " " with "_" in names
3) converts names to lowercase
4) changes prefixesToChange to newPrefixes, if there's any specified
5) attaches a newNamePrefix to each name if specified
6) if file already exists, overwrite/skip question will be asked in console
*/
//change one name to another
private String[] names = {
};
private String[] newNames = {
};
//lowercase only !!! all strings is gonna be converted to lowercase by now
//for example "pic_" to "ic_"
private String[] prefixesToChange = {
};
private String[] newPrefixes = {
};
//attach name prefix to all
private String newNamePrefix = null;
public static void main(String[] args) {
Main main = new Main();
main.run();
}
private void run() {
System.out.println("run!");
scheme = useFigmaV2Scheme ? figmaV2 : figma;
try {
File figmaDir = new File(FIGMA_RES);
for (File file : figmaDir.listFiles()) {
moveFile(file, false);
}
if (includeTablets) {
File figmaTabletsDir = new File(FIGMA_RES+SW_600);
for (File file : figmaTabletsDir.listFiles()) {
moveFile(file, true);
}
}
System.out.println("finish!");
} catch (Exception ex) {
ex.printStackTrace();
System.out.println("ERROR!");
}
}
private ArrayList<String> namesToOverwrite = new ArrayList<>();
private ArrayList<String> namesToSkip = new ArrayList<>();
private void moveFile(File file, boolean forTablets) {
String fileName = file.getName();
int extensionIndex = fileName.lastIndexOf(".");
String name = fileName.substring(0, extensionIndex);
String extension = fileName.substring(extensionIndex);
int index = getIndex(name);
String cleanName = getCleanName(name, index);
String newName = getNewName(cleanName);
File destination = getDestination(index, forTablets);
if (!destination.exists()) {
destination.mkdir();
}
File newFile = new File(destination + "\\" + newName + extension);
if (newFile.exists()) {
for(String skip : namesToSkip) {
if (skip.equals(newName)) {
System.out.println(newName + " skip");
return;
}
}
boolean shouldBeOverwritten = false;
for (String overwrite : namesToOverwrite) {
if (overwrite.equals(newName)) {
shouldBeOverwritten = true;
break;
}
}
if (shouldBeOverwritten) {
boolean deleted = newFile.delete();
if (!deleted) {
System.out.println("STOPPED WITH ERROR: couldn't delete file: ");
System.out.println(newFile.toString());
System.exit(0);
}
} else {
System.out.println(newFile.toString());
System.out.println("is already Exists!");
System.out.print("Do you want to overwrite it and alternative files? (skip otherwise): (Y/N)");
try {
Scanner scanner = new Scanner(System.in);
// get their input as a String
String answer = scanner.next();
if (answer.equalsIgnoreCase("N")) {
namesToSkip.add(newName);
System.out.println("skip");
System.out.println("---");
return;
} else if (!answer.equalsIgnoreCase("Y")) {
System.out.println("STOPPED WITH ERROR: expected \"y\" or \"n\" ");
System.exit(0);
}
boolean deleted = newFile.delete();
if (!deleted) {
System.out.println("STOPPED WITH ERROR: couldn't delete file: ");
System.out.println(newFile.toString());
System.exit(0);
}
namesToOverwrite.add(newName);
} catch (Exception ex) {
ex.printStackTrace();
System.exit(0);
}
}
}
boolean result = file.renameTo(newFile);
if (result) {
System.out.println("SUCCESS:");
System.out.println(fileName + " moved as " + newName + " to:");
System.out.println(newFile.toString());
}
}
private int getIndex(String name) {
if (useFigmaV2Scheme) {
//from right to left cause there's -1 at right side and @2-1 at left side
for (int i = scheme.length - 1; i > 0; i--) {
if (name.endsWith(scheme[i])) {
return i;
}
}
} else {
for (int i = 1; i < scheme.length; i++) {
if (name.endsWith(scheme[i])) {
return i;
}
}
}
return 0;
}
private String getCleanName(String name, int index) {
if (index > 0) {
return name.substring(0, name.length() - scheme[index].length());
}
return name;
}
private String getNewName(String name) {
String newName = name;
for (int i = 0; i < names.length; i++) {
if (name.equals(names[i])) {
newName = newNames[i];
}
}
newName = newName.replaceAll(" ", "_");
newName = newName.toLowerCase();
for (int i = 0; i < prefixesToChange.length; i++) {
newName = newName.replace(prefixesToChange[i], newPrefixes[i]);
}
if (newNamePrefix != null) {
newName = newNamePrefix + newName;
}
return newName;
}
private File getDestination(int index, boolean tablets) {
if (goNodpi) {
return new File(APP_RES + DRAWABLE + NODPI);
}
StringBuilder sb = new StringBuilder(APP_RES);
sb.append(DRAWABLE);
if (tablets) {
sb.append(SW_600);
}
sb.append(destination[index]);
return new File(sb.toString());
}
}