Commit 389431b
authored
refactor: API for deriving customized versions of the base rules (#2610)
This implements a "builder style" API to allow arbitrary modification of
rule, attr, etc
objects used when defining a rule. The net effect is users are able to
use the base
definition for our rules, but define their own with the modifications
they need, without
having to copy/paste portions our implementation, load private files, or
patch source.
The basic way it works is a mutable object ("builder") holds the args
and state that would
be used to create the immutable Bazel object. When `build()` is called,
the immutable
Bazel object (e.g. `attr.string()`) is created. Builders are implemented
for most objects
and their settings (rule, attrs, and supporting objects).
This design is necessary because of three Bazel behaviors:
* attr etc objects are immutable, which means we must keep our own state
* attr etc objects aren't inspectable, which means we must store the
arguments for
creating the immutable objects.
* Starlark objects are frozen after initial bzl file evaluation, which
means creation
of any mutable object must be done at the point of use.
The resulting API resembles the builder APIs common in other languages:
```
r = create_py_binary_rule_builder()
r.attrs.get("srcs").set_mandatory(True)
r.attrs.get("deps").aspects().append(my_aspect)
my_py_binary = r.build()
```
Most objects are thin wrappers for managing a kwargs dict. As such, and
because they're
wrapping a foreign API, they aren't strict in enforcing their internal
state and the
kwargs dict is publicly exposed as an escape hatch.
As of this PR, no public API for e.g. `create_py_binary_rule_builder()`
is exposed. That'll come in a separate PR (to add public access points
under python/api).
Work towards #16471 parent 5a8f6c4 commit 389431b
22 files changed
Lines changed: 3238 additions & 547 deletions
File tree
- docs
- _includes
- python/private
- sphinxdocs/inventories
- tests
- builders
- support
- empty_toolchain
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
103 | 103 | | |
104 | 104 | | |
105 | 105 | | |
| 106 | + | |
| 107 | + | |
106 | 108 | | |
107 | 109 | | |
108 | 110 | | |
109 | 111 | | |
110 | 112 | | |
| 113 | + | |
111 | 114 | | |
112 | 115 | | |
113 | 116 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
51 | 51 | | |
52 | 52 | | |
53 | 53 | | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
54 | 63 | | |
55 | 64 | | |
56 | 65 | | |
57 | 66 | | |
| 67 | + | |
58 | 68 | | |
59 | 69 | | |
60 | 70 | | |
| |||
92 | 102 | | |
93 | 103 | | |
94 | 104 | | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
95 | 113 | | |
96 | 114 | | |
97 | 115 | | |
| |||
283 | 301 | | |
284 | 302 | | |
285 | 303 | | |
| 304 | + | |
286 | 305 | | |
287 | 306 | | |
288 | 307 | | |
| |||
410 | 429 | | |
411 | 430 | | |
412 | 431 | | |
| 432 | + | |
413 | 433 | | |
414 | 434 | | |
415 | 435 | | |
| |||
475 | 495 | | |
476 | 496 | | |
477 | 497 | | |
| 498 | + | |
478 | 499 | | |
479 | 500 | | |
480 | 501 | | |
| |||
515 | 536 | | |
516 | 537 | | |
517 | 538 | | |
| 539 | + | |
518 | 540 | | |
519 | 541 | | |
520 | 542 | | |
| |||
563 | 585 | | |
564 | 586 | | |
565 | 587 | | |
| 588 | + | |
| 589 | + | |
| 590 | + | |
| 591 | + | |
| 592 | + | |
| 593 | + | |
| 594 | + | |
| 595 | + | |
| 596 | + | |
| 597 | + | |
566 | 598 | | |
567 | 599 | | |
568 | 600 | | |
| |||
0 commit comments