TextForge
v1.0.0
published 1 year ago
1 stars
0 forks
1 watchers
Apache License 2.0
public
1 assets
751 downloads
237 KB
Compatibility level 0
T1PaE0FkD709cGGlZWUhv/Dhr38RODCuJvG29sBAoPk=
Maintained byRoberto Rojas
v1.0.0
June 7, 2024
[expand for release notes]

TextForge

TextForge

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.

How to use it

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(
...
)

Rule attributes

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

Example

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"],
)

Generated content

bazel-bin/templateA.txt

Template A value: 123

bazel-bin/templateB.txt

Template B value: 456

bazel-bin/templateC.txt

Template C value: 789

License

This project is licensed under the Apache 2.0 License - see the LICENSE file for details.

Authors