Search in sources :

Example 11 with ArtifactContext

use of org.eclipse.ceylon.cmr.api.ArtifactContext in project ceylon by eclipse.

the class ModuleCopycat method copyModules.

/**
 * This method basically calls <code>copyModule</code> on each of the artifact
 * contexts in the list it gets passed.
 * @param contexts
 * @throws Exception
 */
public void copyModules(List<ArtifactContext> contexts) throws Exception {
    init();
    count = 0;
    maxCount = contexts.size();
    for (ArtifactContext context : contexts) {
        copyModuleInternal(context);
    }
}
Also used : ArtifactContext(org.eclipse.ceylon.cmr.api.ArtifactContext)

Example 12 with ArtifactContext

use of org.eclipse.ceylon.cmr.api.ArtifactContext in project ceylon by eclipse.

the class ModuleCopycat method copyModuleInternal.

private void copyModuleInternal(ArtifactContext context) throws Exception {
    assert (context != null);
    if (!shouldExclude(context.getName())) {
        String module = ModuleUtil.makeModuleName(context.getName(), context.getVersion());
        // Skip all duplicates and artifacts from repositories that don't support copying
        if (!copiedModules.add(module) || !canBeCopied(context)) {
            // Faking a copy here for feedback because it was already done and we never copy twice
            if (feedback != null) {
                feedback.beforeCopyModule(context, count++, maxCount);
            }
            if (feedback != null) {
                feedback.afterCopyModule(context, count, maxCount, false);
            }
            return;
        }
        Collection<ModuleVersionDetails> versions = getModuleVersions(srcRepoman, context.getName(), context.getVersion(), ModuleQuery.Type.ALL, null, null, null, null);
        if (!versions.isEmpty()) {
            ArtifactContext depContext = context.copy();
            ModuleVersionDetails ver = versions.iterator().next();
            boolean copyModule = true;
            if (feedback != null) {
                copyModule = feedback.beforeCopyModule(context, count++, maxCount);
            }
            boolean copiedModule = false;
            if (copyModule) {
                List<ArtifactResult> results = srcRepoman.getArtifactResults(context);
                int artCnt = 0;
                for (ArtifactResult r : results) {
                    boolean copyArtifact = true;
                    if (feedback != null) {
                        copyArtifact = feedback.beforeCopyArtifact(context, r, artCnt++, results.size());
                    }
                    boolean copied = copyArtifact && copyArtifact(context, r);
                    if (feedback != null) {
                        feedback.afterCopyArtifact(context, r, artCnt, results.size(), copied);
                    }
                    copiedModule |= copied;
                }
            }
            if (feedback != null) {
                feedback.afterCopyModule(context, count, maxCount, copiedModule);
            }
            if (copyModule && !context.isIgnoreDependencies()) {
                maxCount += countNonExcludedDeps(ver.getDependencies());
                for (ModuleDependencyInfo dep : ver.getDependencies()) {
                    if (skipDependency(dep)) {
                        continue;
                    }
                    ModuleSpec depModule = new ModuleSpec(dep.getNamespace(), dep.getName(), dep.getVersion());
                    ArtifactContext copyContext = depContext.copy();
                    copyContext.setNamespace(dep.getNamespace());
                    copyContext.setName(depModule.getName());
                    copyContext.setVersion(depModule.getVersion());
                    copyModuleInternal(copyContext);
                }
            }
        } else {
            if (feedback != null) {
                feedback.notFound(context);
            }
        }
    }
}
Also used : ModuleSpec(org.eclipse.ceylon.common.ModuleSpec) ModuleVersionDetails(org.eclipse.ceylon.cmr.api.ModuleVersionDetails) ArtifactContext(org.eclipse.ceylon.cmr.api.ArtifactContext) ModuleDependencyInfo(org.eclipse.ceylon.cmr.api.ModuleDependencyInfo) ArtifactResult(org.eclipse.ceylon.model.cmr.ArtifactResult)

