]> git.argeo.org Git - cc0/argeo-build.git/blobdiff - src/org/argeo/build/Make.java
Simplify build configuration and support branches
[cc0/argeo-build.git] / src / org / argeo / build / Make.java
index ba085175cd7e601cd1479abefe893055476a2b21..90e97961bf848e3d98065bbd3ebfeb329604febb 100644 (file)
@@ -27,6 +27,7 @@ import java.util.Objects;
 import java.util.Properties;
 import java.util.StringJoiner;
 import java.util.StringTokenizer;
+import java.util.concurrent.CompletableFuture;
 import java.util.jar.JarEntry;
 import java.util.jar.JarOutputStream;
 import java.util.jar.Manifest;
@@ -156,6 +157,9 @@ public class Make {
                        compilerArgs.add(sb.toString());
                }
 
+               if (logger.isLoggable(INFO))
+                       compilerArgs.add("-time");
+
                boolean success = org.eclipse.jdt.core.compiler.batch.BatchCompiler.compile(
                                compilerArgs.toArray(new String[compilerArgs.size()]), new PrintWriter(System.out),
                                new PrintWriter(System.err), new MakeCompilationProgress());
@@ -164,7 +168,7 @@ public class Make {
        }
 
        /** Package the bundles. */
-       void bundle(Map<String, List<String>> options) throws IOException {
+       void bundle(Map<String, List<String>> options) {
                // check arguments
                List<String> bundles = options.get("--bundles");
                Objects.requireNonNull(bundles, "--bundles argument must be set");
@@ -172,21 +176,38 @@ public class Make {
                        return;
 
                List<String> categories = options.get("--category");
-               Objects.requireNonNull(bundles, "--bundles argument must be set");
+               Objects.requireNonNull(bundles, "--category argument must be set");
                if (categories.size() != 1)
-                       throw new IllegalArgumentException("One and only one category must be specified");
+                       throw new IllegalArgumentException("One and only one --category must be specified");
                String category = categories.get(0);
 
-               // create jars
-               for (String bundle : bundles)
-                       createBundle(bundle, category);
+               List<String> branches = options.get("--branch");
+               if (branches.size() != 1)
+                       throw new IllegalArgumentException("One and only one --branch must be specified");
+               String branch = branches.get(0);
+
+               long begin = System.currentTimeMillis();
+               // create jars in parallel
+               List<CompletableFuture<Void>> toDos = new ArrayList<>();
+               for (String bundle : bundles) {
+                       toDos.add(CompletableFuture.runAsync(() -> {
+                               try {
+                                       createBundle(branch, bundle, category);
+                               } catch (IOException e) {
+                                       throw new RuntimeException("Packaging of " + bundle + " failed", e);
+                               }
+                       }));
+               }
+               CompletableFuture.allOf(toDos.toArray(new CompletableFuture[toDos.size()])).join();
+               long duration = System.currentTimeMillis() - begin;
+               logger.log(INFO, "Packaging took " + duration + " ms");
        }
 
        /*
         * UTILITIES
         */
        /** Package a single bundle. */
-       void createBundle(String bundle, String category) throws IOException {
+       void createBundle(String branch, String bundle, String category) throws IOException {
                Path source = execDirectory.resolve(bundle);
                Path compiled = buildBase.resolve(bundle);
                String bundleSymbolicName = source.getFileName().toString();
@@ -197,8 +218,8 @@ public class Make {
                try (InputStream in = Files.newInputStream(argeoBnd)) {
                        properties.load(in);
                }
-               // FIXME make it configurable
-               Path branchBnd = sdkSrcBase.resolve("cnf/unstable.bnd");
+
+               Path branchBnd = sdkSrcBase.resolve("sdk/branches/" + branch + ".bnd");
                try (InputStream in = Files.newInputStream(branchBnd)) {
                        properties.load(in);
                }
@@ -225,10 +246,10 @@ public class Make {
                        throw new RuntimeException("Bnd analysis of " + compiled + " failed", e);
                }
 
-               String major = properties.getProperty("MAJOR");
-               Objects.requireNonNull(major, "MAJOR must be set");
-               String minor = properties.getProperty("MINOR");
-               Objects.requireNonNull(minor, "MINOR must be set");
+               String major = properties.getProperty("major");
+               Objects.requireNonNull(major, "'major' must be set");
+               String minor = properties.getProperty("minor");
+               Objects.requireNonNull(minor, "'minor' must be set");
 
                // Write manifest
                Path manifestP = compiled.resolve("META-INF/MANIFEST.MF");
@@ -254,7 +275,6 @@ public class Make {
                try (JarOutputStream jarOut = new JarOutputStream(Files.newOutputStream(jarP), manifest)) {
                        // add all classes first
                        Files.walkFileTree(binP, new SimpleFileVisitor<Path>() {
-
                                @Override
                                public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                                        jarOut.putNextEntry(new JarEntry(binP.relativize(file).toString()));
@@ -265,7 +285,6 @@ public class Make {
 
                        // add resources
                        Files.walkFileTree(source, new SimpleFileVisitor<Path>() {
-
                                @Override
                                public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
                                        Path relativeP = source.relativize(dir);
@@ -287,7 +306,6 @@ public class Make {
                                        Files.copy(file, jarOut);
                                        return FileVisitResult.CONTINUE;
                                }
-
                        });
 
                        // add sources
@@ -295,7 +313,6 @@ public class Make {
                        // repackage
                        Path srcP = source.resolve("src");
                        Files.walkFileTree(srcP, new SimpleFileVisitor<Path>() {
-
                                @Override
                                public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                                        jarOut.putNextEntry(new JarEntry("OSGI-OPT/src/" + srcP.relativize(file).toString()));
@@ -303,9 +320,7 @@ public class Make {
                                        return FileVisitResult.CONTINUE;
                                }
                        });
-
                }
-
        }
 
        /**
@@ -356,13 +371,12 @@ public class Make {
                        }
 
                        long jvmUptime = ManagementFactory.getRuntimeMXBean().getUptime();
-                       logger.log(INFO, "Make action '" + action + "' succesfully completed after " + (jvmUptime / 1000) + "."
+                       logger.log(INFO, "Make.java action '" + action + "' succesfully completed after " + (jvmUptime / 1000) + "."
                                        + (jvmUptime % 1000) + " s");
                } catch (Exception e) {
                        long jvmUptime = ManagementFactory.getRuntimeMXBean().getUptime();
-                       logger.log(ERROR,
-                                       "Make action '" + action + "' failed after " + (jvmUptime / 1000) + "." + (jvmUptime % 1000) + " s",
-                                       e);
+                       logger.log(ERROR, "Make.java action '" + action + "' failed after " + (jvmUptime / 1000) + "."
+                                       + (jvmUptime % 1000) + " s", e);
                        System.exit(1);
                }
        }
@@ -371,10 +385,9 @@ public class Make {
         * An ECJ {@link CompilationProgress} printing a progress bar while compiling.
         */
        static class MakeCompilationProgress extends CompilationProgress {
-               int totalWork;
-               long currentChunk = 0;
-
-               long chunksCount = 80;
+               private int totalWork;
+               private long currentChunk = 0;
+               private long chunksCount = 80;
 
                @Override
                public void worked(int workIncrement, int remainingWork) {
@@ -412,7 +425,5 @@ public class Make {
                public void begin(int remainingWork) {
                        this.totalWork = remainingWork;
                }
-
        }
-
 }