Search in sources :

Example 6 with ArtifactResult

use of com.redhat.ceylon.model.cmr.ArtifactResult in project ceylon-compiler by ceylon.

the class CeylonDocModuleManager method addOutputModuleToClassPath.

private void addOutputModuleToClassPath(Module module) {
    ArtifactContext ctx = new ArtifactContext(module.getNameAsString(), module.getVersion(), ArtifactContext.CAR);
    ArtifactResult result = outputRepositoryManager.getArtifactResult(ctx);
    if (result != null)
        getModelLoader().addModuleToClassPath(module, result);
}
Also used : ArtifactContext(com.redhat.ceylon.cmr.api.ArtifactContext) ArtifactResult(com.redhat.ceylon.model.cmr.ArtifactResult)

Example 7 with ArtifactResult

use of com.redhat.ceylon.model.cmr.ArtifactResult in project ceylon-compiler by ceylon.

the class LazyModuleSourceMapper method resolveModule.

@Override
public void resolveModule(ArtifactResult artifact, Module module, ModuleImport moduleImport, LinkedList<Module> dependencyTree, List<PhasedUnits> phasedUnitsOfDependencies, boolean forCompiledModule) {
    String moduleName = module.getNameAsString();
    LazyModuleManager moduleManager = getModuleManager();
    boolean moduleLoadedFromSource = moduleManager.isModuleLoadedFromSource(moduleName);
    boolean isLanguageModule = module == module.getLanguageModule();
    // module in question will be in the classpath
    if (moduleLoadedFromSource || forCompiledModule) {
        String standardisedModuleName = ModuleUtil.toCeylonModuleName(moduleName);
        // check for an already loaded module with the same name but different version
        for (Module loadedModule : getContext().getModules().getListOfModules()) {
            String loadedModuleName = loadedModule.getNameAsString();
            String standardisedLoadedModuleName = ModuleUtil.toCeylonModuleName(loadedModuleName);
            boolean sameModule = loadedModuleName.equals(moduleName);
            boolean similarModule = standardisedLoadedModuleName.equals(standardisedModuleName);
            if ((sameModule || similarModule) && !loadedModule.getVersion().equals(module.getVersion()) && moduleManager.getModelLoader().isModuleInClassPath(loadedModule)) {
                if (sameModule) {
                    String[] versions = VersionComparator.orderVersions(module.getVersion(), loadedModule.getVersion());
                    String error = "source code imports two different versions of module '" + moduleName + "': " + "version '" + versions[0] + "' and version '" + versions[1] + "'";
                    addErrorToModule(dependencyTree.getFirst(), error);
                } else {
                    String moduleA;
                    String moduleB;
                    if (loadedModuleName.compareTo(moduleName) < 0) {
                        moduleA = ModuleUtil.makeModuleName(loadedModuleName, loadedModule.getVersion());
                        moduleB = ModuleUtil.makeModuleName(moduleName, module.getVersion());
                    } else {
                        moduleA = ModuleUtil.makeModuleName(moduleName, module.getVersion());
                        moduleB = ModuleUtil.makeModuleName(loadedModuleName, loadedModule.getVersion());
                    }
                    String error = "source code imports two different versions of similar modules '" + moduleA + "' and '" + moduleB + "'";
                    addWarningToModule(dependencyTree.getFirst(), Warning.similarModule, error);
                }
                return;
            }
        }
    }
    if (moduleLoadedFromSource) {
        super.resolveModule(artifact, module, moduleImport, dependencyTree, phasedUnitsOfDependencies, forCompiledModule);
    } else if (forCompiledModule || isLanguageModule || moduleManager.shouldLoadTransitiveDependencies()) {
        // we only add stuff to the classpath and load the modules if we need them to compile our modules
        // To be able to load it from the corresponding archive
        moduleManager.getModelLoader().addModuleToClassPath(module, artifact);
        if (!module.isDefault() && !moduleManager.getModelLoader().loadCompiledModule(module)) {
            // we didn't find module.class so it must be a java module if it's not the default module
            ((LazyModule) module).setJava(true);
            module.setNativeBackends(Backend.Java.asSet());
            List<ArtifactResult> deps = artifact.dependencies();
            for (ArtifactResult dep : deps) {
                Module dependency = moduleManager.getOrCreateModule(ModuleManager.splitModuleName(dep.name()), dep.version());
                ModuleImport depImport = moduleManager.findImport(module, dependency);
                if (depImport == null) {
                    moduleImport = new ModuleImport(dependency, dep.importType() == ImportType.OPTIONAL, dep.importType() == ImportType.EXPORT, Backend.Java);
                    module.addImport(moduleImport);
                }
            }
        }
        LazyModule lazyModule = (LazyModule) module;
        if (!lazyModule.isJava() && !module.isDefault()) {
            // it must be a Ceylon module
            // default modules don't have any module descriptors so we can't check them
            Overrides overrides = getContext().getRepositoryManager().getOverrides();
            if (overrides != null) {
                if (overrides.getArtifactOverrides(new ArtifactContext(artifact.name(), artifact.version())) != null) {
                    Set<ModuleDependencyInfo> existingModuleDependencies = new HashSet<>();
                    for (ModuleImport i : lazyModule.getImports()) {
                        Module m = i.getModule();
                        if (m != null) {
                            existingModuleDependencies.add(new ModuleDependencyInfo(m.getNameAsString(), m.getVersion(), i.isOptional(), i.isExport()));
                        }
                    }
                    ModuleInfo sourceModuleInfo = new ModuleInfo(null, existingModuleDependencies);
                    ModuleInfo newModuleInfo = overrides.applyOverrides(artifact.name(), artifact.version(), sourceModuleInfo);
                    List<ModuleImport> newModuleImports = new ArrayList<>();
                    for (ModuleDependencyInfo dep : newModuleInfo.getDependencies()) {
                        Module dependency = moduleManager.getOrCreateModule(ModuleManager.splitModuleName(dep.getName()), dep.getVersion());
                        Backends backends = dependency.getNativeBackends();
                        moduleImport = new ModuleImport(dependency, dep.isOptional(), dep.isExport(), backends);
                        newModuleImports.add(moduleImport);
                    }
                    module.overrideImports(newModuleImports);
                }
            }
            if (!Versions.isJvmBinaryVersionSupported(lazyModule.getMajor(), lazyModule.getMinor())) {
                attachErrorToDependencyDeclaration(moduleImport, dependencyTree, "version '" + lazyModule.getVersion() + "' of module '" + module.getNameAsString() + "' was compiled by an incompatible version of the compiler (binary version " + lazyModule.getMajor() + "." + lazyModule.getMinor() + " of module is not compatible with binary version " + Versions.JVM_BINARY_MAJOR_VERSION + "." + Versions.JVM_BINARY_MINOR_VERSION + " of this compiler)");
            }
        }
        // module is now available
        module.setAvailable(true);
    }
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) LazyModuleManager(com.redhat.ceylon.model.loader.model.LazyModuleManager) ArtifactContext(com.redhat.ceylon.cmr.api.ArtifactContext) LazyModule(com.redhat.ceylon.model.loader.model.LazyModule) ArtifactResult(com.redhat.ceylon.model.cmr.ArtifactResult) Backends(com.redhat.ceylon.common.Backends) ModuleInfo(com.redhat.ceylon.cmr.api.ModuleInfo) ModuleImport(com.redhat.ceylon.model.typechecker.model.ModuleImport) Overrides(com.redhat.ceylon.cmr.api.Overrides) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) ModuleDependencyInfo(com.redhat.ceylon.cmr.api.ModuleDependencyInfo) LazyModule(com.redhat.ceylon.model.loader.model.LazyModule) Module(com.redhat.ceylon.model.typechecker.model.Module)

