Skip to content

Commit eaa742a

Browse files
authored
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.
1 parent 5337043 commit eaa742a

File tree

4 files changed

+51
-22
lines changed

4 files changed

+51
-22
lines changed

internal/gapicgen/cmd/genbot/generate.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,13 @@ func generate(ctx context.Context, githubClient *GithubClient) error {
6767
}
6868

6969
// Regen.
70-
changes, err := generator.Generate(ctx, googleapisDir, genprotoDir, gocloudDir, protoDir, "")
70+
conf := &generator.Config{
71+
GoogleapisDir: googleapisDir,
72+
GenprotoDir: genprotoDir,
73+
GapicDir: gocloudDir,
74+
ProtoDir: protoDir,
75+
}
76+
changes, err := generator.Generate(ctx, conf)
7177
if err != nil {
7278
return err
7379
}

internal/gapicgen/cmd/genlocal/main.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ func main() {
5555
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.")
5656
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.")
5757
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.`)
58+
onlyGapics := flag.Bool("only-gapics", false, "Enabling stops regenerating genproto.")
5859
verbose := flag.Bool("verbose", false, "Enables verbose logging.")
5960
flag.Parse()
6061

@@ -71,7 +72,15 @@ func main() {
7172
}
7273

7374
// Regen.
74-
changes, err := generator.Generate(ctx, *googleapisDir, *genprotoDir, *gocloudDir, *protoDir, *gapicToGenerate)
75+
conf := &generator.Config{
76+
GoogleapisDir: *googleapisDir,
77+
GenprotoDir: *genprotoDir,
78+
GapicDir: *gocloudDir,
79+
ProtoDir: *protoDir,
80+
GapicToGenerate: *gapicToGenerate,
81+
OnlyGenerateGapic: *onlyGapics,
82+
}
83+
changes, err := generator.Generate(ctx, conf)
7584
if err != nil {
7685
log.Printf("Generator ran (and failed) in %s\n", tmpDir)
7786
log.Fatal(err)

internal/gapicgen/generator/gapics.go

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,31 +28,33 @@ import (
2828

2929
// GapicGenerator is used to regenerate gapic libraries.
3030
type GapicGenerator struct {
31-
googleapisDir string
32-
protoDir string
33-
googleCloudDir string
34-
genprotoDir string
31+
googleapisDir string
32+
protoDir string
33+
googleCloudDir string
34+
genprotoDir string
35+
gapicToGenerate string
3536
}
3637

3738
// NewGapicGenerator creates a GapicGenerator.
38-
func NewGapicGenerator(googleapisDir, protoDir, googleCloudDir, genprotoDir string) *GapicGenerator {
39+
func NewGapicGenerator(googleapisDir, protoDir, googleCloudDir, genprotoDir string, gapicToGenerate string) *GapicGenerator {
3940
return &GapicGenerator{
40-
googleapisDir: googleapisDir,
41-
protoDir: protoDir,
42-
googleCloudDir: googleCloudDir,
43-
genprotoDir: genprotoDir,
41+
googleapisDir: googleapisDir,
42+
protoDir: protoDir,
43+
googleCloudDir: googleCloudDir,
44+
genprotoDir: genprotoDir,
45+
gapicToGenerate: gapicToGenerate,
4446
}
4547
}
4648

4749
// Regen generates gapics.
48-
func (g *GapicGenerator) Regen(ctx context.Context, gapicToGenerate string) error {
50+
func (g *GapicGenerator) Regen(ctx context.Context) error {
4951
log.Println("regenerating gapics")
5052
for _, c := range microgenGapicConfigs {
5153
// Skip generation if generating all of the gapics and the associated
5254
// config has a block on it. Or if generating a single gapic and it does
5355
// not match the specified import path.
54-
if (c.stopGeneration && gapicToGenerate == "") ||
55-
(gapicToGenerate != "" && gapicToGenerate != c.importPath) {
56+
if (c.stopGeneration && g.gapicToGenerate == "") ||
57+
(g.gapicToGenerate != "" && g.gapicToGenerate != c.importPath) {
5658
continue
5759
}
5860
if err := g.microgen(c); err != nil {

internal/gapicgen/generator/generator.go

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,23 +26,35 @@ import (
2626
"strings"
2727
)
2828

29+
// Config contains inputs needed to generate sources.
30+
type Config struct {
31+
GoogleapisDir string
32+
GenprotoDir string
33+
GapicDir string
34+
ProtoDir string
35+
GapicToGenerate string
36+
OnlyGenerateGapic bool
37+
}
38+
2939
// Generate generates genproto and gapics.
30-
func Generate(ctx context.Context, googleapisDir, genprotoDir, gocloudDir, protoDir string, gapicToGenerate string) ([]*ChangeInfo, error) {
31-
protoGenerator := NewGenprotoGenerator(genprotoDir, googleapisDir, protoDir)
32-
gapicGenerator := NewGapicGenerator(googleapisDir, protoDir, gocloudDir, genprotoDir)
33-
if err := protoGenerator.Regen(ctx); err != nil {
34-
return nil, fmt.Errorf("error generating genproto (may need to check logs for more errors): %v", err)
40+
func Generate(ctx context.Context, conf *Config) ([]*ChangeInfo, error) {
41+
if !conf.OnlyGenerateGapic {
42+
protoGenerator := NewGenprotoGenerator(conf.GenprotoDir, conf.GoogleapisDir, conf.ProtoDir)
43+
if err := protoGenerator.Regen(ctx); err != nil {
44+
return nil, fmt.Errorf("error generating genproto (may need to check logs for more errors): %v", err)
45+
}
3546
}
36-
if err := gapicGenerator.Regen(ctx, gapicToGenerate); err != nil {
47+
gapicGenerator := NewGapicGenerator(conf.GoogleapisDir, conf.ProtoDir, conf.GapicDir, conf.GenprotoDir, conf.GapicToGenerate)
48+
if err := gapicGenerator.Regen(ctx); err != nil {
3749
return nil, fmt.Errorf("error generating gapics (may need to check logs for more errors): %v", err)
3850
}
3951

40-
changes, err := gatherChanges(googleapisDir, genprotoDir)
52+
changes, err := gatherChanges(conf.GoogleapisDir, conf.GenprotoDir)
4153
if err != nil {
4254
return nil, fmt.Errorf("error gathering commit info")
4355
}
4456

45-
if err := recordGoogleapisHash(googleapisDir, genprotoDir); err != nil {
57+
if err := recordGoogleapisHash(conf.GoogleapisDir, conf.GenprotoDir); err != nil {
4658
return nil, err
4759
}
4860

0 commit comments

Comments
 (0)