Search in sources :

Example 1 with Overrides

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

Example 2 with Overrides

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

the class LazyModuleSourceMapper method preResolveDependenciesIfRequired.

@Override
public void preResolveDependenciesIfRequired(RepositoryManager repositoryManager) {
    AbstractModelLoader modelLoader = getModuleManager().getModelLoader();
    if (!modelLoader.isFullyExportMavenDependencies())
        return;
    if (statusPrinter != null) {
        statusPrinter.clearLine();
        statusPrinter.log("Pre-resolving dependencies");
    }
    if (verbose) {
        log.printRawLines(WriterKind.NOTICE, "[Pre-resolving dependencies]");
    }
    Set<Module> compiledModules = getCompiledModules();
    Map<String, String> modules = new HashMap<>();
    ModuleImport anyImport = null;
    for (Module module : compiledModules) {
        for (ModuleImport imp : module.getImports()) {
            if (imp.getModule() == null || !compiledModules.contains(imp.getModule())) {
                if (anyImport == null)
                    anyImport = imp;
                String name = imp.getModule().getNameAsString();
                if (imp.getNamespace() != null)
                    name = imp.getNamespace() + ":" + name;
                modules.put(name, imp.getModule().getVersion());
            }
        }
    }
    if (statusPrinter != null) {
        statusPrinter.clearLine();
        statusPrinter.log("Pre-resolving found " + modules.size() + " to pre-resolve");
    }
    if (verbose) {
        log.printRawLines(WriterKind.NOTICE, "[Pre-resolving " + modules.size() + " modules]");
    }
    if (modules.isEmpty())
        return;
    Entry<String, String> first = modules.entrySet().iterator().next();
    CompilerModuleLoader ml = new CompilerModuleLoader(repositoryManager, null, modules, verbose, statusPrinter, log);
    boolean giveup = false;
    try {
        ml.loadModule(first.getKey(), first.getValue(), ModuleScope.COMPILE);
    } catch (ModuleNotFoundException e) {
        attachErrorToDependencyDeclaration(anyImport, "Pre-resolving of module failed: " + e.getMessage(), true);
        giveup = true;
    }
    if (statusPrinter != null) {
        statusPrinter.clearLine();
        // don't try to read the module count if pre-resolving failed
        if (giveup)
            statusPrinter.log("Pre-resolving failed");
        else
            statusPrinter.log("Pre-resolving resolved " + ml.getModuleCount());
    }
    if (verbose) {
        // don't try to read the module count if pre-resolving failed
        if (giveup)
            log.printRawLines(WriterKind.NOTICE, "[Pre-resolved failed]");
        else
            log.printRawLines(WriterKind.NOTICE, "[Pre-resolved " + ml.getModuleCount() + " modules]");
    }
    if (giveup)
        return;
    Overrides overrides = repositoryManager.getOverrides();
    if (overrides == null) {
        overrides = Overrides.create();
        repositoryManager.setOverrides(overrides);
    }
    ml.setupOverrides(overrides);
    ml.cleanup();
}
Also used : AbstractModelLoader(org.eclipse.ceylon.model.loader.AbstractModelLoader) ModuleNotFoundException(org.eclipse.ceylon.cmr.ceylon.loader.ModuleNotFoundException) HashMap(java.util.HashMap) ModuleImport(org.eclipse.ceylon.model.typechecker.model.ModuleImport) Overrides(org.eclipse.ceylon.cmr.api.Overrides) CompilerModuleLoader(org.eclipse.ceylon.compiler.java.loader.CompilerModuleLoader) LazyModule(org.eclipse.ceylon.model.loader.model.LazyModule) Module(org.eclipse.ceylon.model.typechecker.model.Module)

Example 3 with Overrides

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

the class ModuleSourceMapper method overrideModuleImports.

