Showing the changes to java/tools/genpkg/UpdateJavaPackages.java (1 of 2 files). Show all
Unified · Split
| @@ -0,0 +1,147 @@ | ||
| 1 | + | |
| 2 | +import java.io.*; | |
| 3 | + | |
| 4 | +public class UpdateJavaPackages { | |
| 5 | + | |
| 6 | + private File sourcePath; | |
| 7 | + | |
| 8 | + UpdateJavaPackages(String sourcePath) { | |
| 9 | + this.sourcePath = new File(sourcePath); | |
| 10 | + if (!this.sourcePath.exists() || !this.sourcePath.isDirectory()) | |
| 11 | + throw new IllegalArgumentException("Bad source path!"); | |
| 12 | + } | |
| 13 | + | |
| 14 | + private String relativePath(File base, File file) { | |
| 15 | + if (file.getAbsolutePath().equals(base.getAbsolutePath())) | |
| 16 | + return "/"; | |
| 17 | + return file.getAbsolutePath().substring(base.getAbsolutePath().length() + 1); | |
| 18 | + } | |
| 19 | + | |
| 20 | + private int getDirLevel(File path) { | |
| 21 | + return relativePath(sourcePath, path).split("/").length; | |
| 22 | + } | |
| 23 | + | |
| 24 | + private String generateTargSection(File[] files) { | |
| 25 | + boolean success = false; | |
| 26 | + StringBuilder result = new StringBuilder(); | |
| 27 | + result.append("TARG=\\").append('\n'); | |
| 28 | + for (File f: files) { | |
| 29 | + if (f.isFile() && f.getName().endsWith(".java")) { | |
| 30 | + String className = f.getName(); | |
| 31 | + // strip .java extension | |
| 32 | + className = className.substring(0, className.lastIndexOf('.')); | |
| 33 | + // add to mkfile | |
| 34 | + result.append('\t').append(className).append(".dis\\\n"); | |
| 35 | + success = true; | |
| 36 | + } | |
| 37 | + } | |
| 38 | + if (!success) | |
| 39 | + return null; | |
| 40 | + return result.toString(); | |
| 41 | + } | |
| 42 | + private String generateDirsSection(File[] files) { | |
| 43 | + boolean success = false; | |
| 44 | + StringBuilder result = new StringBuilder(); | |
| 45 | + result.append("DIRS=\\").append('\n'); | |
| 46 | + for (File f: files) { | |
| 47 | + if (f.isDirectory()) { | |
| 48 | + result.append('\t').append(f.getName()).append("\\\n"); | |
| 49 | + success = true; | |
| 50 | + } | |
| 51 | + } | |
| 52 | + if (!success) | |
| 53 | + return null; | |
| 54 | + return result.toString(); | |
| 55 | + } | |
| 56 | + | |
| 57 | + private void updateMkfile(File file) { | |
| 58 | + // a bit hacky... | |
| 59 | + file.delete(); | |
| 60 | + createMkfile(file); | |
| 61 | + } | |
| 62 | + | |
| 63 | + private void createMkfile(File file) { | |
| 64 | + try { | |
| 65 | + File dir = file.getParentFile(); | |
| 66 | + PrintWriter writer = new PrintWriter(file); | |
| 67 | + | |
| 68 | + writer.print("<"); | |
| 69 | + // assume we have config in two folders higher | |
| 70 | + int level = getDirLevel(dir) + 2; | |
| 71 | + while (level-- > 0) | |
| 72 | + writer.print("../"); | |
| 73 | + writer.print("mkconfig"); | |
| 74 | + writer.println(); | |
| 75 | + writer.println(); | |
| 76 | + File[] files = dir.listFiles(); | |
| 77 | + | |
| 78 | + String dirs = generateDirsSection(files); | |
| 79 | + if (dirs != null) | |
| 80 | + writer.println(dirs); | |
| 81 | + | |
| 82 | + String targ = generateTargSection(files); | |
| 83 | + if (targ != null) | |
| 84 | + writer.println(targ); | |
| 85 | + | |
| 86 | + if (targ != null) { | |
| 87 | + writer.println("DISBIN=$ROOT/java/dis/java/java/" + relativePath(sourcePath, dir)); | |
| 88 | + writer.println(); | |
| 89 | + writer.println("<$ROOT/mkfiles/mkjava"); | |
| 90 | + } | |
| 91 | + | |
| 92 | + if (dirs != null) | |
| 93 | + writer.println("<$ROOT/mkfiles/mksubdirs"); | |
| 94 | + | |
| 95 | + if (dirs == null && targ == null) { | |
| 96 | + // Dummy entry to do anything | |
| 97 | + writer.println("install:"); | |
| 98 | + writer.println("\techo"); | |
| 99 | + } | |
| 100 | + | |
| 101 | + writer.close(); | |
| 102 | + } catch (FileNotFoundException e) { | |
| 103 | + e.printStackTrace(); | |
| 104 | + } | |
| 105 | + } | |
| 106 | + | |
| 107 | + private void processDir(File path) { | |
| 108 | + System.out.println("Processing " + path.getPath()); | |
| 109 | + File[] files = path.listFiles(); | |
| 110 | + | |
| 111 | + // walk all the dirs in source folder | |
| 112 | + for (File f: files) | |
| 113 | + if (f.isDirectory()) | |
| 114 | + processDir(f); | |
| 115 | + | |
| 116 | + File mkfile = new File(path.getPath() + "/mkfile"); | |
| 117 | + if (!mkfile.exists()) | |
| 118 | + createMkfile(mkfile); | |
| 119 | + else | |
| 120 | + updateMkfile(mkfile); | |
| 121 | + } | |
| 122 | + public void run() { | |
| 123 | + processDir(sourcePath); | |
| 124 | + } | |
| 125 | + | |
| 126 | + public static void main(String[] args) { | |
| 127 | + if (args.length == 0) { | |
| 128 | + System.out.println("Generates or updates mkfiles for java"); | |
| 129 | + System.out.println("packages in inferno java subsystem."); | |
| 130 | + System.out.println(); | |
| 131 | + System.out.println("Usage: <source> <target>"); | |
| 132 | + System.out.println("Usage: <source>"); | |
| 133 | + System.out.println("\tsource - the path to unpacked src.zip from jdk"); | |
| 134 | + System.out.println("\ttarget - absolute path to /java/pkg/ in inferno root dir"); | |
| 135 | + return; | |
| 136 | + } | |
| 137 | + | |
| 138 | + try { | |
| 139 | + UpdateJavaPackages updater = new UpdateJavaPackages(args[0]); | |
| 140 | + updater.run(); | |
| 141 | + } | |
| 142 | + catch (IllegalArgumentException e) { | |
| 143 | + System.out.println("Error: " + e.getMessage()); | |
| 144 | + } | |
| 145 | + } | |
| 146 | + | |
| 147 | +} | |