Search in sources :

Example 36 with ProcessExecutorParams

use of com.facebook.buck.util.ProcessExecutorParams in project buck by facebook.

the class ShellStepTest method testStdErrPrintedOnErrorIfShouldPrintStdErrEvenIfSilent.

@Test
public void testStdErrPrintedOnErrorIfShouldPrintStdErrEvenIfSilent() throws Exception {
    ShellStep command = createCommand(/*shouldPrintStdErr*/
    true, /*shouldPrintStdOut*/
    false);
    ProcessExecutorParams params = createParams();
    FakeProcess process = new FakeProcess(EXIT_FAILURE, OUTPUT_MSG, ERROR_MSG);
    TestConsole console = new TestConsole(Verbosity.SILENT);
    ExecutionContext context = createContext(ImmutableMap.of(params, process), console);
    command.launchAndInteractWithProcess(context, params);
    assertEquals(ERROR_MSG, console.getTextWrittenToStdErr());
}
Also used : ProcessExecutorParams(com.facebook.buck.util.ProcessExecutorParams) ExecutionContext(com.facebook.buck.step.ExecutionContext) TestExecutionContext(com.facebook.buck.step.TestExecutionContext) FakeProcess(com.facebook.buck.util.FakeProcess) TestConsole(com.facebook.buck.testutil.TestConsole) Test(org.junit.Test)

Example 37 with ProcessExecutorParams

use of com.facebook.buck.util.ProcessExecutorParams in project buck by facebook.

the class ProjectWorkspace method doRunCommand.

private ProcessExecutor.Result doRunCommand(List<String> command) throws IOException, InterruptedException {
    ProcessExecutorParams params = ProcessExecutorParams.builder().setCommand(command).build();
    ProcessExecutor executor = new DefaultProcessExecutor(new TestConsole());
    String currentDir = System.getProperty("user.dir");
    try {
        System.setProperty("user.dir", destPath.toAbsolutePath().toString());
        return executor.launchAndExecute(params);
    } finally {
        System.setProperty("user.dir", currentDir);
    }
}
Also used : ProcessExecutorParams(com.facebook.buck.util.ProcessExecutorParams) DefaultProcessExecutor(com.facebook.buck.util.DefaultProcessExecutor) ProcessExecutor(com.facebook.buck.util.ProcessExecutor) DefaultProcessExecutor(com.facebook.buck.util.DefaultProcessExecutor) TestConsole(com.facebook.buck.testutil.TestConsole)

Example 38 with ProcessExecutorParams

use of com.facebook.buck.util.ProcessExecutorParams in project buck by facebook.

the class ProjectCommand method runPreprocessScriptIfNeeded.

private int runPreprocessScriptIfNeeded(CommandRunnerParams params) throws IOException, InterruptedException {
    Optional<String> pathToPreProcessScript = getPathToPreProcessScript(params.getBuckConfig());
    if (!pathToPreProcessScript.isPresent()) {
        return 0;
    }
    String pathToScript = pathToPreProcessScript.get();
    if (!Paths.get(pathToScript).isAbsolute()) {
        pathToScript = params.getCell().getFilesystem().getPathForRelativePath(pathToScript).toAbsolutePath().toString();
    }
    ListeningProcessExecutor processExecutor = new ListeningProcessExecutor();
    ProcessExecutorParams processExecutorParams = ProcessExecutorParams.builder().addCommand(pathToScript).setEnvironment(ImmutableMap.<String, String>builder().putAll(params.getEnvironment()).put("BUCK_PROJECT_TARGETS", Joiner.on(" ").join(getArguments())).build()).setDirectory(params.getCell().getFilesystem().getRootPath()).build();
    ForwardingProcessListener processListener = new ForwardingProcessListener(// because this process finishes before we start parsing process.
    Channels.newChannel(params.getConsole().getStdOut().getRawStream()), Channels.newChannel(params.getConsole().getStdErr().getRawStream()));
    ListeningProcessExecutor.LaunchedProcess process = processExecutor.launchProcess(processExecutorParams, processListener);
    try {
        return processExecutor.waitForProcess(process);
    } finally {
        processExecutor.destroyProcess(process, /* force */
        false);
        processExecutor.waitForProcess(process);
    }
}
Also used : ProcessExecutorParams(com.facebook.buck.util.ProcessExecutorParams) ListeningProcessExecutor(com.facebook.buck.util.ListeningProcessExecutor) ForwardingProcessListener(com.facebook.buck.util.ForwardingProcessListener)

