Skip to content

Commit 2a94cb7

Browse files
refactor
1 parent 96d725a commit 2a94cb7

5 files changed

Lines changed: 81 additions & 89 deletions

File tree

build.gradle

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,14 +140,17 @@ task modGen {
140140

141141
task modPack {
142142
group = 'mods'
143-
dependsOn ':client:compileJava', ':server:compileJava'
143+
dependsOn modApply, ':client:compileJava', ':server:compileJava'
144144
doLast {
145145
def p = project.hasProperty('args') ? project.args.split(',') : null
146146
if (!p || p.length < 2) throw new RuntimeException("Usage: -Pargs=side,mod1,mod2")
147-
Mod.pack(rootDir, p[0], p[1..-1] as String[])
147+
Mod.pack(rootDir, p[0])
148148
}
149149
}
150150

151+
project(':client').tasks.compileJava.mustRunAfter modApply
152+
project(':server').tasks.compileJava.mustRunAfter modApply
153+
151154
task setupServer { group = 'setup'; dependsOn snapServer }
152155
task setupClient { group = 'setup'; dependsOn snapClient }
153156
task setup { group = 'setup'; dependsOn setupServer, setupClient }

buildSrc/src/main/java/Mod.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ public static void apply(File root, String side, String[] names) throws Exceptio
1212
if (!patch.exists()) throw new RuntimeException("Patch not found: " + patch);
1313
Patcher.apply(new File(root, side + "/src/main/java"), patch);
1414
}
15+
16+
Utils.rm(new File(root, side + "/build/classes").toPath());
1517
}
1618

