Search in sources :

Example 1 with ResourceRef

use of dev.jbang.source.ResourceRef in project jbang by jbangdev.

the class ClasspathResourceResolver method getClasspathResource.

private static ResourceRef getClasspathResource(String cpResource) {
    String ref = cpResource.substring(11);
    Util.verboseMsg("Duplicating classpath resource " + ref);
    ClassLoader cl = Thread.currentThread().getContextClassLoader();
    if (cl == null) {
        cl = ResourceRef.class.getClassLoader();
    }
    URL url = cl.getResource(ref);
    if (url == null) {
        throw new ExitException(BaseCommand.EXIT_INVALID_INPUT, "Resource not found on class path: " + ref);
    }
    try {
        File f = new File(url.toURI());
        if (f.canRead()) {
            return ResourceRef.forCachedResource(cpResource, f);
        }
    } catch (URISyntaxException | IllegalArgumentException e) {
    // Ignore
    }
    // We couldn't read the file directly from the class path so let's make a copy
    try (InputStream is = url.openStream()) {
        Path to = Util.getUrlCache(cpResource);
        Files.createDirectories(to.getParent());
        Files.copy(is, to, StandardCopyOption.REPLACE_EXISTING);
        return ResourceRef.forCachedResource(cpResource, to.toFile());
    } catch (IOException e) {
        throw new ExitException(BaseCommand.EXIT_GENERIC_ERROR, "Resource could not be copied from class path: " + ref, e);
    }
}
Also used : Path(java.nio.file.Path) InputStream(java.io.InputStream) ResourceRef(dev.jbang.source.ResourceRef) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) ExitException(dev.jbang.cli.ExitException) File(java.io.File) URL(java.net.URL)

Example 2 with ResourceRef

use of dev.jbang.source.ResourceRef 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 3 with ResourceRef

use of dev.jbang.source.ResourceRef in project jbang by jbangdev.

the class LiteralScriptResourceResolver method stringToResourceRef.

public static ResourceRef stringToResourceRef(String resource, String scriptText) throws IOException {
    ResourceRef result;
    String urlHash = Util.getStableID(scriptText);
    File cache = Settings.getCacheDir(Cache.CacheClass.stdins).resolve(urlHash).toFile();
    cache.mkdirs();
    String basename = urlHash;
    String suffix = ".jsh";
    if (hasMainMethod(scriptText)) {
        suffix = ".java";
        basename = getMainClass(scriptText).orElse(basename);
    }
    File scriptFile = new File(cache, basename + suffix);
    Util.writeString(scriptFile.toPath(), scriptText);
    result = ResourceRef.forCachedResource(resource, scriptFile);
    return result;
}
Also used : ResourceRef(dev.jbang.source.ResourceRef) File(java.io.File)

Example 4 with ResourceRef

use of dev.jbang.source.ResourceRef 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 5 with ResourceRef

use of dev.jbang.source.ResourceRef in project jbang by jbangdev.

the class AliasResourceResolver method resolve.

@Override
public ResourceRef resolve(String resource) {
    ResourceRef ref = resolver.resolve(resource);
    if (ref == null) {
        Alias alias = (catalog != null) ? Alias.get(catalog, resource) : Alias.get(resource);
        if (alias != null) {
            ResourceRef aliasRef = resolver.resolve(alias.resolve());
            if (aliasRef == null) {
                throw new IllegalArgumentException("Alias " + resource + " from " + alias.catalog.catalogRef + " failed to resolve " + alias.scriptRef);
            }
            ref = new AliasedResourceRef(aliasRef.getOriginalResource(), aliasRef.getFile(), alias);
        }
    }
    return ref;
}
Also used : Alias(dev.jbang.catalog.Alias) ResourceRef(dev.jbang.source.ResourceRef)

Aggregations

ResourceRef (dev.jbang.source.ResourceRef)10 File (java.io.File)5 ExitException (dev.jbang.cli.ExitException)3 IOException (java.io.IOException)3 Path (java.nio.file.Path)3 InvalidPathException (java.nio.file.InvalidPathException)2 Alias (dev.jbang.catalog.Alias)1 ModularClassPath (dev.jbang.dependencies.ModularClassPath)1 BufferedReader (java.io.BufferedReader)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 URISyntaxException (java.net.URISyntaxException)1 URL (java.net.URL)1