Search in sources :

Example 1 with ToolUsageError

use of org.eclipse.ceylon.common.tool.ToolUsageError 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 ToolUsageError

use of org.eclipse.ceylon.common.tool.ToolUsageError in project ceylon by eclipse.

the class SourceArgumentsResolver method parse.

public void parse(Collection<String> modulesOrFiles) throws IOException {
    HashSet<String> srcMods = new HashSet<String>();
    HashSet<String> resMods = new HashSet<String>();
    HashSet<String> singleFileMods = new HashSet<String>();
    srcFiles = new LinkedList<File>();
    resFiles = new LinkedList<File>();
    allFiles = null;
    srcModuleFiles = new HashMap<String, List<File>>();
    resModuleFiles = new HashMap<String, List<File>>();
    allModuleFiles = null;
    Iterable<File> srcs = FileUtil.applyCwd(cwd, sourceDirs);
    Iterable<File> resrcs = FileUtil.applyCwd(cwd, resourceDirs);
    for (String moduleOrFile : modulesOrFiles) {
        File file = FileUtil.applyCwd(cwd, new File(moduleOrFile));
        if (file.isFile()) {
            // It's a single (re)source file instead of a module name, so let's check
            // if it's really located in one of the defined (re)source folders
            File path;
            if (hasAcceptedSuffix(file, sourceSuffixes)) {
                path = FileUtil.selectPath(srcs, file.getPath());
                if (path == null) {
                    String srcPath = sourceDirs.toString();
                    throw new ToolUsageError(CeylonToolMessages.msg("error.not.in.source.path", moduleOrFile, srcPath));
                }
                String module = moduleName(srcs, path, file);
                if (!expandSingleSources) {
                    String relFileName = FileUtil.relativeFile(srcs, file.getPath());
                    addFile(srcFiles, srcModuleFiles, module, new File(path, relFileName));
                } else {
                    // Instead of adding the source file itself we remember
                    // its module name and at the end we expand that and
                    // add all its files
                    singleFileMods.add(module);
                }
                // Determine the module path from the file path
                srcMods.add(module);
            } else {
                if (resrcs != null) {
                    path = FileUtil.selectPath(resrcs, moduleOrFile);
                    if (path == null) {
                        String resrcPath = resourceDirs.toString();
                        throw new ToolUsageError(CeylonToolMessages.msg("error.not.in.resource.path", moduleOrFile, resrcPath));
                    }
                    String module = moduleName(srcs, path, file);
                    String relFileName = FileUtil.relativeFile(resrcs, file.getPath());
                    addFile(resFiles, resModuleFiles, module, new File(path, relFileName));
                    // Determine the module path from the file path
                    resMods.add(module);
                }
            }
        } else {
            visitModuleFiles(srcFiles, srcModuleFiles, srcs, moduleOrFile, sourceSuffixes);
            srcMods.add(moduleOrFile);
            if (resrcs != null) {
                visitModuleFiles(resFiles, resModuleFiles, resrcs, moduleOrFile, null);
                resMods.add(moduleOrFile);
            }
        }
        if (srcFiles.isEmpty() && resFiles.isEmpty()) {
            throw new ToolUsageError(CeylonToolMessages.msg("error.no.files", moduleOrFile));
        }
    }
    // Now expand the sources of any single source modules we encountered
    for (String modName : singleFileMods) {
        visitModuleFiles(srcFiles, srcModuleFiles, srcs, modName, sourceSuffixes);
    }
    srcModules = new ArrayList<String>(srcMods);
    resModules = new ArrayList<String>(resMods);
    allModules = null;
}
Also used : ToolUsageError(org.eclipse.ceylon.common.tool.ToolUsageError) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) File(java.io.File) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 3 with ToolUsageError

use of org.eclipse.ceylon.common.tool.ToolUsageError in project ceylon by eclipse.

the class CeylonWarTool method run.

@Override
public void run() throws Exception {
    String moduleName = null;
    String moduleVersion = null;
    for (ModuleSpec module : modules) {
        String name = module.getName();
        String version = checkModuleVersionsOrShowSuggestions(name, module.isVersioned() ? module.getVersion() : null, ModuleQuery.Type.JVM, Versions.JVM_BINARY_MAJOR_VERSION, Versions.JVM_BINARY_MINOR_VERSION, // JS binary but don't care since JVM
        null, // JS binary but don't care since JVM
        null, null);
        if (version == null)
            continue;
        if (!loadModule(null, name, version))
            throw new ToolUsageError(CeylonWarMessages.msg("abort.missing.modules"));
        // save the first module
        if (moduleName == null) {
            moduleName = name;
            moduleVersion = version;
        }
    }
    loader.resolve();
    List<ArtifactResult> staticMetamodelEntries = new ArrayList<>();
    addLibEntries(staticMetamodelEntries);
    addResources();
    if (this.name == null) {
        this.name = moduleVersion != null && !moduleVersion.isEmpty() ? String.format("%s-%s.war", moduleName, moduleVersion) : String.format("%s.war", moduleName);
        debug("default.name", this.name);
    }
    final File jarFile = getJarFile();
    writeJarFile(jarFile, staticMetamodelEntries);
    String descr = moduleVersion != null && !moduleVersion.isEmpty() ? moduleName + "/" + moduleVersion : moduleName;
    append(CeylonWarMessages.msg("archive.created", descr, jarFile.getAbsolutePath()));
    newline();
}
Also used : ModuleSpec(org.eclipse.ceylon.common.ModuleSpec) ToolUsageError(org.eclipse.ceylon.common.tool.ToolUsageError) ArrayList(java.util.ArrayList) File(java.io.File) ArtifactResult(org.eclipse.ceylon.model.cmr.ArtifactResult)

