Search in sources :

Example 1 with ModuleScope

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

the class MavenUtils method getDependencies.

public static ModuleInfo getDependencies(InputStream stream, String name, String version) throws IOException {
    Document doc;
    try {
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        doc = dBuilder.parse(stream);
    } catch (ParserConfigurationException e) {
        throw new IOException(e);
    } catch (SAXException e) {
        throw new IOException(e);
    }
    doc.getDocumentElement().normalize();
    Element root = doc.getDocumentElement();
    String modGroupId = getText(root, "groupId");
    // can be null, inherited from parent
    if (modGroupId == null) {
        Element parent = getFirstElement(root, "parent");
        if (parent != null)
            modGroupId = getText(parent, "groupId");
    }
    String modArtifactId = getText(root, "artifactId");
    String classifier = getText(root, "classifier");
    String modVersion = getText(root, "version");
    // can be null, inherited from parent
    if (modVersion == null) {
        Element parent = getFirstElement(root, "parent");
        if (parent != null)
            modVersion = getText(parent, "version");
    }
    String modName = modGroupId + ":" + modArtifactId;
    if (name != null && !name.equals(modName))
        return null;
    if (version != null && !version.equals(modVersion))
        return null;
    Element deps = getFirstElement(root, "dependencies");
    Set<ModuleDependencyInfo> ret = new HashSet<>();
    if (deps != null) {
        NodeList depList = deps.getElementsByTagName("dependency");
        if (depList != null) {
            for (int i = 0; i < depList.getLength(); i++) {
                Element dep = (Element) depList.item(i);
                String depGroupId = getText(dep, "groupId");
                String depArtifactId = getText(dep, "artifactId");
                String depClassifier = getText(dep, "classifier");
                String depVersion = getText(dep, "version");
                String depScope = getText(dep, "scope");
                String depOptional = getText(dep, "optional");
                ModuleScope scope;
                // keep compile, runtime, provided
                if (depScope != null && (depScope.equals("system") || depScope.equals("test")))
                    continue;
                if ("provided".equals(depScope))
                    scope = ModuleScope.PROVIDED;
                else if ("runtime".equals(depScope))
                    scope = ModuleScope.RUNTIME;
                else
                    scope = ModuleScope.COMPILE;
                ret.add(new ModuleDependencyInfo(MavenRepository.NAMESPACE, moduleName(depGroupId, depArtifactId, depClassifier), depVersion, "true".equals(depOptional), false, Backends.JAVA, scope));
            }
        }
    }
    return new ModuleInfo(MavenRepository.NAMESPACE, modName, modVersion, modGroupId, modArtifactId, classifier, null, ret);
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) ModuleScope(org.eclipse.ceylon.model.cmr.ModuleScope) IOException(java.io.IOException) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ModuleInfo(org.eclipse.ceylon.cmr.api.ModuleInfo) ModuleDependencyInfo(org.eclipse.ceylon.cmr.api.ModuleDependencyInfo) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) HashSet(java.util.HashSet)

Example 2 with ModuleScope

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

the class JvmBackendUtil method loadStaticMetamodel.

