Search in sources :

Example 6 with Command

use of com.google.devtools.build.lib.shell.Command in project bazel by bazelbuild.

the class BazelBuilder method getListOfOutputFromCommand.

private ImmutableList<String> getListOfOutputFromCommand(String... command) throws CommandException {
    Command cmd = new Command(command, null, builderDir.toFile());
    CommandResult result = cmd.execute();
    String output = new String(result.getStdout(), UTF_8).trim();
    return ImmutableList.copyOf(output.split("\n"));
}
Also used : Command(com.google.devtools.build.lib.shell.Command) CommandResult(com.google.devtools.build.lib.shell.CommandResult)

Example 7 with Command

use of com.google.devtools.build.lib.shell.Command in project bazel by bazelbuild.

the class BazelBuilder method pullGitRepo.

private void pullGitRepo() throws CommandException {
    String[] gitCloneCommand = { "git", "pull" };
    Command cmd = new Command(gitCloneCommand, null, builderDir.toFile());
    cmd.execute();
}
Also used : Command(com.google.devtools.build.lib.shell.Command)

Example 8 with Command

use of com.google.devtools.build.lib.shell.Command in project bazel by bazelbuild.

the class BazelBuilder method getBuildBinary.

@Override
public Path getBuildBinary(String codeVersion) throws IOException, CommandException {
    if (buildBinary != null && currentCodeVersion.equals(codeVersion)) {
        return buildBinary;
    }
    // git checkout codeVersion
    String[] checkoutCommand = { "git", "checkout", codeVersion };
    Command cmd = new Command(checkoutCommand, null, builderDir.toFile());
    cmd.execute();
    // bazel build src:bazel
    String[] buildBazelCommand = { "bazel", "build", "src:bazel" };
    cmd = new Command(buildBazelCommand, null, builderDir.toFile());
    CommandResult result = cmd.execute();
    // Get binary path, bazel output is in stderr
    String output = new String(result.getStderr(), UTF_8).trim();
    if (!output.contains(BAZEL_BINARY_PATH)) {
        throw new IOException("Bazel binary " + BAZEL_BINARY_PATH + " is not in output of build.");
    }
    buildBinary = builderDir.resolve(BAZEL_BINARY_PATH);
    currentCodeVersion = codeVersion;
    return buildBinary;
}
Also used : Command(com.google.devtools.build.lib.shell.Command) IOException(java.io.IOException) CommandResult(com.google.devtools.build.lib.shell.CommandResult)

Example 9 with Command

use of com.google.devtools.build.lib.shell.Command in project bazel by bazelbuild.

the class BazelBuilderTest method testPrepareFromGitRepo.

@Test
public void testPrepareFromGitRepo() throws IOException, CommandException {
    Path root = folder.newFolder("Prepare").toPath();
    // Create a git repo for clone
    Path repo = root.resolve("SimpleRepo");
    Files.createDirectories(repo);
    Files.createFile(repo.resolve("BUILD"));
    Files.createFile(repo.resolve("WORKSPACE"));
    ImmutableList<String[]> gitCommands = ImmutableList.of(new String[] { "git", "init" }, new String[] { "git", "add", "." }, new String[] { "git", "config", "user.email", "you@example.com" }, new String[] { "git", "config", "user.name", "Your Name" }, new String[] { "git", "commit", "-m", "empty" });
    for (String[] gitCommand : gitCommands) {
        (new Command(gitCommand, null, repo.toFile())).execute();
    }
    BazelBuilder builder = new BazelBuilder(root.resolve("GeneratedCode"), root.resolve("Builder"));
    builder.prepareFromGitRepo(repo.toString());
    ImmutableSet<String> fileList = fileArrayToImmutableSet(root.resolve("Builder").toFile().listFiles());
    assertThat(fileList).containsExactly(".git", "BUILD", "WORKSPACE");
}
Also used : Path(java.nio.file.Path) Command(com.google.devtools.build.lib.shell.Command) Test(org.junit.Test)

Example 10 with Command

use of com.google.devtools.build.lib.shell.Command in project bazel by bazelbuild.

the class CommandUtilsTest method failingCommand.

@Test
public void failingCommand() throws Exception {
    String[] args = new String[3];
    args[0] = "/bin/sh";
    args[1] = "-c";
    args[2] = "echo Some errors 1>&2; echo Some output; exit 42";
    Map<String, String> env = Maps.newTreeMap();
    env.put("FOO", "foo");
    env.put("PATH", "/usr/bin:/bin:/sbin");
    try {
        new Command(args, env, null).execute();
        fail();
    } catch (CommandException exception) {
        String message = CommandUtils.describeCommandFailure(false, exception);
        String verboseMessage = CommandUtils.describeCommandFailure(true, exception);
        assertEquals("sh failed: error executing command " + "/bin/sh -c 'echo Some errors 1>&2; echo Some output; exit 42': " + "Process exited with status 42\n" + "Some output\n" + "Some errors\n", message);
        assertEquals("sh failed: error executing command \n" + "  (exec env - \\\n" + "    FOO=foo \\\n" + "    PATH=/usr/bin:/bin:/sbin \\\n" + "  /bin/sh -c 'echo Some errors 1>&2; echo Some output; exit 42'): " + "Process exited with status 42\n" + "Some output\n" + "Some errors\n", verboseMessage);
    }
}
Also used : Command(com.google.devtools.build.lib.shell.Command) CommandException(com.google.devtools.build.lib.shell.CommandException) Test(org.junit.Test)

Aggregations

Command (com.google.devtools.build.lib.shell.Command)16 CommandException (com.google.devtools.build.lib.shell.CommandException)11 CommandResult (com.google.devtools.build.lib.shell.CommandResult)6 IOException (java.io.IOException)6 UserExecException (com.google.devtools.build.lib.actions.UserExecException)4 AbnormalTerminationException (com.google.devtools.build.lib.shell.AbnormalTerminationException)4 TerminationStatus (com.google.devtools.build.lib.shell.TerminationStatus)4 File (java.io.File)4 ArrayList (java.util.ArrayList)4 Test (org.junit.Test)3 Executor (com.google.devtools.build.lib.actions.Executor)1 FileOutErr (com.google.devtools.build.lib.util.io.FileOutErr)1 Path (com.google.devtools.build.lib.vfs.Path)1 PathFragment (com.google.devtools.build.lib.vfs.PathFragment)1 Path (java.nio.file.Path)1 List (java.util.List)1 Matcher (java.util.regex.Matcher)1