Search in sources :

Example 6 with ModuleSpec

use of org.eclipse.ceylon.common.ModuleSpec in project ceylon by eclipse.

the class ModuleLoadingTool method initialize.

@Override
public void initialize(CeylonTool mainTool) throws Exception {
    super.initialize(mainTool);
    loader = new ToolModuleLoader(this, getRepositoryManager(), getLoaderSuffixes());
    if (jdkProviderModule != null) {
        ModuleSpec moduleSpec = ModuleSpec.parse(jdkProviderModule);
        if (!internalLoadModule(null, moduleSpec.getName(), moduleSpec.getVersion())) {
            throw new ToolUsageError(Messages.msg(bundle, "jdk.provider.not.found", jdkProviderModule));
        }
        ArtifactResult result = loader.getModuleArtifact(moduleSpec.getName());
        jdkProvider = new JdkProvider(moduleSpec.getName(), moduleSpec.getVersion(), null, result.artifact());
    }
// else keep the JDK one
}
Also used : ModuleSpec(org.eclipse.ceylon.common.ModuleSpec) JdkProvider(org.eclipse.ceylon.model.loader.JdkProvider) ToolUsageError(org.eclipse.ceylon.common.tool.ToolUsageError) ArtifactResult(org.eclipse.ceylon.model.cmr.ArtifactResult)

Example 7 with ModuleSpec

use of org.eclipse.ceylon.common.ModuleSpec 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 8 with ModuleSpec

use of org.eclipse.ceylon.common.ModuleSpec 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)

Example 9 with ModuleSpec

use of org.eclipse.ceylon.common.ModuleSpec in project ceylon by eclipse.

the class CompilerTests method mainApiClasspath.

protected String mainApiClasspath(String rep, ModuleSpec module, List<ModuleSpec> extraModules, boolean distDowngrade, int expectedSc, File err1, boolean debug) throws IOException, InterruptedException {
    File dir = new File("build/mainapi");
    dir.mkdirs();
    File out = File.createTempFile("classpath-" + module, ".out", dir);
    File err = err1 != null ? err1 : File.createTempFile("classpath-" + module, ".err", dir);
    ArrayList<String> a = new ArrayList<String>();
    a.add(script());
    a.add("classpath");
    a.add("--sysrep");
    a.add(getSysRepPath());
    a.add("--rep");
    a.add("modules");
    a.add("--rep");
    a.add(rep);
    if (distDowngrade) {
        a.add("--link-with-current-distribution");
    }
    a.add(module.toString());
    for (ModuleSpec extraModule : extraModules) a.add(extraModule.toString());
    System.err.println(a);
    ProcessBuilder pb = new ProcessBuilder(a);
    if (debug) {
        pb.environment().put("JAVA_OPTS", "-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=9090");
    }
    pb.redirectOutput(out);
    pb.redirectError(err);
    Process p = pb.start();
    int sc = p.waitFor();
    if (err.length() != 0) {
        if (err1 == null) {
            try (BufferedReader r = new BufferedReader(new FileReader(err))) {
                String error = r.readLine();
                // CI sets this var, which produces stderr logs
                if (!error.contains("Picked up JAVA_TOOL_OPTIONS")) {
                    String msg = "`ceylon classpath " + module + "` produced standard error output: " + error + " (see " + err + " for full stderr output and " + out + " for stdout output)";
                    System.err.println(msg);
                    Assert.fail(msg);
                    return error;
                }
            }
        }
    }
    try (BufferedReader r = new BufferedReader(new FileReader(out))) {
        String cp = r.readLine();
        System.err.println(cp);
        Assert.assertTrue("ceylon classpath " + module + " produced more than a single line of output", r.readLine() == null);
        Assert.assertEquals(expectedSc, sc);
        return cp;
    }
}
Also used : ModuleSpec(org.eclipse.ceylon.common.ModuleSpec) ArrayList(java.util.ArrayList) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) ZipFile(java.util.zip.ZipFile) File(java.io.File)

Example 10 with ModuleSpec

use of org.eclipse.ceylon.common.ModuleSpec in project ceylon by eclipse.

the class CMRTests method testMdlDependenciesFromMavenFlatClasspath.

@Test
public void testMdlDependenciesFromMavenFlatClasspath() throws Throwable {
    Assume.assumeTrue("Runs on JDK8", JDKUtils.jdk == JDKUtils.JDK.JDK8);
    CeyloncTaskImpl ceylonTask = getCompilerTask(Arrays.asList("-out", destDir), "modules/sparkframework/module.ceylon", "modules/sparkframework/test.ceylon");
    assertEquals("Compilation failed", Boolean.TRUE, ceylonTask.call());
    // flat classpath via API
    runInJBossModules("run", "org.eclipse.ceylon.compiler.java.test.cmr.modules.sparkframework/1", Arrays.asList("--flat-classpath", "--overrides", getPackagePath() + "/modules/sparkframework/overrides-log.xml"));
    // and via main without aether
    runInMainApi(destDir, new ModuleSpec(null, "org.eclipse.ceylon.compiler.java.test.cmr.modules.sparkframework", "1"), "org.eclipse.ceylon.compiler.java.test.cmr.modules.sparkframework.run_", Arrays.<String>asList(), false);
    // and via main with aether
    runInMainApi(destDir, new ModuleSpec(null, "org.eclipse.ceylon.compiler.java.test.cmr.modules.sparkframework", "1"), Arrays.asList(new ModuleSpec(null, "org.eclipse.ceylon.module-resolver-aether", Versions.CEYLON_VERSION_NUMBER)), "org.eclipse.ceylon.compiler.java.test.cmr.modules.sparkframework.run_", Arrays.<String>asList(), false);
}
Also used : ModuleSpec(org.eclipse.ceylon.common.ModuleSpec) CeyloncTaskImpl(org.eclipse.ceylon.compiler.java.tools.CeyloncTaskImpl) Test(org.junit.Test)

Aggregations

ModuleSpec (org.eclipse.ceylon.common.ModuleSpec)35 File (java.io.File)18 ArtifactResult (org.eclipse.ceylon.model.cmr.ArtifactResult)13 ArrayList (java.util.ArrayList)11 ArtifactContext (org.eclipse.ceylon.cmr.api.ArtifactContext)7 IOException (java.io.IOException)6 Test (org.junit.Test)6 ZipFile (java.util.zip.ZipFile)5 ModuleGraph (org.eclipse.ceylon.cmr.ceylon.loader.ModuleGraph)5 HashSet (java.util.HashSet)4 LinkedList (java.util.LinkedList)4 ModuleVersionDetails (org.eclipse.ceylon.cmr.api.ModuleVersionDetails)4 ToolUsageError (org.eclipse.ceylon.common.tool.ToolUsageError)4 List (java.util.List)3 ZipEntry (java.util.zip.ZipEntry)3 RepositoryManager (org.eclipse.ceylon.cmr.api.RepositoryManager)3 Module (org.eclipse.ceylon.cmr.ceylon.loader.ModuleGraph.Module)3 Ignore (org.junit.Ignore)3 BufferedReader (java.io.BufferedReader)2 FileOutputStream (java.io.FileOutputStream)2