use of com.facebook.buck.util.ProcessExecutor in project buck by facebook.
the class SymlinkFileStepTest method testReplaceMalformedSymlink.
@Test
public void testReplaceMalformedSymlink() throws IOException, InterruptedException {
assumeTrue(Platform.detect() != Platform.WINDOWS);
// Run `ln -s /path/that/does/not/exist dummy` in /tmp.
ProcessExecutorParams params = ProcessExecutorParams.builder().setCommand(ImmutableList.of("ln", "-s", "/path/that/does/not/exist", "my_symlink")).setDirectory(tmpDir.getRoot().toPath()).build();
ProcessExecutor executor = new DefaultProcessExecutor(Console.createNullConsole());
executor.launchAndExecute(params);
// Verify that the symlink points to a non-existent file.
Path symlink = Paths.get(tmpDir.getRoot().getAbsolutePath(), "my_symlink");
assertFalse("exists() should reflect the existence of what the symlink points to", symlink.toFile().exists());
assertTrue("even though exists() is false, isSymbolicLink should be true", java.nio.file.Files.isSymbolicLink(symlink));
// Create an ExecutionContext to return the ProjectFilesystem.
ProjectFilesystem projectFilesystem = new ProjectFilesystem(tmpDir.getRoot().toPath());
ExecutionContext executionContext = TestExecutionContext.newInstance();
tmpDir.newFile("dummy");
SymlinkFileStep symlinkStep = new SymlinkFileStep(projectFilesystem, /* source */
Paths.get("dummy"), /* target */
Paths.get("my_symlink"), /* useAbsolutePaths*/
true);
int exitCode = symlinkStep.execute(executionContext).getExitCode();
assertEquals(0, exitCode);
assertTrue(java.nio.file.Files.isSymbolicLink(symlink));
assertTrue(symlink.toFile().exists());
}
use of com.facebook.buck.util.ProcessExecutor 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.ProcessExecutor in project buck by facebook.
the class HostnameFetchingTest method fetchedHostnameMatchesCommandLineHostname.
@Test
public void fetchedHostnameMatchesCommandLineHostname() throws IOException, InterruptedException {
ProcessExecutor executor = new DefaultProcessExecutor(new TestConsole());
ProcessExecutor.Result result = executor.launchAndExecute(ProcessExecutorParams.ofCommand("hostname"));
assumeThat("hostname returns success", result.getExitCode(), equalTo(0));
String expectedHostname = result.getStdout().orElse("").trim();
assumeThat("hostname returns non-empty string", expectedHostname, not(emptyString()));
assertThat("fetched hostname should equal hostname returned from CLI", HostnameFetching.getHostname(), equalTo(expectedHostname));
}
use of com.facebook.buck.util.ProcessExecutor 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;
}
use of com.facebook.buck.util.ProcessExecutor in project buck by facebook.
the class HgCmdLineInterface method executeCommand.
private String executeCommand(Iterable<String> command) throws VersionControlCommandFailedException, InterruptedException {
command = replaceTemplateValue(command, HG_CMD_TEMPLATE, hgCmd);
String commandString = commandAsString(command);
LOG.debug("Executing command: " + commandString);
ProcessExecutorParams processExecutorParams = ProcessExecutorParams.builder().setCommand(command).setDirectory(projectRoot).setEnvironment(environment).build();
ProcessExecutor.Result result;
try (PrintStream stdout = new PrintStream(new ByteArrayOutputStream());
PrintStream stderr = new PrintStream(new ByteArrayOutputStream())) {
ProcessExecutor processExecutor = processExecutorFactory.createProcessExecutor(stdout, stderr);
result = processExecutor.launchAndExecute(processExecutorParams);
} catch (IOException e) {
throw new VersionControlCommandFailedException(e);
}
Optional<String> resultString = result.getStdout();
if (!resultString.isPresent()) {
throw new VersionControlCommandFailedException("Received no output from launched process for command: " + commandString);
}
if (result.getExitCode() != 0) {
throw new VersionControlCommandFailedException(result.getMessageForUnexpectedResult(commandString));
}
return cleanResultString(resultString.get());
}
Aggregations