Example 13 with ArtifactContext

use of org.eclipse.ceylon.cmr.api.ArtifactContext in project ceylon by eclipse.

the class ModuleCopycat method copyArtifact.

private boolean copyArtifact(ArtifactContext orgac, ArtifactResult ar) throws Exception {
    ArtifactContext ac = orgac.copy();
    // Make sure we set the correct suffix for the put
    String suffix = ArtifactContext.getSuffixFromFilename(ar.artifact().getName());
    ac.setSuffixes(suffix);
    // Store the artifact
    dstRepoman.putArtifact(ac, ar.artifact());
    // SHA1 it if required
    signArtifact(ac, ar.artifact());
    return true;
}
Also used : ArtifactContext(org.eclipse.ceylon.cmr.api.ArtifactContext)

Example 14 with ArtifactContext

use of org.eclipse.ceylon.cmr.api.ArtifactContext in project ceylon by eclipse.

the class ShaSigner method signArtifact.

public static void signArtifact(RepositoryManager repoman, ArtifactContext context, File jarFile, Logger log) {
    ArtifactContext sha1Context = context.getSha1Context();
    if (sha1Context != null) {
        sha1Context.setForceOperation(true);
        String sha1 = sha1(jarFile, log);
        if (sha1 != null) {
            File shaFile = writeSha1(sha1, log);
            if (shaFile != null) {
                try {
                    repoman.putArtifact(sha1Context, shaFile);
                } finally {
                    shaFile.delete();
                }
            }
        }
    }
}
Also used : ArtifactContext(org.eclipse.ceylon.cmr.api.ArtifactContext) File(java.io.File)

Example 15 with ArtifactContext

use of org.eclipse.ceylon.cmr.api.ArtifactContext in project ceylon by eclipse.

the class CeylonAssembleTool method run.

