Debian Codegraph @

internal/gapicgen/generator/generator.go

raw
1 // Copyright 2019 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 //go:build !windows
16 // +build !windows
17
18 // Package generator provides tools for generating clients.
19 package generator
20
21 import (
22 "context"
23 "fmt"
24 "os"
25 "path/filepath"
26
27 "cloud.google.com/go/internal/gapicgen/git"
28 )
29
30 // Config contains inputs needed to generate sources.
31 type Config struct {
32 GoogleapisDir string
33 GenprotoDir string
34 ProtoDir string
35 LocalMode bool
36 RegenOnly bool
37 ForceAll bool
38 GenAlias bool
39 }
40
41 // Generate generates genproto and gapics.
42 func Generate(ctx context.Context, conf *Config) ([]*git.ChangeInfo, error) {
43 protoGenerator := NewGenprotoGenerator(conf)
44 if err := protoGenerator.Regen(ctx); err != nil {
45 return nil, fmt.Errorf("error generating genproto (may need to check logs for more errors): %w", err)
46 }
47
48 var changes []*git.ChangeInfo
49 if !conf.LocalMode {
50 var err error
51 changes, err = gatherChanges(conf.GoogleapisDir, conf.GenprotoDir)
52 if err != nil {
53 return nil, fmt.Errorf("error gathering commit info")
54 }
55 if err := recordGoogleapisHash(conf.GoogleapisDir, conf.GenprotoDir); err != nil {
56 return nil, err
57 }
58 }
59 return changes, nil
60 }
61
62 func gatherChanges(googleapisDir, genprotoDir string) ([]*git.ChangeInfo, error) {
63 // Get the last processed googleapis hash.
64 lastHash, err := os.ReadFile(filepath.Join(genprotoDir, "regen.txt"))
65 if err != nil {
66 return nil, err
67 }
68 commits, err := git.CommitsSinceHash(googleapisDir, string(lastHash), false)
69 if err != nil {
70 return nil, err
71 }
72 changes, err := git.ParseChangeInfo(googleapisDir, commits)
73 if err != nil {
74 return nil, err
75 }
76
77 return changes, nil
78 }
79
80 // recordGoogleapisHash parses the latest commit in googleapis and records it to
81 // regen.txt in go-genproto.
82 func recordGoogleapisHash(googleapisDir, genprotoDir string) error {
83 commits, err := git.CommitsSinceHash(googleapisDir, "HEAD", true)
84 if err != nil {
85 return err
86 }
87 if len(commits) != 1 {
88 return fmt.Errorf("only expected one commit, got %d", len(commits))
89 }
90
91 f, err := os.OpenFile(filepath.Join(genprotoDir, "regen.txt"), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
92 if err != nil {
93 return err
94 }
95 defer f.Close()
96 if _, err := f.WriteString(commits[0]); err != nil {
97 return err
98 }
99 return nil
100 }