export Java variables to Bazel

Test: build/bazel/bp2build.sh
Change-Id: Ia06f9265c9f96e6add6edbd0cee925fe37b430d3
This commit is contained in:
Sam Delmerico
2022-03-25 16:33:26 +00:00
parent 7f88956c16
commit 932c01cf9e
9 changed files with 262 additions and 38 deletions

View File

@@ -39,21 +39,26 @@ func PrintBool(item bool) string {
// PrintsStringList returns a Starlark-compatible string of a list of Strings/Labels.
func PrintStringList(items []string, indentLevel int) string {
return PrintList(items, indentLevel, `"%s"`)
return PrintList(items, indentLevel, func(s string) string {
if strings.Contains(s, "\"") {
return `'''%s'''`
}
return `"%s"`
})
}
// PrintList returns a Starlark-compatible string of list formmated as requested.
func PrintList(items []string, indentLevel int, formatString string) string {
func PrintList(items []string, indentLevel int, formatString func(string) string) string {
if len(items) == 0 {
return "[]"
} else if len(items) == 1 {
return fmt.Sprintf("["+formatString+"]", items[0])
return fmt.Sprintf("["+formatString(items[0])+"]", items[0])
}
list := make([]string, 0, len(items)+2)
list = append(list, "[")
innerIndent := Indention(indentLevel + 1)
for _, item := range items {
list = append(list, fmt.Sprintf(`%s`+formatString+`,`, innerIndent, item))
list = append(list, fmt.Sprintf(`%s`+formatString(item)+`,`, innerIndent, item))
}
list = append(list, Indention(indentLevel)+"]")
return strings.Join(list, "\n")

View File

@@ -18,6 +18,10 @@ import (
"testing"
)
func simpleFormat(s string) string {
return "%s"
}
func TestPrintEmptyStringList(t *testing.T) {
in := []string{}
indentLevel := 0
@@ -54,7 +58,7 @@ func TestPrintMultiElementStringList(t *testing.T) {
func TestPrintEmptyList(t *testing.T) {
in := []string{}
indentLevel := 0
out := PrintList(in, indentLevel, "%s")
out := PrintList(in, indentLevel, simpleFormat)
expectedOut := "[]"
if out != expectedOut {
t.Errorf("Expected %q, got %q", expectedOut, out)
@@ -64,7 +68,7 @@ func TestPrintEmptyList(t *testing.T) {
func TestPrintSingleElementList(t *testing.T) {
in := []string{"1"}
indentLevel := 0
out := PrintList(in, indentLevel, "%s")
out := PrintList(in, indentLevel, simpleFormat)
expectedOut := `[1]`
if out != expectedOut {
t.Errorf("Expected %q, got %q", expectedOut, out)
@@ -74,7 +78,7 @@ func TestPrintSingleElementList(t *testing.T) {
func TestPrintMultiElementList(t *testing.T) {
in := []string{"1", "2"}
indentLevel := 0
out := PrintList(in, indentLevel, "%s")
out := PrintList(in, indentLevel, simpleFormat)
expectedOut := `[
1,
2,
@@ -87,7 +91,7 @@ func TestPrintMultiElementList(t *testing.T) {
func TestListWithNonZeroIndent(t *testing.T) {
in := []string{"1", "2"}
indentLevel := 1
out := PrintList(in, indentLevel, "%s")
out := PrintList(in, indentLevel, simpleFormat)
expectedOut := `[
1,
2,