Search in sources :

Example 16 with ProcessExecutor

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());
}
Also used : Path(java.nio.file.Path) ProcessExecutorParams(com.facebook.buck.util.ProcessExecutorParams) ExecutionContext(com.facebook.buck.step.ExecutionContext) TestExecutionContext(com.facebook.buck.step.TestExecutionContext) DefaultProcessExecutor(com.facebook.buck.util.DefaultProcessExecutor) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) ProcessExecutor(com.facebook.buck.util.ProcessExecutor) DefaultProcessExecutor(com.facebook.buck.util.DefaultProcessExecutor) Test(org.junit.Test)

Example 17 with ProcessExecutor

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);
    }
}
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 18 with ProcessExecutor

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));
}
Also used : DefaultProcessExecutor(com.facebook.buck.util.DefaultProcessExecutor) Matchers.emptyString(org.hamcrest.Matchers.emptyString) ProcessExecutor(com.facebook.buck.util.ProcessExecutor) DefaultProcessExecutor(com.facebook.buck.util.DefaultProcessExecutor) TestConsole(com.facebook.buck.testutil.TestConsole) Test(org.junit.Test)

Example 19 with ProcessExecutor

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;
}
Also used : ProcessExecutorParams(com.facebook.buck.util.ProcessExecutorParams) ProcessExecutor(com.facebook.buck.util.ProcessExecutor) File(java.io.File)

Example 20 with ProcessExecutor

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());
}
Also used : PrintStream(java.io.PrintStream) ProcessExecutorParams(com.facebook.buck.util.ProcessExecutorParams) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ProcessExecutor(com.facebook.buck.util.ProcessExecutor)

Aggregations

ProcessExecutor (com.facebook.buck.util.ProcessExecutor)38 DefaultProcessExecutor (com.facebook.buck.util.DefaultProcessExecutor)20 ProcessExecutorParams (com.facebook.buck.util.ProcessExecutorParams)18 TestConsole (com.facebook.buck.testutil.TestConsole)13 IOException (java.io.IOException)13 Path (java.nio.file.Path)12 Test (org.junit.Test)11 ProjectFilesystem (com.facebook.buck.io.ProjectFilesystem)8 HumanReadableException (com.facebook.buck.util.HumanReadableException)6 ImmutableMap (com.google.common.collect.ImmutableMap)5 FakeBuckConfig (com.facebook.buck.cli.FakeBuckConfig)4 Verbosity (com.facebook.buck.util.Verbosity)4 ImmutableSet (com.google.common.collect.ImmutableSet)4 FakeAndroidDirectoryResolver (com.facebook.buck.android.FakeAndroidDirectoryResolver)3 BuckConfig (com.facebook.buck.cli.BuckConfig)3 FakeProcessExecutor (com.facebook.buck.util.FakeProcessExecutor)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 InputStreamReader (java.io.InputStreamReader)3 PrintStream (java.io.PrintStream)3 NSDate (com.dd.plist.NSDate)2