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);
}
}
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;
}
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;
}
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;
}
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;
}
Aggregations