tree: e3586f9d78ad2fd2bb23c674e69a4be3b06b5eb5 [path history] [tgz]
  1. BUILD
  2. jsonnet.BUILD
  3. jsonnet.bzl
  4. jsonnet.WORKSPACE
  5. README.md
tools/build_defs/jsonnet/README.md

Jsonnet Rules

Rules

Overview

These are build rules for working with Jsonnet files with Bazel.

Setup

To use the Jsonnet rules, simply copy the contents of jsonnet.WORKSPACE into your WORKSPACE file.

jsonnet_library

jsonnet_library(name, srcs, deps, imports)

Example

Suppose you have the following directory structure:

[workspace]/
    WORKSPACE
    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:

load("/tools/build_defs/jsonnet/jsonnet", "jsonnet_library")

jsonnet_library(
    name = "configs",
    srcs = [
        "backend.jsonnet",
        "frontend.jsonnet",
    ],
)

jsonnet_to_json

jsonnet_to_json(name, src, deps, outs, multiple_outputs, imports)

{ “foo.json”: foo, } imports List of strings, optional List of import -J flags to be passed to the jsonnet compiler.

Example

Suppose you have the following directory structure:

[workspace]/
    WORKSPACE
    workflows/
        BUILD
        workflow.jsonnet
        wordcount.jsonnet
        intersection.jsonnet

Say that workflow.jsonnet is a base configuration library for a workflow scheduling system and wordcount.jsonnet and intersection.jsonnet both import workflow.jsonnet to define workflows for performing a wordcount and intersection of two files, respectively.

First, create a jsonnet_library target with workflow.jsonnet:

workflows/BUILD:

load("/tools/build_defs/jsonnet/jsonnet", "jsonnet_library")

jsonnet_library(
    name = "workflow",
    srcs = ["workflow.jsonnet"],
)

To compile wordcount.jsonnet and intersection.jsonnet to JSON, define two jsonnet_to_json targets:

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, 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:

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:

jsonnet_to_json(
    name = "shell-workflows",
    src = "shell-workflows.jsonnet",
    deps = [":shell-workflows-lib"],
    outs = [
        "wordcount-workflow.jsonnet",
        "intersection-workflow.jsonnet",
    ],
)