Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
feat(internal/gapicgen): support generating only gapics with genlocal (
…#3383)

This can speed up generation and removes the edge case where an
error gets returned if there are no new proto stubs to generate
since the last regen.
  • Loading branch information
codyoss committed Dec 4, 2020
1 parent 5337043 commit eaa742a
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 22 deletions.
8 changes: 7 additions & 1 deletion internal/gapicgen/cmd/genbot/generate.go
Expand Up @@ -67,7 +67,13 @@ func generate(ctx context.Context, githubClient *GithubClient) error {
}

// Regen.
changes, err := generator.Generate(ctx, googleapisDir, genprotoDir, gocloudDir, protoDir, "")
conf := &generator.Config{
GoogleapisDir: googleapisDir,
GenprotoDir: genprotoDir,
GapicDir: gocloudDir,
ProtoDir: protoDir,
}
changes, err := generator.Generate(ctx, conf)
if err != nil {
return err
}
Expand Down
11 changes: 10 additions & 1 deletion internal/gapicgen/cmd/genlocal/main.go
Expand Up @@ -55,6 +55,7 @@ func main() {
genprotoDir := flag.String("genproto-dir", filepath.Join(tmpDir, "genproto"), "Directory where sources of googleapis/go-genproto resides. If unset the sources will be cloned to a temporary directory that is not cleaned up.")
protoDir := flag.String("proto-dir", filepath.Join(tmpDir, "proto"), "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", "", `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", false, "Enabling stops regenerating genproto.")
verbose := flag.Bool("verbose", false, "Enables verbose logging.")
flag.Parse()

Expand All @@ -71,7 +72,15 @@ func main() {
}

// Regen.
changes, err := generator.Generate(ctx, *googleapisDir, *genprotoDir, *gocloudDir, *protoDir, *gapicToGenerate)
conf := &generator.Config{
GoogleapisDir: *googleapisDir,
GenprotoDir: *genprotoDir,
GapicDir: *gocloudDir,
ProtoDir: *protoDir,
GapicToGenerate: *gapicToGenerate,
OnlyGenerateGapic: *onlyGapics,
}
changes, err := generator.Generate(ctx, conf)
if err != nil {
log.Printf("Generator ran (and failed) in %s\n", tmpDir)
log.Fatal(err)
Expand Down
26 changes: 14 additions & 12 deletions internal/gapicgen/generator/gapics.go
Expand Up @@ -28,31 +28,33 @@ import (

// GapicGenerator is used to regenerate gapic libraries.
type GapicGenerator struct {
googleapisDir string
protoDir string
googleCloudDir string
genprotoDir string
googleapisDir string
protoDir string
googleCloudDir string
genprotoDir string
gapicToGenerate string
}

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

// Regen generates gapics.
func (g *GapicGenerator) Regen(ctx context.Context, gapicToGenerate string) error {
func (g *GapicGenerator) Regen(ctx context.Context) error {
log.Println("regenerating gapics")
for _, c := range microgenGapicConfigs {
// Skip generation if generating all of the gapics and the associated
// config has a block on it. Or if generating a single gapic and it does
// not match the specified import path.
if (c.stopGeneration && gapicToGenerate == "") ||
(gapicToGenerate != "" && gapicToGenerate != c.importPath) {
if (c.stopGeneration && g.gapicToGenerate == "") ||
(g.gapicToGenerate != "" && g.gapicToGenerate != c.importPath) {
continue
}
if err := g.microgen(c); err != nil {
Expand Down
28 changes: 20 additions & 8 deletions internal/gapicgen/generator/generator.go
Expand Up @@ -26,23 +26,35 @@ import (
"strings"
)

// Config contains inputs needed to generate sources.
type Config struct {
GoogleapisDir string
GenprotoDir string
GapicDir string
ProtoDir string
GapicToGenerate string
OnlyGenerateGapic bool
}

// Generate generates genproto and gapics.
func Generate(ctx context.Context, googleapisDir, genprotoDir, gocloudDir, protoDir string, gapicToGenerate string) ([]*ChangeInfo, error) {
protoGenerator := NewGenprotoGenerator(genprotoDir, googleapisDir, protoDir)
gapicGenerator := NewGapicGenerator(googleapisDir, protoDir, gocloudDir, genprotoDir)
if err := protoGenerator.Regen(ctx); err != nil {
return nil, fmt.Errorf("error generating genproto (may need to check logs for more errors): %v", err)
func Generate(ctx context.Context, conf *Config) ([]*ChangeInfo, error) {
if !conf.OnlyGenerateGapic {
protoGenerator := NewGenprotoGenerator(conf.GenprotoDir, conf.GoogleapisDir, conf.ProtoDir)
if err := protoGenerator.Regen(ctx); err != nil {
return nil, fmt.Errorf("error generating genproto (may need to check logs for more errors): %v", err)
}
}
if err := gapicGenerator.Regen(ctx, gapicToGenerate); err != nil {
gapicGenerator := NewGapicGenerator(conf.GoogleapisDir, conf.ProtoDir, conf.GapicDir, conf.GenprotoDir, conf.GapicToGenerate)
if err := gapicGenerator.Regen(ctx); err != nil {
return nil, fmt.Errorf("error generating gapics (may need to check logs for more errors): %v", err)
}

changes, err := gatherChanges(googleapisDir, genprotoDir)
changes, err := gatherChanges(conf.GoogleapisDir, conf.GenprotoDir)
if err != nil {
return nil, fmt.Errorf("error gathering commit info")
}

if err := recordGoogleapisHash(googleapisDir, genprotoDir); err != nil {
if err := recordGoogleapisHash(conf.GoogleapisDir, conf.GenprotoDir); err != nil {
return nil, err
}

Expand Down

0 comments on commit eaa742a

Please sign in to comment.