X-Git-Url: https://git.argeo.org/?a=blobdiff_plain;f=src%2Forg%2Fargeo%2Fbuild%2FRepackage.java;h=7513dc5928f0ad946ff4f80a6f7ae08fa2ae58d5;hb=af5b640e4fa16c3d5ed59a1fd3b1ffcdc37bf4fb;hp=c6919d791eb5274c8c942ce02193d1e4ffb0d120;hpb=8fe681190eeffad79e8ebe122be421352f5fcb7c;p=cc0%2Fargeo-build.git diff --git a/src/org/argeo/build/Repackage.java b/src/org/argeo/build/Repackage.java index c6919d7..7513dc5 100644 --- a/src/org/argeo/build/Repackage.java +++ b/src/org/argeo/build/Repackage.java @@ -1,6 +1,7 @@ package org.argeo.build; import static java.lang.System.Logger.Level.DEBUG; +import static java.lang.System.Logger.Level.WARNING; import static org.argeo.build.Repackage.ManifestConstants.BUNDLE_SYMBOLICNAME; import static org.argeo.build.Repackage.ManifestConstants.BUNDLE_VERSION; import static org.argeo.build.Repackage.ManifestConstants.EXPORT_PACKAGE; @@ -53,6 +54,8 @@ public class Repackage { private final static String ENV_BUILD_SOURCE_BUNDLES = "BUILD_SOURCE_BUNDLES"; + private final static boolean parallel = true; + /** Main entry point. */ public static void main(String[] args) { if (args.length < 2) { @@ -66,7 +69,10 @@ public class Repackage { List> toDos = new ArrayList<>(); for (int i = 1; i < args.length; i++) { Path p = Paths.get(args[i]); - toDos.add(CompletableFuture.runAsync(() -> factory.processCategory(p))); + if (parallel) + toDos.add(CompletableFuture.runAsync(() -> factory.processCategory(p))); + else + factory.processCategory(p); } CompletableFuture.allOf(toDos.toArray(new CompletableFuture[toDos.size()])).join(); } @@ -185,7 +191,7 @@ public class Repackage { throw new IllegalArgumentException("No M2 coordinates available for " + bndFile); M2Artifact artifact = new M2Artifact(m2Coordinates); URL url = M2ConventionsUtils.mavenRepoUrl(repoStr, artifact); - Path downloaded = download(url, originBase, artifact); + Path downloaded = downloadMaven(url, originBase, artifact); Path targetBundleDir = processBndJar(downloaded, targetCategoryBase, fileProps, artifact); @@ -271,7 +277,7 @@ public class Repackage { // download URL url = M2ConventionsUtils.mavenRepoUrl(repoStr, artifact); - Path downloaded = download(url, originBase, artifact); + Path downloaded = downloadMaven(url, originBase, artifact); Path targetBundleDir = processBndJar(downloaded, targetCategoryBase, mergeProps, artifact); // logger.log(Level.DEBUG, () -> "Processed " + downloaded); @@ -334,7 +340,7 @@ public class Repackage { if (artifact.getVersion() == null) artifact.setVersion(m2Version); URL url = M2ConventionsUtils.mavenRepoUrl(repoStr, artifact); - Path downloaded = download(url, originBase, artifact); + Path downloaded = downloadMaven(url, originBase, artifact); JarEntry entry; try (JarInputStream jarIn = new JarInputStream(Files.newInputStream(downloaded), false)) { entries: while ((entry = jarIn.getNextJarEntry()) != null) { @@ -510,12 +516,12 @@ public class Repackage { /** Download and integrates sources for a single Maven artifact. */ protected void downloadAndProcessM2Sources(String repoStr, M2Artifact artifact, Path targetBundleDir) throws IOException { - if (sourceBundles) - return; +// if (sourceBundles) +// return; try { M2Artifact sourcesArtifact = new M2Artifact(artifact.toM2Coordinates(), "sources"); URL sourcesUrl = M2ConventionsUtils.mavenRepoUrl(repoStr, sourcesArtifact); - Path sourcesDownloaded = download(sourcesUrl, originBase, artifact, true); + Path sourcesDownloaded = downloadMaven(sourcesUrl, originBase, artifact, true); processM2SourceJar(sourcesDownloaded, targetBundleDir); logger.log(Level.TRACE, () -> "Processed source " + sourcesDownloaded); } catch (Exception e) { @@ -527,7 +533,9 @@ public class Repackage { /** Integrate sources from a downloaded jar file. */ protected void processM2SourceJar(Path file, Path targetBundleDir) throws IOException { try (JarInputStream jarIn = new JarInputStream(Files.newInputStream(file), false)) { - Path targetSourceDir = targetBundleDir.resolve("OSGI-OPT/src"); + Path targetSourceDir = sourceBundles + ? targetBundleDir.getParent().resolve(targetBundleDir.toString() + ".src") + : targetBundleDir.resolve("OSGI-OPT/src"); // TODO make it less dangerous? if (Files.exists(targetSourceDir)) { @@ -561,12 +569,12 @@ public class Repackage { } /** Download a Maven artifact. */ - protected Path download(URL url, Path dir, M2Artifact artifact) throws IOException { - return download(url, dir, artifact, false); + protected Path downloadMaven(URL url, Path dir, M2Artifact artifact) throws IOException { + return downloadMaven(url, dir, artifact, false); } /** Download a Maven artifact. */ - protected Path download(URL url, Path dir, M2Artifact artifact, boolean sources) throws IOException { + protected Path downloadMaven(URL url, Path dir, M2Artifact artifact, boolean sources) throws IOException { return download(url, dir, artifact.getGroupId() + '/' + artifact.getArtifactId() + "-" + artifact.getVersion() + (sources ? "-sources" : "") + ".jar"); } @@ -579,7 +587,6 @@ public class Repackage { public void processEclipseArchive(Path duDir) { try { Path categoryRelativePath = descriptorsBase.relativize(duDir.getParent()); - // String category = categoryRelativePath.getFileName().toString(); Path targetCategoryBase = a2Base.resolve(categoryRelativePath); Files.createDirectories(targetCategoryBase); // first delete all directories from previous builds @@ -601,7 +608,7 @@ public class Repackage { throw new IllegalStateException("No url available for " + duDir); commonProps.put(ManifestConstants.SLC_ORIGIN_URI.toString(), url); } - Path downloaded = tryDownload(url, originBase); + Path downloaded = tryDownloadArchive(url, originBase); FileSystem zipFs = FileSystems.newFileSystem(downloaded, (ClassLoader) null); @@ -642,10 +649,10 @@ public class Repackage { } } if (file.getFileName().toString().contains(".source_")) { - if (!sourceBundles) { - processEclipseSourceJar(file, targetCategoryBase); - logger.log(Level.DEBUG, () -> "Processed source " + file); - } +// if (!sourceBundles) { + processEclipseSourceJar(file, targetCategoryBase); + logger.log(Level.DEBUG, () -> "Processed source " + file); +// } } else { Map map = new HashMap<>(); @@ -661,8 +668,8 @@ public class Repackage { } }); - DirectoryStream dirs = Files.newDirectoryStream(targetCategoryBase, - (p) -> Files.isDirectory(p) && p.getFileName().toString().indexOf('.') >= 0); + DirectoryStream dirs = Files.newDirectoryStream(targetCategoryBase, (p) -> Files.isDirectory(p) + && p.getFileName().toString().indexOf('.') >= 0 && !p.getFileName().toString().endsWith(".src")); for (Path dir : dirs) { createJar(dir); } @@ -685,7 +692,9 @@ public class Repackage { NameVersion nameVersion = new NameVersion(relatedBundle[0], version); targetBundleDir = targetBase.resolve(nameVersion.getName() + "." + nameVersion.getBranch()); - Path targetSourceDir = targetBundleDir.resolve("OSGI-OPT/src"); + Path targetSourceDir = sourceBundles + ? targetBundleDir.getParent().resolve(targetBundleDir.toString() + ".src") + : targetBundleDir.resolve("OSGI-OPT/src"); // TODO make it less dangerous? if (Files.exists(targetSourceDir)) { @@ -903,7 +912,7 @@ public class Repackage { } /** Try to download from an URI. */ - protected Path tryDownload(String uri, Path dir) throws IOException { + protected Path tryDownloadArchive(String uri, Path dir) throws IOException { // find mirror List urlBases = null; String uriPrefix = null; @@ -918,7 +927,7 @@ public class Repackage { } if (urlBases == null) try { - return download(new URL(uri), dir); + return downloadArchive(new URL(uri), dir); } catch (FileNotFoundException e) { throw new FileNotFoundException("Cannot find " + uri); } @@ -928,7 +937,7 @@ public class Repackage { String relativePath = uri.substring(uriPrefix.length()); URL url = new URL(urlBase + relativePath); try { - return download(url, dir); + return downloadArchive(url, dir); } catch (FileNotFoundException e) { logger.log(Level.WARNING, "Cannot download " + url + ", trying another mirror"); } @@ -936,25 +945,27 @@ public class Repackage { throw new FileNotFoundException("Cannot find " + uri); } - /** Effectively download. */ - protected Path download(URL url, Path dir) throws IOException { - return download(url, dir, (String) null); - } - /** - * Effectively download. Synchronized in order to avoid downloading twice in + * Effectively download. Synchronised in order to avoid downloading twice in * parallel. */ - protected synchronized Path download(URL url, Path dir, String name) throws IOException { + protected synchronized Path downloadArchive(URL url, Path dir) throws IOException { + return download(url, dir, (String) null); + } + + /** Effectively download. */ + protected Path download(URL url, Path dir, String name) throws IOException { Path dest; if (name == null) { - name = url.getPath().substring(url.getPath().lastIndexOf('/') + 1); + // We use also use parent directory in case the archive itself has a fixed name + String[] segments = url.getPath().split("/"); + name = segments.length > 1 ? segments[segments.length - 2] + '-' + segments[segments.length - 1] + : segments[segments.length - 1]; } dest = dir.resolve(name); if (Files.exists(dest)) { - // FIXME If new archives have the same name (e.g. nebula-latest.zip) they will be skipped logger.log(Level.TRACE, () -> "File " + dest + " already exists for " + url + ", not downloading again"); return dest; } else { @@ -995,6 +1006,50 @@ public class Repackage { }); } deleteDirectory(bundleDir); + + if (sourceBundles) { + Path bundleCategoryDir = bundleDir.getParent(); + Path sourceDir = bundleCategoryDir.resolve(bundleDir.toString() + ".src"); + if (!Files.exists(sourceDir)) { + logger.log(WARNING, sourceDir + " does not exist, skipping..."); + return jarPath; + + } + Path guessedA2Base = bundleCategoryDir.getParent(); + Path srcA2Base = guessedA2Base.getParent().resolve(guessedA2Base.getFileName() + ".src"); + Path srcJarP = srcA2Base.resolve(bundleCategoryDir.getFileName()).resolve(sourceDir.getFileName() + ".jar"); + + String bundleSymbolicName = manifest.getMainAttributes().getValue("Bundle-SymbolicName").toString(); + // in case there are additional directives + bundleSymbolicName = bundleSymbolicName.split(";")[0]; + 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)) { + srcJarOut.setLevel(Deflater.BEST_COMPRESSION); + Files.walkFileTree(sourceDir, new SimpleFileVisitor() { + + @Override + public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { + if (file.getFileName().toString().equals("MANIFEST.MF")) + return super.visitFile(file, attrs); + JarEntry entry = new JarEntry( + sourceDir.relativize(file).toString().replace(File.separatorChar, '/')); + srcJarOut.putNextEntry(entry); + Files.copy(file, srcJarOut); + return super.visitFile(file, attrs); + } + + }); + } + deleteDirectory(sourceDir); + } + return jarPath; }