added moveProperty contents functionality to bpmodify

bpmodify can know move the contents of a property into another
property using moveProperty. After moving the contents, the original
property is deleted.

Bug: 226636335
Change-Id: Id68d11d59f00909b4c93aa78666d14f433f236fb
Test: manually ran on several Android.bp files in bug 226636335
This commit is contained in:
Alix 2022-05-11 16:40:26 +00:00
parent e76d4122ee
commit 145d5a8c83
2 changed files with 57 additions and 2 deletions

View file

@ -32,6 +32,8 @@ var (
addIdents = new(identSet)
removeIdents = new(identSet)
removeProperty = flag.Bool("remove-property", false, "remove the property")
moveProperty = flag.Bool("move-property", false, "moves contents of property into newLocation")
newLocation string
setString *string
addLiteral *string
)
@ -40,6 +42,7 @@ func init() {
flag.Var(targetedModules, "m", "comma or whitespace separated list of modules on which to operate")
flag.Var(targetedProperty, "parameter", "alias to -property=`name`")
flag.Var(targetedProperty, "property", "fully qualified `name` of property to modify (default \"deps\")")
flag.StringVar(&newLocation, "newLocation", "", " use with moveProperty to move contents of -property into a property with name newLocation ")
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")
@ -177,6 +180,8 @@ func processModule(module *parser.Module, moduleName string,
} else if *removeProperty {
// remove-property is used solely, so return here.
return parent.RemoveProperty(prop.Name), nil
} else if *moveProperty {
return parent.MovePropertyContents(prop.Name, newLocation), nil
}
m, errs := processParameter(prop.Value, targetedProperty.String(), moduleName, file)
modified = modified || m
@ -329,6 +334,10 @@ func main() {
}()
flag.Parse()
if len(targetedProperty.parts) == 0 && *moveProperty {
report(fmt.Errorf("-move-property must specify property"))
return
}
if len(targetedProperty.parts) == 0 {
targetedProperty.Set("deps")
@ -350,8 +359,8 @@ func main() {
return
}
if len(addIdents.idents) == 0 && len(removeIdents.idents) == 0 && setString == nil && addLiteral == nil && !*removeProperty {
report(fmt.Errorf("-a, -add-literal, -r, -remove-property or -str parameter is required"))
if len(addIdents.idents) == 0 && len(removeIdents.idents) == 0 && setString == nil && addLiteral == nil && !*removeProperty && !*moveProperty {
report(fmt.Errorf("-a, -add-literal, -r, -remove-property, -move-property, or -str parameter is required"))
return
}
@ -360,6 +369,16 @@ func main() {
return
}
if *moveProperty && (len(addIdents.idents) > 0 || len(removeIdents.idents) > 0 || setString != nil || addLiteral != nil) {
report(fmt.Errorf("-move-property cannot be used with other parameter(s)"))
return
}
if *moveProperty && newLocation == "" {
report(fmt.Errorf("-move-property must specify newLocation"))
return
}
for i := 0; i < flag.NArg(); i++ {
path := flag.Arg(i)
switch dir, err := os.Stat(path); {

View file

@ -338,6 +338,42 @@ func (x *Map) RemoveProperty(propertyName string) (removed bool) {
return found
}
// MovePropertyContents moves the contents of propertyName into property newLocation
// If property newLocation doesn't exist, MovePropertyContents renames propertyName as newLocation.
// Otherwise, MovePropertyContents only supports moving contents that are a List of String.
func (x *Map) MovePropertyContents(propertyName string, newLocation string) (removed bool) {
oldProp, oldFound, _ := x.getPropertyImpl(propertyName)
newProp, newFound, _ := x.getPropertyImpl(newLocation)
// newLoc doesn't exist, simply renaming property
if oldFound && !newFound {
oldProp.Name = newLocation
return oldFound
}
if oldFound {
old, oldOk := oldProp.Value.(*List)
new, newOk := newProp.Value.(*List)
if oldOk && newOk {
toBeMoved := make([]string, len(old.Values)) //
for i, p := range old.Values {
toBeMoved[i] = p.(*String).Value
}
for _, moved := range toBeMoved {
RemoveStringFromList(old, moved)
AddStringToList(new, moved)
}
// oldProp should now be empty and needs to be deleted
x.RemoveProperty(oldProp.Name)
} else {
print(`MovePropertyContents currently only supports moving PropertyName
with List of Strings into an existing newLocation with List of Strings\n`)
}
}
return oldFound
}
type List struct {
LBracePos scanner.Position
RBracePos scanner.Position