Search in sources :

Example 1 with RepositoryManager

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

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

the class ToolLoader method loadModule.

public ClassLoader loadModule(String name, String version, String overrides) {
    try {
        // Ok, now for something really crappy to force loading of the required module
        String loaderClassName;
        if (loader.getClass().getName().equals("org.jboss.modules.ModuleClassLoader") || loader.getClass().getName().equals("ceylon.modules.jboss.runtime.CeylonModuleClassLoader")) {
            // If we run using the Ceylon runtime
            loaderClassName = "org.eclipse.ceylon.module.loader.JBossModuleLoader";
        } else {
            // If we run in a normal Java environment
            loaderClassName = "org.eclipse.ceylon.module.loader.FlatpathModuleLoader";
        }
        Class<?> loaderClass = loader.loadClass(loaderClassName);
        CeylonRepoManagerBuilder repositoryManagerBuilder = CeylonUtils.repoManager();
        if (overrides != null)
            repositoryManagerBuilder.overrides(overrides);
        RepositoryManager repositoryManager = repositoryManagerBuilder.buildManager();
        Constructor<?> loaderConstr = loaderClass.getConstructor(RepositoryManager.class, ClassLoader.class);
        Object modLoader = loaderConstr.newInstance(repositoryManager, loader);
        Method loadMth = loaderClass.getMethod("loadModule", String.class, String.class);
        ClassLoader mcl = (ClassLoader) loadMth.invoke(modLoader, name, version);
        return mcl;
    } catch (ReflectiveOperationException e) {
        Throwable cause = (e.getCause() != null) ? e.getCause() : e;
        throw new ToolError("Could not load module '" + name + "/" + version + "' because: " + cause.toString(), e) {
        };
    }
}
Also used : CeylonRepoManagerBuilder(org.eclipse.ceylon.cmr.ceylon.CeylonUtils.CeylonRepoManagerBuilder) RepositoryManager(org.eclipse.ceylon.cmr.api.RepositoryManager) Method(java.lang.reflect.Method)

Example 3 with RepositoryManager

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

the class ModuleLoaderTest method testBug7084.

@Test
public void testBug7084() throws IOException {
    Path flatRepo = Files.createTempDirectory("flat-repo");
    try {
        RepositoryManager repositoryManager = CeylonUtils.repoManager().systemRepo("../dist/dist/repo").userRepos(Arrays.asList("flat:" + flatRepo.toAbsolutePath().toString())).buildManager();
        File artifact = repositoryManager.getArtifact(null, "ceylon.collection", "1.3.2");
        Assert.assertNotNull(artifact);
        Files.copy(artifact.toPath(), flatRepo.resolve("ceylon.collection-1.3.2.jar"));
        ModuleLoader moduleLoader = new FlatpathModuleLoader(repositoryManager, null, Collections.<String, String>emptyMap(), true);
        try {
            moduleLoader.loadModule("ceylon.collection", "1.3.2");
        } catch (ModuleNotFoundException e) {
            throw new RuntimeException(e);
        }
    } finally {
        IOUtils.deleteRecursively(flatRepo.toFile());
    }
}
Also used : Path(java.nio.file.Path) FlatpathModuleLoader(org.eclipse.ceylon.module.loader.FlatpathModuleLoader) ModuleLoader(org.eclipse.ceylon.cmr.ceylon.loader.ModuleLoader) FlatpathModuleLoader(org.eclipse.ceylon.module.loader.FlatpathModuleLoader) ModuleNotFoundException(org.eclipse.ceylon.cmr.ceylon.loader.ModuleNotFoundException) SimpleRepositoryManager(org.eclipse.ceylon.cmr.impl.SimpleRepositoryManager) RepositoryManager(org.eclipse.ceylon.cmr.api.RepositoryManager) File(java.io.File) Test(org.junit.Test)

Example 4 with RepositoryManager

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

the class ModuleLoaderTest method testBug7062.

