Skip to content

Commit 2d92ce7

Browse files
committed
Add header checker/fixer.
1 parent 0514ff1 commit 2d92ce7

File tree

1 file changed

+132
-0
lines changed

1 file changed

+132
-0
lines changed
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
/**
2+
* Copyright 2016 Netflix, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
5+
* compliance with the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License is
10+
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See
11+
* the License for the specific language governing permissions and limitations under the License.
12+
*/
13+
14+
package io.reactivex;
15+
16+
import java.io.*;
17+
import java.util.*;
18+
19+
import org.junit.Test;
20+
21+
/**
22+
* Adds license header to java files.
23+
*/
24+
public class FixLicenseHeaders {
25+
26+
String[] header = {
27+
"/**",
28+
" * Copyright 2016 Netflix, Inc.",
29+
" *",
30+
" * Licensed under the Apache License, Version 2.0 (the \"License\"); you may not use this file except in",
31+
" * compliance with the License. You may obtain a copy of the License at",
32+
" *",
33+
" * http://www.apache.org/licenses/LICENSE-2.0",
34+
" *",
35+
" * Unless required by applicable law or agreed to in writing, software distributed under the License is",
36+
" * distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See",
37+
" * the License for the specific language governing permissions and limitations under the License.",
38+
" */",
39+
""
40+
};
41+
42+
@Test
43+
public void checkAndUpdateLicenses() throws Exception {
44+
if (System.getenv("CI") != null) {
45+
// no point in changing the files in CI
46+
return;
47+
}
48+
File f = MaybeNo2Dot0Since.findSource("Flowable");
49+
if (f == null) {
50+
return;
51+
}
52+
53+
Queue<File> dirs = new ArrayDeque<File>();
54+
55+
File parent = f.getParentFile();
56+
dirs.offer(parent);
57+
dirs.offer(new File(parent.getAbsolutePath().replace('\\', '/').replace("src/main/java", "src/perf/java")));
58+
dirs.offer(new File(parent.getAbsolutePath().replace('\\', '/').replace("src/main/java", "src/test/java")));
59+
60+
StringBuilder fail = new StringBuilder();
61+
62+
while (!dirs.isEmpty()) {
63+
f = dirs.poll();
64+
65+
File[] list = f.listFiles();
66+
if (list != null && list.length != 0) {
67+
68+
for (File u : list) {
69+
if (u.isDirectory()) {
70+
dirs.offer(u);
71+
} else {
72+
if (u.getName().endsWith(".java")) {
73+
74+
List<String> lines = new ArrayList<String>();
75+
BufferedReader in = new BufferedReader(new FileReader(u));
76+
try {
77+
for (;;) {
78+
String line = in.readLine();
79+
if (line == null) {
80+
break;
81+
}
82+
83+
lines.add(line);
84+
}
85+
} finally {
86+
in.close();
87+
}
88+
89+
if (!lines.get(0).equals(header[0]) && !lines.get(1).equals(header[1])) {
90+
fail.append("java.lang.RuntimeException: missing header added, refresh and re-run tests!\r\n")
91+
.append(" at ")
92+
;
93+
94+
String fn = u.toString().replace('\\', '/');
95+
96+
int idx = fn.indexOf("io/reactivex/");
97+
98+
fn = fn.substring(idx).replace('/', '.').replace(".java", "");
99+
100+
fail.append(fn).append(" (")
101+
;
102+
103+
int jdx = fn.lastIndexOf('.');
104+
105+
fail.append(fn.substring(jdx + 1));
106+
107+
fail.append(".java:1)\r\n\r\n");
108+
109+
lines.addAll(0, Arrays.asList(header));
110+
111+
PrintWriter w = new PrintWriter(new FileWriter(u));
112+
113+
try {
114+
for (String s : lines) {
115+
w.println(s);
116+
}
117+
} finally {
118+
w.close();
119+
}
120+
}
121+
}
122+
}
123+
}
124+
}
125+
}
126+
127+
if (fail.length() != 0) {
128+
System.out.println(fail);
129+
throw new AssertionError(fail.toString());
130+
}
131+
}
132+
}

0 commit comments

Comments
 (0)