|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2024, 2024 Hannes Wellmann and others. |
| 3 | + * |
| 4 | + * This program and the accompanying materials |
| 5 | + * are made available under the terms of the Eclipse Public License 2.0 |
| 6 | + * which accompanies this distribution, and is available at |
| 7 | + * https://www.eclipse.org/legal/epl-2.0/ |
| 8 | + * |
| 9 | + * SPDX-License-Identifier: EPL-2.0 |
| 10 | + * |
| 11 | + * Contributors: |
| 12 | + * Hannes Wellmann - initial API and implementation |
| 13 | + *******************************************************************************/ |
| 14 | +import java.io.*; |
| 15 | +import java.nio.file.*; |
| 16 | +import java.util.*; |
| 17 | + |
| 18 | +/** |
| 19 | + * Script to collect the SWT native sources for the specified native fragment. |
| 20 | + * <p> |
| 21 | + * In order to be able to launch this directly as source script <b>only |
| 22 | + * reference standard java classes!</b> |
| 23 | + * </p> |
| 24 | + */ |
| 25 | +public class CollectSources { |
| 26 | + |
| 27 | + private record ScriptEnvironment(Path swtProjectRoot, Path targetDirectory, String ws, String arch) { |
| 28 | + private static ScriptEnvironment read(String[] args) { |
| 29 | + if (args.length != 2) { |
| 30 | + throw new IllegalArgumentException("task and target directory must be defined as only argument"); |
| 31 | + } |
| 32 | + Path swtProjectRoot = Path.of("").toAbsolutePath(); |
| 33 | + if (!swtProjectRoot.endsWith(Path.of("bundles/org.eclipse.swt"))) { // Consistency check |
| 34 | + throw new IllegalStateException("Sript must be excuted from org.eclipse.swt project"); |
| 35 | + } |
| 36 | + Path targetDirectory = Path.of(args[1]); |
| 37 | + String ws = System.getProperty("ws"); |
| 38 | + String arch = System.getProperty("arch"); |
| 39 | + return new ScriptEnvironment(swtProjectRoot, targetDirectory, ws, arch); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + public static void main(String[] args) throws Exception { |
| 44 | + ScriptEnvironment env = ScriptEnvironment.read(args); |
| 45 | + switch (args[0]) { |
| 46 | + case "-nativeSources": |
| 47 | + collectNativeSources(env); |
| 48 | + break; |
| 49 | + default: |
| 50 | + throw new IllegalArgumentException("Unexpected value: " + args[0]); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + private static void collectNativeSources(ScriptEnvironment env) throws IOException { |
| 55 | + Path props = env.swtProjectRoot.resolve("nativeSourceFolders.properties"); |
| 56 | + Map<String, String> sources = loadProperty(props); |
| 57 | + |
| 58 | + String commonSources = sources.get("src_common"); |
| 59 | + String nativeSources = sources.get("src_" + env.ws); |
| 60 | + List<String> allSources = Arrays.asList((commonSources + "," + nativeSources).split(",")); |
| 61 | + System.out.println("Copy " + allSources.size() + " java source folders for " + env.ws + "." + env.arch); |
| 62 | + copySubDirectories(env.swtProjectRoot, allSources, env.targetDirectory); |
| 63 | + } |
| 64 | + |
| 65 | + @SuppressWarnings({ "unchecked", "rawtypes" }) |
| 66 | + private static Map<String, String> loadProperty(Path path) throws IOException { |
| 67 | + try (InputStream in = Files.newInputStream(path)) { |
| 68 | + Properties props = new Properties(); |
| 69 | + props.load(in); |
| 70 | + return (Map) props; |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + private static void copySubDirectories(Path root, Collection<String> allSources, Path target) throws IOException { |
| 75 | + System.out.println("from " + root + "\nto " + target); |
| 76 | + for (String srcPath : allSources) { |
| 77 | + Path srcFolder = root.resolve(srcPath); |
| 78 | + try (var files = Files.walk(srcFolder).filter(Files::isRegularFile)) { |
| 79 | + for (Path sourceFile : (Iterable<Path>) files::iterator) { |
| 80 | + Path targetFile = target.resolve(srcFolder.relativize(sourceFile)); |
| 81 | + Files.createDirectories(targetFile.getParent()); |
| 82 | + Files.copy(sourceFile, targetFile, StandardCopyOption.REPLACE_EXISTING); |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | +} |
0 commit comments