Skip to content

Commit

Permalink
chore: fix genbot (#7982)
Browse files Browse the repository at this point in the history
- Fix error: "Tried to write the same file twice."
- remove some cruft
- skip ai libs until we explicitly onboard
- update some deps
  • Loading branch information
codyoss authored May 24, 2023
1 parent f3402bf commit abfe8dd
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 54 deletions.
4 changes: 2 additions & 2 deletions internal/gapicgen/cmd/genbot/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ ENV PATH /usr/local/go/bin:$PATH
RUN go version

# Install Go tools.
RUN go install github.com/golang/protobuf/protoc-gen-go@v1.5.2 && \
RUN go install github.com/golang/protobuf/protoc-gen-go@v1.5.3 && \
go install golang.org/x/lint/golint@latest && \
go install golang.org/x/tools/cmd/goimports@latest && \
go install honnef.co/go/tools/cmd/staticcheck@latest && \
go install github.com/googleapis/gapic-generator-go/cmd/protoc-gen-go_gapic@v0.35.2
go install github.com/googleapis/gapic-generator-go/cmd/protoc-gen-go_gapic@v0.37.0
ENV PATH="${PATH}:/root/go/bin"

# Source: http://debuggable.com/posts/disable-strict-host-checking-for-git-clone:49896ff3-0ac0-4263-9703-1eae4834cda3
Expand Down
30 changes: 13 additions & 17 deletions internal/gapicgen/cmd/genbot/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,12 @@ import (
)

type localConfig struct {
googleapisDir string
genprotoDir string
protoDir string
gapicToGenerate string
onlyGapics bool
regenOnly bool
forceAll bool
genAlias bool
googleapisDir string
genprotoDir string
protoDir string
regenOnly bool
forceAll bool
genAlias bool
}

func genLocal(ctx context.Context, c localConfig) error {
Expand All @@ -61,15 +59,13 @@ func genLocal(ctx context.Context, c localConfig) error {

// Regen.
conf := &generator.Config{
GoogleapisDir: defaultDir(tmpGoogleapisDir, c.googleapisDir),
GenprotoDir: defaultDir(tmpGenprotoDir, c.genprotoDir),
ProtoDir: defaultDir(tmpProtoDir, c.protoDir),
GapicToGenerate: c.gapicToGenerate,
OnlyGenerateGapic: c.onlyGapics,
LocalMode: true,
RegenOnly: c.regenOnly,
ForceAll: c.forceAll,
GenAlias: c.genAlias,
GoogleapisDir: defaultDir(tmpGoogleapisDir, c.googleapisDir),
GenprotoDir: defaultDir(tmpGenprotoDir, c.genprotoDir),
ProtoDir: defaultDir(tmpProtoDir, c.protoDir),
LocalMode: true,
RegenOnly: c.regenOnly,
ForceAll: c.forceAll,
GenAlias: c.genAlias,
}
if _, err := generator.Generate(ctx, conf); err != nil {
log.Printf("Generator ran (and failed) in %s\n", tmpDir)
Expand Down
16 changes: 6 additions & 10 deletions internal/gapicgen/cmd/genbot/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,23 +48,19 @@ func main() {
googleapisDir := flag.String("googleapis-dir", os.Getenv("GOOGLEAPIS_DIR"), "Directory where sources of googleapis/googleapis resides. If unset the sources will be cloned to a temporary directory that is not cleaned up.")
genprotoDir := flag.String("genproto-dir", os.Getenv("GENPROTO_DIR"), "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", 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.")
genAlias := flag.Bool("generate-alias", strToBool(os.Getenv("GENERATE_ALIAS")), "Enabling means alias files will be generated.")

flag.Parse()

if *localMode {
if err := genLocal(ctx, localConfig{
googleapisDir: *googleapisDir,
genprotoDir: *genprotoDir,
protoDir: *protoDir,
gapicToGenerate: *gapicToGenerate,
onlyGapics: *onlyGapics,
regenOnly: *regenOnly,
forceAll: *forceAll,
genAlias: *genAlias,
googleapisDir: *googleapisDir,
genprotoDir: *genprotoDir,
protoDir: *protoDir,
regenOnly: *regenOnly,
forceAll: *forceAll,
genAlias: *genAlias,
}); err != nil {
log.Fatal(err)
}
Expand Down
24 changes: 10 additions & 14 deletions internal/gapicgen/generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,20 @@ import (

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

// Generate generates genproto and gapics.
func Generate(ctx context.Context, conf *Config) ([]*git.ChangeInfo, error) {
if !conf.OnlyGenerateGapic {
protoGenerator := NewGenprotoGenerator(conf)
if err := protoGenerator.Regen(ctx); err != nil {
return nil, fmt.Errorf("error generating genproto (may need to check logs for more errors): %v", err)
}
protoGenerator := NewGenprotoGenerator(conf)
if err := protoGenerator.Regen(ctx); err != nil {
return nil, fmt.Errorf("error generating genproto (may need to check logs for more errors): %v", err)
}

var changes []*git.ChangeInfo
Expand Down
20 changes: 9 additions & 11 deletions internal/gapicgen/generator/genproto.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,27 +35,26 @@ var goPkgOptRe = regexp.MustCompile(`(?m)^option go_package = (.*);`)

// GenprotoGenerator is used to generate code for googleapis/go-genproto.
type GenprotoGenerator struct {
genprotoDir string
googleapisDir string
protoSrcDir string
gapicToGenerate string
forceAll bool
genprotoDir string
googleapisDir string
protoSrcDir string
forceAll bool
}

// NewGenprotoGenerator creates a new GenprotoGenerator.
func NewGenprotoGenerator(c *Config) *GenprotoGenerator {
return &GenprotoGenerator{
genprotoDir: c.GenprotoDir,
googleapisDir: c.GoogleapisDir,
protoSrcDir: filepath.Join(c.ProtoDir, "/src"),
gapicToGenerate: c.GapicToGenerate,
forceAll: c.ForceAll,
genprotoDir: c.GenprotoDir,
googleapisDir: c.GoogleapisDir,
protoSrcDir: filepath.Join(c.ProtoDir, "/src"),
forceAll: c.ForceAll,
}
}

// TODO: consider flipping this to an allowlist
var skipPrefixes = []string{
"google.golang.org/genproto/googleapis/ads/",
"google.golang.org/genproto/googleapis/ai/",
"google.golang.org/genproto/googleapis/analytics/",
"google.golang.org/genproto/googleapis/api/servicecontrol/",
"google.golang.org/genproto/googleapis/api/servicemanagement/",
Expand Down Expand Up @@ -190,7 +189,6 @@ func goPkg(fileName string) (string, error) {
func (g *GenprotoGenerator) protoc(fileNames []string) error {
args := []string{
"--experimental_allow_proto3_optional",
fmt.Sprintf("--go_out=%s/generated", g.genprotoDir),
fmt.Sprintf("--go_out=plugins=grpc:%s/generated", g.genprotoDir),
"-I", g.googleapisDir,
"-I", g.protoSrcDir,
Expand Down

0 comments on commit abfe8dd

Please sign in to comment.