|
| 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 main |
| 16 | + |
| 17 | +import ( |
| 18 | + "errors" |
| 19 | + "fmt" |
| 20 | + "log/slog" |
| 21 | + "os" |
| 22 | + "path/filepath" |
| 23 | + "strings" |
| 24 | +) |
| 25 | + |
| 26 | +// main is the entrypoint for the stategen tool, which updates a Librarian state.yaml |
| 27 | +// file to include the specified modules. The first argument is a path to the repository |
| 28 | +// root; all subsequent arguments are module names. |
| 29 | +func main() { |
| 30 | + logLevel := slog.LevelInfo |
| 31 | + if os.Getenv("GOOGLE_SDK_GO_LOGGING_LEVEL") == "debug" { |
| 32 | + logLevel = slog.LevelDebug |
| 33 | + } |
| 34 | + slog.SetDefault(slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{ |
| 35 | + Level: logLevel, |
| 36 | + }))) |
| 37 | + slog.Info("stategen: invoked", "args", os.Args) |
| 38 | + if err := run(os.Args[1:]); err != nil { |
| 39 | + slog.Error("stategen: failed", "error", err) |
| 40 | + os.Exit(1) |
| 41 | + } |
| 42 | + slog.Info("stategen: finished successfully") |
| 43 | +} |
| 44 | + |
| 45 | +func run(args []string) error { |
| 46 | + if len(args) < 2 { |
| 47 | + return errors.New("stategen: expected a root directory and at least one module") |
| 48 | + } |
| 49 | + repoRoot := args[0] |
| 50 | + stateFilePath := filepath.Join(repoRoot, ".librarian/state.yaml") |
| 51 | + state, err := parseLibrarianState(stateFilePath) |
| 52 | + if err != nil { |
| 53 | + return err |
| 54 | + } |
| 55 | + for _, moduleName := range args[1:] { |
| 56 | + if stateContainsModule(state, moduleName) { |
| 57 | + slog.Info("skipping existing module", "module", moduleName) |
| 58 | + continue |
| 59 | + } |
| 60 | + if err := addModule(repoRoot, state, moduleName); err != nil { |
| 61 | + return err |
| 62 | + } |
| 63 | + } |
| 64 | + return saveLibrarianState(stateFilePath, state) |
| 65 | +} |
| 66 | + |
| 67 | +func stateContainsModule(state *LibrarianState, moduleName string) bool { |
| 68 | + for _, library := range state.Libraries { |
| 69 | + if library.ID == moduleName { |
| 70 | + return true |
| 71 | + } |
| 72 | + } |
| 73 | + return false |
| 74 | +} |
| 75 | + |
| 76 | +func addModule(repoRoot string, state *LibrarianState, moduleName string) error { |
| 77 | + slog.Info("adding module", "module", moduleName) |
| 78 | + moduleRoot := filepath.Join(repoRoot, moduleName) |
| 79 | + |
| 80 | + // Start off with the basics which need |
| 81 | + library := &LibraryState{ |
| 82 | + ID: moduleName, |
| 83 | + SourceRoots: []string{ |
| 84 | + moduleName, |
| 85 | + "internal/generated/snippets/" + moduleName, |
| 86 | + }, |
| 87 | + RemoveRegex: []string{ |
| 88 | + moduleName + "/README\\.md", |
| 89 | + moduleName + "/go\\.mod", |
| 90 | + moduleName + "/go\\.sum", |
| 91 | + moduleName + "/internal/version\\.go", |
| 92 | + "internal/generated/snippets/" + moduleName, |
| 93 | + }, |
| 94 | + } |
| 95 | + |
| 96 | + version, err := loadVersion(moduleRoot) |
| 97 | + if err != nil { |
| 98 | + return err |
| 99 | + } |
| 100 | + library.Version = version |
| 101 | + |
| 102 | + if err := addAPIProtoPaths(repoRoot, moduleName, library); err != nil { |
| 103 | + return err |
| 104 | + } |
| 105 | + |
| 106 | + if err := addGeneratedCodeRemovals(repoRoot, moduleRoot, library); err != nil { |
| 107 | + return err |
| 108 | + } |
| 109 | + |
| 110 | + state.Libraries = append(state.Libraries, library) |
| 111 | + return nil |
| 112 | +} |
| 113 | + |
| 114 | +// addAPIProtoPaths walks the generates snippets directory to find the API proto paths for the library. |
| 115 | +func addAPIProtoPaths(repoRoot, moduleName string, library *LibraryState) error { |
| 116 | + return filepath.WalkDir(filepath.Join(repoRoot, "internal/generated/snippets/"+moduleName), func(path string, d os.DirEntry, err error) error { |
| 117 | + if err != nil { |
| 118 | + return err |
| 119 | + } |
| 120 | + if d.IsDir() { |
| 121 | + return nil |
| 122 | + } |
| 123 | + if match, _ := filepath.Match("snippet_metadata.*.json", d.Name()); match { |
| 124 | + parts := strings.Split(d.Name(), ".") |
| 125 | + parts = parts[1 : len(parts)-1] |
| 126 | + api := &API{ |
| 127 | + Path: strings.Join(parts, "/"), |
| 128 | + } |
| 129 | + library.APIs = append(library.APIs, api) |
| 130 | + } |
| 131 | + return nil |
| 132 | + }) |
| 133 | +} |
| 134 | + |
| 135 | +// addApiPaths walk the module source directory to find the files to remove. |
| 136 | +func addGeneratedCodeRemovals(repoRoot, moduleRoot string, library *LibraryState) error { |
| 137 | + return filepath.WalkDir(moduleRoot, func(path string, d os.DirEntry, err error) error { |
| 138 | + if err != nil { |
| 139 | + return err |
| 140 | + } |
| 141 | + if !d.IsDir() { |
| 142 | + return nil |
| 143 | + } |
| 144 | + if !strings.HasPrefix(d.Name(), "apiv") { |
| 145 | + return nil |
| 146 | + } |
| 147 | + repoRelativePath, err := filepath.Rel(repoRoot, path) |
| 148 | + if err != nil { |
| 149 | + return err |
| 150 | + } |
| 151 | + apiParts := strings.Split(path, "/") |
| 152 | + protobufDir := apiParts[len(apiParts)-2] + "pb" |
| 153 | + generatedPaths := []string{ |
| 154 | + "[^/]*_client\\.go", |
| 155 | + "[^/]*_client_example_go123_test\\.go", |
| 156 | + "[^/]*_client_example_test\\.go", |
| 157 | + "auxiliary\\.go", |
| 158 | + "auxiliary_go123\\.go", |
| 159 | + "doc\\.go", |
| 160 | + "gapic_metadata\\.json", |
| 161 | + "helpers\\.go", |
| 162 | + "version\\.go", |
| 163 | + protobufDir, |
| 164 | + } |
| 165 | + for _, generatedPath := range generatedPaths { |
| 166 | + library.RemoveRegex = append(library.RemoveRegex, repoRelativePath+"/"+generatedPath) |
| 167 | + } |
| 168 | + return nil |
| 169 | + }) |
| 170 | +} |
| 171 | + |
| 172 | +func loadVersion(moduleRoot string) (string, error) { |
| 173 | + // Load internal/version.go to figure out the existing version. |
| 174 | + versionPath := filepath.Join(moduleRoot, "internal/version.go") |
| 175 | + versionBytes, err := os.ReadFile(versionPath) |
| 176 | + if err != nil { |
| 177 | + return "", err |
| 178 | + } |
| 179 | + lines := strings.Split(string(versionBytes), "\n") |
| 180 | + lastLine := lines[len(lines)-1] |
| 181 | + // If the actual last line is empty, use the previous one instead. |
| 182 | + if lastLine == "" { |
| 183 | + lastLine = lines[len(lines)-2] |
| 184 | + } |
| 185 | + if !strings.HasPrefix(lastLine, "const Version") { |
| 186 | + return "", fmt.Errorf("stategen: version file not in expected format for module: %s; %s", versionPath, lastLine) |
| 187 | + } |
| 188 | + |
| 189 | + versionParts := strings.Split(lastLine, "\"") |
| 190 | + if len(versionParts) != 3 { |
| 191 | + return "", fmt.Errorf("stategen: last line of version file not in expected format for module: %s", versionPath) |
| 192 | + } |
| 193 | + return versionParts[1], nil |
| 194 | +} |
0 commit comments