Example 39 with ProcessExecutorParams

use of com.facebook.buck.util.ProcessExecutorParams in project buck by facebook.

the class RunCommand method runWithoutHelp.

@Override
public int runWithoutHelp(CommandRunnerParams params) throws IOException, InterruptedException {
    if (!hasTargetSpecified()) {
        params.getBuckEventBus().post(ConsoleEvent.severe("No target given to run"));
        params.getBuckEventBus().post(ConsoleEvent.severe("buck run <target> <arg1> <arg2>..."));
        return 1;
    }
    // Make sure the target is built.
    BuildCommand buildCommand = new BuildCommand(ImmutableList.of(getTarget(params.getBuckConfig())));
    int exitCode = buildCommand.runWithoutHelp(params);
    if (exitCode != 0) {
        return exitCode;
    }
    String targetName = getTarget(params.getBuckConfig());
    BuildTarget target = Iterables.getOnlyElement(getBuildTargets(params.getCell().getCellPathResolver(), ImmutableSet.of(targetName)));
    Build build = buildCommand.getBuild();
    BuildRule targetRule;
    try {
        targetRule = build.getRuleResolver().requireRule(target);
    } catch (NoSuchBuildTargetException e) {
        throw new HumanReadableException(e.getHumanReadableErrorMessage());
    }
    BinaryBuildRule binaryBuildRule = null;
    if (targetRule instanceof BinaryBuildRule) {
        binaryBuildRule = (BinaryBuildRule) targetRule;
    }
    if (binaryBuildRule == null) {
        params.getBuckEventBus().post(ConsoleEvent.severe("target " + targetName + " is not a binary rule (only binary rules can be `run`)"));
        return 1;
    }
    // Ideally, we would take fullCommand, disconnect from NailGun, and run the command in the
    // user's shell. Currently, if you use `buck run` with buckd and ctrl-C to kill the command
    // being run, occasionally I get the following error when I try to run `buck run` again:
    //
    //   Daemon is busy, please wait or run "buck kill" to terminate it.
    //
    // Clearly something bad has happened here. If you are using `buck run` to start up a server
    // or some other process that is meant to "run forever," then it's pretty common to do:
    // `buck run`, test server, hit ctrl-C, edit server code, repeat. This should not wedge buckd.
    SourcePathResolver resolver = new SourcePathResolver(new SourcePathRuleFinder(build.getRuleResolver()));
    Tool executable = binaryBuildRule.getExecutableCommand();
    ListeningProcessExecutor processExecutor = new ListeningProcessExecutor();
    ProcessExecutorParams processExecutorParams = ProcessExecutorParams.builder().addAllCommand(executable.getCommandPrefix(resolver)).addAllCommand(getTargetArguments()).setEnvironment(ImmutableMap.<String, String>builder().putAll(params.getEnvironment()).putAll(executable.getEnvironment(resolver)).build()).setDirectory(params.getCell().getFilesystem().getRootPath()).build();
    ForwardingProcessListener processListener = new ForwardingProcessListener(Channels.newChannel(params.getConsole().getStdOut()), Channels.newChannel(params.getConsole().getStdErr()));
    ListeningProcessExecutor.LaunchedProcess process = processExecutor.launchProcess(processExecutorParams, processListener);
    try {
        return processExecutor.waitForProcess(process);
    } finally {
        processExecutor.destroyProcess(process, /* force */
        false);
        processExecutor.waitForProcess(process);
    }
}
Also used : ProcessExecutorParams(com.facebook.buck.util.ProcessExecutorParams) BinaryBuildRule(com.facebook.buck.rules.BinaryBuildRule) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) BuildTarget(com.facebook.buck.model.BuildTarget) Build(com.facebook.buck.command.Build) HumanReadableException(com.facebook.buck.util.HumanReadableException) ListeningProcessExecutor(com.facebook.buck.util.ListeningProcessExecutor) NoSuchBuildTargetException(com.facebook.buck.parser.NoSuchBuildTargetException) ForwardingProcessListener(com.facebook.buck.util.ForwardingProcessListener) BinaryBuildRule(com.facebook.buck.rules.BinaryBuildRule) BuildRule(com.facebook.buck.rules.BuildRule) Tool(com.facebook.buck.rules.Tool)

Example 40 with ProcessExecutorParams

