forked from magefree/mage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RemoveHeaders.java
71 lines (60 loc) · 1.96 KB
/
RemoveHeaders.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
/*
Remove the copy right header from all files inside project.
*/
import java.io.IOException;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.regex.Pattern;
public class RemoveHeaders {
private static String readEntireFile(String filePath) {
String content = "";
try {
content = new String(Files.readAllBytes(Paths.get(filePath)));
}
catch (IOException e) {
e.printStackTrace();
}
return content;
}
private static void saveFileToDisk(String filePath, String content) {
File file = new File(filePath);
Path path = Paths.get(filePath);
try (FileWriter writer = new FileWriter(file)) {
if (Files.isWritable(path) && !Files.isSymbolicLink(path) && !Files.isHidden(path)) {
writer.write(content);
writer.flush();
}
} catch (IOException e) {
e.printStackTrace();
}
}
private static String removeMatchingText(String content, Pattern pattern) {
return pattern.matcher(content).replaceAll("");
}
public static void recursivelyGetFilesAndRemoveHeaders(String path) {
Pattern copyrightHeader = Pattern.compile("(?i)/\\*(?:\r?\n|\r) ?\\*.*?Copyright[\\S\\s]*?\\*/");
File currentDirectory = new File(path);
File[] files = currentDirectory.listFiles();
if (files == null) {
return;
}
for (File file : files) {
if (file.isDirectory()) {
recursivelyGetFilesAndRemoveHeaders(file.getAbsolutePath());
} else {
String filePath = file.getAbsolutePath();
String fileContents = readEntireFile(filePath);
String updatedContents = removeMatchingText(fileContents, copyrightHeader);
if (fileContents != updatedContents) {
saveFileToDisk(filePath, updatedContents);
}
}
}
}
public static void main(String args[]) {
String rootPath = System.getProperty("user.dir");
recursivelyGetFilesAndRemoveHeaders(rootPath);
}
}