Search in sources :

Example 1 with ArtifactResult

use of org.eclipse.ceylon.model.cmr.ArtifactResult in project ceylon by eclipse.

the class RepoUsingTool method checkModuleVersionsOrShowSuggestions.

protected String checkModuleVersionsOrShowSuggestions(String name, String version, ModuleQuery.Type type, Integer jvmBinaryMajor, Integer jvmBinaryMinor, Integer jsBinaryMajor, Integer jsBinaryMinor, String compileFlags) throws IOException {
    RepositoryManager repoMgr = getRepositoryManager();
    if (compileFlags == null || compileFlags.isEmpty() || !compilationPossible()) {
        compileFlags = COMPILE_NEVER;
    }
    boolean forceCompilation = compileFlags.contains(COMPILE_FORCE);
    boolean checkCompilation = compileFlags.contains(COMPILE_CHECK);
    boolean allowCompilation = forceCompilation || checkCompilation || compileFlags.contains(COMPILE_ONCE);
    Collection<ModuleVersionDetails> versions = null;
    if (ModuleUtil.isDefaultModule(name) || version != null) {
        // If we have the default module or a version we first try it the quick way
        ArtifactContext ac = new ArtifactContext(null, name, version, type.getSuffixes());
        ac.setIgnoreDependencies(true);
        ac.setThrowErrorIfMissing(false);
        ArtifactResult result = repoMgr.getArtifactResult(ac);
        if (result != null) {
            if (forceCompilation || checkCompilation) {
                String v = result.version() != null ? result.version() : "unversioned";
                versions = Collections.singletonList(new ModuleVersionDetails(name, v, result.groupId(), result.groupId()));
            } else {
                return (result.version() != null) ? result.version() : "";
            }
        } else if (ModuleUtil.isDefaultModule(name) && !allowCompilation) {
            String err = getModuleNotFoundErrorMessage(repoMgr, name, version);
            throw new ToolUsageError(err);
        }
    }
    boolean suggested = false;
    // try that first
    if (version == null && !ModuleUtil.isDefaultModule(name) && versions == null) {
        versions = findCompiledVersions(getOfflineRepositoryManager(), name, type, jvmBinaryMajor, jvmBinaryMinor, jsBinaryMajor, jsBinaryMinor);
        if (versions != null && versions.size() == 1) {
            ModuleVersionDetails compiledVersion = versions.iterator().next();
            if (compiledVersion != null && compiledVersion.getVersion() != null) {
                if (forceCompilation || checkCompilation) {
                    versions = Collections.singleton(compiledVersion);
                } else {
                    return compiledVersion.getVersion();
                }
            }
        }
    }
    // if we did not find any version in the output repo, see if we have a single one in the source repo, that's
    // a lot cheaper than looking the version up
    ModuleVersionDetails srcVersion = null;
    if (allowCompilation || (versions != null && versions.size() > 1)) {
        srcVersion = getModuleVersionDetailsFromSource(name);
        if (srcVersion != null && version == null) {
            if (versions == null) {
                // we found some source and no local compiled versions exist,
                // let's compile it and not even look up anything else
                versions = Collections.emptyList();
            } else {
                // let's if one of them matches and use that one
                for (ModuleVersionDetails mvd : versions) {
                    if (sameVersion(name, mvd.getVersion(), srcVersion.getVersion())) {
                        if (forceCompilation || checkCompilation) {
                            versions = Collections.singleton(mvd);
                        } else {
                            return mvd.getVersion();
                        }
                    }
                }
            }
        }
    }
    // find versions unless we have one in sources waiting to be compiled
    if (versions == null) {
        // First see which versions we have available locally
        versions = getModuleVersions(getOfflineRepositoryManager(), null, name, version, false, type, jvmBinaryMajor, jvmBinaryMinor, jsBinaryMajor, jsBinaryMinor);
        if (versions.isEmpty() && !offline) {
            // No local versions and we're not offline, so let's try again online
            versions = getModuleVersions(repoMgr, null, name, version, false, type, jvmBinaryMajor, jvmBinaryMinor, jsBinaryMajor, jsBinaryMinor);
        }
        if (version != null && !versions.isEmpty()) {
            // We have one or more matching versions, let's see if one is exactly the same
            // while not having any partial matches
            boolean partialMatch = false;
            ModuleVersionDetails exactMatch = null;
            for (ModuleVersionDetails v : versions) {
                if (version.equals(v.getVersion())) {
                    exactMatch = v;
                } else if (v.getVersion().startsWith(version)) {
                    partialMatch = true;
                }
            }
            if (exactMatch != null && !partialMatch) {
                versions = Collections.singletonList(exactMatch);
            }
        }
    }
    if (version != null && (versions.isEmpty() || exactSingleMatch(versions, version))) {
        // Here we either have a single version or none
        if (versions.isEmpty() || forceCompilation || (checkCompilation && shouldRecompile(getOfflineRepositoryManager(), name, version, type, true))) {
            if (allowCompilation) {
                if (srcVersion != null) {
                    if (version.equals(srcVersion.getVersion())) {
                        // Let's see if we can compile it...
                        if (!runCompiler(repoMgr, name, type, compileFlags)) {
                            throw new ToolUsageError(Messages.msg(bundle, "compilation.failed"));
                        }
                    } else {
                        suggested = true;
                    }
                    // All okay it seems, let's use this version
                    versions = Arrays.asList(srcVersion);
                }
            }
            if (versions.isEmpty()) {
                // Maybe the user specified the wrong version?
                // Let's see if we can find any locally and suggest them
                versions = getModuleVersions(getOfflineRepositoryManager(), null, name, null, false, type, jvmBinaryMajor, jvmBinaryMinor, jsBinaryMajor, jsBinaryMinor);
                if (versions.isEmpty() && !offline) {
                    // No local versions and we're not offline, so let's try again online
                    versions = getModuleVersions(repoMgr, null, name, null, false, type, jvmBinaryMajor, jvmBinaryMinor, jsBinaryMajor, jsBinaryMinor);
                }
                suggested = true;
            }
        }
    } else {
        // Here we can have any number of versions, including none
        if (allowCompilation && (versions.isEmpty() || onlyRemote(versions) || forceCompilation || checkCompilation)) {
            // first check if there's local code we could compile before giving up
            if ((srcVersion != null || ModuleUtil.isDefaultModule(name)) && (version == null || version.equals(srcVersion.getVersion()))) {
                // There seems to be source code
                // Let's see if we can compile it...
                String srcver = ModuleUtil.isDefaultModule(name) ? null : srcVersion.getVersion();
                if (!checkCompilation || shouldRecompile(getOfflineRepositoryManager(), name, srcver, type, true)) {
                    if (!runCompiler(repoMgr, name, type, compileFlags)) {
                        throw new ToolUsageError(Messages.msg(bundle, "compilation.failed"));
                    }
                }
                // All okay it seems, let's use this version
                versions = Arrays.asList(srcVersion);
            }
        }
    }
    if (versions.isEmpty()) {
        String err = getModuleNotFoundErrorMessage(repoMgr, name, version);
        throw new ToolUsageError(err);
    }
    if (versions.size() > 1 || inexactSingleMatch(versions, version) || suggested) {
        StringBuilder err = new StringBuilder();
        if (version == null) {
            err.append(Messages.msg(bundle, "missing.version", name));
        } else {
            err.append(Messages.msg(bundle, "version.not.found", version, name));
        }
        err.append("\n");
        err.append(Messages.msg(bundle, "try.versions"));
        boolean first = true;
        for (ModuleVersionDetails mvd : versions) {
            if (!first) {
                err.append(", ");
            }
            err.append(mvd.getVersion());
            if (mvd.isRemote()) {
                err.append(" (*)");
            }
            first = false;
        }
        err.append("\n");
        throw new ToolUsageError(err.toString());
    }
    if (ModuleUtil.isDefaultModule(name)) {
        return "";
    } else {
        return versions.iterator().next().getVersion();
    }
}
Also used : ModuleVersionDetails(org.eclipse.ceylon.cmr.api.ModuleVersionDetails) ToolUsageError(org.eclipse.ceylon.common.tool.ToolUsageError) RepositoryManager(org.eclipse.ceylon.cmr.api.RepositoryManager) ArtifactContext(org.eclipse.ceylon.cmr.api.ArtifactContext) ArtifactResult(org.eclipse.ceylon.model.cmr.ArtifactResult)

