Skip to content

Commit

Permalink
Add -D --output-dir flag to specify the output directory for multi fi…
Browse files Browse the repository at this point in the history
…le output.
  • Loading branch information
davidzchen committed Sep 12, 2015
1 parent 9346df5 commit 35b61b4
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 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
21 changes: 18 additions & 3 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 @@ -178,6 +178,7 @@ void usage(std::ostream &o)
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 << " -D / --output-dir <dir> Write output files to the specifed directory\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 +240,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 @@ -353,6 +355,16 @@ int main(int argc, const char **argv)
std::cerr << "ERROR: -o output was empty string" << std::endl;
return EXIT_FAILURE;
}
} else if (arg == "-D" || arg == "--output-dir") {
std::string dir = next_arg(i, args);
if (dir.length() == 0) {
std::cerr << "ERROR: -D argument was empty string" << std::endl;
return EXIT_FAILURE;
}
if (dir[dir.length() - 1] != '/') {
dir += '/';
}
output_dir = dir;
} else if (arg == "-S" || arg == "--string") {
jsonnet_string_output(vm, 1);
} else if (arg == "--debug-ast") {
Expand Down Expand Up @@ -431,6 +443,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 +462,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 35b61b4

Please sign in to comment.