use of com.facebook.buck.util.ProcessExecutorParams in project buck by facebook.

the class Project method createIntellijProject.

public int createIntellijProject(File jsonTempFile, ProcessExecutor processExecutor, boolean generateMinimalProject, PrintStream stdOut, PrintStream stdErr) throws IOException, InterruptedException {
    List<SerializableModule> modules = createModulesForProjectConfigs();
    writeJsonConfig(jsonTempFile, modules);
    List<String> modifiedFiles = Lists.newArrayList();
    // Process the JSON config to generate the .xml and .iml files for IntelliJ.
    ExitCodeAndOutput result = processJsonConfig(jsonTempFile, generateMinimalProject);
    if (result.exitCode != 0) {
        Logger.get(Project.class).error(result.stdErr);
        return result.exitCode;
    } else {
        // intellij.py writes the list of modified files to stdout, so parse stdout and add the
        // resulting file paths to the modifiedFiles list.
        Iterable<String> paths = Splitter.on('\n').trimResults().omitEmptyStrings().split(result.stdOut);
        Iterables.addAll(modifiedFiles, paths);
    }
    // Write out the .idea/compiler.xml file (the .idea/ directory is guaranteed to exist).
    CompilerXml compilerXml = new CompilerXml(projectFilesystem, modules);
    final String pathToCompilerXml = ".idea/compiler.xml";
    File compilerXmlFile = projectFilesystem.getPathForRelativePath(pathToCompilerXml).toFile();
    if (compilerXml.write(compilerXmlFile)) {
        modifiedFiles.add(pathToCompilerXml);
    }
    // If the user specified a post-processing script, then run it.
    if (pathToPostProcessScript.isPresent()) {
        String pathToScript = pathToPostProcessScript.get();
        ProcessExecutorParams params = ProcessExecutorParams.builder().setCommand(ImmutableList.of(pathToScript)).build();
        ProcessExecutor.Result postProcessResult = processExecutor.launchAndExecute(params);
        int postProcessExitCode = postProcessResult.getExitCode();
        if (postProcessExitCode != 0) {
            return postProcessExitCode;
        }
    }
    if (executionContext.getConsole().getVerbosity().shouldPrintOutput()) {
        SortedSet<String> modifiedFilesInSortedForder = Sets.newTreeSet(modifiedFiles);
        stdOut.printf("MODIFIED FILES:\n%s\n", Joiner.on('\n').join(modifiedFilesInSortedForder));
    } else {
        // If any files have been modified by `buck project`, then inform the user.
        if (!modifiedFiles.isEmpty()) {
            stdOut.printf("Modified %d IntelliJ project files.\n", modifiedFiles.size());
        } else {
            stdOut.println("No IntelliJ project files modified.");
        }
    }
    // Blit stderr from intellij.py to parent stderr.
    stdErr.print(result.stdErr);
    return 0;
}
Also used : ProcessExecutorParams(com.facebook.buck.util.ProcessExecutorParams) ProcessExecutor(com.facebook.buck.util.ProcessExecutor) File(java.io.File)

Aggregations

ProcessExecutorParams (com.facebook.buck.util.ProcessExecutorParams)72 ProcessExecutor (com.facebook.buck.util.ProcessExecutor)32 FakeProcess (com.facebook.buck.util.FakeProcess)30 Test (org.junit.Test)30 FakeProcessExecutor (com.facebook.buck.util.FakeProcessExecutor)21 ExecutionContext (com.facebook.buck.step.ExecutionContext)19 TestExecutionContext (com.facebook.buck.step.TestExecutionContext)19 IOException (java.io.IOException)18 FakeProjectFilesystem (com.facebook.buck.testutil.FakeProjectFilesystem)12 TestConsole (com.facebook.buck.testutil.TestConsole)12 Path (java.nio.file.Path)11 DefaultProcessExecutor (com.facebook.buck.util.DefaultProcessExecutor)8 ListeningProcessExecutor (com.facebook.buck.util.ListeningProcessExecutor)6 HumanReadableException (com.facebook.buck.util.HumanReadableException)5 ImmutableList (com.google.common.collect.ImmutableList)5 ImmutableSet (com.google.common.collect.ImmutableSet)5 ImmutableMap (com.google.common.collect.ImmutableMap)4 InputStream (java.io.InputStream)4 InputStreamReader (java.io.InputStreamReader)4 Matcher (java.util.regex.Matcher)4