fix: 首次提交
This commit is contained in:
3
Filter/build.gradle
Normal file
3
Filter/build.gradle
Normal file
@@ -0,0 +1,3 @@
|
||||
apply plugin: 'java'
|
||||
apply plugin: 'application'
|
||||
mainClassName = 'java.MainRun'
|
||||
138
Filter/src/main/java/com/siren/filter/FilterAttribute.java
Normal file
138
Filter/src/main/java/com/siren/filter/FilterAttribute.java
Normal file
@@ -0,0 +1,138 @@
|
||||
package com.siren.filter;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* 过滤product特殊属性
|
||||
* Created by Siren on 2022/3/27.
|
||||
*/
|
||||
public class FilterAttribute {
|
||||
|
||||
//需要被取代的标签
|
||||
private final static String TABLET = "product=\"tablet\"";
|
||||
private final static String DEVICE = "product=\"device\"";
|
||||
private final static String NOSDCARD = "product=\"nosdcard\"";
|
||||
private final static String EMULATOR = "product=\"emulator\"";
|
||||
private final static String TV = "product=\"tv\"";
|
||||
|
||||
/**
|
||||
* 执行过滤任务
|
||||
*/
|
||||
public static void filter(String path) {
|
||||
File folder = new File(path);
|
||||
if (!folder.exists()) {
|
||||
System.out.println(folder.getPath() + "路径不存在");
|
||||
return;
|
||||
}
|
||||
filterFile(folder);
|
||||
}
|
||||
|
||||
/**
|
||||
* 匹配需要被移除的属性
|
||||
*/
|
||||
private static boolean matchAttribute(String content) {
|
||||
String regex = ".*(" + TABLET + ")|(" + DEVICE + ")|(" + NOSDCARD + ")|(" + EMULATOR + ")|(" + TV + ").*";
|
||||
Matcher matcher = Pattern.compile(regex).matcher(content);
|
||||
return matcher.find();
|
||||
}
|
||||
|
||||
private static void filterFile(File folder) {
|
||||
File[] files = folder.listFiles();
|
||||
for (File file : files) {
|
||||
if (file.isDirectory()) {
|
||||
filterFile(file);
|
||||
} else if (file.getName().endsWith(".xml")) {
|
||||
String content = readFileToStr(file);
|
||||
List<String> filterList = getFieldListByRegex(content, "string");
|
||||
for (String s : filterList) {
|
||||
content = content.replace(s, "");
|
||||
}
|
||||
if (filterList.size() > 0) {
|
||||
writeToFile(file, content);
|
||||
ShellUtils.ignoreFile(file.getPath());//忽略文件
|
||||
System.out.println("Modified file:" + file.getPath());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取string标签字段并返回list
|
||||
*/
|
||||
private static List<String> getFieldListByRegex(String xml, String label) {
|
||||
List<String> filterList = new ArrayList<>();
|
||||
String regex = "<" + label + "([\\s\\S]*?)</" + label + ">";
|
||||
Matcher matcher = Pattern.compile(regex).matcher(xml);
|
||||
while (matcher.find()) {
|
||||
int start = matcher.start();
|
||||
int end = matcher.end();
|
||||
String content = xml.substring(start, end);
|
||||
if (matchAttribute(content)) {
|
||||
filterList.add(content);
|
||||
}
|
||||
}
|
||||
return filterList;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取文件转String
|
||||
*/
|
||||
private static String readFileToStr(File file) {
|
||||
StringBuffer buffer;
|
||||
try {
|
||||
FileInputStream is = new FileInputStream(file);
|
||||
buffer = streamToStr(is);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return "";
|
||||
}
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* inputStream to string
|
||||
*/
|
||||
private static StringBuffer streamToStr(InputStream is) throws Exception {
|
||||
InputStreamReader reader = new InputStreamReader(is);
|
||||
BufferedReader bufferedReader = new BufferedReader(reader);
|
||||
StringBuffer buffer = new StringBuffer("");
|
||||
String str;
|
||||
while ((str = bufferedReader.readLine()) != null) {
|
||||
buffer.append(str);
|
||||
buffer.append("\n");
|
||||
}
|
||||
return buffer;
|
||||
}
|
||||
|
||||
/**
|
||||
* write content
|
||||
*/
|
||||
private static void writeToFile(File file, String content) {
|
||||
BufferedWriter writer = null;
|
||||
try {
|
||||
writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, false)));
|
||||
writer.write(content);
|
||||
writer.flush();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
if (writer != null) try {
|
||||
writer.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
73
Filter/src/main/java/com/siren/filter/FilterMultiLang.java
Normal file
73
Filter/src/main/java/com/siren/filter/FilterMultiLang.java
Normal file
@@ -0,0 +1,73 @@
|
||||
package com.siren.filter;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
/**
|
||||
* 过滤多语言
|
||||
* Created by Siren on 2022/3/27.
|
||||
*/
|
||||
public class FilterMultiLang {
|
||||
|
||||
//需要被取代的标签
|
||||
private static String[] TAGS = new String[]{
|
||||
"af", "am", "ar", "as", "az",
|
||||
"be", "bg", "bn", "bs", "ca",
|
||||
"cs", "da", "el", "en", "es",
|
||||
"et", "eu", "fa", "fi", "fr",
|
||||
"gl", "gu", "hi", "hr", "hu",
|
||||
"hy", "in", "is", "it", "iw",
|
||||
"ja", "ka", "kk", "km", "kn",
|
||||
"ko", "ky", "lo", "lt", "lv",
|
||||
"mk", "ml", "mn", "mr", "ms",
|
||||
"my", "nb", "ne", "nl", "or",
|
||||
"pa", "pl", "pt", "ro", "rm",
|
||||
"ro", "ru", "si", "sk", "sl",
|
||||
"sq", "sr", "sv", "sw", "ta",
|
||||
"te", "th", "tl", "tr", "uk",
|
||||
"ur", "uz", "vi", "zu", "de",
|
||||
"zh-rHK", "zh-rTW", "b+sr+Latn"
|
||||
};
|
||||
|
||||
/**
|
||||
* 执行过滤任务
|
||||
*/
|
||||
public static void filter(String path) {
|
||||
File folder = new File(path);
|
||||
if (!folder.exists()) {
|
||||
System.out.println(folder.getPath() + "路径不存在");
|
||||
return;
|
||||
}
|
||||
filterFolder(folder);
|
||||
}
|
||||
|
||||
private static void filterFolder(File folder) {
|
||||
File[] files = folder.listFiles();
|
||||
for (File file : files) {
|
||||
if (file.isDirectory() && isMultiLang(file.getName())) {
|
||||
deleteFile(file);
|
||||
System.out.println("Delete folder:" + file.getPath());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean isMultiLang(String name) {
|
||||
for (String tag : TAGS) {
|
||||
if (name.endsWith("-" + tag) || name.contains("-" + tag + "-")) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private static void deleteFile(File dirFile) {
|
||||
if (dirFile.isFile()) {
|
||||
dirFile.delete();
|
||||
ShellUtils.ignoreFile(dirFile.getPath());//忽略文件
|
||||
} else {
|
||||
for (File file : dirFile.listFiles()) {
|
||||
deleteFile(file);
|
||||
}
|
||||
dirFile.delete();
|
||||
}
|
||||
}
|
||||
}
|
||||
34
Filter/src/main/java/com/siren/filter/Main.java
Normal file
34
Filter/src/main/java/com/siren/filter/Main.java
Normal file
@@ -0,0 +1,34 @@
|
||||
package com.siren.filter;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
/**
|
||||
* 主程序
|
||||
* Created by Siren on 2022/4/7.
|
||||
*/
|
||||
public class Main {
|
||||
|
||||
/**
|
||||
* 执行过滤任务
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
String[] arr = new String[]{
|
||||
"res",
|
||||
"res-export",
|
||||
"res-product",
|
||||
"SettingsLib/res",
|
||||
"WifiTrackerLib/res",
|
||||
"SettingsLib/HelpUtils/res",
|
||||
"SettingsLib/RestrictedLockUtils/res",
|
||||
"SettingsLib/SearchWidget/res"
|
||||
};
|
||||
|
||||
for (String name : arr) {
|
||||
String path = System.getProperty("user.dir") + File.separator + name;
|
||||
// 可选项:清除多余的国际化语言,可提高编译效率
|
||||
FilterMultiLang.filter(path);
|
||||
// 必选项:清除string里面的product属性,如tablet、device等,因为AS无法识别该属性,会编译不通过
|
||||
FilterAttribute.filter(path);
|
||||
}
|
||||
}
|
||||
}
|
||||
37
Filter/src/main/java/com/siren/filter/ShellUtils.java
Normal file
37
Filter/src/main/java/com/siren/filter/ShellUtils.java
Normal file
@@ -0,0 +1,37 @@
|
||||
package com.siren.filter;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
/**
|
||||
* shell脚本工具
|
||||
* Created by Siren on 2022/4/7.
|
||||
*/
|
||||
public class ShellUtils {
|
||||
|
||||
/**
|
||||
* 忽略文件
|
||||
*/
|
||||
public static void ignoreFile(String path) {
|
||||
String rootPath = System.getProperty("user.dir") + File.separator;
|
||||
|
||||
execGit("git update-index --assume-unchanged " + (isWindows() ?
|
||||
path.substring(rootPath.length()).replace("\\", "\\\\") :
|
||||
path.substring(rootPath.length())));
|
||||
}
|
||||
|
||||
public static boolean isWindows() {
|
||||
return System.getProperties().getProperty("os.name").toUpperCase().contains("WINDOWS");
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行git指令
|
||||
*/
|
||||
private static void execGit(String cmd) {
|
||||
try {
|
||||
Process process = Runtime.getRuntime().exec(cmd);
|
||||
process.waitFor();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user