Search in sources :

Example 1 with ArtifactContext

use of org.eclipse.ceylon.cmr.api.ArtifactContext 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 ArtifactContext

use of org.eclipse.ceylon.cmr.api.ArtifactContext 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 ArtifactContext

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

the class CeylonModuleLoader method findOverride.

protected ModuleIdentifier findOverride(ModuleIdentifier mi) {
    String namespace = ModuleUtil.getNamespaceFromUri(mi.getName());
    String name = ModuleUtil.getModuleNameFromUri(mi.getName());
    final ArtifactContext context = new ArtifactContext(namespace, name, mi.getSlot(), ArtifactContext.CAR, ArtifactContext.JAR);
    ArtifactContext override = repository.getOverrides().applyOverrides(context);
    String newName = ModuleUtil.makeModuleName(override.getNamespace(), override.getName(), null);
    return ModuleIdentifier.create(newName, override.getVersion());
}
Also used : MavenArtifactContext(org.eclipse.ceylon.cmr.api.MavenArtifactContext) ArtifactContext(org.eclipse.ceylon.cmr.api.ArtifactContext)

Example 4 with ArtifactContext

use of org.eclipse.ceylon.cmr.api.ArtifactContext 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)

Example 5 with ArtifactContext

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

the class CeylonCopyTool method run.

