-
-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy pathjsonnet.bzl
More file actions
950 lines (827 loc) · 32.9 KB
/
jsonnet.bzl
File metadata and controls
950 lines (827 loc) · 32.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
# Copyright 2015 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
load("@bazel_skylib//lib:paths.bzl", "paths")
load("@bazel_skylib//lib:shell.bzl", "shell")
_JSONNET_FILETYPE = [
".jsonnet",
".libsonnet",
".json",
]
JsonnetLibraryInfo = provider(
fields = {
"imports": "Depset of Strings containing import flags set by transitive dependency targets.",
"short_imports": "Depset of Strings containing import flags set by transitive dependency targets, when invoking Jsonnet as part of a test where dependencies are stored in runfiles.",
"transitive_jsonnet_files": "Depset of Files containing sources of transitive dependencies",
},
)
def _get_import_paths(label, files, imports, short_path):
# TODO: Is there a cleaner way to compute the short paths here?
return [
# Implicitly add the workspace root as an import path.
paths.join(
".",
"" if short_path else file.root.path,
label.workspace_root.replace("external/", "../") if short_path else label.workspace_root,
)
for file in files
] + [
# Explicitly provided import paths.
paths.join(
".",
"" if short_path else file.root.path,
label.workspace_root.replace("external/", "../") if short_path else label.workspace_root,
label.package,
im,
)
for file in files
for im in imports
]
def _setup_deps(deps, tla_code_libraries = {}, ext_code_libraries = {}):
"""Collects source files and import flags of transitive dependencies.
Args:
deps: List of deps labels from ctx.attr.deps.
tla_code_libraries: Dict of labels to names from ctx.attr.tla_code_files.
ext_code_libraries: List of deps labels from ctx.attr.ext_code_files.
Returns:
Returns a struct containing the following fields:
transitive_sources: Depset of Files containing sources of transitive
dependencies
imports: Depset of Strings containing import flags set by transitive
dependency targets.
short_imports: Depset of Strings containing import flags set by
transitive dependency targets, when invoking Jsonnet as part
of a test where dependencies are stored in runfiles.
"""
transitive_sources = []
imports = []
short_imports = []
for dep in deps:
transitive_sources.append(dep[JsonnetLibraryInfo].transitive_jsonnet_files)
imports.append(dep[JsonnetLibraryInfo].imports)
short_imports.append(dep[JsonnetLibraryInfo].short_imports)
for code_file in tla_code_libraries.keys() + ext_code_libraries.keys():
transitive_sources.append(code_file[JsonnetLibraryInfo].transitive_jsonnet_files)
imports.append(code_file[JsonnetLibraryInfo].imports)
short_imports.append(code_file[JsonnetLibraryInfo].short_imports)
return struct(
imports = depset(transitive = imports),
short_imports = depset(transitive = short_imports),
transitive_sources = depset(transitive = transitive_sources, order = "postorder"),
)
def _jsonnet_library_impl(ctx):
"""Implementation of the jsonnet_library rule."""
depinfo = _setup_deps(ctx.attr.deps)
sources = depset(ctx.files.srcs, transitive = [depinfo.transitive_sources])
imports = depset(
_get_import_paths(ctx.label, ctx.files.srcs, ctx.attr.imports, False),
transitive = [depinfo.imports],
)
short_imports = depset(
_get_import_paths(ctx.label, ctx.files.srcs, ctx.attr.imports, True),
transitive = [depinfo.short_imports],
)
return [
DefaultInfo(
files = depset(ctx.files.srcs),
runfiles = ctx.runfiles(
files = ctx.files.data,
).merge_all([
target[DefaultInfo].default_runfiles
for attr in (ctx.attr.data, ctx.attr.deps, ctx.attr.srcs)
for target in attr
]),
),
JsonnetLibraryInfo(
imports = imports,
short_imports = short_imports,
transitive_jsonnet_files = sources,
),
]
def _quote(s):
return '"' + s.replace('"', '\\"') + '"'
def _stamp_resolve(ctx, string, output):
stamps = [ctx.info_file, ctx.version_file]
stamp_args = [
"--stamp-info-file=%s" % sf.path
for sf in stamps
]
ctx.actions.run(
executable = ctx.executable._stamper,
arguments = [
"--format=%s" % string,
"--output=%s" % output.path,
] + stamp_args,
inputs = stamps,
tools = [ctx.executable._stamper],
outputs = [output],
mnemonic = "Stamp",
)
def _make_resolve(ctx, val):
if val[0:2] == "$(" and val[-1] == ")":
return ctx.var[val[2:-1]]
else:
return val
def _make_stamp_resolve(ext_vars, ctx, relative = True):
results = {}
stamp_inputs = []
for key, val in ext_vars.items():
# Check for make variables
val = _make_resolve(ctx, val)
# Check for stamp variables
if ctx.attr.stamp_keys:
if key in ctx.attr.stamp_keys:
stamp_file = ctx.actions.declare_file(ctx.label.name + ".jsonnet_" + key)
_stamp_resolve(ctx, val, stamp_file)
if relative:
val = "$(cat %s)" % stamp_file.short_path
else:
val = "$(cat %s)" % stamp_file.path
stamp_inputs.append(stamp_file)
results[key] = val
return results, stamp_inputs
def _jsonnet_to_json_impl(ctx):
"""Implementation of the jsonnet_to_json rule."""
if ctx.attr.vars:
print("'vars' attribute is deprecated, please use 'ext_strs'.")
if ctx.attr.code_vars:
print("'code_vars' attribute is deprecated, please use 'ext_code'.")
jsonnet_ext_strs = ctx.attr.ext_strs or ctx.attr.vars
jsonnet_ext_str_envs = ctx.attr.ext_str_envs
jsonnet_ext_code = ctx.attr.ext_code or ctx.attr.code_vars
jsonnet_ext_code_envs = ctx.attr.ext_code_envs
jsonnet_ext_str_files = ctx.files.ext_str_files
jsonnet_ext_str_file_vars = ctx.attr.ext_str_file_vars
jsonnet_ext_code_files = ctx.files.ext_code_files
jsonnet_ext_code_file_vars = ctx.attr.ext_code_file_vars
jsonnet_ext_code_libraries = ctx.attr.ext_code_libraries
jsonnet_tla_strs = ctx.attr.tla_strs
jsonnet_tla_str_envs = ctx.attr.tla_str_envs
jsonnet_tla_code = ctx.attr.tla_code
jsonnet_tla_code_envs = ctx.attr.tla_code_envs
jsonnet_tla_str_files = ctx.attr.tla_str_files
jsonnet_tla_code_files = ctx.attr.tla_code_files
jsonnet_tla_code_libraries = ctx.attr.tla_code_libraries
depinfo = _setup_deps(ctx.attr.deps, jsonnet_tla_code_libraries, jsonnet_ext_code_libraries)
jsonnet_ext_strs, strs_stamp_inputs = _make_stamp_resolve(ctx.attr.ext_strs, ctx, False)
jsonnet_ext_code, code_stamp_inputs = _make_stamp_resolve(ctx.attr.ext_code, ctx, False)
jsonnet_tla_strs, tla_strs_stamp_inputs = _make_stamp_resolve(ctx.attr.tla_strs, ctx, False)
jsonnet_tla_code, tla_code_stamp_inputs = _make_stamp_resolve(ctx.attr.tla_code, ctx, False)
stamp_inputs = strs_stamp_inputs + code_stamp_inputs + tla_strs_stamp_inputs + tla_code_stamp_inputs
if len(jsonnet_ext_str_file_vars) != len(jsonnet_ext_str_files):
fail("Mismatch of ext_str_file_vars ({}) to ext_str_files ({})".format(jsonnet_ext_str_file_vars, jsonnet_ext_str_files))
if len(jsonnet_ext_code_file_vars) != len(jsonnet_ext_code_files):
fail("Mismatch of ext_code_file_vars ({}) to ext_code_files ({})".format(jsonnet_ext_code_file_vars, jsonnet_ext_code_files))
if ctx.attr.stamp_keys and not stamp_inputs:
fail("Stamping requested but found no stamp variable to resolve for.")
other_args = ctx.attr.extra_args + (["-y"] if ctx.attr.yaml_stream else [])
command = (
[
"set -e;",
ctx.toolchains["//jsonnet:toolchain_type"].jsonnetinfo.compiler.path,
] +
["-J " + shell.quote(im) for im in _get_import_paths(ctx.label, [ctx.file.src], ctx.attr.imports, False)] +
["-J " + shell.quote(im) for im in depinfo.imports.to_list()] +
other_args +
["--ext-str %s=%s" %
(_quote(key), _quote(val)) for key, val in jsonnet_ext_strs.items()] +
["--ext-str '%s'" %
ext_str_env for ext_str_env in jsonnet_ext_str_envs] +
["--ext-code %s=%s" %
(_quote(key), _quote(val)) for key, val in jsonnet_ext_code.items()] +
["--ext-code %s" %
ext_code_env for ext_code_env in jsonnet_ext_code_envs] +
["--ext-str-file %s=%s" %
(var, jfile.path) for var, jfile in zip(jsonnet_ext_str_file_vars, jsonnet_ext_str_files)] +
["--ext-code-file %s=%s" %
(var, jfile.path) for var, jfile in zip(jsonnet_ext_code_file_vars, jsonnet_ext_code_files)] +
["--ext-code-file %s=%s" %
(_quote(val), _quote(key[DefaultInfo].files.to_list()[0].path)) for key, val in jsonnet_ext_code_libraries.items()] +
["--tla-str %s=%s" %
(_quote(key), _quote(val)) for key, val in jsonnet_tla_strs.items()] +
["--tla-str '%s'" %
tla_str_env for tla_str_env in jsonnet_tla_str_envs] +
["--tla-code %s=%s" %
(_quote(key), _quote(val)) for key, val in jsonnet_tla_code.items()] +
["--tla-code %s" %
tla_code_env for tla_code_env in jsonnet_tla_code_envs] +
["--tla-str-file %s=%s" %
(var, jfile.files.to_list()[0].path) for jfile, var in jsonnet_tla_str_files.items()] +
["--tla-code-file %s=%s" %
(var, jfile.files.to_list()[0].path) for jfile, var in jsonnet_tla_code_files.items()] +
["--tla-code-file %s=%s" %
(_quote(val), _quote(key[DefaultInfo].files.to_list()[0].path)) for key, val in jsonnet_tla_code_libraries.items()]
)
outputs = []
if (ctx.attr.outs or ctx.attr.multiple_outputs) and ctx.attr.out_dir:
fail("The \"outs\" and \"multiple_outputs\" attributes are " +
"incompatible with \"out_dir\".")
# If multiple_outputs or out_dir is set, then jsonnet will be
# invoked with the -m flag for multiple outputs. Otherwise, jsonnet
# will write the resulting JSON to stdout, which is redirected into
# a single JSON output file.
if ctx.attr.out_dir:
if ctx.toolchains["//jsonnet:toolchain_type"].jsonnetinfo.manifest_file_support:
output_manifest = ctx.actions.declare_file("_%s_outs.mf" % ctx.label.name)
outputs.append(output_manifest)
command += ["-o", output_manifest.path]
out_dir = ctx.actions.declare_directory(ctx.attr.out_dir)
outputs.append(out_dir)
command += [ctx.file.src.path, "-m", out_dir.path]
command += ctx.toolchains["//jsonnet:toolchain_type"].jsonnetinfo.create_directory_flags
elif len(ctx.attr.outs) > 1 or ctx.attr.multiple_outputs:
# Assume that the output directory is the leading part of the
# directory name that is shared by all output files.
base_dirname = ctx.outputs.outs[0].dirname.split("/")
for output in ctx.outputs.outs[1:]:
component_pairs = zip(base_dirname, output.dirname.split("/"))
base_dirname = base_dirname[:len(component_pairs)]
for i, (part1, part2) in enumerate(component_pairs):
if part1 != part2:
base_dirname = base_dirname[:i]
break
if ctx.toolchains["//jsonnet:toolchain_type"].jsonnetinfo.manifest_file_support:
output_manifest = ctx.actions.declare_file("_%s_outs.mf" % ctx.label.name)
outputs.append(output_manifest)
command += ["-o", output_manifest.path]
outputs += ctx.outputs.outs
command += ["-m", "/".join(base_dirname), ctx.file.src.path]
command += ctx.toolchains["//jsonnet:toolchain_type"].jsonnetinfo.create_directory_flags
elif len(ctx.attr.outs) > 1:
fail("Only one file can be specified in outs if multiple_outputs is " +
"not set.")
else:
compiled_json = ctx.outputs.outs[0]
outputs.append(compiled_json)
command += [ctx.file.src.path, "-o", compiled_json.path]
transitive_data = depset(
transitive =
[dep.data_runfiles.files for dep in ctx.attr.deps] +
[l.files for l in jsonnet_tla_code_files.keys()] +
[l.files for l in jsonnet_tla_str_files.keys()] +
[l[DefaultInfo].data_runfiles.files for l in jsonnet_tla_code_libraries.keys()] +
[l[DefaultInfo].data_runfiles.files for l in jsonnet_ext_code_libraries.keys()],
)
# NB(sparkprime): (1) transitive_data is never used, since runfiles is only
# used when .files is pulled from it. (2) This makes sense - jsonnet does
# not need transitive dependencies to be passed on the commandline. It
# needs the -J but that is handled separately.
files = jsonnet_ext_str_files + jsonnet_ext_code_files
runfiles = ctx.runfiles(
collect_data = True,
files = files,
transitive_files = transitive_data,
)
compile_inputs = (
[ctx.file.src] +
runfiles.files.to_list() +
depinfo.transitive_sources.to_list()
)
ctx.actions.run_shell(
inputs = compile_inputs + stamp_inputs,
toolchain = Label("//jsonnet:toolchain_type"),
tools = [ctx.toolchains["//jsonnet:toolchain_type"].jsonnetinfo.compiler],
outputs = outputs,
mnemonic = "Jsonnet",
command = " ".join(command),
use_default_shell_env = True,
progress_message = "Compiling Jsonnet to JSON for " + ctx.label.name,
)
if ctx.attr.out_dir:
return [DefaultInfo(
files = depset([out_dir]),
runfiles = ctx.runfiles(files = [out_dir]),
)]
return [DefaultInfo(
files = depset(outputs),
runfiles = ctx.runfiles(files = outputs),
)]
_EXIT_CODE_COMPARE_COMMAND = """
EXIT_CODE=$?
EXPECTED_EXIT_CODE=%d
if [ $EXIT_CODE -ne $EXPECTED_EXIT_CODE ] ; then
echo "FAIL (exit code): %s"
echo "Expected: $EXPECTED_EXIT_CODE"
echo "Actual: $EXIT_CODE"
if [ %s = true ]; then
echo "Output: $OUTPUT"
fi
exit 1
fi
"""
_DIFF_COMMAND = """
GOLDEN=$(%s %s)
if [ "$OUTPUT" != "$GOLDEN" ]; then
echo "FAIL (output mismatch): %s"
echo "Diff:"
diff -u <(echo "$GOLDEN") <(echo "$OUTPUT")
if [ %s = true ]; then
echo "Expected: $GOLDEN"
echo "Actual: $OUTPUT"
fi
exit 1
fi
"""
_REGEX_DIFF_COMMAND = """
# Needed due to rust-jsonnet, go-jsonnet and cpp-jsonnet producing different
# output (casing, text etc).
shopt -s nocasematch
GOLDEN_REGEX=$(%s %s)
if [[ ! "$OUTPUT" =~ $GOLDEN_REGEX ]]; then
echo "FAIL (regex mismatch): %s"
if [ %s = true ]; then
echo "Output: $OUTPUT"
fi
exit 1
fi
"""
def _jsonnet_to_json_test_impl(ctx):
"""Implementation of the jsonnet_to_json_test rule."""
depinfo = _setup_deps(ctx.attr.deps, ctx.attr.tla_code_libraries, ctx.attr.ext_code_libraries)
golden_files = []
diff_command = ""
if ctx.file.golden:
golden_files.append(ctx.file.golden)
# Note that we only run jsonnet to canonicalize the golden output if the
# expected return code is 0, and canonicalize_golden was not explicitly disabled.
# Otherwise, the golden file contains the
# expected error output.
# For legacy reasons, we also disable canonicalize_golden for yaml_streams.
canonicalize = ctx.attr.canonicalize_golden and not ctx.attr.yaml_stream
dump_golden_cmd = (ctx.toolchains["//jsonnet:toolchain_type"].jsonnetinfo.compiler.short_path if ctx.attr.error == 0 and canonicalize else "/bin/cat")
if ctx.attr.regex:
diff_command = _REGEX_DIFF_COMMAND % (
dump_golden_cmd,
ctx.file.golden.short_path,
ctx.label.name,
"true" if ctx.attr.output_file_contents else "false",
)
else:
diff_command = _DIFF_COMMAND % (
dump_golden_cmd,
ctx.file.golden.short_path,
ctx.label.name,
"true" if ctx.attr.output_file_contents else "false",
)
jsonnet_ext_str_envs = ctx.attr.ext_str_envs
jsonnet_ext_code_envs = ctx.attr.ext_code_envs
jsonnet_ext_str_files = ctx.files.ext_str_files
jsonnet_ext_str_file_vars = ctx.attr.ext_str_file_vars
jsonnet_ext_code_files = ctx.files.ext_code_files
jsonnet_ext_code_file_vars = ctx.attr.ext_code_file_vars
jsonnet_ext_code_libraries = ctx.attr.ext_code_libraries
jsonnet_tla_str_envs = ctx.attr.tla_str_envs
jsonnet_tla_code_envs = ctx.attr.tla_code_envs
jsonnet_tla_str_files = ctx.attr.tla_str_files
jsonnet_tla_code_files = ctx.attr.tla_code_files
jsonnet_tla_code_libraries = ctx.attr.tla_code_libraries
jsonnet_ext_strs, strs_stamp_inputs = _make_stamp_resolve(ctx.attr.ext_strs, ctx, True)
jsonnet_ext_code, code_stamp_inputs = _make_stamp_resolve(ctx.attr.ext_code, ctx, True)
jsonnet_tla_strs, tla_strs_stamp_inputs = _make_stamp_resolve(ctx.attr.tla_strs, ctx, True)
jsonnet_tla_code, tla_code_stamp_inputs = _make_stamp_resolve(ctx.attr.tla_code, ctx, True)
stamp_inputs = strs_stamp_inputs + code_stamp_inputs + tla_strs_stamp_inputs + tla_code_stamp_inputs
if len(jsonnet_ext_str_file_vars) != len(jsonnet_ext_str_files):
fail("Mismatch of ext_str_file_vars ({}) to ext_str_files ({})".format(jsonnet_ext_str_file_vars, jsonnet_ext_str_files))
if len(jsonnet_ext_code_file_vars) != len(jsonnet_ext_code_files):
fail("Mismatch of ext_code_file_vars ({}) to ext_code_files ({})".format(jsonnet_ext_code_file_vars, jsonnet_ext_code_files))
other_args = ctx.attr.extra_args + (["-y"] if ctx.attr.yaml_stream else [])
jsonnet_command = " ".join(
["OUTPUT=$(%s" % ctx.toolchains["//jsonnet:toolchain_type"].jsonnetinfo.compiler.short_path] +
["-J " + shell.quote(im) for im in _get_import_paths(ctx.label, [ctx.file.src], ctx.attr.imports, True)] +
["-J " + shell.quote(im) for im in depinfo.short_imports.to_list()] +
other_args +
["--ext-str %s=%s" %
(_quote(key), _quote(val)) for key, val in jsonnet_ext_strs.items()] +
["--ext-str %s" %
ext_str_env for ext_str_env in jsonnet_ext_str_envs] +
["--ext-code %s=%s" %
(_quote(key), _quote(val)) for key, val in jsonnet_ext_code.items()] +
["--ext-code %s" %
ext_code_env for ext_code_env in jsonnet_ext_code_envs] +
["--ext-str-file %s=%s" %
(var, jfile.short_path) for var, jfile in zip(jsonnet_ext_str_file_vars, jsonnet_ext_str_files)] +
["--ext-code-file %s=%s" %
(var, jfile.short_path) for var, jfile in zip(jsonnet_ext_code_file_vars, jsonnet_ext_code_files)] +
["--ext-code-file %s=%s" %
(_quote(val), _quote(key[DefaultInfo].files.to_list()[0].short_path)) for key, val in jsonnet_ext_code_libraries.items()] +
["--tla-str %s=%s" %
(_quote(key), _quote(val)) for key, val in jsonnet_tla_strs.items()] +
["--tla-str '%s'" %
tla_str_env for tla_str_env in jsonnet_tla_str_envs] +
["--tla-code %s=%s" %
(_quote(key), _quote(val)) for key, val in jsonnet_tla_code.items()] +
["--tla-code %s" %
tla_code_env for tla_code_env in jsonnet_tla_code_envs] +
["--tla-str-file %s=%s" %
(var, jfile.files.to_list()[0].short_path) for jfile, var in jsonnet_tla_str_files.items()] +
["--tla-code-file %s=%s" %
(var, jfile.files.to_list()[0].short_path) for jfile, var in jsonnet_tla_code_files.items()] +
["--tla-code-file %s=%s" %
(_quote(val), _quote(key[DefaultInfo].files.to_list()[0].short_path)) for key, val in jsonnet_tla_code_libraries.items()] +
[
ctx.file.src.short_path,
"2>&1)",
],
)
command = [
"#!/bin/bash",
jsonnet_command,
_EXIT_CODE_COMPARE_COMMAND % (
ctx.attr.error,
ctx.label.name,
"true" if ctx.attr.output_file_contents else "false",
),
]
if diff_command:
command.append(diff_command)
ctx.actions.write(
output = ctx.outputs.executable,
content = "\n".join(command),
is_executable = True,
)
transitive_data = depset(
transitive = [dep.data_runfiles.files for dep in ctx.attr.deps] +
[l.files for l in jsonnet_tla_code_files.keys()] +
[l.files for l in jsonnet_tla_str_files.keys()] +
[l[DefaultInfo].data_runfiles.files for l in jsonnet_tla_code_libraries.keys()] +
[l[DefaultInfo].data_runfiles.files for l in jsonnet_ext_code_libraries.keys()],
)
test_inputs = (
[
ctx.file.src,
ctx.toolchains["//jsonnet:toolchain_type"].jsonnetinfo.compiler,
] +
golden_files +
transitive_data.to_list() +
depinfo.transitive_sources.to_list() +
jsonnet_ext_str_files +
jsonnet_ext_code_files +
stamp_inputs
)
return [DefaultInfo(
runfiles = ctx.runfiles(
files = test_inputs,
transitive_files = transitive_data,
collect_data = True,
),
)]
_jsonnet_common_attrs = {
"data": attr.label_list(
allow_files = True,
),
"imports": attr.string_list(
doc = "List of import `-J` flags to be passed to the `jsonnet` compiler.",
),
"deps": attr.label_list(
doc = "List of targets that are required by the `srcs` Jsonnet files.",
providers = [JsonnetLibraryInfo],
allow_files = False,
),
}
_jsonnet_library_attrs = {
"srcs": attr.label_list(
doc = "List of `.jsonnet` files that comprises this Jsonnet library",
allow_files = _JSONNET_FILETYPE,
),
}
jsonnet_library = rule(
implementation = _jsonnet_library_impl,
attrs = dict(_jsonnet_library_attrs.items() +
_jsonnet_common_attrs.items()),
doc = """Creates a logical set of Jsonnet files.
Example:
Suppose you have the following directory structure:
```
[workspace]/
MODULE.bazel
configs/
BUILD
backend.jsonnet
frontend.jsonnet
```
You can use the `jsonnet_library` rule to build a collection of `.jsonnet`
files that can be imported by other `.jsonnet` files as dependencies:
`configs/BUILD`:
```python
load("@rules_jsonnet//jsonnet:jsonnet.bzl", "jsonnet_library")
jsonnet_library(
name = "configs",
srcs = [
"backend.jsonnet",
"frontend.jsonnet",
],
)
```
""",
)
_jsonnet_compile_attrs = {
"src": attr.label(
doc = "The `.jsonnet` file to convert to JSON.",
allow_single_file = _JSONNET_FILETYPE,
),
"code_vars": attr.string_dict(
doc = "Deprecated (use 'ext_code').",
),
"ext_code": attr.string_dict(),
"ext_code_envs": attr.string_list(),
"ext_code_file_vars": attr.string_list(),
"ext_code_files": attr.label_list(
allow_files = True,
),
"ext_code_libraries": attr.label_keyed_string_dict(
doc = "Include jsonnet_library as an extvar with the key value",
providers = [JsonnetLibraryInfo],
),
"ext_str_envs": attr.string_list(),
"ext_str_file_vars": attr.string_list(),
"ext_str_files": attr.label_list(
allow_files = True,
),
"ext_strs": attr.string_dict(),
"tla_code": attr.string_dict(),
"tla_code_envs": attr.string_list(),
"tla_strs": attr.string_dict(),
"tla_str_envs": attr.string_list(),
"tla_str_files": attr.label_keyed_string_dict(allow_files = True),
"tla_code_files": attr.label_keyed_string_dict(allow_files = True),
"tla_code_libraries": attr.label_keyed_string_dict(
doc = "Include jsonnet_library as a top-level argument as the given value",
providers = [JsonnetLibraryInfo],
),
"stamp_keys": attr.string_list(
default = [],
mandatory = False,
),
"yaml_stream": attr.bool(
default = False,
mandatory = False,
),
"extra_args": attr.string_list(
doc = """Additional command line arguments for the Jsonnet interpreter.
For example, setting this argument to `["--string"]` causes the interpreter to
manifest the output file(s) as plain text instead of JSON.
""",
),
"vars": attr.string_dict(
doc = "Deprecated (use 'ext_strs').",
),
"_stamper": attr.label(
default = Label("//jsonnet:stamper"),
cfg = "exec",
executable = True,
allow_files = True,
),
}
_jsonnet_to_json_attrs = {
"outs": attr.output_list(
doc = """\
Names of the output `.json` files to be generated by this rule.
If you are generating only a single JSON file and are not using jsonnet
multiple output files, then this attribute should only contain the file
name of the JSON file you are generating.
If you are generating multiple JSON files using jsonnet multiple file output
(`jsonnet -m`), then list the file names of all the JSON files to be
generated. The file names specified here must match the file names
specified in your `src` Jsonnet file.
For the case where multiple file output is used but only for generating one
output file, set the `multiple_outputs` attribute to 1 to explicitly enable
the `-m` flag for multiple file output.
This attribute is incompatible with `out_dir`.
""",
),
"multiple_outputs": attr.bool(
doc = """\
Set to `True` to explicitly enable multiple file output via the `jsonnet -m` flag.
This is used for the case where multiple file output is used but only for
generating a single output file. For example:
```
local foo = import "foo.jsonnet";
{
"foo.json": foo,
}
```
This attribute is incompatible with `out_dir`.
""",
),
"out_dir": attr.string(
doc = """\
Name of the directory where output files are stored.
If the names of output files are not known up front, this option can be
used to write all output files to a single directory artifact. Files in
this directory cannot be referenced individually.
This attribute is incompatible with `outs` and `multiple_outputs`.
""",
),
}
jsonnet_to_json = rule(
_jsonnet_to_json_impl,
attrs = dict(_jsonnet_compile_attrs.items() +
_jsonnet_to_json_attrs.items() +
_jsonnet_common_attrs.items()),
toolchains = ["//jsonnet:toolchain_type"],
doc = """\
Compiles Jsonnet code to JSON.
Example:
### Example
Suppose you have the following directory structure:
```
[workspace]/
MODULE.bazel
workflows/
BUILD
workflow.libsonnet
wordcount.jsonnet
intersection.jsonnet
```
Say that `workflow.libsonnet` is a base configuration library for a workflow
scheduling system and `wordcount.jsonnet` and `intersection.jsonnet` both
import `workflow.libsonnet` to define workflows for performing a wordcount and
intersection of two files, respectively.
First, create a `jsonnet_library` target with `workflow.libsonnet`:
`workflows/BUILD`:
```python
load("@rules_jsonnet//jsonnet:jsonnet.bzl", "jsonnet_library")
jsonnet_library(
name = "workflow",
srcs = ["workflow.libsonnet"],
)
```
To compile `wordcount.jsonnet` and `intersection.jsonnet` to JSON, define two
`jsonnet_to_json` targets:
```python
jsonnet_to_json(
name = "wordcount",
src = "wordcount.jsonnet",
outs = ["wordcount.json"],
deps = [":workflow"],
)
jsonnet_to_json(
name = "intersection",
src = "intersection.jsonnet",
outs = ["intersection.json"],
deps = [":workflow"],
)
```
### Example: Multiple output files
To use Jsonnet's [multiple output files][multiple-output-files], suppose you
add a file `shell-workflows.jsonnet` that imports `wordcount.jsonnet` and
`intersection.jsonnet`:
`workflows/shell-workflows.jsonnet`:
```
local wordcount = import "workflows/wordcount.jsonnet";
local intersection = import "workflows/intersection.jsonnet";
{
"wordcount-workflow.json": wordcount,
"intersection-workflow.json": intersection,
}
```
To compile `shell-workflows.jsonnet` into the two JSON files,
`wordcount-workflow.json` and `intersection-workflow.json`, first create a
`jsonnet_library` target containing the two files that
`shell-workflows.jsonnet` depends on:
```python
jsonnet_library(
name = "shell-workflows-lib",
srcs = [
"wordcount.jsonnet",
"intersection.jsonnet",
],
deps = [":workflow"],
)
```
Then, create a `jsonnet_to_json` target and set `outs` to the list of output
files to indicate that multiple output JSON files are generated:
```python
jsonnet_to_json(
name = "shell-workflows",
src = "shell-workflows.jsonnet",
deps = [":shell-workflows-lib"],
outs = [
"wordcount-workflow.json",
"intersection-workflow.json",
],
)
```
[multiple-output-files]: https://jsonnet.org/learning/getting_started.html#multi
""",
)
_jsonnet_to_json_test_attrs = {
"error": attr.int(
doc = "The expected error code from running `jsonnet` on `src`.",
),
"golden": attr.label(
doc = (
"The expected (combined stdout and stderr) output to compare to the " +
"output of running `jsonnet` on `src`."
),
allow_single_file = True,
),
"regex": attr.bool(
doc = (
"Set to 1 if `golden` contains a regex used to match the output of " +
"running `jsonnet` on `src`."
),
),
"canonicalize_golden": attr.bool(default = True),
"output_file_contents": attr.bool(default = True),
}
jsonnet_to_json_test = rule(
_jsonnet_to_json_test_impl,
attrs = dict(_jsonnet_compile_attrs.items() +
_jsonnet_to_json_test_attrs.items() +
_jsonnet_common_attrs.items()),
toolchains = ["//jsonnet:toolchain_type"],
executable = True,
test = True,
doc = """\
Compiles Jsonnet code to JSON and checks the output.
Example:
Suppose you have the following directory structure:
```
[workspace]/
MODULE.bazel
config/
BUILD
base_config.libsonnet
test_config.jsonnet
test_config.json
```
Suppose that `base_config.libsonnet` is a library Jsonnet file, containing the
base configuration for a service. Suppose that `test_config.jsonnet` is a test
configuration file that is used to test `base_config.jsonnet`, and
`test_config.json` is the expected JSON output from compiling
`test_config.jsonnet`.
The `jsonnet_to_json_test` rule can be used to verify that compiling a Jsonnet
file produces the expected JSON output. Simply define a `jsonnet_to_json_test`
target and provide the input test Jsonnet file and the `golden` file containing
the expected JSON output:
`config/BUILD`:
```python
load(
"@rules_jsonnet//jsonnet:jsonnet.bzl",
"jsonnet_library",
"jsonnet_to_json_test",
)
jsonnet_library(
name = "base_config",
srcs = ["base_config.libsonnet"],
)
jsonnet_to_json_test(
name = "test_config_test",
src = "test_config",
deps = [":base_config"],
golden = "test_config.json",
)
```
To run the test: `bazel test //config:test_config_test`
### Example: Negative tests
Suppose you have the following directory structure:
```
[workspace]/
MODULE.bazel
config/
BUILD
base_config.libsonnet
invalid_config.jsonnet
invalid_config.output
```
Suppose that `invalid_config.jsonnet` is a Jsonnet file used to verify that
an invalid config triggers an assertion in `base_config.jsonnet`, and
`invalid_config.output` is the expected error output.
The `jsonnet_to_json_test` rule can be used to verify that compiling a Jsonnet
file results in an expected error code and error output. Simply define a
`jsonnet_to_json_test` target and provide the input test Jsonnet file, the
expected error code in the `error` attribute, and the `golden` file containing
the expected error output:
`config/BUILD`:
```python
load(
"@rules_jsonnet//jsonnet:jsonnet.bzl",
"jsonnet_library",
"jsonnet_to_json_test",
)
jsonnet_library(
name = "base_config",
srcs = ["base_config.libsonnet"],
)
jsonnet_to_json_test(
name = "invalid_config_test",
src = "invalid_config",
deps = [":base_config"],
golden = "invalid_config.output",
error = 1,
)
```
To run the test: `bazel test //config:invalid_config_test`
""",
)