Search in sources :

Example 6 with ExitException

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

the class BaseBuilder method runNativeBuilder.

protected void runNativeBuilder(List<String> optionList) throws IOException {
    File nilog = File.createTempFile("jbang", "native-image");
    Util.verboseMsg("native-image: " + String.join(" ", optionList));
    Util.infoMsg("log: " + nilog.toString());
    Process process = new ProcessBuilder(optionList).inheritIO().redirectOutput(nilog).start();
    try {
        process.waitFor();
    } catch (InterruptedException e) {
        throw new ExitException(1, e);
    }
    if (process.exitValue() != 0) {
        throw new ExitException(1, "Error during native-image");
    }
}
Also used : ExitException(dev.jbang.cli.ExitException)

Example 7 with ExitException

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

the class BaseBuilder method searchForMain.

protected void searchForMain(File tmpJarDir) {
    try {
        // using Files.walk method with try-with-resources
        try (Stream<Path> paths = Files.walk(tmpJarDir.toPath())) {
            List<Path> items = paths.filter(Files::isRegularFile).filter(f -> !f.toFile().getName().contains("$")).filter(f -> f.toFile().getName().endsWith(".class")).collect(Collectors.toList());
            Indexer indexer = new Indexer();
            Index index;
            for (Path item : items) {
                try (InputStream stream = new FileInputStream(item.toFile())) {
                    indexer.index(stream);
                }
            }
            index = indexer.complete();
            Collection<ClassInfo> classes = index.getKnownClasses();
            List<ClassInfo> mains = classes.stream().filter(getMainFinder()).collect(Collectors.toList());
            String mainName = getSuggestedMain();
            if (mains.size() > 1 && mainName != null) {
                List<ClassInfo> suggestedmain = mains.stream().filter(ci -> ci.simpleName().equals(mainName)).collect(Collectors.toList());
                if (!suggestedmain.isEmpty()) {
                    mains = suggestedmain;
                }
            }
            if (!mains.isEmpty()) {
                ctx.setMainClass(mains.get(0).name().toString());
                if (mains.size() > 1) {
                    Util.warnMsg("Could not locate unique main() method. Use -m to specify explicit main method. Falling back to use first found: " + mains.stream().map(x -> x.name().toString()).collect(Collectors.joining(",")));
                }
            }
            if (ss.getMainSource().isAgent()) {
                Optional<ClassInfo> agentmain = classes.stream().filter(pubClass -> pubClass.method("agentmain", STRINGTYPE, INSTRUMENTATIONTYPE) != null || pubClass.method("agentmain", STRINGTYPE) != null).findFirst();
                if (agentmain.isPresent()) {
                    ctx.setAgentMainClass(agentmain.get().name().toString());
                }
                Optional<ClassInfo> premain = classes.stream().filter(pubClass -> pubClass.method("premain", STRINGTYPE, INSTRUMENTATIONTYPE) != null || pubClass.method("premain", STRINGTYPE) != null).findFirst();
                if (premain.isPresent()) {
                    ctx.setPreMainClass(premain.get().name().toString());
                }
            }
        }
    } catch (IOException e) {
        throw new ExitException(1, e);
    }
}
Also used : Path(java.nio.file.Path) Manifest(java.util.jar.Manifest) JarUtil(dev.jbang.util.JarUtil) Util(dev.jbang.util.Util) java.util(java.util) Files(java.nio.file.Files) JavaUtil(dev.jbang.util.JavaUtil) Predicate(java.util.function.Predicate) DependencyUtil(dev.jbang.dependencies.DependencyUtil) IntegrationManager(dev.jbang.spi.IntegrationManager) Attributes(java.util.jar.Attributes) Collectors(java.util.stream.Collectors) ExitException(dev.jbang.cli.ExitException) IntegrationResult(dev.jbang.spi.IntegrationResult) dev.jbang.source(dev.jbang.source) Stream(java.util.stream.Stream) java.io(java.io) Template(io.quarkus.qute.Template) MavenCoordinate(org.jboss.shrinkwrap.resolver.api.maven.coordinate.MavenCoordinate) TemplateEngine(dev.jbang.util.TemplateEngine) Pattern(java.util.regex.Pattern) Path(java.nio.file.Path) org.jboss.jandex(org.jboss.jandex) ExitException(dev.jbang.cli.ExitException)

Example 8 with ExitException

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

the class GroovyManager method downloadAndInstallGroovy.

