Search in sources :

Example 1 with ModuleVersionDetails

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

the class ModuleVersionReader method getModuleVersionDetailsFromSource.

/**
 * Reads a module descriptor and returns its information
 * @param moduleName The name of the module
 * @param srcDir The source directory where to find the descriptor
 * @return A <code>ModuleVersionsDetails</code> with the encountered information
 * @throws NoSuchModuleException if the module could not be found
 */
public static ModuleVersionDetails getModuleVersionDetailsFromSource(String moduleName, File srcDir) throws NoSuchModuleException {
    ModuleDescriptorReader mdr = new ModuleDescriptorReader(moduleName, srcDir);
    String module = mdr.getModuleName();
    String version = mdr.getModuleVersion();
    // PS In case the module descriptor was found but could not be parsed
    // we'll create an invalid details object
    ModuleVersionDetails mvd = new ModuleVersionDetails(module != null ? module : "", version != null ? version : "", mdr.getModuleGroupId(), mdr.getModuleArtifactId());
    mvd.setLabel(mdr.getModuleLabel());
    mvd.setLicense(mdr.getModuleLicense());
    List<String> by = mdr.getModuleAuthors();
    if (by != null) {
        mvd.getAuthors().addAll(by);
    }
    SortedSet<ModuleDependencyInfo> dependencies = new TreeSet<>();
    for (Object[] dep : mdr.getModuleImports()) {
        dependencies.add(new ModuleDependencyInfo((String) dep[0], (String) dep[1], (String) dep[2], (Boolean) dep[3], (Boolean) dep[4], (Backends) dep[5]));
    }
    mvd.setDependencies(dependencies);
    mvd.setRemote(false);
    mvd.setOrigin("Local source folder");
    return mvd;
}
Also used : ModuleDescriptorReader(org.eclipse.ceylon.common.ModuleDescriptorReader) Backends(org.eclipse.ceylon.common.Backends) ModuleVersionDetails(org.eclipse.ceylon.cmr.api.ModuleVersionDetails) TreeSet(java.util.TreeSet) ModuleDependencyInfo(org.eclipse.ceylon.cmr.api.ModuleDependencyInfo)

Example 2 with ModuleVersionDetails

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

the class RepoUsingTool method findCompiledVersions.

private Collection<ModuleVersionDetails> findCompiledVersions(RepositoryManager repoMgr, String name, Type type, Integer jvmBinaryMajor, Integer jvmBinaryMinor, Integer jsBinaryMajor, Integer jsBinaryMinor) throws IOException {
    String outRepo = DefaultToolOptions.getCompilerOutputRepo();
    if (outRepo != null) {
        File outDir = new File(outRepo);
        CmrRepository rep = null;
        List<CmrRepository> repositories = repoMgr.getRepositories();
        for (CmrRepository repository : repositories) {
            OpenNode root = repository.getRoot();
            // it has binaries if it is not a folder
            if (root.isRemote() || root.hasBinaries())
                continue;
            ContentStore service = root.getService(ContentStore.class);
            if (service == null)
                continue;
            ContentHandle content = service.peekContent(root);
            // again skip binaries
            if (content == null || content.hasBinaries())
                continue;
            File repoFile = content.getContentAsFile();
            if (repoFile != null && FileUtil.sameFile(repoFile, outDir)) {
                rep = repository;
                break;
            }
        }
        if (rep != null && rep.isSearchable()) {
            ModuleVersionQuery query = getModuleVersionQuery(null, name, null, type, jvmBinaryMajor, jvmBinaryMinor, jsBinaryMajor, jsBinaryMinor);
            ModuleVersionResult result = new ModuleVersionResult(query.getName());
            rep.completeVersions(query, result);
            NavigableMap<String, ModuleVersionDetails> outRepoVersions = result.getVersions();
            if (!outRepoVersions.isEmpty()) {
                return outRepoVersions.values();
            }
        }
    }
    return null;
}
Also used : ModuleVersionResult(org.eclipse.ceylon.cmr.api.ModuleVersionResult) ModuleVersionDetails(org.eclipse.ceylon.cmr.api.ModuleVersionDetails) ContentHandle(org.eclipse.ceylon.cmr.spi.ContentHandle) ModuleVersionQuery(org.eclipse.ceylon.cmr.api.ModuleVersionQuery) CmrRepository(org.eclipse.ceylon.cmr.api.CmrRepository) File(java.io.File) OpenNode(org.eclipse.ceylon.cmr.spi.OpenNode) ContentStore(org.eclipse.ceylon.cmr.spi.ContentStore)

