Search in sources :

Example 51 with ArtifactContext

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

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

the class NpmRepository method getArtifactResultInternal.

@Override
protected ArtifactResult getArtifactResultInternal(RepositoryManager manager, Node node) {
    ArtifactContext context = ArtifactContext.fromNode(node);
    if (context == null) {
        return null;
    }
    List<String> suffixes = Arrays.asList(context.getSuffixes());
    if (!suffixes.contains(ArtifactContext.JS) && !suffixes.contains(ArtifactContext.NPM_DESCRIPTOR)) {
        return null;
    }
    return new NpmArtifactResult(this, manager, context.getName(), context.getVersion(), node);
}
Also used : ArtifactContext(org.eclipse.ceylon.cmr.api.ArtifactContext)

Example 53 with ArtifactContext

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

the class AetherTestCase method testWithSources.

@Test
public void testWithSources() throws Throwable {
    CmrRepository repository = AetherRepository.createRepository(log, false, 60000);
    RepositoryManager manager = new SimpleRepositoryManager(repository, log);
    ArtifactResult result = manager.getArtifactResult(new ArtifactContext(MavenRepository.NAMESPACE, "org.slf4j:slf4j-api", "1.6.4", ArtifactContext.LEGACY_SRC));
    Assert.assertNotNull(result);
    File artifact = result.artifact();
    boolean exists = false;
    try {
        Assert.assertNotNull(artifact);
        Assert.assertTrue(artifact.exists());
        exists = true;
    } finally {
        if (exists) {
            // delete this one
            Assert.assertTrue(artifact.delete());
        }
    }
}
Also used : SimpleRepositoryManager(org.eclipse.ceylon.cmr.impl.SimpleRepositoryManager) RepositoryManager(org.eclipse.ceylon.cmr.api.RepositoryManager) SimpleRepositoryManager(org.eclipse.ceylon.cmr.impl.SimpleRepositoryManager) MavenArtifactContext(org.eclipse.ceylon.cmr.api.MavenArtifactContext) ArtifactContext(org.eclipse.ceylon.cmr.api.ArtifactContext) CmrRepository(org.eclipse.ceylon.cmr.api.CmrRepository) File(java.io.File) ArtifactResult(org.eclipse.ceylon.model.cmr.ArtifactResult) Test(org.junit.Test)

Example 54 with ArtifactContext

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

the class ResourceArtifactCreatorImpl method copy.

public Set<String> copy(Collection<String> resFiles) throws IOException {
    if (resFiles == null || resFiles.isEmpty()) {
        return Collections.emptySet();
    }
    Map<String, File> toCopy = new HashMap<String, File>();
    for (String res : resFiles) {
        File relRes = getDestinationFile(moduleName, res);
        if (relRes != null) {
            toCopy.put(res, relRes);
        }
    }
    if (toCopy.isEmpty())
        return Collections.emptySet();
    final ArtifactContext ac = new ArtifactContext(null, moduleName, moduleVersion, ArtifactContext.RESOURCES);
    ac.setThrowErrorIfMissing(false);
    File resDir = Files.createTempDirectory("ceylon-resources-").toFile();
    try {
        for (Map.Entry<String, File> res : toCopy.entrySet()) {
            // Copy the file to the resource dir
            FileUtil.copy(null, new File(res.getKey()), resDir, res.getValue());
        }
        repoManager.putArtifact(ac, resDir);
    } finally {
        FileUtil.deleteQuietly(resDir);
    }
    return new HashSet<String>(resFiles);
}
Also used : HashMap(java.util.HashMap) ArtifactContext(org.eclipse.ceylon.cmr.api.ArtifactContext) File(java.io.File) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Example 55 with ArtifactContext

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

the class RootRepositoryManager method artifactNotFound.

@Override
protected ArtifactResult artifactNotFound(ArtifactContext context) throws RepositoryException {
    boolean hasRemote = false;
    StringBuilder reps = new StringBuilder();
    for (CmrRepository rep : getRepositories()) {
        if (rep.getRoot().isRemote() && !isOffline(rep)) {
            hasRemote = true;
            reps.append(rep.getDisplayString());
            reps.append('\n');
        }
    }
    if (hasRemote && cache != null) {
        // Create a .missing file in the cache to mark that we tried to locate the file but it didn't exist
        Node parent = cache.findParent(context);
        if (parent != null) {
            context.toNode(parent);
            try {
                // fileContentStore cannot be null if we have a cache
                File parentDir = fileContentStore.getFile(parent);
                String[] names = cache.getArtifactNames(context);
                File missingFile = new File(parentDir, names[0].concat(MISSING));
                if (!missingFile.exists()) {
                    if (context.getSearchRepository() == cache) {
                        ArtifactContext unpreferred = new ArtifactContext(context.getNamespace(), context.getName(), context.getVersion(), context.getSuffixes());
                        unpreferred.copySettingsFrom(context);
                        return getArtifactResult(unpreferred);
                    } else {
                        FileUtil.mkdirs(parentDir);
                        try (FileWriter writer = new FileWriter(missingFile, false)) {
                            // We write the list of remote repositories we tried
                            // This is not currently used but might be useful in the future
                            writer.write(reps.toString());
                        } catch (IOException e) {
                            log.error(e.toString());
                        }
                    }
                }
            } finally {
                ArtifactContext.removeNode(parent);
            }
        }
    }
    return super.artifactNotFound(context);
}
Also used : OpenNode(org.eclipse.ceylon.cmr.spi.OpenNode) Node(org.eclipse.ceylon.cmr.spi.Node) FileWriter(java.io.FileWriter) ArtifactContext(org.eclipse.ceylon.cmr.api.ArtifactContext) IOException(java.io.IOException) CmrRepository(org.eclipse.ceylon.cmr.api.CmrRepository) File(java.io.File)

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