@@ -16,13 +16,45 @@ package rust
1616
1717import (
1818 "fmt"
19+ "os"
20+ "path/filepath"
1921 "strings"
2022
2123 "github.com/googleapis/librarian/internal/config"
2224 "github.com/googleapis/librarian/internal/serviceconfig"
2325 sidekickconfig "github.com/googleapis/librarian/internal/sidekick/config"
2426)
2527
28+ type rootMapEntry struct {
29+ path string
30+ key string
31+ }
32+
33+ func newRootMap (sources * Sources ) map [string ]rootMapEntry {
34+ return map [string ]rootMapEntry {
35+ "googleapis" : {path : sources .Googleapis , key : "googleapis-root" },
36+ "discovery" : {path : sources .Discovery , key : "discovery-root" },
37+ "showcase" : {path : sources .Showcase , key : "showcase-root" },
38+ "protobuf-src" : {path : sources .ProtobufSrc , key : "protobuf-src-root" },
39+ "conformance" : {path : sources .Conformance , key : "conformance-root" },
40+ }
41+ }
42+
43+ func determineSourceRoot (source string , sources * Sources , rootMap map [string ]rootMapEntry ) (string , string ) {
44+ if r , ok := rootMap [source ]; ok {
45+ return r .path , ""
46+ }
47+ // Check if the source directory exists in googleapisDir
48+ if _ , err := os .Stat (filepath .Join (sources .Googleapis , source )); err == nil {
49+ return sources .Googleapis , source
50+ }
51+ // Check if the source directory exists locally
52+ if _ , err := os .Stat (source ); err == nil {
53+ return "." , source
54+ }
55+ return "" , ""
56+ }
57+
2658func toSidekickConfig (library * config.Library , ch * config.Channel , sources * Sources ) (* sidekickconfig.Config , error ) {
2759 source := map [string ]string {}
2860 specFormat := "protobuf"
@@ -39,16 +71,7 @@ func toSidekickConfig(library *config.Library, ch *config.Channel, sources *Sour
3971 source ["roots" ] = "googleapis"
4072 } else {
4173 source ["roots" ] = strings .Join (library .Roots , "," )
42- rootMap := map [string ]struct {
43- path string
44- key string
45- }{
46- "googleapis" : {path : sources .Googleapis , key : "googleapis-root" },
47- "discovery" : {path : sources .Discovery , key : "discovery-root" },
48- "showcase" : {path : sources .Showcase , key : "showcase-root" },
49- "protobuf-src" : {path : sources .ProtobufSrc , key : "protobuf-src-root" },
50- "conformance" : {path : sources .Conformance , key : "conformance-root" },
51- }
74+ rootMap := newRootMap (sources )
5275 for _ , root := range library .Roots {
5376 if r , ok := rootMap [root ]; ok && r .path != "" {
5477 source [r .key ] = r .path
@@ -233,13 +256,18 @@ func formatPackageDependency(dep *config.RustPackageDependency) string {
233256 return strings .Join (parts , "," )
234257}
235258
236- func moduleToSidekickConfig (library * config.Library , module * config.RustModule , googleapisDir , protobufSrcDir string ) (* sidekickconfig.Config , error ) {
259+ func moduleToSidekickConfig (library * config.Library , module * config.RustModule , sources * Sources ) (* sidekickconfig.Config , error ) {
237260 source := map [string ]string {
238- "googleapis-root" : googleapisDir ,
239- "protobuf-src-root" : protobufSrcDir ,
261+ "googleapis-root" : sources . Googleapis ,
262+ "protobuf-src-root" : sources . ProtobufSrc ,
240263 }
264+ rootMap := newRootMap (sources )
241265 for root , dir := range module .ModuleRoots {
242- source [root ] = dir
266+ if r , ok := rootMap [dir ]; ok {
267+ source [root ] = r .path
268+ } else {
269+ source [root ] = dir
270+ }
243271 }
244272 if len (module .IncludedIds ) > 0 {
245273 source ["included-ids" ] = strings .Join (module .IncludedIds , "," )
@@ -251,12 +279,15 @@ func moduleToSidekickConfig(library *config.Library, module *config.RustModule,
251279 source ["include-list" ] = module .IncludeList
252280 }
253281 if module .Source != "" {
254- api , err := serviceconfig .Find (googleapisDir , module .Source )
255- if err != nil {
256- return nil , fmt .Errorf ("serviceconfig.Find(%s, %s): %w" , googleapisDir , module .Source , err )
257- }
258- if api != nil && api .Title != "" {
259- source ["title-override" ] = api .Title
282+ root , path := determineSourceRoot (module .Source , sources , rootMap )
283+ if root != "" {
284+ api , err := serviceconfig .Find (root , path )
285+ if err != nil {
286+ return nil , fmt .Errorf ("serviceconfig.Find(%s, %s): %w" , root , path , err )
287+ }
288+ if api != nil && api .Title != "" {
289+ source ["title-override" ] = api .Title
290+ }
260291 }
261292 }
262293
0 commit comments