2021-11-10 14:34:00 +00:00
" Helpers for generating stardoc documentation "
2022-05-26 01:47:32 +00:00
load ( " @io_bazel_stardoc//stardoc:stardoc.bzl " , _stardoc = " stardoc " )
2022-03-03 00:00:53 +00:00
load ( " //lib:write_source_files.bzl " , " write_source_files " )
2021-11-10 14:34:00 +00:00
def stardoc_with_diff_test (
2022-03-03 00:00:53 +00:00
name ,
2021-11-10 14:34:00 +00:00
bzl_library_target ,
2022-04-28 00:22:30 +00:00
* * kwargs ) :
2022-05-26 01:47:32 +00:00
""" Creates a stardoc target that can be auto-detected by update_docs to write the generated doc to the source tree and test that it ' s up to date.
2021-11-10 14:34:00 +00:00
This is helpful for minimizing boilerplate in repos wih lots of stardoc targets .
Args :
2022-03-03 00:00:53 +00:00
name : the name of the stardoc file to be written to the current source directory ( . md will be appended to the name ) . Call bazel run on this target to update the file .
2021-11-10 14:34:00 +00:00
bzl_library_target : the label of the ` bzl_library ` target to generate documentation for
2022-04-28 00:22:30 +00:00
* * kwargs : additional attributes passed to the stardoc ( ) rule , such as for overriding the templates
2021-11-10 14:34:00 +00:00
"""
2023-03-11 19:35:11 +00:00
target_compatible_with = kwargs . pop ( " target_compatible_with " , select ( {
# stardoc produces different line endings on Windows
# which makes the diff_test fail
" @platforms//os:windows " : [ " @platforms//:incompatible " ] ,
" //conditions:default " : [ ] ,
} ) )
2021-11-10 14:34:00 +00:00
# Generate MD from .bzl
2022-05-26 01:47:32 +00:00
_stardoc (
name = name ,
2022-03-03 00:00:53 +00:00
out = name + " -docgen.md " ,
2021-11-10 14:34:00 +00:00
input = bzl_library_target + " .bzl " ,
deps = [ bzl_library_target ] ,
2024-02-06 01:43:58 +00:00
tags = kwargs . pop ( " tags " , [ ] ) + [ " package: " + native . package_name ( ) ] , # Tag the package name which will help us reconstruct the write_source_files label in update_docs
2023-03-11 19:35:11 +00:00
target_compatible_with = target_compatible_with ,
2022-04-28 00:22:30 +00:00
* * kwargs
2021-11-10 14:34:00 +00:00
)
2023-02-19 04:09:27 +00:00
def update_docs ( name = " update " , * * kwargs ) :
2022-03-03 00:00:53 +00:00
""" Stamps an executable run for writing all stardocs declared with stardoc_with_diff_test to the source tree.
2021-11-10 14:34:00 +00:00
This is to be used in tandem with ` stardoc_with_diff_test ( ) ` to produce a convenient workflow
for generating , testing , and updating all doc files as follows :
` ` ` bash
bazel build / / { docs_folder } / . . . & & bazel test / / { docs_folder } / . . . & & bazel run / / { docs_folder } : update
` ` `
eg .
` ` ` bash
bazel build / / docs / . . . & & bazel test / / docs / . . . & & bazel run / / docs : update
` ` `
Args :
2022-03-03 00:00:53 +00:00
name : the name of executable target
2023-02-19 04:09:27 +00:00
* * kwargs : Other common named parameters such as ` tags ` or ` visibility `
2021-11-10 14:34:00 +00:00
"""
2022-03-03 00:00:53 +00:00
2022-05-26 01:47:32 +00:00
update_files = { }
2021-11-10 14:34:00 +00:00
for r in native . existing_rules ( ) . values ( ) :
2023-08-09 19:29:48 +00:00
if r [ " generator_function " ] == " stardoc_with_diff_test " and r [ " generator_name " ] == r [ " name " ] :
2022-03-03 00:00:53 +00:00
for tag in r [ " tags " ] :
if tag . startswith ( " package: " ) :
stardoc_name = r [ " name " ]
2022-05-26 01:47:32 +00:00
source_file_name = stardoc_name + " .md "
generated_file_name = stardoc_name + " -docgen.md "
update_files [ source_file_name ] = generated_file_name
2021-11-10 14:34:00 +00:00
2022-03-03 00:00:53 +00:00
write_source_files (
2021-11-10 14:34:00 +00:00
name = name ,
2022-05-26 01:47:32 +00:00
files = update_files ,
2023-02-19 04:09:27 +00:00
* * kwargs
2021-11-10 14:34:00 +00:00
)