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