Search in sources :

Example 1 with RunConfiguration

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

the class XpectN4JSES5TranspilerHelper method doCompileAndExecute.

/**
 * Compile provided resource, execute in Node.js and return execution output.
 *
 * @param resource
 *            Script to execute
 * @param init
 *            xpect setup-init
 * @param fileSetupContext
 *            xpect injected
 * @param decorateStdStreams
 *            false-just connect stdout+stderr or errors to resulting string, true - decorate the streams with "<==
 *            stdout: ..."
 * @param resourceTweaker
 *            - resource-modifier like QuickFix application, can be null
 * @param systemLoader
 *            the system loader to use (SYSTEM_JS[default], COMMON_JS,...)
 * @return output streams concatenated
 */
public String doCompileAndExecute(final XtextResource resource, org.eclipse.xpect.setup.ISetupInitializer<Object> init, FileSetupContext fileSetupContext, boolean decorateStdStreams, ResourceTweaker resourceTweaker, GeneratorOption[] options, SystemLoaderInfo systemLoader) throws IOException {
    // Apply some modification to the resource here.
    if (resourceTweaker != null) {
        resourceTweaker.tweak(resource);
    }
    loadXpectConfiguration(init, fileSetupContext);
    File artificialRoot = Files.createTempDirectory("n4jsXpect").toFile();
    RunConfiguration runConfig;
    // && (((ReadOutWorkspaceConfiguration) readOutConfiguration).getXpectConfiguredWorkspace() == null)
    if (Platform.isRunning()) {
        // If we are in the IDE, execute the test the same as for "Run in Node.js" and this way avoid
        // the effort of calculating dependencies etc.
        final String implementationId = chooseImplHelper.chooseImplementationIfRequired(NodeRunner.ID, resource.getURI().trimFileExtension());
        boolean replaceQuotes = false;
        // We have to generate JS code for the resource. Because if Xpect test is quickfixAndRun the resource
        // contains errors and hence no generated JS code is available for execution.
        // Then sneak in the path to the generated JS code.
        Script script = (Script) resource.getContents().get(0);
        createTempJsFileWithScript(artificialRoot.toPath(), script, options, replaceQuotes);
        runConfig = runnerFrontEnd.createConfiguration(NodeRunner.ID, (implementationId == ChooseImplementationHelper.CANCEL) ? null : implementationId, systemLoader.getId(), resource.getURI().trimFileExtension(), artificialRoot.getAbsolutePath().toString());
    } else {
        // In the non-GUI case, we need to calculate dependencies etc. manually
        final Iterable<Resource> dependencies = from(getDependentResources());
        boolean replaceQuotes = false;
        // compile all file resources
        StringBuilder errorResult = new StringBuilder();
        Script testScript = (Script) resource.getContents().get(0);
        // replace n4jsd resource with provided js resource
        for (final Resource dep : from(dependencies).filter(r -> !r.getURI().equals(resource.getURI()))) {
            if ("n4jsd".equalsIgnoreCase(dep.getURI().fileExtension())) {
                compileImplementationOfN4JSDFile(artificialRoot.toPath(), errorResult, dep, options, replaceQuotes);
            } else if (xpectGenerator.isCompilable(dep, errorResult)) {
                final Script script = (Script) dep.getContents().get(0);
                createTempJsFileWithScript(artificialRoot.toPath(), script, options, replaceQuotes);
            }
        }
        if (errorResult.length() != 0) {
            return errorResult.toString();
        }
        // No error so far
        // determine module to run
        createTempJsFileWithScript(artificialRoot.toPath(), testScript, options, replaceQuotes);
        String fileToRun = jsModulePathToRun(testScript);
        // Not in UI case, hence manually set up the resources
        runConfig = runnerFrontEnd.createXpectOutputTestConfiguration(NodeRunner.ID, fileToRun, systemLoader, artificialRoot.toPath().toString() + "/" + testScript.getModule().getProjectId());
    }
    return configRunner.executeWithConfig(runConfig, decorateStdStreams);
}
Also used : Script(org.eclipse.n4js.n4JS.Script) RunConfiguration(org.eclipse.n4js.runner.RunConfiguration) XtextResource(org.eclipse.xtext.resource.XtextResource) Resource(org.eclipse.emf.ecore.resource.Resource) File(java.io.File)

Example 2 with RunConfiguration

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

the class TestReactExternalLibraryPluginTest method runClient.

