Search in sources :

Example 11 with ExitException

use of dev.jbang.cli.ExitException in project jbang by jbangdev.

the class TrustedSources method save.

private void save(Set<String> rules, File storage) {
    String newsources = getJSon(rules);
    try {
        Util.writeString(storage.toPath(), newsources);
    } catch (IOException e) {
        throw new ExitException(2, "Error when writing to " + storage, e);
    }
    trustedSources = rules.toArray(new String[0]);
}
Also used : IOException(java.io.IOException) ExitException(dev.jbang.cli.ExitException)

Example 12 with ExitException

use of dev.jbang.cli.ExitException in project jbang by jbangdev.

the class LiteralScriptResourceResolver method resolve.

@Override
public ResourceRef resolve(String resource) {
    ResourceRef result = null;
    try {
        // support stdin
        if (ResourceRef.isStdin(resource)) {
            String scriptText = new BufferedReader(new InputStreamReader(System.in, StandardCharsets.UTF_8)).lines().collect(Collectors.joining(System.lineSeparator()));
            result = stringToResourceRef(resource, scriptText);
        }
    } catch (IOException e) {
        throw new ExitException(BaseCommand.EXIT_UNEXPECTED_STATE, "Could not cache script from stdin", e);
    }
    return result;
}
Also used : InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) ResourceRef(dev.jbang.source.ResourceRef) IOException(java.io.IOException) ExitException(dev.jbang.cli.ExitException)

Example 13 with ExitException

use of dev.jbang.cli.ExitException in project jbang by jbangdev.

the class RenamingScriptResourceResolver method resolve.

@Override
public ResourceRef resolve(String resource) {
    ResourceRef result = null;
    // map script argument to script file
    File probe = null;
    try {
        probe = Util.getCwd().resolve(resource).normalize().toFile();
    } catch (InvalidPathException e) {
    // Ignore
    }
    try {
        if (probe != null && probe.canRead()) {
            String ext = "." + Util.extension(probe.getName());
            if (!ext.equals(".jar") && !Util.EXTENSIONS.contains(ext)) {
                if (probe.isDirectory()) {
                    File defaultApp = new File(probe, "main.java");
                    if (defaultApp.exists()) {
                        Util.verboseMsg("Directory where main.java exists. Running main.java.");
                        probe = defaultApp;
                    } else {
                        throw new ExitException(BaseCommand.EXIT_INVALID_INPUT, "Cannot run " + probe + " as it is a directory and no default application (i.e. `main.java`) found.");
                    }
                }
                String original = Util.readString(probe.toPath());
                // TODO: move temp handling somewhere central
                String urlHash = Util.getStableID(original);
                if (original.startsWith("#!")) {
                    // strip bash !# if exists
                    original = original.substring(original.indexOf("\n"));
                }
                File tempFile = Settings.getCacheDir(Cache.CacheClass.scripts).resolve(urlHash).resolve(Util.unkebabify(probe.getName())).toFile();
                tempFile.getParentFile().mkdirs();
                Util.writeString(tempFile.toPath().toAbsolutePath(), original);
                result = ResourceRef.forCachedResource(resource, tempFile);
            }
        }
    } catch (IOException e) {
        throw new ExitException(BaseCommand.EXIT_UNEXPECTED_STATE, "Could not download " + resource, e);
    }
    return result;
}
Also used : ResourceRef(dev.jbang.source.ResourceRef) IOException(java.io.IOException) File(java.io.File) ExitException(dev.jbang.cli.ExitException) InvalidPathException(java.nio.file.InvalidPathException)

Example 14 with ExitException

use of dev.jbang.cli.ExitException in project jbang by jbangdev.

the class DependencyUtil method resolveDependencies.

/**
 * @param deps
 * @param repos
 * @param loggingEnabled
 * @return string with resolved classpath
 */
