Add ability to replace all resources of a type#2063
Conversation
cmd/gapit/flags.go
Outdated
| Handle string `help:"required. handle of the resource to replace"` | ||
| ResourcePath string `help:"file path for the new resource"` | ||
| At int `help:"command index to replace the resource(s) at"` | ||
| UpdateResourceBinary string `help:"binary to run for every shader; consumes resource data from standard input and writes to standard output"` |
There was a problem hiding this comment.
Is it just shaders or all resources?
cmd/gapit/replace_resource.go
Outdated
|
|
||
| if verb.Handle == "" { | ||
| app.Usage(ctx, "-handle argument is required") | ||
| if verb.Handle == "" && verb.UpdateResourceBinary == "" { |
There was a problem hiding this comment.
Is it acceptable for both be specified? If not, perhaps this should be:
if (verb.Handle == "") != (verb.UpdateResourceBinary == "")
cmd/gapit/replace_resource.go
Outdated
| return fmt.Errorf("Multiple resources matched: %s, %s", matchedResource.GetHandle(), v.GetHandle()) | ||
| if verb.Handle != "" { | ||
| var matchedResource *service.Resource | ||
| for _, v := range types.GetResources() { |
There was a problem hiding this comment.
We already have a function to look for a resource by type and ID - perhaps we should add a more generic one to search using a predicate? We can then have a version that errors if theres more or less than one found.
Something like:
// FindAll returns all the resources that match the predicate f.
func (r *Resources) FindAll(f func(api.ResourceType, Resource) bool) []*Resource { ... }
// FindSingle returns the single resource that matches the predicate f.
// If there are 0 or multiple resources found, FindSingle returns an error.
func (r *Resources) FindSingle(f func(api.ResourceType, Resource) bool) (*Resource, error) { ... }
// Find looks up a resource by type and identifier.
func (r *Resources) Find(ty api.ResourceType, id id.ID) *Resource {
r, _ := r.FindSingle(func(t api.ResourceType, r Resource) bool {
return t == ty && r.ID.ID() == id
})
return r
}
cmd/gapit/replace_resource.go
Outdated
| return log.Errf(ctx, err, "Could not read resource file %s", verb.ResourcePath) | ||
| } | ||
| resourceData = api.NewResourceData(&api.Shader{Type: api.ShaderType_Spirv, Source: string(newResourceBytes)}) | ||
| } else if verb.UpdateResourceBinary != "" { |
There was a problem hiding this comment.
Consider a type switch instead of chained if statements:
switch {
case verb.Handle != "":
...
case verb.UpdateResourceBinary != "":
...
}
cmd/gapit/replace_resource.go
Outdated
| return nil | ||
| } | ||
|
|
||
| func (verb *replaceResourceVerb) getNewResourceData(ctx context.Context, resourceData *api.ResourceData) (string, error) { |
| } | ||
| } | ||
|
|
||
| message MultiResourceData { |
|
|
||
| cmdIdx := p.After.Indices[0] | ||
| // If we change resource data, subcommands do not affect this, so change | ||
| // the main comand. |
| } | ||
| } | ||
|
|
||
| func (n *Command) ResourcesAfter(ids []*ID) *MultiResourceData { |
| fmt.Fprintf(f, "%v.resource-data<%x>", n.Parent(), n.ID) | ||
| } | ||
|
|
||
| func (n MultiResourceData) Format(f fmt.State, c rune) { |
| ) | ||
| } | ||
|
|
||
| func (n *MultiResourceData) Validate() error { |
31c9204 to
f10168d
Compare
cmd/gapit/replace_resource.go
Outdated
|
|
||
| if verb.Handle == "" { | ||
| app.Usage(ctx, "-handle argument is required") | ||
| if verb.Handle == verb.UpdateResourceBinary { |
There was a problem hiding this comment.
Changed, my intention is to error if both are non empty or if both are empty.
cmd/gapit/replace_resource.go
Outdated
| return fmt.Errorf("Multiple resources matched: %s, %s", matchedResource.GetHandle(), v.GetHandle()) | ||
| switch { | ||
| case verb.Handle != "": | ||
| matchedResource, err := resources.FindSingle(func(t api.ResourceType, r service.Resource) bool { |
There was a problem hiding this comment.
This doesn't need to be in a for loop now, right?
f10168d to
9bd4a7b
Compare
ben-clayton
left a comment
There was a problem hiding this comment.
Please reply to review comments with a 'done' if they've been addressed - this way the reviewer knows if things have been missed, or not yet done.
Thanks!
cmd/gapit/replace_resource.go
Outdated
|
|
||
| if verb.Handle == "" { | ||
| app.Usage(ctx, "-handle argument is required") | ||
| if (verb.Handle != "" && verb.UpdateResourceBinary != "") || (verb.Handle == "" && verb.UpdateResourceBinary == "") { |
There was a problem hiding this comment.
Which is equivalent to:
if (verb.Handle == "") != (verb.UpdateResourceBinary == "")
Can keep as-is if you find this clearer.
There was a problem hiding this comment.
Sorry misread your original suggestion.
Updated, though I added : (verb.Handle == "") == (verb.UpdateResourceBinary == "")
since if condition should be true for error cases
cmd/gapit/replace_resource.go
Outdated
| switch { | ||
| case verb.Handle != "": | ||
| matchedResource, err := resources.FindSingle(func(t api.ResourceType, r service.Resource) bool { | ||
| return strings.Contains(r.GetHandle(), verb.Handle) |
There was a problem hiding this comment.
I think you need to check type here too?
cmd/gapit/replace_resource.go
Outdated
| // getNewResourceData runs the update resource binary on the old resource data | ||
| // and returns the newly generated resource data | ||
| func (verb *replaceResourceVerb) getNewResourceData(ctx context.Context, resourceData string) (string, error) { | ||
| updateCmd := exec.Command(verb.UpdateResourceBinary) |
There was a problem hiding this comment.
Did my example code not work for you?
There was a problem hiding this comment.
It does. Sorry, totally missed that comment.
| } | ||
| } | ||
|
|
||
| func NewMultiResourceData(resources []*ResourceData) *MultiResourceData { |
9bd4a7b to
fc76a51
Compare
ben-clayton
left a comment
There was a problem hiding this comment.
Thank you for the changes!
Currently only works for shaders UpdateResourceBinary should read the resource data from standard input and output the modified resource data to standard output. Also modifying replace_resource to consume spir-v disassembly to replace a single shader for consistency
fc76a51 to
9b89cfb
Compare
Currently only works for shaders
UpdateResourceBinary should read the resource data from
standard input and output the modified resource data to
standard output.
Also modifying replace_resource to consume spir-v
disassembly to replace a single shader for consistency
I know MultiResourceData is not the best of names, but couldn't come up with anything better.
Suggestions are welcome.