private ProcessResult runClient() {
    final String pathToModuleToRun = getResourceName(PA, CLIENT_MODULE);
    final org.eclipse.emf.common.util.URI moduleToRun = createPlatformResourceURI(pathToModuleToRun, true);
    final RunConfiguration config = runnerFrontEnd.createConfiguration(ID, null, moduleToRun);
    final Process process = runnerFrontEndUI.runInUI(config);
    final ProcessResult result = processExecutor.execute(process, "", OutputRedirection.REDIRECT);
    assertTrue("Expected 0 error code for the process. Was: " + result.getExitCode() + "\nError message: " + result.getStdErr(), result.isOK());
    return result;
}
Also used : RunConfiguration(org.eclipse.n4js.runner.RunConfiguration) ProcessResult(org.eclipse.n4js.utils.process.ProcessResult)

Example 3 with RunConfiguration

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

the class ShippedCodeRunConfigurationPluginUITest method testBootstrapConfiguration.

/**
 * Checks if exec module, init modules, and class path (core project paths) are in sync.
 */
@Test
public void testBootstrapConfiguration() throws Exception {
    final File projectsRoot = new File(getResourceUri(PROBANDS, WORKSPACE_LOC));
    ProjectTestsUtils.importProject(projectsRoot, CLIENT);
    waitForAutoBuild();
    final RunConfiguration config = runnerFrontEnd.createConfiguration(NODE_RUNNER_ID, MODULE_TO_RUN_URI);
    assertFalse("Expected some init modules", config.getInitModules().isEmpty());
    // compute config
    runnerFrontEnd.computeDerivedValues(config);
    String execModule1 = config.getExecModule();
    assertFalse("Expected exec module to be configured", Strings.isNullOrEmpty(execModule1));
    Set<String> execModules1 = new HashSet<>(config.getInitModules());
    assertFalse("Expected some init modules", execModules1.isEmpty());
    Set<String> corePaths1 = new HashSet<>(config.getCoreProjectPaths());
    assertFalse("Expected core projects paths to contain more than project output", corePaths1.size() <= 1);
    // clear project paths to ensure we only get paths newly computed in #configureFromFileSystem()
    config.setCoreProjectPaths(Collections.emptyList());
    // reconfigure config
    shippedCodeConfigurationHelper.configureFromFileSystem(config);
    String execModule2 = config.getExecModule();
    assertFalse("Expected exec module to be configured", Strings.isNullOrEmpty(execModule2));
    Set<String> execModules2 = new HashSet<>(config.getInitModules());
    assertFalse("Expected some init modules", execModules2.isEmpty());
    Set<String> corePaths2 = new HashSet<>(config.getCoreProjectPaths());
    assertFalse("Expected core projects paths to contain more than project output", corePaths2.size() <= 1);
    // record data to log in case of failure
    recordLogData(">>> Configured paths", corePaths1, "<<< Configured paths");
    recordLogData(">>> Reconfigured paths", corePaths2, "<<< Reconfigured paths");
    // compare if for every path in the first config
    // there exists alternative path in the second config
    Set<String> configuredCorePaths = processPaths(corePaths1);
    Set<String> reconfiguredCorePaths = processPaths(corePaths2);
    final Collection<String> removedPaths = difference(configuredCorePaths, reconfiguredCorePaths);
    final Collection<String> addedPaths = difference(reconfiguredCorePaths, configuredCorePaths);
    assertTrue("Expected no paths removed, but diff was " + removedPaths.size(), removedPaths.isEmpty());
    assertTrue("Expected no paths added, but diff was " + addedPaths.size(), addedPaths.isEmpty());
}
Also used : RunConfiguration(org.eclipse.n4js.runner.RunConfiguration) File(java.io.File) HashSet(java.util.HashSet) Test(org.junit.Test) AbstractBuilderParticipantTest(org.eclipse.n4js.tests.builder.AbstractBuilderParticipantTest)

Example 4 with RunConfiguration

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

the class AbstractRunnerLaunchShortcut method launchFile.

/**
 * Launch a file, using the file information, which means using default launch configurations.
 */
