Skip to content

Commit

Permalink
buildozer: naïve handling of multiple glob arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
stagnation committed Mar 11, 2024
1 parent 4bd717d commit 3c068ff
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
12 changes: 12 additions & 0 deletions buildozer/buildozer_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -1961,6 +1961,18 @@ function test_set_glob_srcs() {
)'
}

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
20 changes: 17 additions & 3 deletions 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,9 +600,8 @@ 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("):
// Single-value glob
return &build.Ident{Name: args[0]}
case IsList(attr) && isFunctionCall("glob", args):
return functionCallExpr(args)
case IsList(attr):
var list []build.Expr
for _, arg := range args {
Expand Down

0 comments on commit 3c068ff

Please sign in to comment.