From 4bd717dea4f571cc96149f381206a2e680901365 Mon Sep 17 00:00:00 2001 From: Nils Wireklint Date: Mon, 11 Mar 2024 14:35:02 +0100 Subject: [PATCH 1/2] buildozer: refactor the glob handling to a special case --- buildozer/buildozer_test.sh | 8 ++++++++ edit/buildozer.go | 5 ++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/buildozer/buildozer_test.sh b/buildozer/buildozer_test.sh index b62d22447..04805f674 100755 --- a/buildozer/buildozer_test.sh +++ b/buildozer/buildozer_test.sh @@ -1953,6 +1953,14 @@ 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_fix_unused_load() { run 'load(":a.bzl", "a") # TODO: refactor diff --git a/edit/buildozer.go b/edit/buildozer.go index 486d71f63..072e35ca3 100644 --- a/edit/buildozer.go +++ b/edit/buildozer.go @@ -585,7 +585,10 @@ 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) && len(args) == 1 && strings.HasPrefix(args[0], "glob("): + // Single-value glob + return &build.Ident{Name: args[0]} + case IsList(attr): var list []build.Expr for _, arg := range args { list = append(list, getStringExpr(arg, env.Pkg)) From 2707289c8e7f161f550ffadc03a2db543e4c7c0f Mon Sep 17 00:00:00 2001 From: Nils Wireklint Date: Mon, 11 Mar 2024 14:35:41 +0100 Subject: [PATCH 2/2] =?UTF-8?q?buildozer:=20na=C3=AFve=20handling=20of=20m?= =?UTF-8?q?ultiple=20glob=20arguments?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In the 'set' command, following the work from #100. --- buildozer/buildozer_test.sh | 12 ++++++++++++ edit/buildozer.go | 20 +++++++++++++++++--- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/buildozer/buildozer_test.sh b/buildozer/buildozer_test.sh index 04805f674..d65b19f51 100755 --- a/buildozer/buildozer_test.sh +++ b/buildozer/buildozer_test.sh @@ -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 diff --git a/edit/buildozer.go b/edit/buildozer.go index 072e35ca3..1cb1b81b9 100644 --- a/edit/buildozer.go +++ b/edit/buildozer.go @@ -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": @@ -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 {