|
| 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 | + "sync" |
| 27 | +) |
| 28 | + |
| 29 | +// Config holds configuration extracted from a googleapis BUILD.bazel file. |
| 30 | +type Config struct { |
| 31 | + gapicYAML string |
| 32 | + grpcServiceConfig string |
| 33 | + restNumericEnums bool |
| 34 | + serviceYAML string |
| 35 | + transport string |
| 36 | + hasGAPIC bool |
| 37 | +} |
| 38 | + |
| 39 | +// HasGAPIC indicates whether the GAPIC generator should be run. |
| 40 | +func (c *Config) HasGAPIC() bool { return c.hasGAPIC } |
| 41 | + |
| 42 | +// GapicYAML is the GAPIC config file in the API version directory in googleapis. |
| 43 | +func (c *Config) GapicYAML() string { return c.gapicYAML } |
| 44 | + |
| 45 | +// ServiceYAML is the client config file in the API version directory in googleapis. |
| 46 | +func (c *Config) ServiceYAML() string { return c.serviceYAML } |
| 47 | + |
| 48 | +// GRPCServiceConfig is name of the gRPC service config JSON file. |
| 49 | +func (c *Config) GRPCServiceConfig() string { return c.grpcServiceConfig } |
| 50 | + |
| 51 | +// Transport is typically one of "grpc", "rest" or "grpc+rest". |
| 52 | +func (c *Config) Transport() string { return c.transport } |
| 53 | + |
| 54 | +// HasRESTNumericEnums indicates whether the generated client should support numeric enums. |
| 55 | +func (c *Config) HasRESTNumericEnums() bool { return c.restNumericEnums } |
| 56 | + |
| 57 | +// Validate ensures that the configuration is valid. |
| 58 | +func (c *Config) Validate() error { |
| 59 | + if c.hasGAPIC { |
| 60 | + if c.serviceYAML == "" { |
| 61 | + return errors.New("librariangen: serviceYAML is not set") |
| 62 | + } |
| 63 | + } |
| 64 | + return nil |
| 65 | +} |
| 66 | + |
| 67 | +var javaGapicLibraryRE = regexp.MustCompile(`java_gapic_library\((?s:.)*?\)`) |
| 68 | + |
| 69 | +// Parse reads a BUILD.bazel file from the given directory and extracts the |
| 70 | +// relevant configuration from the java_gapic_library rule. |
| 71 | +func Parse(dir string) (*Config, error) { |
| 72 | + c := &Config{} |
| 73 | + fp := filepath.Join(dir, "BUILD.bazel") |
| 74 | + data, err := os.ReadFile(fp) |
| 75 | + if err != nil { |
| 76 | + return nil, fmt.Errorf("librariangen: failed to read BUILD.bazel file %s: %w", fp, err) |
| 77 | + } |
| 78 | + content := string(data) |
| 79 | + |
| 80 | + gapicLibraryBlock := javaGapicLibraryRE.FindString(content) |
| 81 | + if gapicLibraryBlock != "" { |
| 82 | + c.hasGAPIC = true |
| 83 | + c.grpcServiceConfig = findString(gapicLibraryBlock, "grpc_service_config") |
| 84 | + c.serviceYAML = strings.TrimPrefix(findString(gapicLibraryBlock, "service_yaml"), ":") |
| 85 | + c.transport = findString(gapicLibraryBlock, "transport") |
| 86 | + if c.restNumericEnums, err = findBool(gapicLibraryBlock, "rest_numeric_enums"); err != nil { |
| 87 | + return nil, fmt.Errorf("librariangen: failed to parse BUILD.bazel file %s: %w", fp, err) |
| 88 | + } |
| 89 | + c.gapicYAML = strings.TrimPrefix(findString(gapicLibraryBlock, "gapic_yaml"), ":") |
| 90 | + } |
| 91 | + if err := c.Validate(); err != nil { |
| 92 | + return nil, fmt.Errorf("librariangen: invalid bazel config in %s: %w", dir, err) |
| 93 | + } |
| 94 | + slog.Debug("librariangen: bazel config loaded", "conf", c) |
| 95 | + return c, nil |
| 96 | +} |
| 97 | + |
| 98 | +var reCache = &sync.Map{} |
| 99 | + |
| 100 | +func getRegexp(key, pattern string) *regexp.Regexp { |
| 101 | + val, ok := reCache.Load(key) |
| 102 | + if !ok { |
| 103 | + val = regexp.MustCompile(pattern) |
| 104 | + reCache.Store(key, val) |
| 105 | + } |
| 106 | + return val.(*regexp.Regexp) |
| 107 | +} |
| 108 | + |
| 109 | +func findString(content, name string) string { |
| 110 | + re := getRegexp("findString_"+name, fmt.Sprintf(`%s\s*=\s*(?:"([^"]+)"|'([^']+)'){1}`, name)) |
| 111 | + match := re.FindStringSubmatch(content) |
| 112 | + if len(match) > 2 { |
| 113 | + if match[1] != "" { |
| 114 | + return match[1] // Double-quoted |
| 115 | + } |
| 116 | + return match[2] // Single-quoted |
| 117 | + } |
| 118 | + slog.Debug("librariangen: failed to find string attr in BUILD.bazel", "name", name) |
| 119 | + return "" |
| 120 | +} |
| 121 | + |
| 122 | +func findBool(content, name string) (bool, error) { |
| 123 | + re := getRegexp("findBool_"+name, fmt.Sprintf(`%s\s*=\s*(\w+)`, name)) |
| 124 | + if match := re.FindStringSubmatch(content); len(match) > 1 { |
| 125 | + if b, err := strconv.ParseBool(match[1]); err == nil { |
| 126 | + return b, nil |
| 127 | + } |
| 128 | + return false, fmt.Errorf("librariangen: failed to parse bool attr in BUILD.bazel: %q, got: %q", name, match[1]) |
| 129 | + } |
| 130 | + slog.Debug("librariangen: failed to find bool attr in BUILD.bazel", "name", name) |
| 131 | + return false, nil |
| 132 | +} |
0 commit comments