Skip to content

Commit

Permalink
Add argument to -m flag to specify the output directory for multi fil…
Browse files Browse the repository at this point in the history
…e output.
  • Loading branch information
davidzchen committed Sep 14, 2015
1 parent fefd654 commit 20bf751
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 9 deletions.
7 changes: 7 additions & 0 deletions BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ genrule(
"echo >> $@",
)

# TODO(dzc): Remove the includes = ["."] lines from all cc_* targets once
# bazelbuild/bazel#445 is fixed.
cc_library(
name = "jsonnet-common",
srcs = [
Expand All @@ -30,31 +32,36 @@ cc_library(
"static_error.h",
"vm.h",
],
includes = ["."],
)

cc_library(
name = "libjsonnet",
srcs = ["libjsonnet.cpp"],
hdrs = ["libjsonnet.h"],
deps = [":jsonnet-common"],
includes = ["."],
)

cc_binary(
name = "jsonnet",
srcs = ["jsonnet.cpp"],
deps = [":libjsonnet"],
includes = ["."],
)

cc_binary(
name = "libjsonnet_test_snippet",
srcs = ["libjsonnet_test_snippet.c"],
deps = [":libjsonnet"],
includes = ["."],
)

cc_binary(
name = "libjsonnet_test_file",
srcs = ["libjsonnet_test_file.c"],
deps = [":libjsonnet"],
includes = ["."],
)

filegroup(
Expand Down
31 changes: 22 additions & 9 deletions jsonnet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ static enum ImportStatus try_path(const std::string &dir, const std::string &rel
}

static char *from_string(JsonnetVm* vm, const std::string &v)
{
{
char *r = jsonnet_realloc(vm, nullptr, v.length() + 1);
std::strcpy(r, v.c_str());
return r;
Expand Down Expand Up @@ -177,7 +177,7 @@ void usage(std::ostream &o)
o << " --code-var <var>=<val> As --var but value is Jsonnet code\n";
o << " --code-env <var> As --env but env var contains Jsonnet code\n";
o << " -o / --output-file <file> Write to the output file rather than stdout\n";
o << " -m / --multi Write multiple files, list files on stdout\n";
o << " -m / --multi <dir> Write multiple files to the directory, list files on stdout\n";
o << " -S / --string Expect a string, manifest as plain text\n";
o << " -s / --max-stack <n> Number of allowed stack frames\n";
o << " -t / --max-trace <n> Max length of stack trace before cropping\n";
Expand Down Expand Up @@ -239,8 +239,9 @@ int main(int argc, const char **argv)

JsonnetVm *vm = jsonnet_make();
bool filename_is_code = false;

bool multi = false;
std::string output_dir = ".";

auto args = simplify_args(argc, argv);
std::vector<std::string> remaining_args;
Expand Down Expand Up @@ -347,12 +348,21 @@ int main(int argc, const char **argv)
filename_is_code = true;
} else if (arg == "-m" || arg == "--multi") {
multi = true;
std::string dir = next_arg(i, args);
if (dir.length() == 0) {
std::cerr << "ERROR: -m argument was empty string" << std::endl;
return EXIT_FAILURE;
}
if (dir[dir.length() - 1] != '/') {
dir += '/';
}
output_dir = dir;
} else if (arg == "-o" || arg == "--output-file") {
output_file = next_arg(i, args);
if (output_file.length() == 0) {
std::cerr << "ERROR: -o output was empty string" << std::endl;
return EXIT_FAILURE;
}
output_file = next_arg(i, args);
if (output_file.length() == 0) {
std::cerr << "ERROR: -o output was empty string" << std::endl;
return EXIT_FAILURE;
}
} else if (arg == "-S" || arg == "--string") {
jsonnet_string_output(vm, 1);
} else if (arg == "--debug-ast") {
Expand Down Expand Up @@ -431,6 +441,9 @@ int main(int argc, const char **argv)
return EXIT_FAILURE;
}

// If multiple file output is used, then iterate over each string from
// the sequence of strings returned by jsonnet_evaluate_snippet_multi,
// construct pairs of filename and content, and write each output file.
if (multi) {
std::map<std::string, std::string> r;
for (const char *c=output ; *c!='\0' ; ) {
Expand All @@ -447,7 +460,7 @@ int main(int argc, const char **argv)
jsonnet_realloc(vm, output, 0);
for (const auto &pair : r) {
const std::string &new_content = pair.second;
const std::string &filename = pair.first;
const std::string &filename = output_dir + pair.first;
std::cout << filename << std::endl;
{
std::ifstream exists(filename.c_str());
Expand Down

0 comments on commit 20bf751

Please sign in to comment.