From 20bf75170adbb6dbecdd36256ade7bd128af638a Mon Sep 17 00:00:00 2001 From: "David Z. Chen" Date: Sat, 12 Sep 2015 01:11:24 -0700 Subject: [PATCH] Add argument to -m flag to specify the output directory for multi file output. --- BUILD | 7 +++++++ jsonnet.cpp | 31 ++++++++++++++++++++++--------- 2 files changed, 29 insertions(+), 9 deletions(-) diff --git a/BUILD b/BUILD index d385b52c9..d4320f433 100644 --- a/BUILD +++ b/BUILD @@ -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 = [ @@ -30,6 +32,7 @@ cc_library( "static_error.h", "vm.h", ], + includes = ["."], ) cc_library( @@ -37,24 +40,28 @@ cc_library( 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( diff --git a/jsonnet.cpp b/jsonnet.cpp index c94d6fde0..67d5efe7c 100644 --- a/jsonnet.cpp +++ b/jsonnet.cpp @@ -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; @@ -177,7 +177,7 @@ void usage(std::ostream &o) o << " --code-var = As --var but value is Jsonnet code\n"; o << " --code-env As --env but env var contains Jsonnet code\n"; o << " -o / --output-file Write to the output file rather than stdout\n"; - o << " -m / --multi Write multiple files, list files on stdout\n"; + o << " -m / --multi 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 Number of allowed stack frames\n"; o << " -t / --max-trace Max length of stack trace before cropping\n"; @@ -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 remaining_args; @@ -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") { @@ -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 r; for (const char *c=output ; *c!='\0' ; ) { @@ -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());