TextForge is a Bazel module designed to generate files from a list of templates and a set of data in JSON format, which can be provided as a string or a file. TextForge uses Jinja2 to render the data into the templates, producing an output with the applied changes.
To define the dependency in MODULE.bazel, follow these steps:
Locate the MODULE.bazel file: This file is typically found at the root of your Bazel workspace.
Specify the dependency: Use the bazel_dep function to add the dependency. The syntax is as follows:
bazel_dep(name = "textforge", version = "1.0.0")
After adding the dependency to your MODULE.bazel file, you can load the rule into your BUILD.bazel file. Example:
load("@textforge//:defs.bzl", "generate_files")
generate_files(
...
)
Name | Description |
---|---|
srcs | The templates files |
data | The data on a string with JSON format |
datafile | The path of the JSON file with the data |
prefix | Prefix to add to the output files |
suffix | Suffix to add to the output files |
extension | Extension for the output files |
templates/templateA.txt.jinja
Template A value: {{ a }}
templates/templateB.txt.jinja
Template B value: {{ b }}
templates/templateC.txt.jinja
Template C value: {{ c }}
data/data.json
{
"a": 123,
"b": 456,
"c": 789
}
BUILD.bazel
load("@textforge//:defs.bzl", "generate_files")
filegroup(
name = "templates",
srcs = glob([
"templates/*.jinja",
]),
visibility = ["//visibility:private"],
)
generate_files(
name = "files",
srcs = [
":templates"
],
datafile = "data/data.json",
visibility = ["//visibility:public"],
)
bazel-bin/templateA.txt
Template A value: 123
bazel-bin/templateB.txt
Template B value: 456
bazel-bin/templateC.txt
Template C value: 789
This project is licensed under the Apache 2.0 License - see the LICENSE file for details.