Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

buildozer: handle multiple glob tokens in set command #1250

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions buildozer/buildozer_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -1953,6 +1953,26 @@ function test_set_config_string() {
)'
}

function test_set_glob_srcs() {
run "$no_deps" 'set srcs glob(["*.go"])' '//pkg:edit'
assert_equals 'go_library(
name = "edit",
srcs = glob(["*.go"]),
)'
}

function test_set_multiple_glob_srcs() {
# Note that the inner tokens are not sorted.
run "$no_deps" 'set srcs glob(["*.go", "*.cgo"])' '//pkg:edit'
assert_equals 'go_library(
name = "edit",
srcs = glob([
"*.go",
"*.cgo",
]),
)'
}

function test_fix_unused_load() {
run 'load(":a.bzl", "a")
# TODO: refactor
Expand Down
19 changes: 18 additions & 1 deletion edit/buildozer.go
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,21 @@ func cmdSetIfAbsent(opts *Options, env CmdEnvironment) (*build.File, error) {
return env.File, nil
}

// isFunctionCall parses the args to see if they are arguments to a single
// function call. This is naïve and does not attempt to parse the inner tokens.
// example:
// the user input: "glob(["*.c", "*.h"])"
// is tokenized to: "glob([\"*.c\",", "\"*.h\"])",
func isFunctionCall(function string, args[] string) bool {
return strings.HasPrefix(args[0], function + "(") &&
strings.HasSuffix(args[len(args) -1], ")")
}

func functionCallExpr(args []string) build.Expr {
joined := strings.Join(args, "")
return &build.Ident{Name: joined}
}

func getAttrValueExpr(attr string, args []string, env CmdEnvironment) build.Expr {
switch {
case attr == "kind":
Expand All @@ -585,7 +600,9 @@ func getAttrValueExpr(attr string, args []string, env CmdEnvironment) build.Expr
list = append(list, &build.LiteralExpr{Token: i})
}
return &build.ListExpr{List: list}
case IsList(attr) && !(len(args) == 1 && strings.HasPrefix(args[0], "glob(")):
case IsList(attr) && isFunctionCall("glob", args):
return functionCallExpr(args)
case IsList(attr):
var list []build.Expr
for _, arg := range args {
list = append(list, getStringExpr(arg, env.Pkg))
Expand Down