public static ModularClassPath resolveDependencies(List<String> deps, List<MavenRepo> repos, boolean offline, boolean updateCache, boolean loggingEnabled, boolean transitivity) {
    // if no dependencies were provided we stop here
    if (deps.isEmpty()) {
        return new ModularClassPath(Collections.emptyList());
    }
    if (repos.isEmpty()) {
        repos = new ArrayList<>();
        repos.add(toMavenRepo("mavencentral"));
    }
    // Turn any URL dependencies into regular GAV coordinates
    List<String> depIds = deps.stream().map(JitPackUtil::ensureGAV).collect(Collectors.toList());
    // And if we encountered URLs let's make sure the JitPack repo is available
    if (!depIds.equals(deps) && !repos.stream().anyMatch(r -> REPO_JITPACK.equals(r.getUrl()))) {
        repos.add(toMavenRepo(ALIAS_JITPACK));
    }
    String depsHash = String.join(CP_SEPARATOR, depIds);
    if (!transitivity) {
        // the cached key need to be different for non-transivity
        depsHash = "notransitivity-" + depsHash;
    }
    List<ArtifactInfo> cachedDeps = null;
    if (!updateCache) {
        cachedDeps = DependencyCache.findDependenciesByHash(depsHash);
        if (cachedDeps != null) {
            return new ModularClassPath(cachedDeps);
        }
    }
    if (loggingEnabled) {
        infoMsg("Resolving dependencies...");
    }
    try {
        List<ArtifactInfo> artifacts = resolveDependenciesViaAether(depIds, repos, offline, loggingEnabled, transitivity);
        ModularClassPath classPath = new ModularClassPath(artifacts);
        if (loggingEnabled) {
            infoMsg("Dependencies resolved");
        }
        DependencyCache.cache(depsHash, classPath.getArtifacts());
        // Print the classpath
        return classPath;
    } catch (DependencyException e) {
        // Probably a wrapped Nullpointer from
        // 'DefaultRepositorySystem.resolveDependencies()', this however is probably
        // a connection problem.
        errorMsg("Exception: " + e.getMessage());
        throw new ExitException(0, "Failed while connecting to the server. Check the connection (http/https, port, proxy, credentials, etc.) of your maven dependency locators.", e);
    }
}
Also used : ExitException(dev.jbang.cli.ExitException)

Example 15 with ExitException

use of dev.jbang.cli.ExitException in project jbang by jbangdev.

the class EditorManager method downloadAndInstallEditor.

public static Path downloadAndInstallEditor() {
    // https://github.com/VSCodium/vscodium/releases/download/1.52.1/VSCodium-darwin-x64-1.52.1.zip
    String version = getLatestVSCodiumVersion();
    Util.infoMsg("Downloading VSCodium " + version + ". Be patient, this can take several minutes...(Ctrl+C if you want to cancel)");
    String url = getVSCodiumDownloadURL(version);
    Util.verboseMsg("Downloading " + url);
    Path editorDir = getVSCodiumPath();
    Path editorTmpDir = editorDir.getParent().resolve(editorDir.getFileName().toString() + ".tmp");
    Path editorOldDir = editorDir.getParent().resolve(editorDir.getFileName().toString() + ".old");
    Util.deletePath(editorTmpDir, false);
    Util.deletePath(editorOldDir, false);
    try {
        Path jdkPkg = Util.downloadAndCacheFile(url);
        Util.infoMsg("Installing VSCodium " + version + "...");
        Util.verboseMsg("Unpacking to " + editorDir.toString());
        UnpackUtil.unpackEditor(jdkPkg, editorTmpDir);
        if (Files.isDirectory(editorDir)) {
            Files.move(editorDir, editorOldDir);
        }
        Files.move(editorTmpDir, editorDir);
        Util.deletePath(editorOldDir, false);
        return editorDir;
    } catch (Exception e) {
        Util.deletePath(editorTmpDir, true);
        if (!Files.isDirectory(editorDir) && Files.isDirectory(editorOldDir)) {
            try {
                Files.move(editorOldDir, editorDir);
            } catch (IOException ex) {
            // Ignore
            }
        }
        Util.errorMsg("VSCode editor not possible to download or install.");
        throw new ExitException(EXIT_UNEXPECTED_STATE, "Unable to download or install VSCodium version " + version, e);
    }
}
Also used : Path(java.nio.file.Path) IOException(java.io.IOException) ExitException(dev.jbang.cli.ExitException) IOException(java.io.IOException) ExitException(dev.jbang.cli.ExitException)

Aggregations

ExitException (dev.jbang.cli.ExitException)25 IOException (java.io.IOException)17 Path (java.nio.file.Path)16 File (java.io.File)4 ArrayList (java.util.ArrayList)4 ResourceRef (dev.jbang.source.ResourceRef)3 Util (dev.jbang.util.Util)3 Collectors (java.util.stream.Collectors)3 Template (io.quarkus.qute.Template)2 BufferedReader (java.io.BufferedReader)2 InputStream (java.io.InputStream)2 InputStreamReader (java.io.InputStreamReader)2 URISyntaxException (java.net.URISyntaxException)2 URL (java.net.URL)2 Files (java.nio.file.Files)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 Set (java.util.Set)2 JsonParseException (com.google.gson.JsonParseException)1