Example 2 with ArtifactResult

use of org.eclipse.ceylon.model.cmr.ArtifactResult in project ceylon by eclipse.

the class PhasedUnitsModuleManager method addOutputModuleToClassPath.

private void addOutputModuleToClassPath(Module module) {
    ArtifactContext ctx = new ArtifactContext(null, module.getNameAsString(), module.getVersion(), ArtifactContext.CAR);
    ArtifactResult result = outputRepositoryManager.getArtifactResult(ctx);
    if (result != null)
        getModelLoader().addModuleToClassPath(module, result);
}
Also used : ArtifactContext(org.eclipse.ceylon.cmr.api.ArtifactContext) ArtifactResult(org.eclipse.ceylon.model.cmr.ArtifactResult)

Example 3 with ArtifactResult

use of org.eclipse.ceylon.model.cmr.ArtifactResult in project ceylon by eclipse.

the class CeylonModuleLoader method init.

protected void init() throws Exception {
    // The runtime model needs knowledge of these modules existing at runtime, since the language module
    // implementation contains types from these modules
    ModuleLoader bootModuleLoader = org.jboss.modules.Module.getBootModuleLoader();
    for (ModuleIdentifier initialModule : BOOTSTRAP) {
        org.jboss.modules.Module module = bootModuleLoader.loadModule(initialModule);
        ArtifactResult moduleArtifactResult = findArtifact(initialModule);
        UtilRegistryTransformer.registerModule(initialModule.getName(), initialModule.getSlot(), moduleArtifactResult, SecurityActions.getClassLoader(module), false);
    }
    for (ModuleIdentifier initialModule : BOOTSTRAP_OPTIONAL) {
        try {
            org.jboss.modules.Module module = bootModuleLoader.loadModule(initialModule);
            ArtifactResult moduleArtifactResult = findArtifact(initialModule);
            UtilRegistryTransformer.registerModule(initialModule.getName(), initialModule.getSlot(), moduleArtifactResult, SecurityActions.getClassLoader(module), false);
        } catch (Exception ex) {
        // HACK: These are optional modules, just continue
        }
    }
}
Also used : ModuleLoader(org.jboss.modules.ModuleLoader) Module(org.jboss.modules.Module) ModuleIdentifier(org.jboss.modules.ModuleIdentifier) ModuleLoadException(org.jboss.modules.ModuleLoadException) ArtifactResult(org.eclipse.ceylon.model.cmr.ArtifactResult)

