Skip to content

Commit

Permalink
compile: rewrite json if it's out of sync (#379)
Browse files Browse the repository at this point in the history
  • Loading branch information
passichenko authored May 2, 2024
1 parent 20d47ee commit ce191d7
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions pkg/star/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package star

import (
"bytes"
"context"
"fmt"
"path/filepath"
Expand Down Expand Up @@ -111,21 +112,27 @@ func (c *compiler) Persist(ctx context.Context, f *feature.Feature, nv feature.N
jsonGenPath := filepath.Join(c.ff.NamespaceName, metadata.GenFolderPathJSON)
protoGenPath := filepath.Join(c.ff.NamespaceName, metadata.GenFolderPathProto)
protoBinFile := filepath.Join(protoGenPath, fmt.Sprintf("%s.proto.bin", c.ff.Name))
jsonFile := filepath.Join(jsonGenPath, fmt.Sprintf("%s.json", c.ff.Name))

diffExists, err := compareExistingProto(ctx, protoBinFile, fProto, c.cw)
if err != nil && !ignoreBackwardsCompatibility { // exit due to backwards incompatibility
return false, false, errors.Wrap(err, "comparing with existing proto")
}
if !diffExists || dryRun {
// skipping i/o
return false, diffExists, nil
}

// Create the json file
jBytes, err := feature.ProtoToJSON(fProto, c.registry)
if err != nil {
return false, diffExists, errors.Wrap(err, "proto to json")
}
jsonFile := filepath.Join(jsonGenPath, fmt.Sprintf("%s.json", c.ff.Name))
oldJBytes, err := c.cw.GetFileContents(ctx, jsonFile)
if err != nil || !bytes.Equal(jBytes, oldJBytes) {
diffExists = true
}

if !diffExists || dryRun {
return false, diffExists, nil
}

// Create the json file
if err := c.cw.MkdirAll(jsonGenPath, 0775); err != nil {
return false, diffExists, errors.Wrap(err, "failed to make gen json directory")
}
Expand Down

0 comments on commit ce191d7

Please sign in to comment.