Example 8 with ArtifactResult

use of com.redhat.ceylon.model.cmr.ArtifactResult in project ceylon-compiler by ceylon.

the class CeylonCopyTool method run.

@Override
public void run() throws Exception {
    Set<String> artifacts = new LinkedHashSet<String>();
    boolean defaults = js == null && jvm == null && src == null && scripts == null && docs == null && all == null;
    if (BooleanUtil.isTrue(all)) {
        artifacts.addAll(Arrays.asList(ArtifactContext.allSuffixes()));
    }
    if (BooleanUtil.isTrue(js) || defaults) {
        artifacts.add(ArtifactContext.JS);
        artifacts.add(ArtifactContext.JS_MODEL);
        artifacts.add(ArtifactContext.RESOURCES);
    } else if (BooleanUtil.isFalse(js)) {
        artifacts.remove(ArtifactContext.JS);
        artifacts.remove(ArtifactContext.JS_MODEL);
        artifacts.remove(ArtifactContext.RESOURCES);
    }
    if (BooleanUtil.isTrue(jvm) || defaults) {
        // put the CAR first since its presence will shortcut the other three
        artifacts.add(ArtifactContext.CAR);
        artifacts.add(ArtifactContext.JAR);
        artifacts.add(ArtifactContext.MODULE_PROPERTIES);
        artifacts.add(ArtifactContext.MODULE_XML);
    } else if (BooleanUtil.isFalse(jvm)) {
        artifacts.remove(ArtifactContext.CAR);
        artifacts.remove(ArtifactContext.JAR);
        artifacts.remove(ArtifactContext.MODULE_PROPERTIES);
        artifacts.remove(ArtifactContext.MODULE_XML);
    }
    if (BooleanUtil.isTrue(src)) {
        artifacts.add(ArtifactContext.SRC);
    } else if (BooleanUtil.isFalse(src)) {
        artifacts.remove(ArtifactContext.SRC);
    }
    if (BooleanUtil.isTrue(scripts)) {
        artifacts.add(ArtifactContext.SCRIPTS_ZIPPED);
    } else if (BooleanUtil.isFalse(scripts)) {
        artifacts.remove(ArtifactContext.SCRIPTS_ZIPPED);
    }
    if (BooleanUtil.isTrue(docs)) {
        artifacts.add(ArtifactContext.DOCS);
    } else if (BooleanUtil.isFalse(docs)) {
        artifacts.remove(ArtifactContext.DOCS);
    }
    // Create the list of ArtifactContexts to copy
    List<ArtifactContext> acs = new ArrayList<ArtifactContext>();
    String[] artifactsArray = new String[artifacts.size()];
    artifacts.toArray(artifactsArray);
    for (ModuleSpec module : modules) {
        if (module != ModuleSpec.DEFAULT_MODULE && !module.isVersioned()) {
            String version = checkModuleVersionsOrShowSuggestions(getRepositoryManager(), module.getName(), null, ModuleQuery.Type.ALL, null, null);
            module = new ModuleSpec(module.getName(), version);
        }
        ArtifactContext ac = new ArtifactContext(module.getName(), module.getVersion(), artifactsArray);
        ac.setIgnoreDependencies(!withDependencies);
        ac.setForceOperation(true);
        acs.add(ac);
    }
    // Now do the actual copying
    final boolean logArtifacts = verbose != null && (verbose.contains("all") || verbose.contains("files"));
    ModuleCopycat copier = new ModuleCopycat(getRepositoryManager(), getOutputRepositoryManager(), log, new ModuleCopycat.CopycatFeedback() {

        @Override
        public boolean beforeCopyModule(ArtifactContext ac, int count, int max) throws IOException {
            String module = ModuleUtil.makeModuleName(ac.getName(), ac.getVersion());
            msg("copying.module", module, count + 1, max).flush();
            return true;
        }

        @Override
        public void afterCopyModule(ArtifactContext ac, int count, int max, boolean copied) throws IOException {
            if (!logArtifacts) {
                append(") ").msg((copied) ? "copying.ok" : "copying.skipped").newline().flush();
            }
        }

        @Override
        public boolean beforeCopyArtifact(ArtifactContext ac, ArtifactResult ar, int count, int max) throws IOException {
            if (logArtifacts) {
                if (count == 0) {
                    append(" -- ");
                    append(ar.repositoryDisplayString());
                    newline().flush();
                }
                append("    ").msg("copying.artifact", ar.artifact().getName(), count + 1, max).flush();
            } else {
                if (count > 0) {
                    append(", ");
                } else {
                    append(" (");
                }
                String name = ArtifactContext.getSuffixFromFilename(ar.artifact().getName());
                if (name.startsWith(".") || name.startsWith("-")) {
                    name = name.substring(1);
                } else if ("module-doc".equals(name)) {
                    name = "doc";
                }
                append(name);
            }
            return true;
        }

        @Override
        public void afterCopyArtifact(ArtifactContext ac, ArtifactResult ar, int count, int max, boolean copied) throws IOException {
            if (logArtifacts) {
                append(" ").msg((copied) ? "copying.ok" : "copying.skipped").newline().flush();
            }
        }

        @Override
        public void notFound(ArtifactContext ac) throws IOException {
            String err = getModuleNotFoundErrorMessage(getRepositoryManager(), ac.getName(), ac.getVersion());
            errorAppend(err);
            errorNewline();
        }
    });
    copier.copyModules(acs);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ArrayList(java.util.ArrayList) ArtifactContext(com.redhat.ceylon.cmr.api.ArtifactContext) IOException(java.io.IOException) ModuleCopycat(com.redhat.ceylon.cmr.ceylon.ModuleCopycat) ArtifactResult(com.redhat.ceylon.model.cmr.ArtifactResult) ModuleSpec(com.redhat.ceylon.common.tools.ModuleSpec)

Example 9 with ArtifactResult

use of com.redhat.ceylon.model.cmr.ArtifactResult in project ceylon-compiler by ceylon.

the class ModuleLoadingTool method internalLoadModule.

private boolean internalLoadModule(String name, String version, boolean optional) throws IOException {
    String key = name + "/" + version;
    if (loadedModules.containsKey(key))
        return true;
    if (shouldExclude(name)) {
        // let's not check the version and assume it's provided
        // treat it as a missing optional for the purpose of classpath
        loadedModules.put(key, null);
        return true;
    }
    // remember which version we loaded
    SortedSet<String> loadedVersions = loadedModuleVersions.get(name);
    if (loadedVersions == null) {
        loadedVersions = new TreeSet<>(VersionComparator.INSTANCE);
        loadedModuleVersions.put(name, loadedVersions);
    }
    loadedVersions.add(version);
    RepositoryManager repositoryManager = getRepositoryManager();
    ArtifactContext artifactContext = new ArtifactContext(name, version, ArtifactContext.CAR, ArtifactContext.JAR);
    ArtifactResult result = repositoryManager.getArtifactResult(artifactContext);
    if (!optional && (result == null || result.artifact() == null || !result.artifact().exists())) {
        String err = getModuleNotFoundErrorMessage(repositoryManager, name, version);
        errorAppend(err);
        errorNewline();
        return false;
    }
    // save even missing optional modules as nulls to not re-resolve them
    loadedModules.put(key, result);
    if (result != null) {
        for (ArtifactResult dep : result.dependencies()) {
            internalLoadModule(dep.name(), dep.version(), dep.importType() == ImportType.OPTIONAL);
        }
    }
    return true;
}
Also used : RepositoryManager(com.redhat.ceylon.cmr.api.RepositoryManager) ArtifactContext(com.redhat.ceylon.cmr.api.ArtifactContext) ArtifactResult(com.redhat.ceylon.model.cmr.ArtifactResult)

Example 10 with ArtifactResult

use of com.redhat.ceylon.model.cmr.ArtifactResult in project ceylon-compiler by ceylon.

the class CeylonPluginTool method installScripts.

private boolean installScripts(RepositoryManager repositoryManager, ModuleSpec module, boolean errorIfMissing) throws IOException {
    String version = module.getVersion();
    if ((version == null || version.isEmpty()) && !module.getName().equals(Module.DEFAULT_MODULE_NAME)) {
        version = checkModuleVersionsOrShowSuggestions(getRepositoryManager(), module.getName(), null, ModuleQuery.Type.ALL, null, null);
        if (version == null)
            return false;
    }
    File zipSource = null;
    List<File> existingScriptFolders = null;
    if (isSourceModule(module.getName(), version, applyCwd(sourceFolders))) {
        // copy it directly from the source
        existingScriptFolders = findExistingScriptFolders(module.getName(), errorIfMissing);
        if (existingScriptFolders.isEmpty()) {
            return false;
        }
    } else {
        // obtain it from the repo
        ArtifactContext context = new ArtifactContext(module.getName(), version, ArtifactContext.SCRIPTS_ZIPPED);
        ArtifactResult result = repositoryManager.getArtifactResult(context);
        if (result == null) {
            String err = getModuleNotFoundErrorMessage(repositoryManager, module.getName(), version);
            errorAppend(err);
            errorNewline();
            return false;
        }
        zipSource = result.artifact();
    }
    File moduleScriptDir = getModuleScriptDir(module);
    if (moduleScriptDir.exists()) {
        if (force)
            FileUtil.delete(moduleScriptDir);
        else {
            errorMsg("error.module.already.installed", module.getName(), moduleScriptDir);
            return false;
        }
    }
    if (!moduleScriptDir.mkdirs()) {
        errorMsg("error.unable.create.dest.dir", moduleScriptDir);
        return false;
    }
    if (zipSource != null)
        extractScripts(zipSource, moduleScriptDir);
    else {
        copyScripts(existingScriptFolders, moduleScriptDir);
    }
    msg("success.installed", module.getName(), moduleScriptDir);
    newline();
    return true;
}
Also used : ArtifactContext(com.redhat.ceylon.cmr.api.ArtifactContext) File(java.io.File) ArtifactResult(com.redhat.ceylon.model.cmr.ArtifactResult)

Aggregations

ArtifactResult (com.redhat.ceylon.model.cmr.ArtifactResult)10 ArtifactContext (com.redhat.ceylon.cmr.api.ArtifactContext)8 File (java.io.File)5 ArrayList (java.util.ArrayList)3 RepositoryManager (com.redhat.ceylon.cmr.api.RepositoryManager)2 ModuleSpec (com.redhat.ceylon.common.tools.ModuleSpec)2 ModuleDependencyInfo (com.redhat.ceylon.cmr.api.ModuleDependencyInfo)1 ModuleInfo (com.redhat.ceylon.cmr.api.ModuleInfo)1 Overrides (com.redhat.ceylon.cmr.api.Overrides)1 ModuleCopycat (com.redhat.ceylon.cmr.ceylon.ModuleCopycat)1 InvalidArchiveException (com.redhat.ceylon.cmr.impl.InvalidArchiveException)1 Backends (com.redhat.ceylon.common.Backends)1 ToolUsageError (com.redhat.ceylon.common.tool.ToolUsageError)1 LazyModule (com.redhat.ceylon.model.loader.model.LazyModule)1 LazyModuleManager (com.redhat.ceylon.model.loader.model.LazyModuleManager)1 Module (com.redhat.ceylon.model.typechecker.model.Module)1 ModuleImport (com.redhat.ceylon.model.typechecker.model.ModuleImport)1 IOException (java.io.IOException)1 HashSet (java.util.HashSet)1 LinkedHashSet (java.util.LinkedHashSet)1