Search in sources :

Example 1 with SystemLoaderInfo

use of org.eclipse.n4js.runner.SystemLoaderInfo in project n4js by eclipse.

the class N4jscBase method doMain.

/**
 * This method can be used when the headless builder (a.k.a. n4jsc.jar) is to be invoked programmatically from
 * outside bundle {@code org.eclipse.n4js.hlc}, e.g. in tests or in MWE2 work flows.
 * <p>
 * Obviously, this method is not intended to be used in the IDE product (this bundle should not be included in the
 * IDE product anyway).
 *
 * @param args
 *            parameters from command-line
 * @throws ExitCodeException
 *             in case of errors.
 *
 * @return SuccessExitStatus {@link SuccessExitStatus#INSTANCE success status} when everything went fine
 */
public SuccessExitStatus doMain(String... args) throws ExitCodeException {
    try {
        CmdLineParser parser = new CmdLineParser(this);
        parser.setUsageWidth(130);
        try {
            parser.parseArgument(args);
        } catch (CmdLineException e) {
            System.err.println(e.getMessage());
            printExtendedUsage(parser, System.err);
            // exit with error-code 1
            throw new ExitCodeException(EXITCODE_WRONG_CMDLINE_OPTIONS);
        }
        // debug before help, shortcut for check-settings without running
        if (debug) {
            printDebugLines();
        } else {
            // Reconfigure Logging to be quiet:
            Logger.getRootLogger().removeAllAppenders();
            Logger.getRootLogger().addAppender(new NullAppender());
        }
        // Check help option:
        if (help) {
            // print and exit:
            printExtendedUsage(parser, System.out);
            return SuccessExitStatus.INSTANCE;
        }
        // Injection should not be called before making sure the argument parsing successfully finished. Such as
        // help.
        initInjection(refProperties());
        // Register extensions manually
        headlessExtensionRegistrationHelper.registerExtensions();
        if (listRunners) {
            printAvailableRunners(System.out);
            return SuccessExitStatus.INSTANCE;
        }
        if (listTesters) {
            printAvailableTesters(System.out);
            return SuccessExitStatus.INSTANCE;
        }
        EnumSet<BuildType> noSrcRequired = EnumSet.of(BuildType.allprojects, BuildType.dontcompile);
        // missing arguments (only build all doesn't require one OR if nothing will be compiled).
        if (srcFiles.isEmpty() && (!noSrcRequired.contains(buildtype))) {
            // print and exit:
            System.out.println("Missing arguments.");
            printExtendedUsage(parser, System.out);
            throw new ExitCodeException(EXITCODE_WRONG_CMDLINE_OPTIONS);
        }
        // check missing runner
        if ((runThisFile != null) && isNotGiven(runner)) {
            // print and exit:
            System.out.println("Missing arguments for running: runner must be named with option -rw");
            printExtendedUsage(parser, System.out);
            throw new ExitCodeException(EXITCODE_WRONG_CMDLINE_OPTIONS);
        }
        // check missing tester
        if ((testThisLocation != null) && isNotGiven(tester)) {
            // print and exit:
            System.out.println("Missing arguments for testing: tester must be named with option -tw");
            printExtendedUsage(parser, System.out);
            throw new ExitCodeException(EXITCODE_WRONG_CMDLINE_OPTIONS);
        }
        // check runner/tester conflict
        if (runThisFile != null && testThisLocation != null) {
            // print and exit:
            System.out.println("Conflicting arguments: must not provide both -r and -t");
            printExtendedUsage(parser, System.out);
            throw new ExitCodeException(EXITCODE_WRONG_CMDLINE_OPTIONS);
        }
        if (null != testCatalogFile) {
            if (testCatalogFile.exists()) {
                if (testCatalogFile.isDirectory()) {
                    final String msg = "The location of the test catalog file points to a directory and not to a file. " + testCatalogFile;
                    System.out.println(msg);
                    printExtendedUsage(parser, System.out);
                    throw new ExitCodeException(EXITCODE_TEST_CATALOG_ASSEMBLATION_ERROR, msg);
                }
                if (!testCatalogFile.delete()) {
                    final String msg = "Error while deleting existing test catalog file. " + testCatalogFile;
                    System.out.println(msg);
                    printExtendedUsage(parser, System.out);
                    throw new ExitCodeException(EXITCODE_TEST_CATALOG_ASSEMBLATION_ERROR, msg);
                }
            }
            try {
                if (!testCatalogFile.createNewFile()) {
                    final String msg = "Error while creating test catalog file at: " + testCatalogFile;
                    System.out.println(msg);
                    printExtendedUsage(parser, System.out);
                    throw new ExitCodeException(EXITCODE_TEST_CATALOG_ASSEMBLATION_ERROR, msg);
                }
            } catch (final IOException e) {
                System.out.println("Error while creating test catalog file. " + e.getMessage());
                printExtendedUsage(parser, System.out);
                throw new ExitCodeException(EXITCODE_TEST_CATALOG_ASSEMBLATION_ERROR, e);
            }
            if (!testCatalogFile.exists() || !testCatalogFile.canWrite()) {
                final String msg = "Cannot access test catalog file at: " + testCatalogFile;
                System.out.println(msg);
                printExtendedUsage(parser, System.out);
                throw new ExitCodeException(EXITCODE_TEST_CATALOG_ASSEMBLATION_ERROR, msg);
            }
        }
        if (clean && ((buildtype == BuildType.dontcompile) || (buildtype == BuildType.singlefile))) {
            String msg = "";
            if (buildtype == BuildType.dontcompile) {
                msg = "The flag -bt must be specified when --clean/-c is activated";
            } else if (buildtype == BuildType.singlefile) {
                msg = "The flag --clean/-c flag can not be used in combination with -bt singlefile";
            }
            System.out.println(msg);
            printExtendedUsage(parser, System.out);
            throw new ExitCodeException(EXITCODE_WRONG_CMDLINE_OPTIONS);
        }
        final SystemLoaderInfo systemLoaderType = SystemLoaderInfo.fromString(systemLoader);
        if (null == systemLoaderType) {
            systemLoader = SystemLoaderInfo.SYSTEM_JS.getId();
        }
        checkTargetPlatformConfigurations();
        if (null != nodeJsBinaryRoot) {
            binariesPreferenceStore.setPath(nodeJsBinaryProvider.get(), nodeJsBinaryRoot.toURI());
            binariesPreferenceStore.save();
        }
        if (npmrcRoot != null) {
            binariesPreferenceStore.setPath(npmrcBinaryProvider.get(), npmrcRoot.toURI());
            binariesPreferenceStore.save();
        }
        validateBinaries();
        cloneGitRepositoryAndInstallNpmPackages();
        if (clean) {
            // clean without compiling anything.
            clean();
        } else {
            if (installMissingDependencies) {
                Map<String, String> dependencies = dependencyHelper.discoverMissingDependencies(projectLocations, srcFiles);
                if (verbose) {
                    System.out.println("installing missing dependencies:");
                    dependencies.forEach((name, version) -> {
                        System.out.println("  # " + name + version);
                    });
                }
                IStatus status = npmManager.installDependencies(dependencies, new NullProgressMonitor(), false);
                if (!status.isOK())
                    if (keepCompiling)
                        warn(status.getMessage());
                    else
                        throw new ExitCodeException(EXITCODE_DEPENDENCY_NOT_FOUND, "Cannot install dependencies.");
            }
            // run and dispatch.
            doCompileAndTestAndRun();
        }
    } catch (ExitCodeException e) {
        dumpThrowable(e);
        throw e;
    } finally {
        targetPlatformInstallLocation = null;
    }
    // did everything there was to be done
    return SuccessExitStatus.INSTANCE;
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IStatus(org.eclipse.core.runtime.IStatus) CmdLineParser(org.kohsuke.args4j.CmdLineParser) SystemLoaderInfo(org.eclipse.n4js.runner.SystemLoaderInfo) NullAppender(org.apache.log4j.varia.NullAppender) IOException(java.io.IOException) CmdLineException(org.kohsuke.args4j.CmdLineException)

Aggregations

IOException (java.io.IOException)1 NullAppender (org.apache.log4j.varia.NullAppender)1 IStatus (org.eclipse.core.runtime.IStatus)1 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)1 SystemLoaderInfo (org.eclipse.n4js.runner.SystemLoaderInfo)1 CmdLineException (org.kohsuke.args4j.CmdLineException)1 CmdLineParser (org.kohsuke.args4j.CmdLineParser)1