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