@Override
public void run() throws Exception {
    if (includeRuntime) {
        // includeRuntime implies includeLanguage
        includeLanguage = true;
    }
    String firstModuleName = null, firstModuleVersion = null;
    for (ModuleSpec module : modules) {
        String moduleName = module.getName();
        String version = checkModuleVersionsOrShowSuggestions(moduleName, module.isVersioned() ? module.getVersion() : null, mqt, Versions.JVM_BINARY_MAJOR_VERSION, Versions.JVM_BINARY_MINOR_VERSION, Versions.JS_BINARY_MAJOR_VERSION, Versions.JS_BINARY_MINOR_VERSION, null);
        if (version == null)
            return;
        if (firstModuleName == null) {
            firstModuleName = moduleName;
            firstModuleVersion = version;
        }
        loadModule(null, moduleName, version);
        if (!force)
            errorOnConflictingModule(moduleName, version);
    }
    if (includeRuntime) {
        loadModule(null, "ceylon.runtime", Versions.CEYLON_VERSION_NUMBER);
        loadModule(null, "org.eclipse.ceylon.module-resolver-aether", Versions.CEYLON_VERSION_NUMBER);
    }
    loader.resolve();
    String versionSuffix = firstModuleVersion != null && !firstModuleVersion.isEmpty() ? "-" + firstModuleVersion : "";
    File outputCas = applyCwd(out != null ? out : new File(firstModuleName + versionSuffix + CEYLON_ASSEMBLY_SUFFIX));
    if (outputCas.getParentFile() != null && !outputCas.getParentFile().exists()) {
        FileUtil.mkdirs(outputCas.getParentFile());
    }
    if (outputCas.exists()) {
        FileUtil.delete(outputCas);
    }
    final Set<String> added = new HashSet<>();
    // Create a MANIFEST.MF and add it to the assembly
    Manifest manifest = new Manifest();
    Attributes mainAttributes = manifest.getMainAttributes();
    mainAttributes.putValue("Manifest-Version", "1.0");
    mainAttributes.putValue("Created-By", "Ceylon assemble for module " + firstModuleName + "/" + firstModuleVersion);
    mainAttributes.putValue(Constants.ATTR_ASSEMBLY_MAIN_MODULE, ModuleUtil.makeModuleName(firstModuleName, firstModuleVersion));
    if (run != null) {
        mainAttributes.putValue(Constants.ATTR_ASSEMBLY_RUN, run);
    }
    mainAttributes.putValue(Constants.ATTR_ASSEMBLY_REPOSITORY, "modules");
    File ovrFile = getOverridesFile();
    if (ovrFile != null) {
        mainAttributes.putValue(Constants.ATTR_ASSEMBLY_OVERRIDES, ovrFile.getName());
    }
    if (includeLanguage) {
        String className = JVMModuleUtil.javaClassNameFromCeylon(firstModuleName, run != null ? run : (firstModuleName + "::run"));
        mainAttributes.putValue("Main-Class", "org.eclipse.ceylon.tools.assemble.CeylonAssemblyRunner");
        mainAttributes.putValue(Constants.ATTR_ASSEMBLY_MAIN_CLASS, className);
    }
    added.add("META-INF/");
    added.add("META-INF/MANIFEST.MF");
    try (ZipOutputStream zipFile = new JarOutputStream(new FileOutputStream(outputCas), manifest)) {
        if (ovrFile != null) {
            // Copy the overrides.xml file to the output CAS
            try (InputStream is = new FileInputStream(ovrFile)) {
                zipFile.putNextEntry(new ZipEntry(ovrFile.getName()));
                IOUtils.copyStream(is, zipFile, true, false);
            }
        }
        if (includeLanguage) {
            // Copy the CeylonAssemblyRunner class and dependencies to the output CAS
            String prefix = CeylonAssemblyRunner.class.getName().replace('.', '/');
            String[] postfixes = { "", "$CeylonAssemblyClassLoader", "$CeylonAssemblyClassLoader$1" };
            for (String postfix : postfixes) {
                String clsName = prefix + postfix + ".class";
                try (InputStream is = CeylonAssemblyRunner.class.getResourceAsStream("/" + clsName)) {
                    zipFile.putNextEntry(new ZipEntry(clsName));
                    IOUtils.copyStream(is, zipFile, true, false);
                }
            }
        }
        // Visit the module and all its dependencies
        loader.visitModules(new ModuleGraph.Visitor() {

            @Override
            public void visit(ModuleGraph.Module module) {
                if (module.artifact != null) {
                    File file = module.artifact.artifact();
                    try {
                        if (file != null) {
                            if (isVerbose()) {
                                append(file.getAbsolutePath());
                                newline();
                            }
                            if (module.artifact.namespace() == null) {
                                // Copy all the "interesting" artifacts to the assembly, not just the JVM one
                                ArtifactContext ac = new ArtifactContext(module.artifact.namespace(), module.artifact.name(), module.artifact.version(), assemblySuffixes);
                                List<ArtifactResult> artifacts = getRepositoryManager().getArtifactResults(ac);
                                for (ArtifactResult ar : artifacts) {
                                    String name = "modules/" + moduleToPath(module.name) + "/" + module.version + "/" + ar.artifact().getName();
                                    addEntry(zipFile, ar.artifact(), name);
                                }
                            } else if (module.artifact.namespace().equals(MavenRepository.NAMESPACE)) {
                                String name = "maven/" + moduleToPath(module.name) + "/" + module.version + "/" + file.getName();
                                addEntry(zipFile, file, name);
                                // Copy the Maven artifact's pom file as well
                                String pomName = module.artifact.artifactId() + "-" + module.version + ".pom";
                                File mfile = new File(file.getParentFile(), pomName);
                                if (mfile.isFile()) {
                                    name = "maven/" + moduleToPath(module.name) + "/" + module.version + "/" + mfile.getName();
                                    addEntry(zipFile, mfile, name);
                                }
                            } else if (module.artifact.namespace().equals("npm")) {
                                File parent = module.artifact.artifact().getParentFile().getCanonicalFile();
                                while (parent != null && !(new File(parent, "package.json")).exists()) {
                                    parent = parent.getParentFile();
                                }
                                String name = "node_modules/" + parent.getName();
                                addEntry(zipFile, parent, name);
                            }
                        }
                    } catch (IOException x) {
                        // lame
                        throw new RuntimeException(x);
                    }
                }
            }

            private String moduleToPath(String name) {
                return ModuleUtil.moduleToPath(name).getPath().replace(':', File.separatorChar);
            }

            private void addEntry(final ZipOutputStream zipFile, final File file, final String name) throws IOException {
                if (file.isFile()) {
                    addFileEntry(zipFile, file, name);
                } else if (file.isDirectory()) {
                    Files.walkFileTree(file.toPath(), new SimpleFileVisitor<Path>() {

                        @Override
                        public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) throws IOException {
                            if (path.toFile().isFile()) {
                                Path relPath = file.toPath().relativize(path);
                                String newName = name + "/" + relPath;
                                addFileEntry(zipFile, path.toFile(), newName);
                            }
                            return super.visitFile(path, attrs);
                        }
                    });
                }
            }

            private void addFileEntry(ZipOutputStream zipFile, File file, String name) throws IOException {
                try (InputStream is = new FileInputStream(file)) {
                    zipFile.putNextEntry(new ZipEntry(name));
                    IOUtils.copyStream(is, zipFile, true, false);
                }
            }
        });
        zipFile.flush();
    }
    flush();
}
Also used : ZipEntry(java.util.zip.ZipEntry) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) Attributes(java.util.jar.Attributes) ArtifactContext(org.eclipse.ceylon.cmr.api.ArtifactContext) ModuleSpec(org.eclipse.ceylon.common.ModuleSpec) ArrayList(java.util.ArrayList) List(java.util.List) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) HashSet(java.util.HashSet) Path(java.nio.file.Path) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) JarOutputStream(java.util.jar.JarOutputStream) FileVisitResult(java.nio.file.FileVisitResult) IOException(java.io.IOException) Manifest(java.util.jar.Manifest) FileInputStream(java.io.FileInputStream) ArtifactResult(org.eclipse.ceylon.model.cmr.ArtifactResult) ZipOutputStream(java.util.zip.ZipOutputStream) FileOutputStream(java.io.FileOutputStream) ModuleGraph(org.eclipse.ceylon.cmr.ceylon.loader.ModuleGraph) File(java.io.File)