Example 4 with ToolUsageError

use of org.eclipse.ceylon.common.tool.ToolUsageError in project ceylon by eclipse.

the class CeylonWarTool method addLibEntries.

protected void addLibEntries(final List<ArtifactResult> staticMetamodelEntries) throws MalformedURLException {
    final List<String> libs = new ArrayList<>();
    loader.visitModules(new ModuleGraph.Visitor() {

        @Override
        public void visit(Module module) {
            if (module.artifact != null) {
                File artifact = module.artifact.artifact();
                try {
                    if (artifact != null) {
                        staticMetamodelEntries.add(module.artifact);
                        // write the metamodel, but not the jar, for provided modules
                        if (isProvided(module.name, module.version))
                            return;
                        final String moduleName = module.name;
                        // use "-" for the version separator
                        // use ".jar" so they'll get loaded by the container classloader
                        String version = module.version;
                        String versionSuffix = version != null && !version.isEmpty() ? "-" + version : "";
                        final String name = ModuleUtil.moduleName(moduleName).replace(":", ".") + versionSuffix + ".jar";
                        if (name.contains("/") || name.contains("\\") || name.length() == 0) {
                            throw new ToolUsageError(CeylonWarMessages.msg("module.name.illegal", name));
                        }
                        addSpec(new URLEntrySpec(artifact.toURI().toURL(), "WEB-INF/lib/" + name));
                        libs.add(name);
                    }
                } catch (IOException x) {
                    // lame
                    throw new RuntimeException(x);
                }
            }
        }
    });
}
Also used : ArrayList(java.util.ArrayList) ToolUsageError(org.eclipse.ceylon.common.tool.ToolUsageError) IOException(java.io.IOException) ModuleGraph(org.eclipse.ceylon.cmr.ceylon.loader.ModuleGraph) Module(org.eclipse.ceylon.cmr.ceylon.loader.ModuleGraph.Module) File(java.io.File)

Example 5 with ToolUsageError

use of org.eclipse.ceylon.common.tool.ToolUsageError in project ceylon by eclipse.

the class ModuleLoadingTool method initialize.

@Override
public void initialize(CeylonTool mainTool) throws Exception {
    super.initialize(mainTool);
    loader = new ToolModuleLoader(this, getRepositoryManager(), getLoaderSuffixes());
    if (jdkProviderModule != null) {
        ModuleSpec moduleSpec = ModuleSpec.parse(jdkProviderModule);
        if (!internalLoadModule(null, moduleSpec.getName(), moduleSpec.getVersion())) {
            throw new ToolUsageError(Messages.msg(bundle, "jdk.provider.not.found", jdkProviderModule));
        }
        ArtifactResult result = loader.getModuleArtifact(moduleSpec.getName());
        jdkProvider = new JdkProvider(moduleSpec.getName(), moduleSpec.getVersion(), null, result.artifact());
    }
// else keep the JDK one
}
Also used : ModuleSpec(org.eclipse.ceylon.common.ModuleSpec) JdkProvider(org.eclipse.ceylon.model.loader.JdkProvider) ToolUsageError(org.eclipse.ceylon.common.tool.ToolUsageError) ArtifactResult(org.eclipse.ceylon.model.cmr.ArtifactResult)

Aggregations

ToolUsageError (org.eclipse.ceylon.common.tool.ToolUsageError)20 File (java.io.File)9 IOException (java.io.IOException)6 Test (org.junit.Test)6 ArrayList (java.util.ArrayList)5 ArtifactResult (org.eclipse.ceylon.model.cmr.ArtifactResult)5 ModuleSpec (org.eclipse.ceylon.common.ModuleSpec)4 ModuleVersionDetails (org.eclipse.ceylon.cmr.api.ModuleVersionDetails)3 RepositoryManager (org.eclipse.ceylon.cmr.api.RepositoryManager)3 CeylonImportJarTool (org.eclipse.ceylon.tools.importjar.CeylonImportJarTool)3 List (java.util.List)2 ModuleGraph (org.eclipse.ceylon.cmr.ceylon.loader.ModuleGraph)2 Module (org.eclipse.ceylon.cmr.ceylon.loader.ModuleGraph.Module)2 NonFatalToolMessage (org.eclipse.ceylon.common.tool.NonFatalToolMessage)2 SourceArgumentsResolver (org.eclipse.ceylon.common.tools.SourceArgumentsResolver)2 SourceDependencyResolver (org.eclipse.ceylon.common.tools.SourceDependencyResolver)2 CeylonClasspathTool (org.eclipse.ceylon.tools.classpath.CeylonClasspathTool)2 OutputStream (java.io.OutputStream)1 URI (java.net.URI)1 IllegalCharsetNameException (java.nio.charset.IllegalCharsetNameException)1