| 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 config |
| 16 |
|
| 17 |
import ( |
| 18 |
"fmt" |
| 19 |
"os" |
| 20 |
"path/filepath" |
| 21 |
"slices" |
| 22 |
"strings" |
| 23 |
|
| 24 |
"gopkg.in/yaml.v3" |
| 25 |
) |
| 26 |
|
| 27 |
// GeneratorInputDir is the name of the directory containing the repo configuration |
| 28 |
// file, relative to the .librarian directory. |
| 29 |
const GeneratorInputDir string = "generator-input" |
| 30 |
|
| 31 |
// RepoConfigFile is the name of the repo configuration file, relative to |
| 32 |
// GeneratorInputDir. |
| 33 |
const RepoConfigFile string = "repo-config.yaml" |
| 34 |
|
| 35 |
// RepoConfig is the configuration for all modules in the repository. |
| 36 |
type RepoConfig struct { |
| 37 |
// Modules is the list of all the modules in the repository which need overrides. |
| 38 |
Modules []*ModuleConfig `yaml:"modules"` |
| 39 |
} |
| 40 |
|
| 41 |
// ModuleConfig is the configuration for a single module. |
| 42 |
type ModuleConfig struct { |
| 43 |
// Name is the top-level name of the module, matching the top-level |
| 44 |
// subdirectory. |
| 45 |
Name string `yaml:"name"` |
| 46 |
// ModulePathVersion is the major version for the overall module, e.g. "v2" |
| 47 |
// to create a module path of cloud.google.com/go/{Name}/v2 |
| 48 |
ModulePathVersion string `yaml:"module_path_version"` |
| 49 |
// APIs is the list of APIs within this module (that need overrides). |
| 50 |
APIs []*APIConfig `yaml:"apis"` |
| 51 |
} |
| 52 |
|
| 53 |
// APIConfig provides per-API configuration to override defaults, |
| 54 |
// e.g. for snippet metadata locations. |
| 55 |
type APIConfig struct { |
| 56 |
// Path is the Path within googleapis, e.g. "google/cloud/functions/v2" |
| 57 |
Path string `yaml:"path"` |
| 58 |
// ProtoPackage is the protobuf package, when it doesn't match the Path |
| 59 |
// (after replacing slash with period). |
| 60 |
ProtoPackage string `yaml:"proto_package"` |
| 61 |
// ClientDirectory is the directory containing the client code, relative to the module root. |
| 62 |
// (This is currently only used to find snippet metadata files.) |
| 63 |
ClientDirectory string `yaml:"client_directory"` |
| 64 |
// DisableGAPIC is a flag to disable GAPIC generation for an API, overriding |
| 65 |
// settings from the BUILD.bazel file. |
| 66 |
DisableGAPIC bool `yaml:"disable_gapic"` |
| 67 |
// ModuleName is the name of the module this API config belongs to. |
| 68 |
// This is only exported for ease of testing, and is not expected to be |
| 69 |
// present in the YAML file. It is populated when the APIConfig is returned |
| 70 |
// from GetAPIConfig(). |
| 71 |
ModuleName string |
| 72 |
} |
| 73 |
|
| 74 |
// LoadRepoConfig loads the repository configuration with module-specific overrides, |
| 75 |
// from a file derived from the .librarian directory (specified as librarianDir). |
| 76 |
func LoadRepoConfig(librarianDir string) (*RepoConfig, error) { |
| 77 |
var config RepoConfig |
| 78 |
b, err := os.ReadFile(filepath.Join(librarianDir, GeneratorInputDir, RepoConfigFile)) |
| 79 |
if err != nil { |
| 80 |
return nil, err |
| 81 |
} |
| 82 |
if err := yaml.Unmarshal(b, &config); err != nil { |
| 83 |
return nil, err |
| 84 |
} |
| 85 |
return &config, nil |
| 86 |
} |
| 87 |
|
| 88 |
// GetModuleConfig returns the configuration for the named module |
| 89 |
// (top-level directory). If no module-specific configuration is found, |
| 90 |
// an empty configuration (with the right name) is returned. |
| 91 |
func (rc *RepoConfig) GetModuleConfig(name string) *ModuleConfig { |
| 92 |
for _, moduleConfig := range rc.Modules { |
| 93 |
if moduleConfig.Name == name { |
| 94 |
return moduleConfig |
| 95 |
} |
| 96 |
} |
| 97 |
return &ModuleConfig{ |
| 98 |
Name: name, |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
// GetAPIConfig returns the configuration for the API identified by |
| 103 |
// its path within googleapis (e.g. "google/cloud/functions/v2"). |
| 104 |
// If no API-specific configuration is found, an empty configuration |
| 105 |
// (with the right name) is returned. |
| 106 |
func (mc *ModuleConfig) GetAPIConfig(path string) *APIConfig { |
| 107 |
for _, apiConfig := range mc.APIs { |
| 108 |
if apiConfig.Path == path { |
| 109 |
apiConfig.ModuleName = mc.Name |
| 110 |
return apiConfig |
| 111 |
} |
| 112 |
} |
| 113 |
return &APIConfig{ |
| 114 |
Path: path, |
| 115 |
ModuleName: mc.Name, |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
// GetModulePath returns the module path for the module, applying |
| 120 |
// any configured version. |
| 121 |
func (mc *ModuleConfig) GetModulePath() string { |
| 122 |
prefix := "cloud.google.com/go/" + mc.Name |
| 123 |
if mc.ModulePathVersion != "" { |
| 124 |
return prefix + "/" + mc.ModulePathVersion |
| 125 |
} |
| 126 |
|
| 127 |
// No override: assume implicit v1. |
| 128 |
return prefix |
| 129 |
} |
| 130 |
|
| 131 |
// GetProtoPackage returns the protobuf package for the API config, |
| 132 |
// which is derived from the path unless overridden. |
| 133 |
func (ac *APIConfig) GetProtoPackage() string { |
| 134 |
if ac.ProtoPackage != "" { |
| 135 |
return ac.ProtoPackage |
| 136 |
} |
| 137 |
|
| 138 |
// No override: derive the value. |
| 139 |
return strings.ReplaceAll(ac.Path, "/", ".") |
| 140 |
} |
| 141 |
|
| 142 |
// GetClientDirectory returns the directory for the clients of this |
| 143 |
// API, relative to the module root. |
| 144 |
func (ac *APIConfig) GetClientDirectory() (string, error) { |
| 145 |
if ac.ClientDirectory != "" { |
| 146 |
return ac.ClientDirectory, nil |
| 147 |
} |
| 148 |
|
| 149 |
// No override: derive the value. |
| 150 |
startOfModuleName := strings.Index(ac.Path, ac.ModuleName+"/") |
| 151 |
if startOfModuleName == -1 { |
| 152 |
return "", fmt.Errorf("librariangen: unexpected API path format: %s", ac.Path) |
| 153 |
} |
| 154 |
|
| 155 |
// google/spanner/v1 => ["google", "spanner", "v1"] |
| 156 |
// google/spanner/admin/instance/v1 => ["google", "spanner", "admin", "instance", "v1"] |
| 157 |
parts := strings.Split(ac.Path, "/") |
| 158 |
moduleIndex := slices.Index(parts, ac.ModuleName) |
| 159 |
if moduleIndex == -1 { |
| 160 |
return "", fmt.Errorf("librariangen: module name '%s' not found in API path '%s'", ac.ModuleName, ac.Path) |
| 161 |
} |
| 162 |
|
| 163 |
// Remove everything up to and include the module name. |
| 164 |
// google/spanner/v1 => ["v1"] |
| 165 |
// google/spanner/admin/instance/v1 => ["admin", "instance", "v1"] |
| 166 |
parts = parts[moduleIndex+1:] |
| 167 |
parts[len(parts)-1] = "api" + parts[len(parts)-1] |
| 168 |
return strings.Join(parts, "/"), nil |
| 169 |
} |
| 170 |
|
| 171 |
// HasDisableGAPIC returns a value saying whether GAPIC generation is explicitly |
| 172 |
// disabled for this module. |
| 173 |
func (ac *APIConfig) HasDisableGAPIC() bool { |
| 174 |
return ac.DisableGAPIC |
| 175 |
} |