Aggregations

ArtifactContext (org.eclipse.ceylon.cmr.api.ArtifactContext)62 File (java.io.File)33 ArtifactResult (org.eclipse.ceylon.model.cmr.ArtifactResult)25 RepositoryManager (org.eclipse.ceylon.cmr.api.RepositoryManager)22 Test (org.junit.Test)20 SimpleRepositoryManager (org.eclipse.ceylon.cmr.impl.SimpleRepositoryManager)17 IOException (java.io.IOException)13 CmrRepository (org.eclipse.ceylon.cmr.api.CmrRepository)9 MavenArtifactContext (org.eclipse.ceylon.cmr.api.MavenArtifactContext)7 ModuleSpec (org.eclipse.ceylon.common.ModuleSpec)6 ArrayList (java.util.ArrayList)5 RepositoryManagerBuilder (org.eclipse.ceylon.cmr.api.RepositoryManagerBuilder)5 Manifest (java.util.jar.Manifest)4 ModuleVersionDetails (org.eclipse.ceylon.cmr.api.ModuleVersionDetails)4 Module (org.eclipse.ceylon.model.typechecker.model.Module)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 FileWriter (java.io.FileWriter)3 HashMap (java.util.HashMap)3 HashSet (java.util.HashSet)3 Map (java.util.Map)3