public static Path downloadAndInstallGroovy(String version) {
    Util.infoMsg("Downloading Groovy " + version + ". Be patient, this can take several minutes...");
    String url = getGroovyDownloadUrl(version);
    Util.verboseMsg("Downloading " + url);
    Path groovyDir = getGroovyPath(version);
    Path groovyTmpDir = groovyDir.getParent().resolve(groovyDir.getFileName().toString() + ".tmp");
    Path groovyOldDir = groovyDir.getParent().resolve(groovyDir.getFileName().toString() + ".old");
    Util.deletePath(groovyTmpDir, false);
    Util.deletePath(groovyOldDir, false);
    try {
        Path groovyPkg = Util.downloadAndCacheFile(url);
        Util.infoMsg("Installing Groovy " + version + "...");
        Util.verboseMsg("Unpacking to " + groovyDir);
        UnpackUtil.unpack(groovyPkg, groovyTmpDir);
        if (Files.isDirectory(groovyDir)) {
            Files.move(groovyDir, groovyOldDir);
        }
        Files.move(groovyTmpDir, groovyDir);
        Util.deletePath(groovyOldDir, false);
        return groovyDir;
    } catch (Exception e) {
        Util.deletePath(groovyTmpDir, true);
        if (!Files.isDirectory(groovyDir) && Files.isDirectory(groovyOldDir)) {
            try {
                Files.move(groovyOldDir, groovyDir);
            } catch (IOException ex) {
            // Ignore
            }
        }
        Util.errorMsg("Required Groovy version not possible to download or install.");
        throw new ExitException(EXIT_UNEXPECTED_STATE, "Unable to download or install Groovy 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)

Example 9 with ExitException

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

the class JdkManager method downloadAndInstallJdk.

public static Path downloadAndInstallJdk(int version) {
    Util.infoMsg("Downloading JDK " + version + ". Be patient, this can take several minutes...");
    String url = getJDKUrl(version, Util.getOS().name(), Util.getArch().name(), Util.getVendor(), Util.getRelease());
    Util.verboseMsg("Downloading " + url);
    Path jdkDir = getJdkPath(version);
    Path jdkTmpDir = jdkDir.getParent().resolve(jdkDir.getFileName().toString() + ".tmp");
    Path jdkOldDir = jdkDir.getParent().resolve(jdkDir.getFileName().toString() + ".old");
    Util.deletePath(jdkTmpDir, false);
    Util.deletePath(jdkOldDir, false);
    try {
        Path jdkPkg = Util.downloadAndCacheFile(url);
        Util.infoMsg("Installing JDK " + version + "...");
        Util.verboseMsg("Unpacking to " + jdkDir.toString());
        UnpackUtil.unpackJdk(jdkPkg, jdkTmpDir);
        if (Files.isDirectory(jdkDir)) {
            Files.move(jdkDir, jdkOldDir);
        }
        Files.move(jdkTmpDir, jdkDir);
        Util.deletePath(jdkOldDir, false);
        if (getDefaultJdk() < 0) {
            setDefaultJdk(version);
        }
        return jdkDir;
    } catch (Exception e) {
        Util.deletePath(jdkTmpDir, true);
        if (!Files.isDirectory(jdkDir) && Files.isDirectory(jdkOldDir)) {
            try {
                Files.move(jdkOldDir, jdkDir);
            } catch (IOException ex) {
            // Ignore
            }
        }
        Util.errorMsg("Required Java version not possible to download or install. You can run with '--java " + JavaUtil.determineJavaVersion() + "' to force using the default installed Java.");
        throw new ExitException(EXIT_UNEXPECTED_STATE, "Unable to download or install JDK 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) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 10 with ExitException

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

the class JdkManager method linkToExistingJdk.

/**
 * Links JBang JDK folder to an already existing JDK path with a link. It checks
 * if the incoming version number is the same that the linked JDK has, if not an
 * exception will be raised.
 *
 * @param path    path to the pre-installed JDK.
 * @param version requested version to link.
 */
public static void linkToExistingJdk(String path, int version) {
    Path jdkPath = getJdkPath(version);
    Util.verboseMsg("Trying to link " + path + " to " + jdkPath);
    if (Files.exists(jdkPath)) {
        Util.verboseMsg("JBang managed JDK already exists, must be deleted to make sure linking works");
        Util.deletePath(jdkPath, false);
    }
    Path linkedJdkPath = Paths.get(path);
    if (!Files.isDirectory(linkedJdkPath)) {
        throw new ExitException(EXIT_INVALID_INPUT, "Unable to resolve path as directory: " + path);
    }
    Optional<Integer> ver = resolveJavaVersionFromPath(linkedJdkPath);
    if (ver.isPresent()) {
        Integer linkedJdkVersion = ver.get();
        if (linkedJdkVersion == version) {
            Util.createLink(jdkPath, linkedJdkPath);
            Util.infoMsg("JDK " + version + " has been linked to: " + linkedJdkPath);
        } else {
            throw new ExitException(EXIT_INVALID_INPUT, "Java version in given path: " + path + " is " + linkedJdkVersion + " which does not match the requested version " + version + "");
        }
    } else {
        throw new ExitException(EXIT_INVALID_INPUT, "Unable to determine Java version in given path: " + path);
    }
}
Also used : Path(java.nio.file.Path) 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