protected void overrideModuleImports(Module module, ArtifactResult artifact) {
    Overrides overrides = getContext().getRepositoryManager().getOverrides();
    if (overrides != null) {
        Set<ModuleDependencyInfo> existingModuleDependencies = new HashSet<>();
        for (ModuleImport i : module.getImports()) {
            Module m = i.getModule();
            if (m != null) {
                existingModuleDependencies.add(new ModuleDependencyInfo(i.getNamespace(), m.getNameAsString(), m.getVersion(), i.isOptional(), i.isExport(), i.getNativeBackends()));
            }
        }
        ModuleInfo sourceModuleInfo = new ModuleInfo(artifact.namespace(), artifact.name(), artifact.version(), artifact.groupId(), artifact.artifactId(), artifact.classifier(), null, existingModuleDependencies);
        ModuleInfo newModuleInfo = overrides.applyOverrides(artifact.name(), artifact.version(), sourceModuleInfo);
        List<ModuleImport> newModuleImports = new ArrayList<>();
        for (ModuleDependencyInfo dep : newModuleInfo.getDependencies()) {
            Module dependency = getModuleManager().getOrCreateModule(ModuleManager.splitModuleName(dep.getName()), dep.getVersion());
            Backends backends = dependency.getNativeBackends();
            ModuleImport newImport = new ModuleImport(dep.getNamespace(), dependency, dep.isOptional(), dep.isExport(), backends);
            newModuleImports.add(newImport);
        }
        module.overrideImports(newModuleImports);
    }
}
Also used : Backends(org.eclipse.ceylon.common.Backends) ModuleInfo(org.eclipse.ceylon.cmr.api.ModuleInfo) ModuleImport(org.eclipse.ceylon.model.typechecker.model.ModuleImport) ArrayList(java.util.ArrayList) Overrides(org.eclipse.ceylon.cmr.api.Overrides) ModuleDependencyInfo(org.eclipse.ceylon.cmr.api.ModuleDependencyInfo) Module(org.eclipse.ceylon.model.typechecker.model.Module) HashSet(java.util.HashSet)

Example 4 with Overrides

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

the class Main method setup.

/**
 * Sets up the Ceylon metamodel by adding the specified module to it. This does not run any Ceylon code,
 * nor does it reset the metamodel first. You can repeatedly invoke this method to add new Ceylon modules
 * to the metamodel.
 *
 * @param module the module name to load.
 * @param version the version to load. Ignored if the module is the default module.
 */
