Skip to content

Commit

Permalink
chore(internal/gapicgen): add regen-only flag (#4015)
Browse files Browse the repository at this point in the history
Adds a `regen-only` flag to local mode that disables generation version updates, manifest updates, and vetting/compilation. This is useful for iterating on generator changes or just fiddling. 

This also makes `gensnippets` respect the `only-gapics` flag, disabling snippet regen when that flag is enabled.

Finally, it optionally supplies the `release-level` option to the generator depending on if it is empty or not in the `microgenConfig`, and uses the flag name `api-service-config` instead of `gapic-service-config`, because that is the more accurate name.
  • Loading branch information
noahdietz committed Apr 29, 2021
1 parent bc0549d commit 022fc35
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 17 deletions.
2 changes: 2 additions & 0 deletions internal/gapicgen/cmd/genbot/local.go
Expand Up @@ -35,6 +35,7 @@ type localConfig struct {
protoDir string
gapicToGenerate string
onlyGapics bool
regenOnly bool
}

func genLocal(ctx context.Context, c localConfig) error {
Expand Down Expand Up @@ -68,6 +69,7 @@ func genLocal(ctx context.Context, c localConfig) error {
GapicToGenerate: c.gapicToGenerate,
OnlyGenerateGapic: c.onlyGapics,
LocalMode: true,
RegenOnly: c.regenOnly,
}
if _, err := generator.Generate(ctx, conf); err != nil {
log.Printf("Generator ran (and failed) in %s\n", tmpDir)
Expand Down
2 changes: 2 additions & 0 deletions internal/gapicgen/cmd/genbot/main.go
Expand Up @@ -49,6 +49,7 @@ func main() {
protoDir := flag.String("proto-dir", os.Getenv("PROTO_DIR"), "Directory where sources of google/protobuf resides. If unset the sources will be cloned to a temporary directory that is not cleaned up.")
gapicToGenerate := flag.String("gapic", os.Getenv("GAPIC_TO_GENERATE"), `Specifies which gapic to generate. The value should be in the form of an import path (Ex: cloud.google.com/go/pubsub/apiv1). The default "" generates all gapics.`)
onlyGapics := flag.Bool("only-gapics", strToBool(os.Getenv("ONLY_GAPICS")), "Enabling stops regenerating genproto.")
regenOnly := flag.Bool("regen-only", strToBool(os.Getenv("REGEN_ONLY")), "Enabling means no vetting, manifest updates, or compilation.")

flag.Parse()

Expand All @@ -60,6 +61,7 @@ func main() {
protoDir: *protoDir,
gapicToGenerate: *gapicToGenerate,
onlyGapics: *onlyGapics,
regenOnly: *regenOnly,
}); err != nil {
log.Fatal(err)
}
Expand Down
46 changes: 30 additions & 16 deletions internal/gapicgen/generator/gapics.go
Expand Up @@ -35,16 +35,18 @@ type GapicGenerator struct {
googleCloudDir string
genprotoDir string
gapicToGenerate string
regenOnly bool
}

// NewGapicGenerator creates a GapicGenerator.
func NewGapicGenerator(googleapisDir, protoDir, googleCloudDir, genprotoDir string, gapicToGenerate string) *GapicGenerator {
func NewGapicGenerator(googleapisDir, protoDir, googleCloudDir, genprotoDir string, gapicToGenerate string, regenOnly bool) *GapicGenerator {
return &GapicGenerator{
googleapisDir: googleapisDir,
protoDir: protoDir,
googleCloudDir: googleCloudDir,
genprotoDir: genprotoDir,
gapicToGenerate: gapicToGenerate,
regenOnly: regenOnly,
}
}

Expand All @@ -68,6 +70,10 @@ func (g *GapicGenerator) Regen(ctx context.Context) error {
return err
}

if g.regenOnly {
return nil
}

if err := g.manifest(microgenGapicConfigs); err != nil {
return err
}
Expand All @@ -80,6 +86,25 @@ func (g *GapicGenerator) Regen(ctx context.Context) error {
return err
}

if err := vet(g.googleCloudDir); err != nil {
return err
}

if err := build(g.googleCloudDir); err != nil {
return err
}

if err := execv.ForEachMod(g.googleCloudDir, g.dropModReplaceGenproto); err != nil {
return err
}

return nil
}

// RegenSnippets regenerates the snippets for all GAPICs configured to be generated.
func (g *GapicGenerator) RegenSnippets(ctx context.Context) error {
log.Println("regenerating snippets")

snippetDir := filepath.Join(g.googleCloudDir, "internal", "generated", "snippets")
apiShortnames, err := g.parseAPIShortnames(microgenGapicConfigs, manualEntries)
if err != nil {
Expand All @@ -94,19 +119,6 @@ func (g *GapicGenerator) Regen(ctx context.Context) error {
if err := goModTidy(snippetDir); err != nil {
return err
}

if err := vet(g.googleCloudDir); err != nil {
return err
}

if err := build(g.googleCloudDir); err != nil {
return err
}

if err := execv.ForEachMod(g.googleCloudDir, g.dropModReplaceGenproto); err != nil {
return err
}

return nil
}

Expand Down Expand Up @@ -229,9 +241,11 @@ func (g *GapicGenerator) microgen(conf *microgenConfig) error {
"-I", g.protoDir,
"--go_gapic_out", g.googleCloudDir,
"--go_gapic_opt", fmt.Sprintf("go-gapic-package=%s;%s", conf.importPath, conf.pkg),
"--go_gapic_opt", fmt.Sprintf("gapic-service-config=%s", conf.apiServiceConfigPath),
"--go_gapic_opt", fmt.Sprintf("release-level=%s", conf.releaseLevel)}
"--go_gapic_opt", fmt.Sprintf("api-service-config=%s", conf.apiServiceConfigPath)}

if conf.releaseLevel != "" {
args = append(args, "--go_gapic_opt", fmt.Sprintf("release-level=%s", conf.releaseLevel))
}
if conf.gRPCServiceConfigPath != "" {
args = append(args, "--go_gapic_opt", fmt.Sprintf("grpc-service-config=%s", conf.gRPCServiceConfigPath))
}
Expand Down
8 changes: 7 additions & 1 deletion internal/gapicgen/generator/generator.go
Expand Up @@ -38,6 +38,7 @@ type Config struct {
GapicToGenerate string
OnlyGenerateGapic bool
LocalMode bool
RegenOnly bool
}

// Generate generates genproto and gapics.
Expand All @@ -48,10 +49,15 @@ func Generate(ctx context.Context, conf *Config) ([]*git.ChangeInfo, error) {
return nil, fmt.Errorf("error generating genproto (may need to check logs for more errors): %v", err)
}
}
gapicGenerator := NewGapicGenerator(conf.GoogleapisDir, conf.ProtoDir, conf.GapicDir, conf.GenprotoDir, conf.GapicToGenerate)
gapicGenerator := NewGapicGenerator(conf.GoogleapisDir, conf.ProtoDir, conf.GapicDir, conf.GenprotoDir, conf.GapicToGenerate, conf.RegenOnly)
if err := gapicGenerator.Regen(ctx); err != nil {
return nil, fmt.Errorf("error generating gapics (may need to check logs for more errors): %v", err)
}
if !conf.OnlyGenerateGapic {
if err := gapicGenerator.RegenSnippets(ctx); err != nil {
return nil, fmt.Errorf("error generating snippets (may need to check logs for more errors): %v", err)
}
}

var changes []*git.ChangeInfo
if !conf.LocalMode {
Expand Down

0 comments on commit 022fc35

Please sign in to comment.