@Test
public void testBug7062() throws ModuleNotFoundException {
    RepositoryManager repositoryManager = CeylonUtils.repoManager().systemRepo("../dist/dist/repo").overrides("test/" + ModuleLoaderTest.class.getPackage().getName().replace('.', '/') + "/bug7062-overrides.xml").buildManager();
    Map<String, String> extraModules = new HashMap<>();
    TestableModuleLoader moduleLoader = new TestableModuleLoader(repositoryManager, null, extraModules, false);
    moduleLoader.loadModule("ceylon.interop.persistence", "1.3.1", ModuleScope.RUNTIME);
}
Also used : HashMap(java.util.HashMap) SimpleRepositoryManager(org.eclipse.ceylon.cmr.impl.SimpleRepositoryManager) RepositoryManager(org.eclipse.ceylon.cmr.api.RepositoryManager) Test(org.junit.Test)

Example 5 with RepositoryManager

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

the class ModuleLoaderTest method testNoDotsFromMavenWithOverrides.

@Test
public void testNoDotsFromMavenWithOverrides() throws ModuleNotFoundException {
    CmrRepository repository = AetherRepository.createRepository(log, false, 60000);
    Overrides ov = RepositoryManagerBuilder.parseOverrides(overrides);
    Assert.assertTrue(ov.getAddedArtifacts().isEmpty());
    RepositoryManager manager = new SimpleRepositoryManager(repository, log, ov);
    Map<String, String> extraModules = new HashMap<>();
    extraModules.put("org.antlr:stringtemplate", "3.2.1");
    // this one has an override that adds aopalliance:aopalliance/1.0
    extraModules.put("org.postgresql:postgresql", "9.4.1208");
    TestableModuleLoader moduleLoader = new TestableModuleLoader(manager, null, extraModules, true);
    moduleLoader.loadModule("com.google.inject:guice", "4.0", ModuleScope.RUNTIME);
    // Check that we got them
    Assert.assertEquals("2.7.7", moduleLoader.getModuleVersion("maven:antlr:antlr"));
    Assert.assertEquals("1.0", moduleLoader.getModuleVersion("maven:aopalliance:aopalliance"));
}
Also used : SimpleRepositoryManager(org.eclipse.ceylon.cmr.impl.SimpleRepositoryManager) HashMap(java.util.HashMap) Overrides(org.eclipse.ceylon.cmr.api.Overrides) SimpleRepositoryManager(org.eclipse.ceylon.cmr.impl.SimpleRepositoryManager) RepositoryManager(org.eclipse.ceylon.cmr.api.RepositoryManager) CmrRepository(org.eclipse.ceylon.cmr.api.CmrRepository) Test(org.junit.Test)

Aggregations

RepositoryManager (org.eclipse.ceylon.cmr.api.RepositoryManager)95 Test (org.junit.Test)71 SimpleRepositoryManager (org.eclipse.ceylon.cmr.impl.SimpleRepositoryManager)68 File (java.io.File)47 ArtifactResult (org.eclipse.ceylon.model.cmr.ArtifactResult)32 CmrRepository (org.eclipse.ceylon.cmr.api.CmrRepository)26 ArtifactContext (org.eclipse.ceylon.cmr.api.ArtifactContext)23 ModuleDetails (org.eclipse.ceylon.cmr.api.ModuleSearchResult.ModuleDetails)14 TypeCheckerBuilder (org.eclipse.ceylon.compiler.typechecker.TypeCheckerBuilder)11 RepositoryManagerBuilder (org.eclipse.ceylon.cmr.api.RepositoryManagerBuilder)8 HashMap (java.util.HashMap)7 TypeChecker (org.eclipse.ceylon.compiler.typechecker.TypeChecker)7 ByteArrayInputStream (java.io.ByteArrayInputStream)5 ArrayList (java.util.ArrayList)5 JsModuleManagerFactory (org.eclipse.ceylon.compiler.js.loader.JsModuleManagerFactory)5 ModuleSearchResult (org.eclipse.ceylon.cmr.api.ModuleSearchResult)4 CeylonRepoManagerBuilder (org.eclipse.ceylon.cmr.ceylon.CeylonUtils.CeylonRepoManagerBuilder)4 IOException (java.io.IOException)3 Manifest (java.util.jar.Manifest)3 DefaultRepository (org.eclipse.ceylon.cmr.impl.DefaultRepository)3