Example 3 with ModuleVersionDetails

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

the class RepoUsingTool method getModuleVersions.

protected Collection<ModuleVersionDetails> getModuleVersions(RepositoryManager repoMgr, String namespace, String name, String version, boolean exactVersionMatch, ModuleQuery.Type type, Integer jvmBinaryMajor, Integer jvmBinaryMinor, Integer jsBinaryMajor, Integer jsBinaryMinor) {
    ModuleVersionQuery query = getModuleVersionQuery(namespace, name, version, type, jvmBinaryMajor, jvmBinaryMinor, jsBinaryMajor, jsBinaryMinor);
    query.setExactVersionMatch(exactVersionMatch);
    ModuleVersionResult result = repoMgr.completeVersions(query);
    NavigableMap<String, ModuleVersionDetails> versionMap = result.getVersions();
    return versionMap.values();
}
Also used : ModuleVersionResult(org.eclipse.ceylon.cmr.api.ModuleVersionResult) ModuleVersionDetails(org.eclipse.ceylon.cmr.api.ModuleVersionDetails) ModuleVersionQuery(org.eclipse.ceylon.cmr.api.ModuleVersionQuery)

Example 4 with ModuleVersionDetails

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

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

the class SourceDependencyResolver method collectModulesFromDependencies.

// Recursively traverses and collects all dependencies from the
// given module and adds their dependencies to the given set, but
// only those that have locally available sources
private void collectModulesFromDependencies(Set<ModuleVersionDetails> modules, String moduleName) {
    ModuleVersionDetails mvd = moduleVersionReader.fromSource(moduleName);
    if (mvd != null) {
        modules.add(mvd);
        collectModulesFromDependencies(modules, mvd);
    }
}
Also used : ModuleVersionDetails(org.eclipse.ceylon.cmr.api.ModuleVersionDetails)

Aggregations

ModuleVersionDetails (org.eclipse.ceylon.cmr.api.ModuleVersionDetails)32 ModuleDependencyInfo (org.eclipse.ceylon.cmr.api.ModuleDependencyInfo)10 ModuleVersionArtifact (org.eclipse.ceylon.cmr.api.ModuleVersionArtifact)10 File (java.io.File)7 ArrayList (java.util.ArrayList)6 ModuleVersionQuery (org.eclipse.ceylon.cmr.api.ModuleVersionQuery)5 ModuleVersionResult (org.eclipse.ceylon.cmr.api.ModuleVersionResult)5 OpenNode (org.eclipse.ceylon.cmr.spi.OpenNode)5 ArtifactContext (org.eclipse.ceylon.cmr.api.ArtifactContext)4 Node (org.eclipse.ceylon.cmr.spi.Node)4 ModuleSpec (org.eclipse.ceylon.common.ModuleSpec)4 HashSet (java.util.HashSet)3 TreeSet (java.util.TreeSet)3 RepositoryManager (org.eclipse.ceylon.cmr.api.RepositoryManager)3 ToolUsageError (org.eclipse.ceylon.common.tool.ToolUsageError)3 ArtifactResult (org.eclipse.ceylon.model.cmr.ArtifactResult)3 List (java.util.List)2 Map (java.util.Map)2 CmrRepository (org.eclipse.ceylon.cmr.api.CmrRepository)2 ModuleInfo (org.eclipse.ceylon.cmr.api.ModuleInfo)2