]> git.argeo.org Git - cc0/argeo-build.git/blobdiff - src/org/argeo/build/Make.java
Store source bundles separately
[cc0/argeo-build.git] / src / org / argeo / build / Make.java
index b9fd48d3e96ca08539c74fd53c726b66fa1a0796..97318ab610d5abc44caa4e7df77c9ad7d37d6b31 100644 (file)
@@ -28,6 +28,7 @@ import java.util.Properties;
 import java.util.StringJoiner;
 import java.util.StringTokenizer;
 import java.util.concurrent.CompletableFuture;
+import java.util.jar.Attributes;
 import java.util.jar.JarEntry;
 import java.util.jar.JarOutputStream;
 import java.util.jar.Manifest;
@@ -76,6 +77,8 @@ public class Make {
        final Path buildBase;
        /** The base of the a2 output for all layers. */
        final Path a2Output;
+       /** The base of the a2 sources when packages separately. */
+       final Path a2srcOutput;
 
        /** Whether sources should be packaged separately */
        final boolean sourceBundles;
@@ -101,6 +104,7 @@ public class Make {
                })).toAbsolutePath();
                buildBase = sdkBuildBase.resolve(sdkSrcBase.getFileName());
                a2Output = sdkBuildBase.resolve("a2");
+               a2srcOutput = sdkBuildBase.resolve("a2.src");
        }
 
        /*
@@ -195,12 +199,14 @@ public class Make {
                        throw new IllegalArgumentException("One and only one --category must be specified");
                String category = categories.get(0);
 
+               final String branch;
                Path branchMk = sdkSrcBase.resolve(BRANCH_MK);
-               if (!Files.exists(branchMk))
-                       throw new IllegalStateException("No " + branchMk + " file available");
-               Map<String, String> branchVariables = readeMakefileVariables(branchMk);
-
-               String branch = branchVariables.get("BRANCH");
+               if (Files.exists(branchMk)) {
+                       Map<String, String> branchVariables = readeMakefileVariables(branchMk);
+                       branch = branchVariables.get("BRANCH");
+               } else {
+                       branch = null;
+               }
 
                long begin = System.currentTimeMillis();
                // create jars in parallel
@@ -235,15 +241,19 @@ public class Make {
                        properties.load(in);
                }
 
-               Path branchBnd = sdkSrcBase.resolve("sdk/branches/" + branch + ".bnd");
-               try (InputStream in = Files.newInputStream(branchBnd)) {
-                       properties.load(in);
+               if (branch != null) {
+                       Path branchBnd = sdkSrcBase.resolve("sdk/branches/" + branch + ".bnd");
+                       if (Files.exists(branchBnd))
+                               try (InputStream in = Files.newInputStream(branchBnd)) {
+                                       properties.load(in);
+                               }
                }
 
                Path bndBnd = source.resolve("bnd.bnd");
-               try (InputStream in = Files.newInputStream(bndBnd)) {
-                       properties.load(in);
-               }
+               if (Files.exists(bndBnd))
+                       try (InputStream in = Files.newInputStream(bndBnd)) {
+                               properties.load(in);
+                       }
 
                // Normalise
                if (!properties.containsKey("Bundle-SymbolicName"))
@@ -345,21 +355,40 @@ public class Make {
                        // TODO add effective BND, Eclipse project file, etc., in order to be able to
                        // repackage
                        if (sourceBundles) {
-                               // TODO package sources separately
+                               Path a2srcJarDirectory = bundleParent != null ? a2srcOutput.resolve(bundleParent).resolve(category)
+                                               : a2srcOutput.resolve(category);
+                               Files.createDirectories(a2srcJarDirectory);
+                               Path srcJarP = a2srcJarDirectory
+                                               .resolve(compiled.getFileName() + "." + major + "." + minor + ".src.jar");
+                               Manifest srcManifest = new Manifest();
+                               srcManifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
+                               srcManifest.getMainAttributes().putValue("Bundle-SymbolicName", bundleSymbolicName + ".src");
+                               srcManifest.getMainAttributes().putValue("Bundle-Version",
+                                               manifest.getMainAttributes().getValue("Bundle-Version").toString());
+                               srcManifest.getMainAttributes().putValue("Eclipse-SourceBundle",
+                                               bundleSymbolicName + ";version=\"" + manifest.getMainAttributes().getValue("Bundle-Version"));
+
+                               try (JarOutputStream srcJarOut = new JarOutputStream(Files.newOutputStream(srcJarP), srcManifest)) {
+                                       copySourcesToJar(srcP, srcJarOut, "");
+                               }
                        } else {
-                               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()));
-                                               if (!Files.isDirectory(file))
-                                                       Files.copy(file, jarOut);
-                                               return FileVisitResult.CONTINUE;
-                                       }
-                               });
+                               copySourcesToJar(srcP, jarOut, "OSGI-OPT/src/");
                        }
                }
        }
 
+       void copySourcesToJar(Path srcP, JarOutputStream srcJarOut, String prefix) throws IOException {
+               Files.walkFileTree(srcP, new SimpleFileVisitor<Path>() {
+                       @Override
+                       public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
+                               srcJarOut.putNextEntry(new JarEntry(prefix + srcP.relativize(file).toString()));
+                               if (!Files.isDirectory(file))
+                                       Files.copy(file, srcJarOut);
+                               return FileVisitResult.CONTINUE;
+                       }
+               });
+       }
+
        /**
         * Recursively find the base source directory (which contains the
         * <code>{@value #SDK_MK}</code> file).