1719
public static void revert(File root, String side, String[] names) throws Exception {
@@ -31,9 +33,7 @@ public static void gen(File root, String side, String name) throws Exception {
3133
Patcher.diff(base, src, out, true);
3234
}
3335

34-
public static void pack(File root, String side, String[] names) throws Exception {
35-
apply(root, side, names);
36-
36+
public static void pack(File root, String side) throws Exception {
3737
File base = new File(root, "patchSrc/" + side + "-base.zip");
3838
File src = new File(root, side + "/src/main/java");
3939
File classes = new File(root, side + "/build/classes/java/main");
Lines changed: 6 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
11
import java.io.*;
22
import java.nio.file.*;
3-
import java.util.*;
43
import java.util.zip.*;
54

65
public class Patcher {
76

87
public static void apply(File dir, File patch) throws Exception {
98
if (!patch.exists()) return;
109

11-
run(dir, "patch", "-p1", "-i", patch.getAbsolutePath());
10+
Utils.run(dir, "patch", "-p1", "-i", patch.getAbsolutePath());
1211
}
1312

1413
public static void unapply(File dir, File patch) throws Exception {
1514
if (!patch.exists()) return;
1615

17-
run(dir, "patch", "-R", "-p1", "-i", patch.getAbsolutePath());
16+
Utils.run(dir, "patch", "-R", "-p1", "-i", patch.getAbsolutePath());
1817
}
1918

2019
public static void snap(File src, File dest) throws IOException {
@@ -39,8 +38,8 @@ public static void diff(File base, File cur, File out, boolean isZip) throws Exc
3938

4039
Path tmp = Files.createTempDirectory("diff");
4140
try {
42-
if (isZip) unzip(base, tmp.toFile());
43-
else cp(base.toPath(), tmp);
41+
if (isZip) Utils.unzip(base, tmp.toFile());
42+
else Utils.cp(base.toPath(), tmp);
4443

4544
StringBuilder sb = new StringBuilder();
4645
for (String pkg : new String[]{"com", "net"}) {
@@ -53,7 +52,7 @@ public static void diff(File base, File cur, File out, boolean isZip) throws Exc
5352
if (!curFile.exists()) return;
5453

5554
try {
56-
String d = runOut(tmp.toFile(), "diff", "-u", rel, curFile.getAbsolutePath());
55+
String d = Utils.runOut(tmp.toFile(), "diff", "-u", rel, curFile.getAbsolutePath());
5756
if (!d.isEmpty()) {
5857
d = d.replace(rel, "a/" + rel);
5958
d = d.replace(curFile.getAbsolutePath(), "b/" + rel);
@@ -66,54 +65,7 @@ public static void diff(File base, File cur, File out, boolean isZip) throws Exc
6665
out.getParentFile().mkdirs();
6766
Files.writeString(out.toPath(), sb.toString());
6867
} finally {
69-
rm(tmp);
68+
Utils.rm(tmp);
7069
}
7170
}
72-
73-
private static void run(File dir, String... cmd) throws Exception {
74-
Process p = new ProcessBuilder(cmd).directory(dir).inheritIO().start();
75-
if (p.waitFor() != 0) throw new RuntimeException("Command failed: " + String.join(" ", cmd));
76-
}
77-
78-
private static String runOut(File dir, String... cmd) throws Exception {
79-
Process p = new ProcessBuilder(cmd).directory(dir).redirectErrorStream(true).start();
80-
String out = new String(p.getInputStream().readAllBytes());
81-
p.waitFor();
82-
83-
return out;
84-
}
85-
86-
private static void unzip(File zip, File dest) throws IOException {
87-
try (ZipFile zf = new ZipFile(zip)) {
88-
for (ZipEntry e : Collections.list(zf.entries())) {
89-
if (e.isDirectory()) continue;
90-
91-
File f = new File(dest, e.getName());
92-
f.getParentFile().mkdirs();
93-
try (InputStream in = zf.getInputStream(e)) {
94-
Files.copy(in, f.toPath());
95-
}
96-
}
97-
}
98-
}
99-
100-
private static void cp(Path src, Path dst) throws IOException {
101-
Files.walk(src).forEach(s -> {
102-
try {
103-
Files.copy(s, dst.resolve(src.relativize(s)), StandardCopyOption.REPLACE_EXISTING);
104-
} catch (IOException e) {
105-
throw new UncheckedIOException(e);
106-
}
107-
});
108-
}
109-
110-
private static void rm(Path p) throws IOException {
111-
if (!Files.exists(p)) return;
112-
113-
Files.walk(p).sorted(Comparator.reverseOrder()).forEach(f -> {
114-
try {
115-
Files.delete(f);
116-
} catch (IOException e) {}
117-
});
118-
}
11971
}

buildSrc/src/main/java/Setup.java

Lines changed: 7 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ public static void copySrc(File root, String side) throws IOException {
1212
Path j = root.toPath().resolve(side + "/src/main/java");
1313
Path r = root.toPath().resolve(side + "/src/main/resources");
1414

15-
rm(j); mkdir(j);
16-
cp(d.resolve("com"), j.resolve("com"));
17-
cp(d.resolve("net"), j.resolve("net"));
15+
Utils.rm(j); Utils.mkdir(j);
16+
Utils.cp(d.resolve("com"), j.resolve("com"));
17+
Utils.cp(d.resolve("net"), j.resolve("net"));
1818

19-
rm(r); mkdir(r);
20-
cp(d.resolve("assets"), r.resolve("assets"));
21-
cp(d.resolve("data"), r.resolve("data"));
22-
cp(d.resolve("META-INF"), r.resolve("META-INF"));
19+
Utils.rm(r); Utils.mkdir(r);
20+
Utils.cp(d.resolve("assets"), r.resolve("assets"));
21+
Utils.cp(d.resolve("data"), r.resolve("data"));
22+
Utils.cp(d.resolve("META-INF"), r.resolve("META-INF"));
2323

2424
for (File f : d.toFile().listFiles((dir, name) -> name.endsWith(".json") || name.endsWith(".jfc")))
2525
Files.copy(f.toPath(), r.resolve(f.getName()));
@@ -65,27 +65,4 @@ public static void assets(File run, String version, String url) throws Exception
6565
}
6666
}
6767
}
68-
69-
private static void rm(Path p) throws IOException {
70-
if (!Files.exists(p)) return;
71-
72-
Files.walk(p).sorted(Comparator.reverseOrder()).forEach(f -> {
73-
try {
74-
Files.delete(f);
75-
} catch (IOException e) {}
76-
});
77-
}
78-
79-
private static void mkdir(Path p) throws IOException { Files.createDirectories(p); }
80-
81-
private static void cp(Path src, Path dst) throws IOException {
82-
if (!Files.exists(src)) return;
83-
Files.walk(src).forEach(s -> {
84-
try {
85-
Files.copy(s, dst.resolve(src.relativize(s)), StandardCopyOption.REPLACE_EXISTING);
86-
} catch (IOException e) {
87-
throw new UncheckedIOException(e);
88-
}
89-
});
90-
}
9168
}

buildSrc/src/main/java/Utils.java

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import java.io.*;
2+
import java.nio.file.*;
3+
import java.util.*;
4+
import java.util.zip.*;
5+
6+
public class Utils {
7+
8+
public static void rm(Path p) throws IOException {
9+
if (!Files.exists(p)) return;
10+
11+
Files.walk(p).sorted(Comparator.reverseOrder()).forEach(f -> {
12+
try {
13+
Files.delete(f);
14+
} catch (IOException e) {}
15+
});
16+
}
17+
18+
public static void mkdir(Path p) throws IOException {
19+
Files.createDirectories(p);
20+
}
21+
22+
public static void cp(Path src, Path dst) throws IOException {
23+
if (!Files.exists(src)) return;
24+
25+
Files.walk(src).forEach(s -> {
26+
try {
27+
Files.copy(s, dst.resolve(src.relativize(s)), StandardCopyOption.REPLACE_EXISTING);
28+
} catch (IOException e) {
29+
throw new UncheckedIOException(e);
30+
}
31+
});
32+
}
33+
34+
public static void unzip(File zip, File dest) throws IOException {
35+
try (ZipFile zf = new ZipFile(zip)) {
36+
for (ZipEntry e : Collections.list(zf.entries())) {
37+
if (e.isDirectory()) continue;
38+
39+
File f = new File(dest, e.getName());
40+
f.getParentFile().mkdirs();
41+
try (InputStream in = zf.getInputStream(e)) {
42+
Files.copy(in, f.toPath());
43+
}
44+
}
45+
}
46+
}
47+
48+
public static void run(File dir, String... cmd) throws Exception {
49+
Process p = new ProcessBuilder(cmd).directory(dir).inheritIO().start();
50+
if (p.waitFor() != 0) throw new RuntimeException("Command failed: " + String.join(" ", cmd));
51+
}
52+
53+
public static String runOut(File dir, String... cmd) throws Exception {
54+
Process p = new ProcessBuilder(cmd).directory(dir).redirectErrorStream(true).start();
55+
String out = new String(p.getInputStream().readAllBytes());
56+
p.waitFor();
57+
58+
return out;
59+
}
60+
}

0 commit comments

Comments
 (0)