c-parsons | c7d5b70 | 2019-09-23 13:27:13 -0400 | [diff] [blame] | 1 | <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 | |
| 10 | The following are some examples of how to use Stardoc. |
| 11 | |
Alexandre Rostovtsev | ec43802 | 2024-04-23 16:05:45 -0400 | [diff] [blame] | 12 | **Note**: By default - in other words, when using Bzlmod for dependency |
| 13 | management - Stardoc uses `@stardoc` as its repo name. However, if you are |
| 14 | using the legacy `WORSKPACE`-based setup for dependency management, replace |
| 15 | `@stardoc` with `@io_bazel_stardoc` in the examples below. |
| 16 | |
c-parsons | c7d5b70 | 2019-09-23 13:27:13 -0400 | [diff] [blame] | 17 | <a name="single-file"></a> |
| 18 | ## Single File |
| 19 | |
| 20 | Suppose 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 | |
| 30 | To generate documentation for the rules in `checkstyle.bzl`, add the |
Grzegorz Lukasik | a5e1b79 | 2024-01-19 20:59:54 +0100 | [diff] [blame] | 31 | following target to `checkstyle/BUILD`: |
c-parsons | c7d5b70 | 2019-09-23 13:27:13 -0400 | [diff] [blame] | 32 | |
Alexandre Rostovtsev | ec43802 | 2024-04-23 16:05:45 -0400 | [diff] [blame] | 33 | ```starlark |
| 34 | load("@stardoc//stardoc:stardoc.bzl", "stardoc") |
c-parsons | c7d5b70 | 2019-09-23 13:27:13 -0400 | [diff] [blame] | 35 | |
| 36 | stardoc( |
| 37 | name = "checkstyle-docs", |
| 38 | input = "checkstyle.bzl", |
| 39 | out = "checkstyle_doc.md", |
| 40 | ) |
| 41 | ``` |
| 42 | |
| 43 | Running `bazel build //checkstyle:checkstyle-docs` will generate a markdown file |
| 44 | containing documentation for all Starlark rules defined in `checkstyle.bzl`. |
| 45 | |
| 46 | To generate a subset of rules defined in `checkstyle.bzl`, you may specify which |
| 47 | rule names you specifically want documentation for using the `symbol_names` attribute |
| 48 | of the `stardoc` rule. If `symbol_names` is specified, only rules matching a name |
| 49 | in `symbol_names` will be documented: |
| 50 | |
Alexandre Rostovtsev | ec43802 | 2024-04-23 16:05:45 -0400 | [diff] [blame] | 51 | ```starlark |
| 52 | load("@stardoc//stardoc:stardoc.bzl", "stardoc") |
c-parsons | c7d5b70 | 2019-09-23 13:27:13 -0400 | [diff] [blame] | 53 | |
| 54 | stardoc( |
| 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 | |
| 65 | If you would like to generate documentation for a `.bzl` with dependencies on |
| 66 | other `.bzl` files, use the `bzl_library` rule to create logical collections of |
| 67 | Starlark sources and depend on these libraries via the `deps` attribute of your |
| 68 | `stardoc` target. |
| 69 | |
| 70 | Suppose 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 Rostovtsev | ec43802 | 2024-04-23 16:05:45 -0400 | [diff] [blame] | 89 | ```starlark |
c-parsons | c7d5b70 | 2019-09-23 13:27:13 -0400 | [diff] [blame] | 90 | load("//lua:lua.bzl", "lua_utility") |
| 91 | |
| 92 | lua_utility() |
| 93 | |
| 94 | checkstyle_rule = rule( |
| 95 | ... |
| 96 | ) |
| 97 | ``` |
| 98 | |
| 99 | In this case, you can have a `bzl_library` target in `lua/BUILD`: |
| 100 | |
| 101 | `lua/BUILD`: |
| 102 | |
| 103 | ```python |
| 104 | load("@bazel_skylib//:bzl_library.bzl", "bzl_library") |
| 105 | |
| 106 | bzl_library( |
| 107 | name = "lua-rules", |
| 108 | srcs = [ |
| 109 | "lua.bzl", |
| 110 | "luarocks.bzl", |
| 111 | ], |
| 112 | ) |
| 113 | ``` |
| 114 | |
| 115 | To build documentation for `checkstyle.bzl`, specify the `bzl_library` target |
| 116 | as a dependency of the `stardoc` target: |
| 117 | |
| 118 | `checkstyle/BUILD`: |
| 119 | |
Alexandre Rostovtsev | ec43802 | 2024-04-23 16:05:45 -0400 | [diff] [blame] | 120 | ```starlark |
| 121 | load("@stardoc//stardoc:stardoc.bzl", "stardoc") |
c-parsons | c7d5b70 | 2019-09-23 13:27:13 -0400 | [diff] [blame] | 122 | |
| 123 | stardoc( |
| 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 | |
| 134 | If you would like to generate documentation for multiple .bzl files in various |
| 135 | packages in your workspace, you will need to create a single `.bzl` file that depends |
| 136 | on all those `.bzl` files. You can then explicitly whitelist rules for which you would |
| 137 | like documentation to be generated. |
| 138 | |
| 139 | For 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 |
Casey | 383aa76 | 2022-03-23 13:39:24 -0700 | [diff] [blame] | 141 | which loads these files and binds the rules to be documented as globals: |
c-parsons | c7d5b70 | 2019-09-23 13:27:13 -0400 | [diff] [blame] | 142 | |
| 143 | `doc_hub.bzl`: |
| 144 | |
Alexandre Rostovtsev | ec43802 | 2024-04-23 16:05:45 -0400 | [diff] [blame] | 145 | ```starlark |
Casey | 383aa76 | 2022-03-23 13:39:24 -0700 | [diff] [blame] | 146 | load("//foo:foo.bzl", _foo_rule = "foo_rule") |
| 147 | load("//bar:bar.bzl", _bar_rule = "bar_rule") |
| 148 | load("//baz:baz.bzl", _baz_rule = "baz_rule") |
| 149 | |
| 150 | foo_rule = _foo_rule |
| 151 | bar_rule = _bar_rule |
| 152 | baz_rule = _baz_rule |
c-parsons | c7d5b70 | 2019-09-23 13:27:13 -0400 | [diff] [blame] | 153 | |
| 154 | # No need for any implementation here. The rules need only be loaded. |
| 155 | ``` |
| 156 | |
| 157 | A single `stardoc` target can then be used to generate their documentation: |
| 158 | |
| 159 | `BUILD`: |
| 160 | |
| 161 | ```python |
Alexandre Rostovtsev | ec43802 | 2024-04-23 16:05:45 -0400 | [diff] [blame] | 162 | load("@stardoc//stardoc:stardoc.bzl", "stardoc") |
c-parsons | c7d5b70 | 2019-09-23 13:27:13 -0400 | [diff] [blame] | 163 | |
| 164 | stardoc( |
| 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 | |