public void setup(String module, String version) {
    Overrides parsedOverrides = Overrides.getDistOverrides();
    if (overrides != null) {
        try {
            parsedOverrides = parsedOverrides.append(overrides);
        } catch (RuntimeException e) {
            throw e;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    Metamodel.resetModuleManager(new OverridesRuntimeResolver(parsedOverrides));
    if (moduleClassLoader == null)
        setupModuleClassLoader(module);
    if (classPath == null) {
        classPath = new ClassPath(parsedOverrides);
        visited = new HashSet<ClassPath.Module>();
        registerInMetamodel("ceylon.language", Versions.CEYLON_VERSION_NUMBER, false);
        registerInMetamodel("org.eclipse.ceylon.common", Versions.CEYLON_VERSION_NUMBER, false);
        registerInMetamodel("org.eclipse.ceylon.model", Versions.CEYLON_VERSION_NUMBER, false);
    }
    if (module.equals(org.eclipse.ceylon.model.typechecker.model.Module.DEFAULT_MODULE_NAME))
        version = null;
    registerInMetamodel(module, version, false);
}
Also used : Overrides(org.eclipse.ceylon.cmr.api.Overrides) ArtifactOverrides(org.eclipse.ceylon.cmr.api.ArtifactOverrides) Java9Module(org.eclipse.ceylon.model.loader.Java9ModuleReader.Java9Module) InvocationTargetException(java.lang.reflect.InvocationTargetException) RepositoryException(org.eclipse.ceylon.model.cmr.RepositoryException) IOException(java.io.IOException) ConstantPoolException(org.eclipse.ceylon.langtools.classfile.ConstantPoolException) OverridesRuntimeResolver(org.eclipse.ceylon.cmr.api.OverridesRuntimeResolver)

Example 5 with Overrides

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

the class AetherUtils method fetchDependencies.

private ArtifactResult fetchDependencies(RepositoryManager manager, CmrRepository repository, String groupId, String artifactId, String classifier, String version, boolean fetchSingleArtifact, String repositoryDisplayString) {
    Overrides overrides = repository.getRoot().getService(Overrides.class);
    ArtifactOverrides ao = null;
    log.debug("Overrides for " + canonicalForm(groupId, artifactId, classifier, version) + " in " + overrides.getSource());
    ArtifactContext context = getArtifactContext(groupId, artifactId, classifier, version);
    if (overrides != null) {
        ao = overrides.getArtifactOverrides(context);
        log.debug(ao != null ? "-> found overrides" : "-> no overrides");
    }
    // entire replacement
    ArtifactContext replacementContext = null;
    if (ao != null && ao.getReplace() != null) {
        replacementContext = ao.getReplace().getArtifactContext();
    } else if (overrides != null) {
        replacementContext = overrides.replace(context);
    }
    if (replacementContext != null) {
        log.debug(String.format("[Maven-Overrides] Replacing %s with %s.", context, replacementContext));
        // replace fetched dependency
        String[] nameToGroupArtifactIds = nameToGroupArtifactIds(replacementContext.getName());
        if (nameToGroupArtifactIds != null) {
            groupId = nameToGroupArtifactIds[0];
            artifactId = nameToGroupArtifactIds[1];
            classifier = nameToGroupArtifactIds[2];
            version = replacementContext.getVersion();
            // new AO
            context = getArtifactContext(groupId, artifactId, classifier, version);
            ao = overrides.getArtifactOverrides(context);
        }
    }
    // version replacement
    if (overrides != null && overrides.isVersionOverridden(context)) {
        version = overrides.getVersionOverride(context);
        context.setVersion(version);
    }
    if (ao != null && ao.hasVersion()) {
        version = ao.getVersion();
        context.setVersion(version);
        log.debug("Using version " + version);
    }
    // classifier replacement
    if (ao != null && ao.hasClassifier()) {
        classifier = ao.getClassifier();
        log.debug("Using classifier " + classifier);
    }
    final String name = MavenUtils.moduleName(groupId, artifactId, classifier);
    // only used for messages
    final String coordinates = canonicalForm(groupId, artifactId, classifier, version);
    try {
        DependencyDescriptor info = impl.getDependencies(groupId, artifactId, version, classifier, null, fetchSingleArtifact);
        if (info == null) {
            log.debug("No artifact found: " + coordinates);
            return null;
        }
        final SingleArtifactResult result;
        if (fetchSingleArtifact) {
            result = new SingleArtifactResult(repository, name, version, groupId, artifactId, classifier, info.getFile(), repositoryDisplayString);
        } else {
            final List<ArtifactResult> dependencies = new ArrayList<>();
            for (DependencyDescriptor dep : info.getDependencies()) {
                String dGroupId = dep.getGroupId();
                String dArtifactId = dep.getArtifactId();
                String dClassifier = dep.getClassifier();
                String dVersion = dep.getVersion();
                boolean export = false;
                boolean optional = dep.isOptional();
                boolean isCeylon = false;
                ModuleScope scope = toModuleScope(dep);
                ArtifactContext dContext = null;
                if (overrides != null)
                    dContext = getArtifactContext(dGroupId, dArtifactId, dClassifier, dVersion);
                if (overrides != null) {
                    if (overrides.isRemoved(dContext) || (ao != null && ao.isRemoved(dContext))) {
                        log.debug(String.format("[Maven-Overrides] Removing %s from %s.", dep, context));
                        // skip dependency
                        continue;
                    }
                    if (ao != null && ao.isAddedOrUpdated(dContext)) {
                        log.debug(String.format("[Maven-Overrides] Replacing %s from %s.", dep, context));
                        // skip dependency
                        continue;
                    }
                    ArtifactContext replace = overrides.replace(dContext);
                    if (replace != null) {
                        dContext = replace;
                        String[] groupArtifactIds = nameToGroupArtifactIds(replace.getName());
                        if (groupArtifactIds == null) {
                            isCeylon = true;
                        } else {
                            dGroupId = groupArtifactIds[0];
                            dArtifactId = groupArtifactIds[1];
                            dClassifier = groupArtifactIds[2];
                        }
                        dVersion = replace.getVersion();
                    }
                    if (ao != null) {
                        if (ao.isShareOverridden(dContext))
                            export = ao.isShared(dContext);
                        if (ao.isOptionalOverridden(dContext))
                            optional = ao.isOptional(dContext);
                    }
                }
                // do we have a version update?
                if (overrides != null && overrides.isVersionOverridden(dContext)) {
                    dVersion = overrides.getVersionOverride(dContext);
                }
                ArtifactResult dr;
                if (isCeylon)
                    dr = createArtifactResult(manager, dContext.getNamespace(), dContext.getName(), dVersion, export, optional, scope, repositoryDisplayString);
                else
                    dr = createArtifactResult(manager, repository, dGroupId, dArtifactId, dClassifier, dVersion, export, optional, scope, repositoryDisplayString, dep.getExclusions());
                dependencies.add(dr);
            }
            if (ao != null) {
                for (DependencyOverride addon : ao.getAdd()) {
                    ArtifactContext dContext = addon.getArtifactContext();
                    String dVersion = overrides.getVersionOverride(dContext);
                    dependencies.add(createArtifactResult(manager, repository, dContext, dVersion, addon.isShared(), addon.isOptional(), ModuleScope.COMPILE, repositoryDisplayString, null));
                    log.debug(String.format("[Maven-Overrides] Added %s to %s.", addon.getArtifactContext(), context));
                }
            }
            result = new AetherArtifactResult(repository, name, version, groupId, artifactId, classifier, info.getFile(), dependencies, repositoryDisplayString);
        }
        if (ao != null && ao.getFilter() != null) {
            result.setFilter(PathFilterParser.parse(ao.getFilter()));
        }
        return result;
    } catch (IOException e) {
        throw new IllegalStateException(e);
    } catch (AetherException e) {
        log.debug("Could not resolve artifact [" + coordinates + "] : " + e);
        return null;
    }
}
Also used : ArtifactOverrides(org.eclipse.ceylon.cmr.api.ArtifactOverrides) DependencyDescriptor(org.eclipse.ceylon.cmr.resolver.aether.DependencyDescriptor) ArrayList(java.util.ArrayList) ModuleScope(org.eclipse.ceylon.model.cmr.ModuleScope) MavenArtifactContext(org.eclipse.ceylon.cmr.api.MavenArtifactContext) ArtifactContext(org.eclipse.ceylon.cmr.api.ArtifactContext) IOException(java.io.IOException) LazyArtifactResult(org.eclipse.ceylon.cmr.impl.LazyArtifactResult) AbstractArtifactResult(org.eclipse.ceylon.cmr.impl.AbstractArtifactResult) ArtifactResult(org.eclipse.ceylon.model.cmr.ArtifactResult) ArtifactOverrides(org.eclipse.ceylon.cmr.api.ArtifactOverrides) Overrides(org.eclipse.ceylon.cmr.api.Overrides) DependencyOverride(org.eclipse.ceylon.cmr.api.DependencyOverride) AetherException(org.eclipse.ceylon.cmr.resolver.aether.AetherException)

Aggregations

Overrides (org.eclipse.ceylon.cmr.api.Overrides)6 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 ArtifactOverrides (org.eclipse.ceylon.cmr.api.ArtifactOverrides)2 Module (org.eclipse.ceylon.model.typechecker.model.Module)2 ModuleImport (org.eclipse.ceylon.model.typechecker.model.ModuleImport)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 HashSet (java.util.HashSet)1 ArtifactContext (org.eclipse.ceylon.cmr.api.ArtifactContext)1 CmrRepository (org.eclipse.ceylon.cmr.api.CmrRepository)1 DependencyOverride (org.eclipse.ceylon.cmr.api.DependencyOverride)1 MavenArtifactContext (org.eclipse.ceylon.cmr.api.MavenArtifactContext)1 ModuleDependencyInfo (org.eclipse.ceylon.cmr.api.ModuleDependencyInfo)1 ModuleInfo (org.eclipse.ceylon.cmr.api.ModuleInfo)1 OverridesRuntimeResolver (org.eclipse.ceylon.cmr.api.OverridesRuntimeResolver)1 RepositoryManager (org.eclipse.ceylon.cmr.api.RepositoryManager)1 ModuleNotFoundException (org.eclipse.ceylon.cmr.ceylon.loader.ModuleNotFoundException)1 AbstractArtifactResult (org.eclipse.ceylon.cmr.impl.AbstractArtifactResult)1 LazyArtifactResult (org.eclipse.ceylon.cmr.impl.LazyArtifactResult)1