|
| 1 | +import java.io.*; |
| 2 | +import java.nio.file.*; |
| 3 | +import java.util.*; |
| 4 | +import java.util.jar.*; |
| 5 | +import java.util.regex.*; |
| 6 | + |
| 7 | +/** |
| 8 | + * Extracts the original (traditional) Eclipse icons referenced in |
| 9 | + * iconpacks/eclipse-dual-tone/icon-mapping.json from an existing Eclipse |
| 10 | + * installation, storing them under iconpacks/eclipse-traditional-icons/. |
| 11 | + * |
| 12 | + * Also produces icon-restore-mapping.json in that folder, which maps every |
| 13 | + * Eclipse-relative icon path back to the dual-tone icon name that replaced it. |
| 14 | + * A restore script can use that file to undo the dual-tone replacement. |
| 15 | + * |
| 16 | + * Usage: |
| 17 | + * java ExtractTraditionalIcons.java <eclipse-dir> [icon-mapping.json] [output-dir] |
| 18 | + * |
| 19 | + * Defaults: |
| 20 | + * icon-mapping.json -> iconpacks/eclipse-dual-tone/icon-mapping.json |
| 21 | + * output-dir -> iconpacks/eclipse-traditional-icons |
| 22 | + */ |
| 23 | +public class ExtractTraditionalIcons { |
| 24 | + |
| 25 | + public static void main(String[] args) throws Exception { |
| 26 | + if (args.length < 1) { |
| 27 | + System.err.println("Usage: java ExtractTraditionalIcons.java <eclipse-dir> [icon-mapping.json] [output-dir]"); |
| 28 | + System.exit(1); |
| 29 | + } |
| 30 | + |
| 31 | + Path eclipseDir = Path.of(args[0]); |
| 32 | + Path mappingFile = Path.of(args.length > 1 ? args[1] : "iconpacks/eclipse-dual-tone/icon-mapping.json"); |
| 33 | + Path outputDir = Path.of(args.length > 2 ? args[2] : "iconpacks/eclipse-traditional-icons"); |
| 34 | + Path pluginsDir = eclipseDir.resolve("plugins"); |
| 35 | + |
| 36 | + if (!Files.isDirectory(pluginsDir)) { |
| 37 | + System.err.println("Eclipse plugins directory not found: " + pluginsDir); |
| 38 | + System.exit(1); |
| 39 | + } |
| 40 | + |
| 41 | + System.out.println("Eclipse dir : " + eclipseDir); |
| 42 | + System.out.println("Mapping file : " + mappingFile); |
| 43 | + System.out.println("Output dir : " + outputDir); |
| 44 | + System.out.println(); |
| 45 | + |
| 46 | + Map<String, List<String>> iconMapping = parseIconMapping(mappingFile); |
| 47 | + |
| 48 | + // Collect all unique Eclipse icon paths (values in the mapping). |
| 49 | + // Preserve insertion order so the output mapping stays readable. |
| 50 | + Set<String> allEclipsePaths = new LinkedHashSet<>(); |
| 51 | + for (List<String> paths : iconMapping.values()) { |
| 52 | + allEclipsePaths.addAll(paths); |
| 53 | + } |
| 54 | + |
| 55 | + Files.createDirectories(outputDir); |
| 56 | + |
| 57 | + // Index: pluginId -> matching path (dir or jar) in the Eclipse plugins folder. |
| 58 | + // Built lazily and cached to avoid repeated directory scans. |
| 59 | + Map<String, Path> pluginCache = new HashMap<>(); |
| 60 | + List<Path> pluginEntries = Files.list(pluginsDir).toList(); |
| 61 | + |
| 62 | + int extracted = 0; |
| 63 | + int missing = 0; |
| 64 | + |
| 65 | + // restoreMapping: eclipsePath -> dualToneIconName |
| 66 | + Map<String, String> restoreMapping = new LinkedHashMap<>(); |
| 67 | + |
| 68 | + // Build the dual-tone lookup once (eclipse path -> dual-tone key) |
| 69 | + Map<String, String> eclipsePathToDualTone = new LinkedHashMap<>(); |
| 70 | + for (Map.Entry<String, List<String>> entry : iconMapping.entrySet()) { |
| 71 | + for (String eclipsePath : entry.getValue()) { |
| 72 | + eclipsePathToDualTone.put(eclipsePath, entry.getKey()); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + for (String eclipsePath : allEclipsePaths) { |
| 77 | + int slash = eclipsePath.indexOf('/'); |
| 78 | + if (slash < 0) { |
| 79 | + System.err.println("Skipping malformed path: " + eclipsePath); |
| 80 | + continue; |
| 81 | + } |
| 82 | + String pluginId = eclipsePath.substring(0, slash); |
| 83 | + String iconRelPath = eclipsePath.substring(slash + 1); |
| 84 | + |
| 85 | + Path pluginEntry = pluginCache.computeIfAbsent(pluginId, |
| 86 | + id -> findPlugin(pluginEntries, id)); |
| 87 | + |
| 88 | + if (pluginEntry == null) { |
| 89 | + System.err.println(" MISSING plugin : " + pluginId); |
| 90 | + missing++; |
| 91 | + continue; |
| 92 | + } |
| 93 | + |
| 94 | + Path dest = outputDir.resolve(eclipsePath); |
| 95 | + boolean ok; |
| 96 | + if (Files.isDirectory(pluginEntry)) { |
| 97 | + ok = extractFromDirectory(pluginEntry, iconRelPath, dest); |
| 98 | + } else { |
| 99 | + ok = extractFromJar(pluginEntry, iconRelPath, dest); |
| 100 | + } |
| 101 | + |
| 102 | + if (ok) { |
| 103 | + restoreMapping.put(eclipsePath, eclipsePathToDualTone.get(eclipsePath)); |
| 104 | + System.out.println(" OK " + eclipsePath); |
| 105 | + extracted++; |
| 106 | + } else { |
| 107 | + System.err.println(" MISSING icon : " + eclipsePath + " (in " + pluginEntry.getFileName() + ")"); |
| 108 | + missing++; |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + Path restoreMappingFile = outputDir.resolve("icon-restore-mapping.json"); |
| 113 | + writeRestoreMapping(restoreMappingFile, restoreMapping); |
| 114 | + |
| 115 | + System.out.println(); |
| 116 | + System.out.println("Extracted : " + extracted + " / " + allEclipsePaths.size()); |
| 117 | + System.out.println("Missing : " + missing); |
| 118 | + System.out.println("Restore mapping -> " + restoreMappingFile); |
| 119 | + } |
| 120 | + |
| 121 | + // ------------------------------------------------------------------------- |
| 122 | + // Plugin lookup |
| 123 | + // ------------------------------------------------------------------------- |
| 124 | + |
| 125 | + /** Finds the first directory or JAR in {@code entries} whose name matches {@code pluginId}. */ |
| 126 | + private static Path findPlugin(List<Path> entries, String pluginId) { |
| 127 | + for (Path entry : entries) { |
| 128 | + String name = entry.getFileName().toString(); |
| 129 | + // Matches: "org.eclipse.foo", "org.eclipse.foo_1.2.3.jar", "org.eclipse.foo_1.2.3/" |
| 130 | + if (name.equals(pluginId) |
| 131 | + || name.startsWith(pluginId + "_") |
| 132 | + || name.equals(pluginId + ".jar")) { |
| 133 | + return entry; |
| 134 | + } |
| 135 | + } |
| 136 | + return null; |
| 137 | + } |
| 138 | + |
| 139 | + // ------------------------------------------------------------------------- |
| 140 | + // Extraction helpers |
| 141 | + // ------------------------------------------------------------------------- |
| 142 | + |
| 143 | + private static boolean extractFromDirectory(Path pluginDir, String iconRelPath, Path dest) throws IOException { |
| 144 | + Path source = pluginDir.resolve(iconRelPath); |
| 145 | + if (!Files.exists(source)) return false; |
| 146 | + Files.createDirectories(dest.getParent()); |
| 147 | + Files.copy(source, dest, StandardCopyOption.REPLACE_EXISTING); |
| 148 | + return true; |
| 149 | + } |
| 150 | + |
| 151 | + private static boolean extractFromJar(Path jarPath, String iconRelPath, Path dest) throws IOException { |
| 152 | + try (JarFile jar = new JarFile(jarPath.toFile())) { |
| 153 | + JarEntry entry = jar.getJarEntry(iconRelPath); |
| 154 | + if (entry == null) return false; |
| 155 | + Files.createDirectories(dest.getParent()); |
| 156 | + try (InputStream is = jar.getInputStream(entry)) { |
| 157 | + Files.copy(is, dest, StandardCopyOption.REPLACE_EXISTING); |
| 158 | + } |
| 159 | + return true; |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + // ------------------------------------------------------------------------- |
| 164 | + // JSON parsing (no external library required) |
| 165 | + // ------------------------------------------------------------------------- |
| 166 | + |
| 167 | + /** |
| 168 | + * Parses the icon-mapping.json format: |
| 169 | + * <pre> |
| 170 | + * { |
| 171 | + * "key": ["path/a.svg", "path/b.svg"], |
| 172 | + * ... |
| 173 | + * } |
| 174 | + * </pre> |
| 175 | + */ |
| 176 | + private static Map<String, List<String>> parseIconMapping(Path file) throws IOException { |
| 177 | + String content = Files.readString(file); |
| 178 | + Map<String, List<String>> result = new LinkedHashMap<>(); |
| 179 | + |
| 180 | + // Match each "key": [...] block |
| 181 | + Pattern blockPattern = Pattern.compile( |
| 182 | + "\"([^\"]+)\"\\s*:\\s*\\[([^\\]]*?)\\]", |
| 183 | + Pattern.DOTALL); |
| 184 | + Pattern stringPattern = Pattern.compile("\"([^\"]+)\""); |
| 185 | + |
| 186 | + Matcher blockMatcher = blockPattern.matcher(content); |
| 187 | + while (blockMatcher.find()) { |
| 188 | + String key = blockMatcher.group(1); |
| 189 | + String arrayStr = blockMatcher.group(2); |
| 190 | + List<String> values = new ArrayList<>(); |
| 191 | + Matcher valMatcher = stringPattern.matcher(arrayStr); |
| 192 | + while (valMatcher.find()) { |
| 193 | + values.add(valMatcher.group(1)); |
| 194 | + } |
| 195 | + result.put(key, values); |
| 196 | + } |
| 197 | + return result; |
| 198 | + } |
| 199 | + |
| 200 | + // ------------------------------------------------------------------------- |
| 201 | + // Output mapping |
| 202 | + // ------------------------------------------------------------------------- |
| 203 | + |
| 204 | + /** |
| 205 | + * Writes icon-restore-mapping.json. |
| 206 | + * Format: { "eclipse/plugin/path/icon.svg": "dual-tone-icon-name.svg", ... } |
| 207 | + * |
| 208 | + * A restore script reads this file and, for each entry: |
| 209 | + * - copies eclipse-traditional-icons/<key> |
| 210 | + * - back to Eclipse plugins/<pluginId_version>/<iconRelPath> |
| 211 | + */ |
| 212 | + private static void writeRestoreMapping(Path outputFile, Map<String, String> mapping) throws IOException { |
| 213 | + StringBuilder sb = new StringBuilder("{\n"); |
| 214 | + int i = 0; |
| 215 | + for (Map.Entry<String, String> entry : mapping.entrySet()) { |
| 216 | + if (i++ > 0) sb.append(",\n"); |
| 217 | + sb.append(" \"").append(entry.getKey()).append("\": \"").append(entry.getValue()).append("\""); |
| 218 | + } |
| 219 | + sb.append("\n}\n"); |
| 220 | + Files.writeString(outputFile, sb.toString()); |
| 221 | + } |
| 222 | +} |
0 commit comments