Skip to content

Commit

Permalink
Add attrs as a special attribute of the print command (#1323)
Browse files Browse the repository at this point in the history
Before, there was no way to get a list of all of the attributes of a
rule.  Now print attrs will print a list containing all of the
attribute names of a given rule.

Fixes #1322.

RELNOTES: "print 'attrs' //rule" prints all attributes of the rule
  • Loading branch information
iflan authored Jan 21, 2025
1 parent 50e9b3f commit 733b4a2
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
1 change: 1 addition & 0 deletions buildozer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ the attribute, a warning is printed on stderr.
There are some special attributes in the `print` command:

* `kind`: displays the name of the function
* `attrs`: displays the attribute names of the rule
* `label`: the fully qualified label
* `rule`: the entire rule definition
* `startline`: the line number on which the rule begins in the BUILD file
Expand Down
12 changes: 12 additions & 0 deletions buildozer/buildozer_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -1251,6 +1251,18 @@ function test_print_version() {
assert_output '12345'
}

function test_print_attrs() {
in='package()
cc_library(
name = "a",
srcs = ["a.cc"],
deps = ["//foo"],
)'
run "$in" 'print attrs' '//pkg:*'
assert_output '[]
[name srcs deps]'
}

function test_new_cc_library() {
in='cc_test(name = "a")
Expand Down
4 changes: 4 additions & 0 deletions edit/buildozer.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,10 @@ func cmdPrint(opts *Options, env CmdEnvironment) (*build.File, error) {
fields[i] = &apipb.Output_Record_Field{
Value: &apipb.Output_Record_Field_Text{Text: env.File.Path},
}
} else if str == "attrs" {
fields[i] = &apipb.Output_Record_Field{
Value: &apipb.Output_Record_Field_List{List: &apipb.RepeatedString{Strings: env.Rule.AttrKeys()}},
}
} else if value == nil {
fmt.Fprintf(opts.ErrWriter, "rule \"//%s:%s\" has no attribute \"%s\"\n",
env.Pkg, env.Rule.Name(), str)
Expand Down

0 comments on commit 733b4a2

Please sign in to comment.