@Override
public void run() throws Exception {
    Set<String> artifacts = new LinkedHashSet<String>();
    boolean defaults = js == null && jvm == null && dart == null && src == null && scripts == null && docs == null && all == null;
    if (BooleanUtil.isTrue(all)) {
        artifacts.addAll(Arrays.asList(ArtifactContext.allSuffixes()));
    }
    if (BooleanUtil.isTrue(js) || defaults) {
        artifacts.add(ArtifactContext.JS);
        artifacts.add(ArtifactContext.JS_MODEL);
        artifacts.add(ArtifactContext.RESOURCES);
    } else if (BooleanUtil.isFalse(js)) {
        artifacts.remove(ArtifactContext.JS);
        artifacts.remove(ArtifactContext.JS_MODEL);
        artifacts.remove(ArtifactContext.RESOURCES);
    }
    if (BooleanUtil.isTrue(jvm) || defaults) {
        // put the CAR first since its presence will shortcut the other three
        artifacts.add(ArtifactContext.CAR);
        artifacts.add(ArtifactContext.JAR);
        artifacts.add(ArtifactContext.MODULE_PROPERTIES);
        artifacts.add(ArtifactContext.MODULE_XML);
    } else if (BooleanUtil.isFalse(jvm)) {
        artifacts.remove(ArtifactContext.CAR);
        artifacts.remove(ArtifactContext.JAR);
        artifacts.remove(ArtifactContext.MODULE_PROPERTIES);
        artifacts.remove(ArtifactContext.MODULE_XML);
    }
    if (BooleanUtil.isTrue(dart) || defaults) {
        artifacts.add(ArtifactContext.DART);
        artifacts.add(ArtifactContext.DART_MODEL);
        artifacts.add(ArtifactContext.RESOURCES);
    } else if (BooleanUtil.isFalse(dart)) {
        artifacts.remove(ArtifactContext.DART);
        artifacts.remove(ArtifactContext.DART_MODEL);
        artifacts.remove(ArtifactContext.RESOURCES);
    }
    if (BooleanUtil.isTrue(src)) {
        artifacts.add(ArtifactContext.SRC);
    } else if (BooleanUtil.isFalse(src)) {
        artifacts.remove(ArtifactContext.SRC);
    }
    if (BooleanUtil.isTrue(scripts)) {
        artifacts.add(ArtifactContext.SCRIPTS_ZIPPED);
    } else if (BooleanUtil.isFalse(scripts)) {
        artifacts.remove(ArtifactContext.SCRIPTS_ZIPPED);
    }
    if (BooleanUtil.isTrue(docs)) {
        artifacts.add(ArtifactContext.DOCS);
    } else if (BooleanUtil.isFalse(docs)) {
        artifacts.remove(ArtifactContext.DOCS);
    }
    // Create the list of ArtifactContexts to copy
    List<ArtifactContext> acs = new ArrayList<ArtifactContext>();
    String[] artifactsArray = new String[artifacts.size()];
    artifacts.toArray(artifactsArray);
    for (ModuleSpec module : modules) {
        if (module != ModuleSpec.DEFAULT_MODULE && !module.isVersioned()) {
            String version = checkModuleVersionsOrShowSuggestions(module.getName(), null, ModuleQuery.Type.ALL, null, null, null, null);
            module = new ModuleSpec(module.getNamespace(), module.getName(), version);
        }
        ArtifactContext ac = new ArtifactContext(null, module.getName(), module.getVersion(), artifactsArray);
        ac.setIgnoreDependencies(!withDependencies);
        ac.setForceOperation(true);
        acs.add(ac);
    }
    // Now do the actual copying
    final boolean logArtifacts = verbose != null && (verbose.contains("all") || verbose.contains("files"));
    ModuleCopycat copier = new ModuleCopycat(getRepositoryManager(), getOutputRepositoryManager(), getLogger(), new ModuleCopycat.CopycatFeedback() {

        boolean haveSeenArtifacts = false;

        @Override
        public boolean beforeCopyModule(ArtifactContext ac, int count, int max) throws IOException {
            String module = ModuleUtil.makeModuleName(ac.getName(), ac.getVersion());
            msg("copying.module", module, count + 1, max).flush();
            haveSeenArtifacts = false;
            return true;
        }

        @Override
        public void afterCopyModule(ArtifactContext ac, int count, int max, boolean copied) throws IOException {
            if (!logArtifacts || !haveSeenArtifacts) {
                String msg;
                if (copied) {
                    msg = OSUtil.color(Messages.msg(bundle, "copying.ok"), OSUtil.Color.green);
                } else {
                    msg = OSUtil.color(Messages.msg(bundle, "copying.skipped"), OSUtil.Color.yellow);
                }
                if (haveSeenArtifacts) {
                    append(")");
                }
                append(" ").append(msg).newline().flush();
            }
        }

        @Override
        public boolean beforeCopyArtifact(ArtifactContext ac, ArtifactResult ar, int count, int max) throws IOException {
            haveSeenArtifacts = true;
            if (logArtifacts) {
                if (count == 0) {
                    append(" -- ");
                    append(ar.repositoryDisplayString());
                    newline().flush();
                }
                append("    ").msg("copying.artifact", ar.artifact().getName(), count + 1, max).flush();
            } else {
                if (count > 0) {
                    append(", ");
                } else {
                    append(" (");
                }
                String name = ArtifactContext.getSuffixFromFilename(ar.artifact().getName());
                if (name.startsWith(".") || name.startsWith("-")) {
                    name = name.substring(1);
                } else if ("module-doc".equals(name)) {
                    name = "doc";
                }
                append(name);
            }
            return true;
        }

        @Override
        public void afterCopyArtifact(ArtifactContext ac, ArtifactResult ar, int count, int max, boolean copied) throws IOException {
            haveSeenArtifacts = true;
            if (logArtifacts) {
                append(" ").msg((copied) ? "copying.ok" : "copying.skipped").newline().flush();
            }
        }

        @Override
        public void notFound(ArtifactContext ac) throws IOException {
            String err = getModuleNotFoundErrorMessage(getRepositoryManager(), ac.getName(), ac.getVersion());
            errorAppend(err);
            errorNewline();
        }
    });
    copier.includeLanguage(includeLanguage).excludeModules(excludeModules).copyModules(acs);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ArrayList(java.util.ArrayList) ArtifactContext(org.eclipse.ceylon.cmr.api.ArtifactContext) IOException(java.io.IOException) ModuleCopycat(org.eclipse.ceylon.cmr.ceylon.ModuleCopycat) ArtifactResult(org.eclipse.ceylon.model.cmr.ArtifactResult) ModuleSpec(org.eclipse.ceylon.common.ModuleSpec)

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