bpmodify: -add-literal to add a value to the list am: 451dd63611 am: 34f9c28f0f am: 94474d68bb

Original change: https://android-review.googlesource.com/c/platform/build/blueprint/+/1977927

Change-Id: I0bb4dbab747a6d4fc41d3e53f2822a0d3c3b9315
This commit is contained in:
Jooyung Han 2022-02-11 00:45:54 +00:00 committed by Automerger Merge Worker
commit 724340527a
3 changed files with 57 additions and 11 deletions

View file

@ -32,6 +32,7 @@ var (
removeIdents = new(identSet) removeIdents = new(identSet)
setString *string setString *string
addLiteral *string
) )
func init() { func init() {
@ -39,6 +40,7 @@ func init() {
flag.Var(targetedProperty, "parameter", "alias to -property=`name`") flag.Var(targetedProperty, "parameter", "alias to -property=`name`")
flag.Var(targetedProperty, "property", "fully qualified `name` of property to modify (default \"deps\")") flag.Var(targetedProperty, "property", "fully qualified `name` of property to modify (default \"deps\")")
flag.Var(addIdents, "a", "comma or whitespace separated list of identifiers to add") flag.Var(addIdents, "a", "comma or whitespace separated list of identifiers to add")
flag.Var(stringPtrFlag{&addLiteral}, "add-literal", "a literal to add")
flag.Var(removeIdents, "r", "comma or whitespace separated list of identifiers to remove") flag.Var(removeIdents, "r", "comma or whitespace separated list of identifiers to remove")
flag.Var(stringPtrFlag{&setString}, "str", "set a string property") flag.Var(stringPtrFlag{&setString}, "str", "set a string property")
flag.Usage = usage flag.Usage = usage
@ -150,7 +152,7 @@ func processModule(module *parser.Module, moduleName string,
return false, []error{err} return false, []error{err}
} }
if prop == nil { if prop == nil {
if len(addIdents.idents) > 0 { if len(addIdents.idents) > 0 || addLiteral != nil {
// We are adding something to a non-existing list prop, so we need to create it first. // We are adding something to a non-existing list prop, so we need to create it first.
prop, modified, err = createRecursiveProperty(module, targetedProperty.name(), targetedProperty.prefixes(), &parser.List{}) prop, modified, err = createRecursiveProperty(module, targetedProperty.name(), targetedProperty.prefixes(), &parser.List{})
} else if setString != nil { } else if setString != nil {
@ -253,6 +255,21 @@ func processParameter(value parser.Expression, paramName, moduleName string,
if (wasSorted || *sortLists) && modified { if (wasSorted || *sortLists) && modified {
parser.SortList(file, list) parser.SortList(file, list)
} }
} else if addLiteral != nil {
if *sortLists {
return false, []error{fmt.Errorf("sorting not supported when adding a literal")}
}
list, ok := value.(*parser.List)
if !ok {
return false, []error{fmt.Errorf("expected parameter %s in module %s to be list, found %s",
paramName, moduleName, value.Type().String())}
}
value, errs := parser.ParseExpression(strings.NewReader(*addLiteral))
if errs != nil {
return false, errs
}
list.Values = append(list.Values, value)
modified = true
} else if setString != nil { } else if setString != nil {
str, ok := value.(*parser.String) str, ok := value.(*parser.String)
if !ok { if !ok {
@ -324,8 +341,8 @@ func main() {
return return
} }
if len(addIdents.idents) == 0 && len(removeIdents.idents) == 0 && setString == nil { if len(addIdents.idents) == 0 && len(removeIdents.idents) == 0 && setString == nil && addLiteral == nil {
report(fmt.Errorf("-a, -r or -str parameter is required")) report(fmt.Errorf("-a, -add-literal, -r or -str parameter is required"))
return return
} }

View file

@ -29,6 +29,7 @@ var testCases = []struct {
property string property string
addSet string addSet string
removeSet string removeSet string
addLiteral *string
setString *string setString *string
}{ }{
{ {
@ -251,6 +252,25 @@ var testCases = []struct {
property: "deps", property: "deps",
addSet: "bar-v10-bar", addSet: "bar-v10-bar",
}, },
{
name: "add a struct with literal",
input: `cc_foo {name: "foo"}`,
output: `cc_foo {
name: "foo",
structs: [
{
version: "1",
imports: [
"bar1",
"bar2",
],
},
],
}
`,
property: "structs",
addLiteral: proptools.StringPtr(`{version: "1", imports: ["bar1", "bar2"]}`),
},
{ {
name: "set string", name: "set string",
input: ` input: `
@ -301,6 +321,7 @@ func TestProcessModule(t *testing.T) {
addIdents.Set(testCase.addSet) addIdents.Set(testCase.addSet)
removeIdents.Set(testCase.removeSet) removeIdents.Set(testCase.removeSet)
setString = testCase.setString setString = testCase.setString
addLiteral = testCase.addLiteral
inAst, errs := parser.ParseAndEval("", strings.NewReader(testCase.input), parser.NewScope(nil)) inAst, errs := parser.ParseAndEval("", strings.NewReader(testCase.input), parser.NewScope(nil))
if len(errs) > 0 { if len(errs) > 0 {

View file

@ -98,6 +98,14 @@ func Parse(filename string, r io.Reader, scope *Scope) (file *File, errs []error
return parse(p) return parse(p)
} }
func ParseExpression(r io.Reader) (value Expression, errs []error) {
p := newParser(r, NewScope(nil))
value = p.parseExpression()
p.accept(scanner.EOF)
errs = p.errors
return
}
type parser struct { type parser struct {
scanner scanner.Scanner scanner scanner.Scanner
tok rune tok rune