| 1 |
// Copyright 2025 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 |
package bazel |
| 16 |
|
| 17 |
import ( |
| 18 |
"errors" |
| 19 |
"fmt" |
| 20 |
"log/slog" |
| 21 |
"os" |
| 22 |
"path/filepath" |
| 23 |
"regexp" |
| 24 |
"strconv" |
| 25 |
"strings" |
| 26 |
) |
| 27 |
|
| 28 |
// Config holds configuration extracted from the Go rules in a googleapis BUILD.bazel file. |
| 29 |
// The fields are from the Go rules in a API version BUILD.bazel file. |
| 30 |
// E.g., googleapis/google/cloud/asset/v1/BUILD.bazel |
| 31 |
// Note that not all fields are present in every Bazel rule usage. |
| 32 |
type Config struct { |
| 33 |
// The fields below are all from the go_gapic_library rule. |
| 34 |
grpcServiceConfig string |
| 35 |
gapicImportPath string |
| 36 |
metadata bool |
| 37 |
releaseLevel string |
| 38 |
restNumericEnums bool |
| 39 |
serviceYAML string |
| 40 |
transport string |
| 41 |
diregapic bool |
| 42 |
|
| 43 |
// Meta configuration |
| 44 |
// TODO(quartzmo): Remove this field once the googleapis migration from go_proto_library |
| 45 |
// to go_grpc_library is complete. |
| 46 |
// See https://github.com/googleapis/librarian/issues/1021. |
| 47 |
hasGoGRPC bool |
| 48 |
|
| 49 |
// Whether this library has a GAPIC rule at all. |
| 50 |
hasGAPIC bool |
| 51 |
|
| 52 |
// Whether the go_proto_library rule uses @io_bazel_rules_go//proto:go_grpc |
| 53 |
hasLegacyGRPC bool |
| 54 |
} |
| 55 |
|
| 56 |
// HasGAPIC indicates whether the GAPIC generator should be run. |
| 57 |
// This is typically true. If this is false, none of the other GAPIC-related |
| 58 |
// fields should be used. |
| 59 |
func (c *Config) HasGAPIC() bool { return c.hasGAPIC } |
| 60 |
|
| 61 |
// DisableGAPIC overrides any previous configuration, disabling GAPIC generation. |
| 62 |
func (c *Config) DisableGAPIC() { |
| 63 |
c.hasGAPIC = false |
| 64 |
} |
| 65 |
|
| 66 |
// GAPICImportPath is importpath in the go_gapic_library rule. |
| 67 |
// The Go package name is typically appended to the end, separated by a `;`. |
| 68 |
// E.g., cloud.google.com/go/asset/apiv1;asset |
| 69 |
func (c *Config) GAPICImportPath() string { return c.gapicImportPath } |
| 70 |
|
| 71 |
// ServiceYAML is the client config file in the API version directory in googleapis. |
| 72 |
// This is a required input to the GAPIC generator. |
| 73 |
// E.g., googleapis/google/cloud/asset/v1/cloudasset_v1.yaml |
| 74 |
func (c *Config) ServiceYAML() string { return c.serviceYAML } |
| 75 |
|
| 76 |
// GRPCServiceConfig is name of the gRPC service config JSON file. |
| 77 |
// E.g., cloudasset_grpc_service_config.json |
| 78 |
func (c *Config) GRPCServiceConfig() string { return c.grpcServiceConfig } |
| 79 |
|
| 80 |
// Transport is typically one of "grpc", "rest" or "grpc+rest". |
| 81 |
func (c *Config) Transport() string { return c.transport } |
| 82 |
|
| 83 |
// ReleaseLevel is typically one of "beta", "" (same as beta) or "ga". |
| 84 |
// If "ga", gapic-generator-go does not print a warning in the package docs. |
| 85 |
func (c *Config) ReleaseLevel() string { return c.releaseLevel } |
| 86 |
|
| 87 |
// HasMetadata indicates whether a gapic_metadata.json should be generated. |
| 88 |
// This is typically true. |
| 89 |
func (c *Config) HasMetadata() bool { return c.metadata } |
| 90 |
|
| 91 |
// HasDiregapic indicates whether generation uses DIREGAPIC (Discovery REST GAPICs). |
| 92 |
// This is typically false or not present. Used for the GCE (compute) client. |
| 93 |
func (c *Config) HasDiregapic() bool { return c.diregapic } |
| 94 |
|
| 95 |
// HasRESTNumericEnums indicates whether the generated Go REST client should support |
| 96 |
// numeric enums. This is typically true. |
| 97 |
func (c *Config) HasRESTNumericEnums() bool { return c.restNumericEnums } |
| 98 |
|
| 99 |
// HasGoGRPC is meta-configuration that indicates if a go_grpc_library rule is used |
| 100 |
// instead of a go_proto_library in the BUILD.bazel file. This is not part of the |
| 101 |
// BUILD.bazel configuration passed to the GAPIC generator. If true, --go-grpc_out |
| 102 |
// is passed to the protoc command. Will be removed once the googleapis migration |
| 103 |
// from go_proto_library to go_grpc_library is complete and --go-grpc_out is always |
| 104 |
// used. This is trending toward typically true. |
| 105 |
func (c *Config) HasGoGRPC() bool { return c.hasGoGRPC } |
| 106 |
|
| 107 |
// HasLegacyGRPC indicates whether a go_proto_library rule uses |
| 108 |
// @io_bazel_rules_go//proto:go_grpc to generate gRPC code. If so, |
| 109 |
// the "plugins=grpc" option is passed to the legacy Go plugin. |
| 110 |
func (c *Config) HasLegacyGRPC() bool { return c.hasLegacyGRPC } |
| 111 |
|
| 112 |
// Validate ensures that the configuration is valid. |
| 113 |
func (c *Config) Validate() error { |
| 114 |
if c.hasGAPIC { |
| 115 |
if c.gapicImportPath == "" { |
| 116 |
return errors.New("librariangen: gapicImportPath is not set") |
| 117 |
} |
| 118 |
if c.serviceYAML == "" { |
| 119 |
return errors.New("librariangen: serviceYAML is not set") |
| 120 |
} |
| 121 |
} |
| 122 |
return nil |
| 123 |
} |
| 124 |
|
| 125 |
// Parse reads a BUILD.bazel file from the given directory and extracts the |
| 126 |
// relevant configuration from the go_gapic_library and go_proto_library rules. |
| 127 |
func Parse(dir string) (*Config, error) { |
| 128 |
c := &Config{} |
| 129 |
fp := filepath.Join(dir, "BUILD.bazel") |
| 130 |
data, err := os.ReadFile(fp) |
| 131 |
if err != nil { |
| 132 |
return nil, fmt.Errorf("librariangen: failed to read BUILD.bazel file %s: %w", fp, err) |
| 133 |
} |
| 134 |
content := string(data) |
| 135 |
|
| 136 |
// First, find the go_gapic_library block. |
| 137 |
re := regexp.MustCompile(`go_gapic_library\((?s:.)*?\)`) |
| 138 |
gapicLibraryBlock := re.FindString(content) |
| 139 |
if gapicLibraryBlock != "" { |
| 140 |
// GAPIC build target |
| 141 |
c.hasGAPIC = true |
| 142 |
c.grpcServiceConfig = findString(gapicLibraryBlock, "grpc_service_config") |
| 143 |
c.gapicImportPath = findString(gapicLibraryBlock, "importpath") |
| 144 |
c.releaseLevel = findString(gapicLibraryBlock, "release_level") |
| 145 |
// If the service config is actually a bazel target instead of a file, just assume there's a file with the same name. |
| 146 |
c.serviceYAML = strings.TrimPrefix(findString(gapicLibraryBlock, "service_yaml"), ":") |
| 147 |
c.transport = findString(gapicLibraryBlock, "transport") |
| 148 |
if c.metadata, err = findBool(gapicLibraryBlock, "metadata"); err != nil { |
| 149 |
return nil, fmt.Errorf("librariangen: failed to parse BUILD.bazel file %s: %w", fp, err) |
| 150 |
} |
| 151 |
if c.restNumericEnums, err = findBool(gapicLibraryBlock, "rest_numeric_enums"); err != nil { |
| 152 |
return nil, fmt.Errorf("librariangen: failed to parse BUILD.bazel file %s: %w", fp, err) |
| 153 |
} |
| 154 |
if c.diregapic, err = findBool(gapicLibraryBlock, "diregapic"); err != nil { |
| 155 |
return nil, fmt.Errorf("librariangen: failed to parse BUILD.bazel file %s: %w", fp, err) |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
// We are currently migrating go_proto_library to go_grpc_library. |
| 160 |
// Only one is expect to be present |
| 161 |
if strings.Contains(content, "go_grpc_library") { |
| 162 |
c.hasGoGRPC = true |
| 163 |
} |
| 164 |
goProtoLibraryPattern := regexp.MustCompile(`go_proto_library\((?s:.)*?\)`) |
| 165 |
goProtoLibraryBlock := goProtoLibraryPattern.FindString(content) |
| 166 |
if goProtoLibraryBlock != "" { |
| 167 |
if c.hasGoGRPC { |
| 168 |
return nil, fmt.Errorf("librariangen: misconfiguration in BUILD.bazel file, only one of go_grpc_library and go_proto_library rules should be present: %s", fp) |
| 169 |
} |
| 170 |
c.hasLegacyGRPC = strings.Contains(goProtoLibraryBlock, "@io_bazel_rules_go//proto:go_grpc") |
| 171 |
} |
| 172 |
if err := c.Validate(); err != nil { |
| 173 |
return nil, fmt.Errorf("librariangen: invalid bazel config in %s: %w", dir, err) |
| 174 |
} |
| 175 |
slog.Debug("librariangen: bazel config loaded", "conf", fmt.Sprintf("%+v", c)) |
| 176 |
return c, nil |
| 177 |
} |
| 178 |
|
| 179 |
func findString(content, name string) string { |
| 180 |
re := regexp.MustCompile(fmt.Sprintf(`%s\s*=\s*"([^"]+)"`, name)) |
| 181 |
if match := re.FindStringSubmatch(content); len(match) > 1 { |
| 182 |
return match[1] |
| 183 |
} |
| 184 |
slog.Debug("librariangen: failed to find string attr in BUILD.bazel", "name", name) |
| 185 |
return "" |
| 186 |
} |
| 187 |
|
| 188 |
func findBool(content, name string) (bool, error) { |
| 189 |
re := regexp.MustCompile(fmt.Sprintf(`%s\s*=\s*(\w+)`, name)) |
| 190 |
if match := re.FindStringSubmatch(content); len(match) > 1 { |
| 191 |
if b, err := strconv.ParseBool(match[1]); err == nil { |
| 192 |
return b, nil |
| 193 |
} |
| 194 |
return false, fmt.Errorf("librariangen: failed to parse bool attr in BUILD.bazel: %q, got: %q", name, match[1]) |
| 195 |
} |
| 196 |
slog.Debug("librariangen: failed to find bool attr in BUILD.bazel", "name", name) |
| 197 |
return false, nil |
| 198 |
} |