Skip to content
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

Add ability to replace all resources of a type #2063

Merged
merged 1 commit into from
Jul 17, 2018

Conversation

apazylbe
Copy link
Contributor

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.

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"`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it just shaders or all resources?

@@ -50,13 +54,13 @@ func (verb *replaceResourceVerb) Run(ctx context.Context, flags flag.FlagSet) er
return nil
}

if verb.Handle == "" {
app.Usage(ctx, "-handle argument is required")
if verb.Handle == "" && verb.UpdateResourceBinary == "" {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it acceptable for both be specified? If not, perhaps this should be:

if (verb.Handle == "") != (verb.UpdateResourceBinary == "")

return fmt.Errorf("Multiple resources matched: %s, %s", matchedResource.GetHandle(), v.GetHandle())
if verb.Handle != "" {
var matchedResource *service.Resource
for _, v := range types.GetResources() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
}

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 != "" {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider a type switch instead of chained if statements:

switch {
    case verb.Handle != "":
        ...
    case verb.UpdateResourceBinary != "":
        ...
}

return nil
}

func (verb *replaceResourceVerb) getNewResourceData(ctx context.Context, resourceData *api.ResourceData) (string, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Docs please

@@ -127,6 +127,10 @@ message ResourceData {
}
}

message MultiResourceData {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Docs please


cmdIdx := p.After.Indices[0]
// If we change resource data, subcommands do not affect this, so change
// the main comand.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

command

@@ -602,6 +609,13 @@ func (n *Command) ResourceAfter(id *ID) *ResourceData {
}
}

func (n *Command) ResourcesAfter(ids []*ID) *MultiResourceData {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Docs please

@@ -246,6 +249,10 @@ func (n ResourceData) Format(f fmt.State, c rune) {
fmt.Fprintf(f, "%v.resource-data<%x>", n.Parent(), n.ID)
}

func (n MultiResourceData) Format(f fmt.State, c rune) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Docs please

@@ -249,6 +249,18 @@ func (n *ResourceData) Validate() error {
)
}

func (n *MultiResourceData) Validate() error {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Docs please

@apazylbe apazylbe force-pushed the replace_all_shaders branch 2 times, most recently from 31c9204 to f10168d Compare July 13, 2018 20:53
@@ -50,13 +54,13 @@ func (verb *replaceResourceVerb) Run(ctx context.Context, flags flag.FlagSet) er
return nil
}

if verb.Handle == "" {
app.Usage(ctx, "-handle argument is required")
if verb.Handle == verb.UpdateResourceBinary {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed, my intention is to error if both are non empty or if both are empty.

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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't need to be in a for loop now, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, thanks!

@apazylbe apazylbe force-pushed the replace_all_shaders branch from f10168d to 9bd4a7b Compare July 16, 2018 14:26
Copy link
Contributor

@ben-clayton ben-clayton left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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!

@@ -50,13 +54,13 @@ func (verb *replaceResourceVerb) Run(ctx context.Context, flags flag.FlagSet) er
return nil
}

if verb.Handle == "" {
app.Usage(ctx, "-handle argument is required")
if (verb.Handle != "" && verb.UpdateResourceBinary != "") || (verb.Handle == "" && verb.UpdateResourceBinary == "") {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which is equivalent to:
if (verb.Handle == "") != (verb.UpdateResourceBinary == "")

Can keep as-is if you find this clearer.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry misread your original suggestion.
Updated, though I added : (verb.Handle == "") == (verb.UpdateResourceBinary == "")
since if condition should be true for error cases

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, just testing 😉

switch {
case verb.Handle != "":
matchedResource, err := resources.FindSingle(func(t api.ResourceType, r service.Resource) bool {
return strings.Contains(r.GetHandle(), verb.Handle)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you need to check type here too?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

// 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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did my example code not work for you?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does. Sorry, totally missed that comment.

@@ -100,3 +100,7 @@ func NewResourceData(data interface{}) *ResourceData {
panic(fmt.Errorf("%T is not a ResourceData type", data))
}
}

func NewMultiResourceData(resources []*ResourceData) *MultiResourceData {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still missing docs.

@apazylbe apazylbe force-pushed the replace_all_shaders branch from 9bd4a7b to fc76a51 Compare July 17, 2018 14:02
Copy link
Contributor

@ben-clayton ben-clayton left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
@apazylbe apazylbe force-pushed the replace_all_shaders branch from fc76a51 to 9b89cfb Compare July 17, 2018 15:20
@apazylbe apazylbe merged commit bca43f3 into google:master Jul 17, 2018
@apazylbe apazylbe deleted the replace_all_shaders branch July 17, 2018 18:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants