+ List<String> targetDirs = options.get("--target");
+ Objects.requireNonNull(targetDirs, "--target argument must be set");
+ if (targetDirs.size() != 1)
+ throw new IllegalArgumentException("One and only one --target must be specified");
+ Path targetA2 = Paths.get(targetDirs.get(0));
+ logger.log(INFO, (uninstall ? "Uninstalling from " : "Installing to ") + targetA2);
+
+ final String branch;
+ Path branchMk = sdkSrcBase.resolve(BRANCH_MK);
+ if (Files.exists(branchMk)) {
+ Map<String, String> branchVariables = readMakefileVariables(branchMk);
+ branch = branchVariables.get(VAR_BRANCH);
+ } else {
+ throw new IllegalArgumentException(VAR_BRANCH + " variable must be set.");
+ }
+
+ Properties properties = new Properties();
+ Path branchBnd = sdkSrcBase.resolve("sdk/branches/" + branch + ".bnd");
+ if (Files.exists(branchBnd))
+ try (InputStream in = Files.newInputStream(branchBnd)) {
+ properties.load(in);
+ }
+ String major = properties.getProperty("major");
+ Objects.requireNonNull(major, "'major' must be set");
+ String minor = properties.getProperty("minor");
+ Objects.requireNonNull(minor, "'minor' must be set");
+
+ int count = 0;
+ for (String bundle : bundles) {
+ Path bundlePath = Paths.get(bundle);
+ Path bundleParent = bundlePath.getParent();
+ Path a2JarDirectory = bundleParent != null ? a2Output.resolve(bundleParent).resolve(category)
+ : a2Output.resolve(category);
+ Path jarP = a2JarDirectory.resolve(bundlePath.getFileName() + "." + major + "." + minor + ".jar");
+
+ Path targetJarP = targetA2.resolve(a2Output.relativize(jarP));
+ if (uninstall) { // uninstall
+ if (Files.exists(targetJarP)) {
+ Files.delete(targetJarP);
+ logger.log(DEBUG, "Removed " + targetJarP);
+ count++;
+ }
+ Path targetParent = targetJarP.getParent();
+ deleteEmptyParents(targetA2, targetParent);
+ } else { // install
+ Files.createDirectories(targetJarP.getParent());
+ boolean update = Files.exists(targetJarP);
+ Files.copy(jarP, targetJarP, StandardCopyOption.REPLACE_EXISTING);
+ logger.log(DEBUG, (update ? "Updated " : "Installed ") + targetJarP);
+ count++;
+ }
+ }
+ logger.log(INFO, uninstall ? count + " bundles removed" : count + " bundles installed or updated");
+ }
+
+ /** Delete empty parent directory up to the A2 target (included). */
+ void deleteEmptyParents(Path targetA2, Path targetParent) throws IOException {
+ if (!Files.isDirectory(targetParent))
+ throw new IllegalArgumentException(targetParent + " must be a directory");
+ boolean isA2target = Files.isSameFile(targetA2, targetParent);
+ if (!Files.list(targetParent).iterator().hasNext()) {
+ Files.delete(targetParent);
+ if (isA2target)
+ return;// stop after deleting A2 base
+ deleteEmptyParents(targetA2, targetParent.getParent());
+ }