-
Notifications
You must be signed in to change notification settings - Fork 462
Adding support for .bzl file operations #1464
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -86,6 +86,20 @@ type CmdEnvironment struct { | |
| // The cmdXXX functions implement the various commands. | ||
|
|
||
| func cmdAdd(opts *Options, env CmdEnvironment) (*build.File, error) { | ||
| // Variable targets are represented by a dummy Rule with a nil Call. | ||
| if env.Rule != nil && env.Rule.Call == nil { | ||
| varName := env.Rule.ImplicitName | ||
| varAssign, ok := env.Vars[varName] | ||
| if !ok { | ||
| return nil, fmt.Errorf("variable %s is not defined", varName) | ||
| } | ||
| for _, val := range env.Args[1:] { | ||
| strVal := getStringExpr(val, env.Pkg) | ||
| varAssign.RHS = AddValueToList(varAssign.RHS, env.Pkg, strVal, true) | ||
| } | ||
|
|
||
| return env.File, nil | ||
| } | ||
| attr := env.Args[0] | ||
| for _, val := range env.Args[1:] { | ||
| if IsIntList(attr) { | ||
|
|
@@ -211,6 +225,23 @@ func cmdPrintComment(opts *Options, env CmdEnvironment) (*build.File, error) { | |
| } | ||
|
|
||
| func cmdDelete(opts *Options, env CmdEnvironment) (*build.File, error) { | ||
| // Variable targets are represented by a dummy Rule with a nil Call. | ||
| if env.Rule != nil && env.Rule.Call == nil { | ||
| varName := env.Rule.ImplicitName | ||
| varAssign, ok := env.Vars[varName] | ||
| if !ok { | ||
| return nil, fmt.Errorf("variable %s is not defined", varName) | ||
| } | ||
| var all []build.Expr | ||
| for _, stmt := range env.File.Stmt { | ||
| if stmt == varAssign { | ||
| continue | ||
| } | ||
| all = append(all, stmt) | ||
| } | ||
| env.File.Stmt = all | ||
| return env.File, nil | ||
| } | ||
| return DeleteRule(env.File, env.Rule), nil | ||
| } | ||
|
|
||
|
|
@@ -331,6 +362,47 @@ func cmdSubstituteLoad(opts *Options, env CmdEnvironment) (*build.File, error) { | |
| } | ||
|
|
||
| func cmdPrint(opts *Options, env CmdEnvironment) (*build.File, error) { | ||
| // Variable targets are represented by a dummy Rule with a nil Call. | ||
| if env.Rule != nil && env.Rule.Call == nil { | ||
| varName := env.Rule.ImplicitName | ||
| varAssign, ok := env.Vars[varName] | ||
| if !ok { | ||
| return nil, fmt.Errorf("variable %s is not defined", varName) | ||
| } | ||
| format := env.Args | ||
| if len(format) == 0 { | ||
| format = []string{"value"} | ||
| } | ||
| fields := make([]*apipb.Output_Record_Field, len(format)) | ||
| for i, str := range format { | ||
| switch str { | ||
| case "name": | ||
| fields[i] = &apipb.Output_Record_Field{Value: &apipb.Output_Record_Field_Text{Text: varName}} | ||
| case "kind": | ||
| fields[i] = &apipb.Output_Record_Field{Value: &apipb.Output_Record_Field_Text{Text: "var"}} | ||
| case "label": | ||
| label := labels.Label{Package: env.Pkg, Target: varName} | ||
| fields[i] = &apipb.Output_Record_Field{Value: &apipb.Output_Record_Field_Text{Text: label.Format()}} | ||
| case "path": | ||
| fields[i] = &apipb.Output_Record_Field{Value: &apipb.Output_Record_Field_Text{Text: env.File.Path}} | ||
| case "startline": | ||
| start, _ := varAssign.Span() | ||
| fields[i] = &apipb.Output_Record_Field{Value: &apipb.Output_Record_Field_Number{Number: int32(start.Line)}} | ||
| case "endline": | ||
| _, end := varAssign.Span() | ||
| fields[i] = &apipb.Output_Record_Field{Value: &apipb.Output_Record_Field_Number{Number: int32(end.Line)}} | ||
| case "rule": | ||
| fields[i] = &apipb.Output_Record_Field{Value: &apipb.Output_Record_Field_Text{Text: build.FormatString(varAssign)}} | ||
| case "value": | ||
| fields[i] = &apipb.Output_Record_Field{Value: &apipb.Output_Record_Field_Text{Text: build.FormatString(varAssign.RHS)}} | ||
| default: | ||
| fields[i] = &apipb.Output_Record_Field{Value: &apipb.Output_Record_Field_Error{Error: apipb.Output_Record_Field_MISSING}} | ||
| } | ||
| } | ||
| env.output.Fields = fields | ||
| return nil, nil | ||
| } | ||
|
|
||
| format := env.Args | ||
| if len(format) == 0 { | ||
| format = []string{"name", "kind"} | ||
|
|
@@ -419,6 +491,18 @@ func attrKeysForPattern(rule *build.Rule, pattern string) []string { | |
| } | ||
|
|
||
| func cmdRemove(opts *Options, env CmdEnvironment) (*build.File, error) { | ||
| // Variable targets are represented by a dummy Rule with a nil Call. | ||
| if env.Rule != nil && env.Rule.Call == nil { | ||
| varName := env.Rule.ImplicitName | ||
| varAssign, ok := env.Vars[varName] | ||
| if !ok { | ||
| return nil, fmt.Errorf("variable %s is not defined", varName) | ||
| } | ||
| for _, val := range env.Args[1:] { | ||
| ListDelete(varAssign.RHS, val, env.Pkg) | ||
| } | ||
|
mydatascience marked this conversation as resolved.
|
||
| return env.File, nil | ||
| } | ||
| if len(env.Args) == 1 { // Remove the attribute | ||
| if env.Args[0] == "*" { | ||
| didDelete := false | ||
|
|
@@ -560,6 +644,16 @@ func cmdSubstitute(opts *Options, env CmdEnvironment) (*build.File, error) { | |
| func cmdSet(opts *Options, env CmdEnvironment) (*build.File, error) { | ||
| attr := env.Args[0] | ||
| args := env.Args[1:] | ||
| // Variable targets are represented by a dummy Rule with a nil Call. | ||
| if env.Rule != nil && env.Rule.Call == nil { | ||
| varName := env.Rule.ImplicitName | ||
| varAssign, ok := env.Vars[varName] | ||
| if !ok { | ||
| return nil, fmt.Errorf("variable %s is not defined", varName) | ||
| } | ||
| varAssign.RHS = getAttrValueExpr(attr, args, env) | ||
| return env.File, nil | ||
| } | ||
| if attr == "kind" { | ||
| env.Rule.SetKind(args[0]) | ||
| } else { | ||
|
|
@@ -962,7 +1056,7 @@ var readonlyCommands = map[string]bool{ | |
| "print_comment": true, | ||
| } | ||
|
|
||
| func expandTargets(f *build.File, rule string) ([]*build.Rule, error) { | ||
| func expandTargets(f *build.File, rule string, vars map[string]*build.AssignExpr) ([]*build.Rule, error) { | ||
| if r := FindRuleByName(f, rule); r != nil { | ||
| return []*build.Rule{r}, nil | ||
| } else if r := FindExportedFile(f, rule); r != nil { | ||
|
|
@@ -982,6 +1076,12 @@ func expandTargets(f *build.File, rule string) ([]*build.Rule, error) { | |
| } else { | ||
| return f.Rules(kind), nil | ||
| } | ||
| } else if vars != nil { | ||
| // When variable editing is enabled, allow targeting global variables directly, | ||
| // but only if the variable is actually defined in the file. | ||
| if _, ok := vars[rule]; ok { | ||
| return []*build.Rule{{ImplicitName: rule}}, nil | ||
| } | ||
| } | ||
| return nil, fmt.Errorf("rule '%s' not found", rule) | ||
| } | ||
|
|
@@ -1132,11 +1232,19 @@ type rewriteResult struct { | |
| func getGlobalVariables(exprs []build.Expr) (vars map[string]*build.AssignExpr) { | ||
| vars = make(map[string]*build.AssignExpr) | ||
| for _, expr := range exprs { | ||
| if as, ok := expr.(*build.AssignExpr); ok { | ||
| if lhs, ok := as.LHS.(*build.Ident); ok { | ||
| vars[lhs.Name] = as | ||
| build.Walk(expr, func(x build.Expr, stk []build.Expr) { | ||
| //Skip variables defined inside functions | ||
| for _, frame := range stk { | ||
| if _, ok := frame.(*build.DefStmt); ok { | ||
| return | ||
| } | ||
| } | ||
| } | ||
| if as, ok := x.(*build.AssignExpr); ok { | ||
| if lhs, ok := as.LHS.(*build.Ident); ok { | ||
| vars[lhs.Name] = as | ||
| } | ||
| } | ||
|
Comment on lines
+1242
to
+1246
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This implementation only detects global variables assigned via simple identifiers (e.g., |
||
| }) | ||
| } | ||
| return vars | ||
| } | ||
|
|
@@ -1238,7 +1346,7 @@ func rewrite(opts *Options, commandsForFile commandsForFile) *rewriteResult { | |
| absPkg = f.Pkg | ||
| } | ||
|
|
||
| targets, err := expandTargets(f, rule) | ||
| targets, err := expandTargets(f, rule, vars) | ||
| if err != nil { | ||
| cerr := commandError(cft.commands, cft.target, err) | ||
| errs = append(errs, cerr) | ||
|
|
@@ -1298,6 +1406,7 @@ func executeCommandsInFile( | |
| ) (*build.File, error) { | ||
| changed := false | ||
| for _, cmd := range cft.commands { | ||
| cmdName := cmd.tokens[0] | ||
| cmdInfo := AllCommands[cmd.tokens[0]] | ||
| // Depending on whether a transformation is rule-specific or not, it should be applied to | ||
| // every rule that satisfies the filter or just once to the file. | ||
|
|
@@ -1306,6 +1415,22 @@ func executeCommandsInFile( | |
| cmdTargets = []*build.Rule{nil} | ||
| } | ||
| for _, r := range cmdTargets { | ||
| // Variable targets are represented by a dummy Rule with a nil Call. | ||
| // Most rule-specific commands assume a non-nil Call, so guard centrally to avoid panics. | ||
| if r != nil && r.Call == nil { | ||
| switch cmdName { | ||
| case "add", "remove", "set", "print", "delete": | ||
| // Supported variable-target commands. | ||
| default: | ||
| err := fmt.Errorf("command %q does not support variable targets", cmdName) | ||
| cerr := commandError([]command{cmd}, cft.target, err) | ||
| if opts.KeepGoing { | ||
| *errs = append(*errs, cerr) | ||
| continue | ||
| } | ||
| return nil, cerr | ||
| } | ||
| } | ||
| record := &apipb.Output_Record{} | ||
| newf, err := cmdInfo.Fn(opts, CmdEnvironment{f, r, vars, absPkg, cmd.tokens[1:], record}) | ||
| if len(record.Fields) != 0 { | ||
|
|
@@ -1691,7 +1816,7 @@ func ExecuteCommandsOnInlineFile(fileContent []byte, commands []string) ([]byte, | |
| f.Type = build.TypeBuild | ||
| } | ||
| for _, cft := range commandsByTargetName { | ||
| rules, err := expandTargets(f, cft.target) | ||
| rules, err := expandTargets(f, cft.target, nil) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.