blob: 5c399f8462bf2b18d6f5c2085780503b290a38cd [file] [log] [blame] [view]
c-parsonsc7d5b702019-09-23 13:27:13 -04001<nav class="toc">
2 <h2>Contents</h2>
3 <ul>
4 <li><a href="#single-file">Single File</a></li>
5 <li><a href="#files-with-deps">Files with Dependencies</a></li>
6 <li><a href="#multiple-files">Multiple Files</a></li>
7 </ul>
8</nav>
9
10The following are some examples of how to use Stardoc.
11
Alexandre Rostovtsevec438022024-04-23 16:05:45 -040012**Note**: By default - in other words, when using Bzlmod for dependency
13management - Stardoc uses `@stardoc` as its repo name. However, if you are
14using the legacy `WORSKPACE`-based setup for dependency management, replace
15`@stardoc` with `@io_bazel_stardoc` in the examples below.
16
c-parsonsc7d5b702019-09-23 13:27:13 -040017<a name="single-file"></a>
18## Single File
19
20Suppose you have a project containing Stardoc rules you want to document:
21
22```
23[workspace]/
24 WORKSPACE
25 checkstyle/
26 BUILD
27 checkstyle.bzl
28```
29
30To generate documentation for the rules in `checkstyle.bzl`, add the
Grzegorz Lukasika5e1b792024-01-19 20:59:54 +010031following target to `checkstyle/BUILD`:
c-parsonsc7d5b702019-09-23 13:27:13 -040032
Alexandre Rostovtsevec438022024-04-23 16:05:45 -040033```starlark
34load("@stardoc//stardoc:stardoc.bzl", "stardoc")
c-parsonsc7d5b702019-09-23 13:27:13 -040035
36stardoc(
37 name = "checkstyle-docs",
38 input = "checkstyle.bzl",
39 out = "checkstyle_doc.md",
40)
41```
42
43Running `bazel build //checkstyle:checkstyle-docs` will generate a markdown file
44containing documentation for all Starlark rules defined in `checkstyle.bzl`.
45
46To generate a subset of rules defined in `checkstyle.bzl`, you may specify which
47rule names you specifically want documentation for using the `symbol_names` attribute
48of the `stardoc` rule. If `symbol_names` is specified, only rules matching a name
49in `symbol_names` will be documented:
50
Alexandre Rostovtsevec438022024-04-23 16:05:45 -040051```starlark
52load("@stardoc//stardoc:stardoc.bzl", "stardoc")
c-parsonsc7d5b702019-09-23 13:27:13 -040053
54stardoc(
55 name = "checkstyle-docs",
56 input = "checkstyle.bzl",
57 out = "checkstyle_doc.md",
58 symbol_names = ["checkstyle_rule", "other_rule"],
59)
60```
61
62<a name="files-with-deps"></a>
63## Files with Dependencies
64
65If you would like to generate documentation for a `.bzl` with dependencies on
66other `.bzl` files, use the `bzl_library` rule to create logical collections of
67Starlark sources and depend on these libraries via the `deps` attribute of your
68`stardoc` target.
69
70Suppose your project has the following structure:
71
72```
73[workspace]/
74 WORKSPACE
75 BUILD
76 checkstyle/
77 BUILD
78 checkstyle.bzl
79 lua/
80 BUILD
81 lua.bzl
82 luarocks.bzl
83```
84
85...and suppose your target `.bzl` file depends on other `.bzl` files in your workspace:
86
87`checkstyle/checkstyle.bzl`:
88
Alexandre Rostovtsevec438022024-04-23 16:05:45 -040089```starlark
c-parsonsc7d5b702019-09-23 13:27:13 -040090load("//lua:lua.bzl", "lua_utility")
91
92lua_utility()
93
94checkstyle_rule = rule(
95 ...
96)
97```
98
99In this case, you can have a `bzl_library` target in `lua/BUILD`:
100
101`lua/BUILD`:
102
103```python
104load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
105
106bzl_library(
107 name = "lua-rules",
108 srcs = [
109 "lua.bzl",
110 "luarocks.bzl",
111 ],
112)
113```
114
115To build documentation for `checkstyle.bzl`, specify the `bzl_library` target
116as a dependency of the `stardoc` target:
117
118`checkstyle/BUILD`:
119
Alexandre Rostovtsevec438022024-04-23 16:05:45 -0400120```starlark
121load("@stardoc//stardoc:stardoc.bzl", "stardoc")
c-parsonsc7d5b702019-09-23 13:27:13 -0400122
123stardoc(
124 name = "checkstyle-docs",
125 input = "checkstyle.bzl",
126 out = "checkstyle_doc.md",
127 deps = ["//lua:lua-rules"],
128)
129```
130
131<a name="multiple-files"></a>
132## Multiple Files
133
134If you would like to generate documentation for multiple .bzl files in various
135packages in your workspace, you will need to create a single `.bzl` file that depends
136on all those `.bzl` files. You can then explicitly whitelist rules for which you would
137like documentation to be generated.
138
139For example, you may want to generate documentation for `foo_rule`, `bar_rule`, and
140`baz_rule`, all in different `.bzl` files. First, you would create a single `.bzl` file
Casey383aa762022-03-23 13:39:24 -0700141which loads these files and binds the rules to be documented as globals:
c-parsonsc7d5b702019-09-23 13:27:13 -0400142
143`doc_hub.bzl`:
144
Alexandre Rostovtsevec438022024-04-23 16:05:45 -0400145```starlark
Casey383aa762022-03-23 13:39:24 -0700146load("//foo:foo.bzl", _foo_rule = "foo_rule")
147load("//bar:bar.bzl", _bar_rule = "bar_rule")
148load("//baz:baz.bzl", _baz_rule = "baz_rule")
149
150foo_rule = _foo_rule
151bar_rule = _bar_rule
152baz_rule = _baz_rule
c-parsonsc7d5b702019-09-23 13:27:13 -0400153
154# No need for any implementation here. The rules need only be loaded.
155```
156
157A single `stardoc` target can then be used to generate their documentation:
158
159`BUILD`:
160
161```python
Alexandre Rostovtsevec438022024-04-23 16:05:45 -0400162load("@stardoc//stardoc:stardoc.bzl", "stardoc")
c-parsonsc7d5b702019-09-23 13:27:13 -0400163
164stardoc(
165 name = "my-docs",
166 input = "doc_hub.bzl",
167 out = "docs.md",
168 symbol_names = ["foo_rule", "bar_rule", "baz_rule"],
169)
170```
171
172