angular/adev/shared-docs/pipeline/_guides.bzl
Joey Perrott 2d8635d29d refactor(docs-infra): migrate @angular/docs from dev-infra into adev directory (#57132)
To increase the ease of development we are moving @angular/docs into the adev directory within this repo. While
we are doing this to improve our development experience in the short term, efforts are also in place
to maintain a division between this @angular/docs (shared) code and adev itself, so that it can be extracted
back out in the future when components is ready to leverage it as well.

PR Close #57132
2024-07-30 15:51:26 +00:00

90 lines
3.6 KiB
Python

load("@build_bazel_rules_nodejs//:providers.bzl", "run_node")
load("@npm//@angular/build-tooling/bazel/private:path_relative_to_label.bzl", "path_relative_to_label")
def _generate_guides(ctx):
"""Implementation of the markdown rule"""
# Define arguments that will be passed to the underlying nodejs program.
args = ctx.actions.args()
# Use a param file because we may have a large number of inputs.
args.set_param_file_format("multiline")
args.use_param_file("%s", use_always = True)
# Pass the set of source files.
args.add_joined(ctx.files.srcs, join_with = ",")
# Pass the the output directory path (which is the bazel-bin directory).
args.add(ctx.bin_dir.path)
# Determine the set of html output files. For each input markdown file, produce an html
# file with the same name (replacing the markdown extension with `.html`).
html_outputs = []
for input_file in ctx.files.srcs:
# Determine the input file path relatively to the current package path. We do this
# because we want to preserve directories for the input files and `declare_file` expects a
# path that is relative to the current package. We don't use `.replace`
# here because the extension can be also in upper case.
relative_basepath = path_relative_to_label(ctx.label, input_file.short_path)
# For each input file "xxx.md", we want to write an output file "xxx.html"
html_outputs += [ctx.actions.declare_file("%s.html" % relative_basepath)]
# Define an action that runs the nodejs_binary executable. This is the main thing that this
# rule does. If mermaid blocks are enabled then a different executable is used.
if (ctx.attr.mermaid_blocks):
run_node(
ctx = ctx,
inputs = depset(ctx.files.srcs + ctx.files.data),
executable = "_generate_guides",
outputs = html_outputs,
arguments = [args],
)
else:
run_node(
ctx = ctx,
inputs = depset(ctx.files.srcs + ctx.files.data),
executable = "_generate_guides_no_mermaid",
outputs = html_outputs,
arguments = [args],
)
# The return value describes what the rule is producing. In this case we need to specify
# the "DefaultInfo" with the output html files.
return [DefaultInfo(files = depset(html_outputs))]
generate_guides = rule(
# Point to the starlark function that will execute for this rule.
implementation = _generate_guides,
doc = """Rule that renders markdown sources to html""",
# The attributes that can be set to this rule.
attrs = {
"srcs": attr.label_list(
doc = """Markdown sources to render to html.""",
allow_empty = False,
allow_files = True,
),
"data": attr.label_list(
doc = """Source referenced from within the markdown.""",
allow_files = True,
),
"mermaid_blocks": attr.bool(
doc = """Whether to transform mermaid blocks.""",
default = False,
),
# The executable for this rule (private).
"_generate_guides": attr.label(
default = Label("//adev/shared-docs/pipeline:markdown"),
executable = True,
cfg = "exec",
),
# The executable for this rule when no mermaid process is required (private).
"_generate_guides_no_mermaid": attr.label(
default = Label("//adev/shared-docs/pipeline:markdown_no_mermaid"),
executable = True,
cfg = "exec",
),
},
)