Example 4 with ArtifactResult

use of org.eclipse.ceylon.model.cmr.ArtifactResult in project ceylon by eclipse.

the class CachedTOCJarsTest method spacesOnPathTest.

@Test
public void spacesOnPathTest() throws IOException {
    CachedTOCJars cachedTOCJars = new CachedTOCJars();
    Module module = new Module();
    module.setName(Collections.singletonList("testModule"));
    module.setVersion("testVersion");
    final File artifactFile = File.createTempFile("path with spaces", ".jar");
    try {
        artifactFile.deleteOnExit();
        ZipOutputStream artifactOut = new ZipOutputStream(new FileOutputStream(artifactFile));
        try {
            artifactOut.putNextEntry(new ZipEntry(ALSO_INSIDE_JAR));
        } finally {
            artifactOut.close();
        }
        ArtifactResult artifact = new TestArtifactResult(artifactFile, module);
        cachedTOCJars.addJar(artifact, module);
        String uriPart = cachedTOCJars.getContentUri(module, ALSO_INSIDE_JAR).getSchemeSpecificPart();
        // I'm not comparing URI part for equality to avoid symbolic links resolution issues.
        Assert.assertTrue(uriPart, uriPart.endsWith(artifactFile.getName() + "!" + ALSO_INSIDE_JAR));
    } finally {
        artifactFile.delete();
    }
}
Also used : ZipOutputStream(java.util.zip.ZipOutputStream) FileOutputStream(java.io.FileOutputStream) ZipEntry(java.util.zip.ZipEntry) CachedTOCJars(org.eclipse.ceylon.model.loader.impl.reflect.CachedTOCJars) Module(org.eclipse.ceylon.model.typechecker.model.Module) File(java.io.File) ArtifactResult(org.eclipse.ceylon.model.cmr.ArtifactResult) Test(org.junit.Test)

