11import java .io .*;
22import java .nio .file .*;
3- import java .util .*;
43import java .util .zip .*;
54
65public 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}
0 commit comments