public static void loadStaticMetamodel(InputStream is, List<String> dexEntries, StaticMetamodelLoader staticMetamodelLoader) {
    try (BufferedReader reader = new BufferedReader(new InputStreamReader(is))) {
        String line;
        ModuleSpec module = null;
        SortedSet<String> packages = new TreeSet<>();
        List<ArtifactResult> imports = new LinkedList<ArtifactResult>();
        while ((line = reader.readLine()) != null) {
            if (line.startsWith("=")) {
                if (module != null)
                    finishLoadingModule(module, packages, imports, dexEntries, staticMetamodelLoader);
                module = ModuleSpec.parse(line.substring(1));
                packages.clear();
                imports.clear();
                continue;
            }
            boolean _optional = false;
            boolean _shared = false;
            if (line.startsWith("?")) {
                _optional = true;
                line = line.substring(1);
            }
            if (line.startsWith("+")) {
                _shared = true;
                line = line.substring(1);
            }
            // SERIOUSLY!!
            final boolean optional = _optional;
            final boolean shared = _shared;
            if (line.startsWith("@")) {
                packages.add(line.substring(1));
                continue;
            }
            // it's an import
            ModuleSpec importSpec = ModuleSpec.parse(line);
            final String namespace = ModuleUtil.getNamespaceFromUri(importSpec.getName());
            final String name = ModuleUtil.getModuleNameFromUri(importSpec.getName());
            final String version = importSpec.getVersion();
            imports.add(new ArtifactResult() {

                @Override
                public String namespace() {
                    return namespace;
                }

                @Override
                public String name() {
                    return name;
                }

                @Override
                public String version() {
                    return version;
                }

                @Override
                public boolean optional() {
                    return optional;
                }

                @Override
                public boolean exported() {
                    return shared;
                }

                @Override
                public ArtifactResultType type() {
                    // Is this important?
                    return ArtifactResultType.OTHER;
                }

                @Override
                public VisibilityType visibilityType() {
                    return VisibilityType.STRICT;
                }

                @Override
                public File artifact() throws RepositoryException {
                    return null;
                }

                @Override
                public PathFilter filter() {
                    return null;
                }

                @Override
                public List<ArtifactResult> dependencies() throws RepositoryException {
                    return null;
                }

                @Override
                public String artifactId() {
                    return ModuleUtil.getMavenArtifactIdIfMavenModule(name);
                }

                @Override
                public String groupId() {
                    return ModuleUtil.getMavenGroupIdIfMavenModule(name);
                }

                @Override
                public String classifier() {
                    return ModuleUtil.getMavenClassifierIfMavenModule(name);
                }

                @Override
                public String repositoryDisplayString() {
                    return "Android dependency";
                }

                @Override
                public Repository repository() {
                    return null;
                }

                @Override
                public ModuleScope moduleScope() {
                    return ModuleScope.COMPILE;
                }

                @Override
                public List<Exclusion> getExclusions() {
                    return null;
                }
            });
        }
        if (module != null)
            finishLoadingModule(module, packages, imports, dexEntries, staticMetamodelLoader);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : PathFilter(org.eclipse.ceylon.model.cmr.PathFilter) InputStreamReader(java.io.InputStreamReader) ModuleScope(org.eclipse.ceylon.model.cmr.ModuleScope) RepositoryException(org.eclipse.ceylon.model.cmr.RepositoryException) IOException(java.io.IOException) LinkedList(java.util.LinkedList) ArtifactResult(org.eclipse.ceylon.model.cmr.ArtifactResult) Repository(org.eclipse.ceylon.model.cmr.Repository) ArtifactResultType(org.eclipse.ceylon.model.cmr.ArtifactResultType) ModuleSpec(org.eclipse.ceylon.common.ModuleSpec) VisibilityType(org.eclipse.ceylon.model.cmr.VisibilityType) TreeSet(java.util.TreeSet) BufferedReader(java.io.BufferedReader) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) ZipFile(java.util.zip.ZipFile) File(java.io.File)

Example 3 with ModuleScope

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

Example 4 with ModuleScope

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

the class Overrides method applyOverrides.

public ModuleInfo applyOverrides(String module, String version, ModuleInfo source) {
    ArtifactOverrides artifactOverrides = getArtifactOverrides(new ArtifactContext(null, module, version));
    Set<ModuleDependencyInfo> result = new HashSet<ModuleDependencyInfo>();
    for (ModuleDependencyInfo dep : source.getDependencies()) {
        String depNamespace = dep.getNamespace();
        String depName = dep.getName();
        String depVersion = dep.getVersion();
        boolean optional = dep.isOptional();
        boolean export = dep.isExport();
        ModuleScope scope = dep.getModuleScope();
        Backends backends = dep.getNativeBackends();
        ArtifactContext ctx = new ArtifactContext(depNamespace, depName, depVersion);
        if ((artifactOverrides != null && artifactOverrides.isRemoved(ctx)) || isRemoved(ctx))
            continue;
        if (artifactOverrides != null && artifactOverrides.isAddedOrUpdated(ctx))
            continue;
        ArtifactContext replacement = replace(ctx);
        if (replacement != null) {
            depNamespace = replacement.getNamespace();
            depName = replacement.getName();
            depVersion = replacement.getVersion();
            ctx = replacement;
        }
        if (isVersionOverridden(ctx))
            depVersion = getVersionOverride(ctx);
        if (artifactOverrides != null) {
            if (artifactOverrides.isShareOverridden(ctx))
                export = artifactOverrides.isShared(ctx);
            if (artifactOverrides.isOptionalOverridden(ctx))
                optional = artifactOverrides.isOptional(ctx);
        }
        result.add(new ModuleDependencyInfo(depNamespace, depName, depVersion, optional, export, backends, scope));
    }
    String filter = source.getFilter();
    if (artifactOverrides != null) {
        if (artifactOverrides.getFilter() != null)
            filter = artifactOverrides.getFilter();
        for (DependencyOverride add : artifactOverrides.getAdd()) {
            ArtifactContext addContext = add.getArtifactContext();
            result.add(new ModuleDependencyInfo(addContext.getNamespace(), addContext.getName(), addContext.getVersion(), add.isOptional(), add.isShared()));
        }
    }
    return new ModuleInfo(source.getNamespace(), module, version, source.getGroupId(), source.getArtifactId(), source.getClassifier(), filter, result);
}
Also used : Backends(org.eclipse.ceylon.common.Backends) ModuleScope(org.eclipse.ceylon.model.cmr.ModuleScope) HashSet(java.util.HashSet)

Aggregations

ModuleScope (org.eclipse.ceylon.model.cmr.ModuleScope)4 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 ArtifactResult (org.eclipse.ceylon.model.cmr.ArtifactResult)2 BufferedReader (java.io.BufferedReader)1 File (java.io.File)1 InputStreamReader (java.io.InputStreamReader)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 TreeSet (java.util.TreeSet)1 ZipFile (java.util.zip.ZipFile)1 DocumentBuilder (javax.xml.parsers.DocumentBuilder)1 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 ArtifactContext (org.eclipse.ceylon.cmr.api.ArtifactContext)1 ArtifactOverrides (org.eclipse.ceylon.cmr.api.ArtifactOverrides)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