Example 5 with ArtifactResult

use of org.eclipse.ceylon.model.cmr.ArtifactResult in project ceylon by eclipse.

the class CeylonSrcTool method run.

@Override
public void run() throws Exception {
    // First check if all the arguments point to source archives
    for (ModuleSpec module : modules) {
        if (module != ModuleSpec.DEFAULT_MODULE && !module.isVersioned()) {
            if (checkModuleVersionsOrShowSuggestions(module.getName(), null, ModuleQuery.Type.SRC, null, null, null, null) == null) {
                return;
            }
        }
    }
    // If all are correct we unpack them
    for (ModuleSpec module : modules) {
        String version = module.getVersion();
        if (module != ModuleSpec.DEFAULT_MODULE && !module.isVersioned()) {
            version = checkModuleVersionsOrShowSuggestions(module.getName(), null, ModuleQuery.Type.SRC, null, null, null, null);
        }
        msg("retrieving.module", module).newline();
        ArtifactContext allArtifacts = new ArtifactContext(null, module.getName(), version, ArtifactContext.SRC, ArtifactContext.RESOURCES, ArtifactContext.DOCS, ArtifactContext.SCRIPTS_ZIPPED);
        List<ArtifactResult> results = getRepositoryManager().getArtifactResults(allArtifacts);
        if (results == null) {
            String err = getModuleNotFoundErrorMessage(getRepositoryManager(), module.getName(), module.getVersion());
            errorAppend(err);
            errorNewline();
            continue;
        }
        String modFolder = module.getName().replace('.', File.separatorChar);
        boolean hasSources = false;
        for (ArtifactResult result : results) {
            String suffix = ArtifactContext.getSuffixFromFilename(result.artifact().getName());
            if (ArtifactContext.SRC.equals(suffix)) {
                append("    ").msg("extracting.sources").newline();
                extractArchive(result, applyCwd(src), "source");
                hasSources = true;
            } else if (ArtifactContext.SCRIPTS_ZIPPED.equals(suffix)) {
                append("    ").msg("extracting.scripts").newline();
                extractArchive(result, new File(applyCwd(script), modFolder), "script");
            } else if (ArtifactContext.RESOURCES.equals(suffix)) {
                append("    ").msg("extracting.resources").newline();
                copyResources(result, applyCwd(resource));
            } else if (ArtifactContext.DOCS.equals(suffix)) {
                append("    ").msg("extracting.docs").newline();
                copyFiles(result, "doc", new File(applyCwd(doc), modFolder), "doc", false);
            }
        }
        if (!hasSources) {
            msg("no.sources.found", module).newline();
        }
    }
}
Also used : ModuleSpec(org.eclipse.ceylon.common.ModuleSpec) ArtifactContext(org.eclipse.ceylon.cmr.api.ArtifactContext) File(java.io.File) ArtifactResult(org.eclipse.ceylon.model.cmr.ArtifactResult)

Aggregations

ArtifactResult (org.eclipse.ceylon.model.cmr.ArtifactResult)79 File (java.io.File)40 RepositoryManager (org.eclipse.ceylon.cmr.api.RepositoryManager)32 SimpleRepositoryManager (org.eclipse.ceylon.cmr.impl.SimpleRepositoryManager)28 Test (org.junit.Test)28 ArtifactContext (org.eclipse.ceylon.cmr.api.ArtifactContext)25 IOException (java.io.IOException)17 ArrayList (java.util.ArrayList)17 CmrRepository (org.eclipse.ceylon.cmr.api.CmrRepository)15 ModuleSpec (org.eclipse.ceylon.common.ModuleSpec)13 FileOutputStream (java.io.FileOutputStream)6 InputStream (java.io.InputStream)5 ToolUsageError (org.eclipse.ceylon.common.tool.ToolUsageError)5 Module (org.eclipse.ceylon.model.typechecker.model.Module)5 LinkedList (java.util.LinkedList)4 List (java.util.List)4 ZipEntry (java.util.zip.ZipEntry)4 FileInputStream (java.io.FileInputStream)3 OutputStream (java.io.OutputStream)3 OutputStreamWriter (java.io.OutputStreamWriter)3