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