protected void launchFile(IFile originalFileToRun, String mode) {
    final String runnerId = getRunnerId();
    final String path = originalFileToRun.getFullPath().toOSString();
    final URI moduleToRun = URI.createPlatformResourceURI(path, true);
    final String implementationId = chooseImplHelper.chooseImplementationIfRequired(runnerId, moduleToRun);
    if (implementationId == ChooseImplementationHelper.CANCEL)
        return;
    RunConfiguration runConfig = runnerFrontEnd.createConfiguration(runnerId, implementationId, moduleToRun);
    ILaunchManager launchManager = DebugPlugin.getDefault().getLaunchManager();
    ILaunchConfigurationType type = launchManager.getLaunchConfigurationType(getLaunchConfigTypeID());
    DebugUITools.launch(runConfigConverter.toLaunchConfiguration(type, runConfig), mode);
// execution dispatched to proper delegate LaunchConfigurationDelegate
}
Also used : RunConfiguration(org.eclipse.n4js.runner.RunConfiguration) ILaunchConfigurationType(org.eclipse.debug.core.ILaunchConfigurationType) ILaunchManager(org.eclipse.debug.core.ILaunchManager) URI(org.eclipse.emf.common.util.URI)

Example 5 with RunConfiguration

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

the class HeadlessRunner method startRunner.

/**
 * Actually start the requested file with the chosen runner. Workspace from headlesCompiler should be configured
 * before calling this method.
 *
 * @param runner
 *            the runner to be used
 * @param implementationId
 *            to be used for API-IMPL projects
 * @param systemLoader
 *            to be used when loading the modules
 * @param locationToRun
 *            location of the code to be executed
 * @param targetPlatformInstallLocation
 *            location for externally installed node_modules
 * @throws ExitCodeException
 *             in cases of errors
 */
public void startRunner(String runner, String implementationId, String systemLoader, URI locationToRun, File targetPlatformInstallLocation) throws ExitCodeException {
    IRunnerDescriptor runnerDescriptor = checkRunner(runner);
    logger.info("Using runner :" + runnerDescriptor.getId());
    RunConfiguration runConfiguration = null;
    try {
        if (targetPlatformInstallLocation != null) {
            runConfiguration = runnerFrontEnd.createConfiguration(runnerDescriptor.getId(), implementationId, systemLoader, locationToRun, targetPlatformInstallLocation.toPath().resolve("node_modules").toAbsolutePath().toString());
        } else {
            runConfiguration = runnerFrontEnd.createConfiguration(runnerDescriptor.getId(), implementationId, systemLoader, locationToRun);
        }
    } catch (java.lang.IllegalStateException e2) {
        logger.error(Throwables.getStackTraceAsString(e2));
        throw new ExitCodeException(EXITCODE_RUNNER_STOPPED_WITH_ERROR, "Cannot create run configuration.", e2);
    }
    try {
        Process process = runnerFrontEnd.run(runConfiguration);
        int exit = process.waitFor();
        if (exit != 0) {
            throw new ExitCodeException(EXITCODE_RUNNER_STOPPED_WITH_ERROR, "The spawned runner '" + runnerDescriptor.getId() + "' exited with code=" + exit);
        }
    } catch (InterruptedException e1) {
        logger.error(Throwables.getStackTraceAsString(e1));
        throw new ExitCodeException(EXITCODE_RUNNER_STOPPED_WITH_ERROR, "The spawned runner exited by throwing an exception", e1);
    }
}
Also used : RunConfiguration(org.eclipse.n4js.runner.RunConfiguration) IRunnerDescriptor(org.eclipse.n4js.runner.extension.IRunnerDescriptor) ExitCodeException(org.eclipse.n4js.hlc.base.ExitCodeException)

Aggregations

RunConfiguration (org.eclipse.n4js.runner.RunConfiguration)6 File (java.io.File)2 ProcessResult (org.eclipse.n4js.utils.process.ProcessResult)2 HashSet (java.util.HashSet)1 ILaunchConfigurationType (org.eclipse.debug.core.ILaunchConfigurationType)1 ILaunchManager (org.eclipse.debug.core.ILaunchManager)1 URI (org.eclipse.emf.common.util.URI)1 Resource (org.eclipse.emf.ecore.resource.Resource)1 ExitCodeException (org.eclipse.n4js.hlc.base.ExitCodeException)1 Script (org.eclipse.n4js.n4JS.Script)1 IRunnerDescriptor (org.eclipse.n4js.runner.extension.IRunnerDescriptor)1 AbstractBuilderParticipantTest (org.eclipse.n4js.tests.builder.AbstractBuilderParticipantTest)1 XtextResource (org.eclipse.xtext.resource.XtextResource)1 Test (org.junit.Test)1