Skip to content

Commit 627b4d7

Browse files
authored
feat(devtools/cmd/migrate-librarian): migration go-specific configuration (#3215)
In this PR: - Add Go specific configurations - Modify migrate-librarian to parse go specific configurations in google-cloud-go. Fixes #3116
1 parent 8023558 commit 627b4d7

File tree

7 files changed

+334
-14
lines changed

7 files changed

+334
-14
lines changed

devtools/cmd/migrate-librarian/main.go

Lines changed: 100 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"log"
2525
"os"
2626
"path/filepath"
27+
"reflect"
2728
"sort"
2829
"strings"
2930

@@ -51,6 +52,36 @@ var (
5152
fetchSource = fetchGoogleapis
5253
)
5354

55+
// RepoConfig represents the .librarian/generator-input/repo-config.yaml file in google-cloud-go repository.
56+
type RepoConfig struct {
57+
Modules []*RepoConfigModule `yaml:"modules"`
58+
}
59+
60+
// RepoConfigModule represents a module in repo-config.yaml.
61+
type RepoConfigModule struct {
62+
Name string `yaml:"name"`
63+
ModulePathVersion string `yaml:"module_path_version,omitempty"`
64+
DeleteGenerationOutputPaths []string `yaml:"delete_generation_output_paths,omitempty"`
65+
APIs []*RepoConfigAPI `yaml:"apis,omitempty"`
66+
}
67+
68+
// RepoConfigAPI represents an API in repo-config.yaml.
69+
type RepoConfigAPI struct {
70+
Path string `yaml:"path"`
71+
ClientDirectory string `yaml:"client_directory,omitempty"`
72+
DisableGAPIC bool `yaml:"disable_gapic,omitempty"`
73+
NestedProtos []string `yaml:"nested_protos,omitempty"`
74+
ProtoPackage string `yaml:"proto_package,omitempty"`
75+
}
76+
77+
// MigrationInput holds all intermediate configuration and state necessary for migration from legacy files.
78+
type MigrationInput struct {
79+
librarianState *legacyconfig.LibrarianState
80+
librarianConfig *legacyconfig.LibrarianConfig
81+
repoConfig *RepoConfig
82+
lang string
83+
}
84+
5485
func main() {
5586
ctx := context.Background()
5687
if err := run(ctx, os.Args[1:]); err != nil {
@@ -84,7 +115,18 @@ func run(ctx context.Context, args []string) error {
84115
return err
85116
}
86117

87-
cfg, err := buildConfig(ctx, librarianState, librarianConfig, language)
118+
repoConfig, err := readRepoConfig(repoPath)
119+
if err != nil {
120+
return err
121+
}
122+
123+
cfg, err := buildConfig(ctx, &MigrationInput{
124+
librarianState: librarianState,
125+
librarianConfig: librarianConfig,
126+
repoConfig: repoConfig,
127+
lang: language,
128+
})
129+
88130
if err != nil {
89131
return err
90132
}
@@ -114,11 +156,9 @@ func deriveLanguage(repoPath string) (string, error) {
114156

115157
func buildConfig(
116158
ctx context.Context,
117-
librarianState *legacyconfig.LibrarianState,
118-
librarianConfig *legacyconfig.LibrarianConfig,
119-
lang string) (*config.Config, error) {
159+
input *MigrationInput) (*config.Config, error) {
120160
repo := "googleapis/google-cloud-go"
121-
if lang == "python" {
161+
if input.lang == "python" {
122162
repo = "googleapis/google-cloud-python"
123163
}
124164

@@ -128,7 +168,7 @@ func buildConfig(
128168
}
129169

130170
cfg := &config.Config{
131-
Language: lang,
171+
Language: input.lang,
132172
Repo: repo,
133173
Sources: &config.Sources{
134174
Googleapis: src,
@@ -138,7 +178,7 @@ func buildConfig(
138178
},
139179
}
140180

141-
cfg.Libraries = buildLibraries(librarianState, librarianConfig)
181+
cfg.Libraries = buildLibraries(input)
142182

143183
return cfg, nil
144184
}
@@ -169,21 +209,29 @@ func fetchGoogleapis(ctx context.Context) (*config.Source, error) {
169209
}, nil
170210
}
171211

172-
func buildLibraries(
173-
librarianState *legacyconfig.LibrarianState,
174-
librarianConfig *legacyconfig.LibrarianConfig) []*config.Library {
212+
func buildLibraries(input *MigrationInput) []*config.Library {
175213
var libraries []*config.Library
176214
idToLibraryState := sliceToMap[legacyconfig.LibraryState](
177-
librarianState.Libraries,
215+
input.librarianState.Libraries,
178216
func(lib *legacyconfig.LibraryState) string {
179217
return lib.ID
180218
})
219+
181220
idToLibraryConfig := sliceToMap[legacyconfig.LibraryConfig](
182-
librarianConfig.Libraries,
221+
input.librarianConfig.Libraries,
183222
func(lib *legacyconfig.LibraryConfig) string {
184223
return lib.LibraryID
185224
})
186225

226+
idToGoModule := make(map[string]*RepoConfigModule)
227+
if input.repoConfig != nil {
228+
idToGoModule = sliceToMap[RepoConfigModule](
229+
input.repoConfig.Modules,
230+
func(mod *RepoConfigModule) string {
231+
return mod.Name
232+
})
233+
}
234+
187235
// Iterate libraries from idToLibraryState because librarianConfig.Libraries is a
188236
// subset of librarianState.Libraries.
189237
for id, libState := range idToLibraryState {
@@ -201,6 +249,30 @@ func buildLibraries(
201249
library.SkipRelease = libCfg.ReleaseBlocked
202250
}
203251

252+
libGoModule, ok := idToGoModule[id]
253+
if ok {
254+
var goAPIs []*config.GoAPI
255+
for _, api := range libGoModule.APIs {
256+
goAPIs = append(goAPIs, &config.GoAPI{
257+
Path: api.Path,
258+
ClientDirectory: api.ClientDirectory,
259+
DisableGAPIC: api.DisableGAPIC,
260+
NestedProtos: api.NestedProtos,
261+
ProtoPackage: api.ProtoPackage,
262+
})
263+
}
264+
265+
goModule := &config.GoModule{
266+
DeleteGenerationOutputPaths: libGoModule.DeleteGenerationOutputPaths,
267+
GoAPIs: goAPIs,
268+
ModulePathVersion: libGoModule.ModulePathVersion,
269+
}
270+
271+
if !isEmptyGoModule(goModule) {
272+
library.Go = goModule
273+
}
274+
}
275+
204276
libraries = append(libraries, library)
205277
}
206278

@@ -232,6 +304,10 @@ func toChannels(apis []*legacyconfig.API) []*config.Channel {
232304
return channels
233305
}
234306

307+
func isEmptyGoModule(mod *config.GoModule) bool {
308+
return reflect.DeepEqual(mod, &config.GoModule{})
309+
}
310+
235311
func readState(path string) (*legacyconfig.LibrarianState, error) {
236312
stateFile := filepath.Join(path, librarianDir, librarianStateFile)
237313
return yaml.Read[legacyconfig.LibrarianState](stateFile)
@@ -241,3 +317,15 @@ func readConfig(path string) (*legacyconfig.LibrarianConfig, error) {
241317
configFile := filepath.Join(path, librarianDir, librarianConfigFile)
242318
return yaml.Read[legacyconfig.LibrarianConfig](configFile)
243319
}
320+
321+
func readRepoConfig(path string) (*RepoConfig, error) {
322+
configFile := filepath.Join(path, librarianDir, "generator-input/repo-config.yaml")
323+
if _, err := os.Stat(configFile); err != nil {
324+
if os.IsNotExist(err) {
325+
return nil, nil
326+
}
327+
return nil, err
328+
}
329+
330+
return yaml.Read[RepoConfig](configFile)
331+
}

0 commit comments

Comments
 (0)