@@ -29,6 +29,60 @@ const (
2929 generationConfigFileName = "generation_config.yaml"
3030)
3131
32+ type javaGAPICInfo struct {
33+ NoRestNumericEnums bool
34+ NoSamples bool
35+ AdditionalProtos []string
36+ }
37+
38+ func parseJavaBazel (googleapisDir , dir string ) (* javaGAPICInfo , error ) {
39+ file , err := parseBazel (googleapisDir , dir )
40+ if err != nil {
41+ return nil , err
42+ }
43+ if file == nil {
44+ return nil , nil
45+ }
46+ info := & javaGAPICInfo {}
47+ // 1. From java_gapic_library
48+ if rules := file .Rules ("java_gapic_library" ); len (rules ) > 0 {
49+ if len (rules ) > 1 {
50+ log .Printf ("Warning: multiple java_gapic_library in %s/BUILD.bazel, using first" , dir )
51+ }
52+ rule := rules [0 ]
53+ info .NoRestNumericEnums = rule .AttrLiteral ("rest_numeric_enums" ) == "False"
54+ }
55+ // 2. From java_gapic_assembly_gradle_pkg
56+ if rules := file .Rules ("java_gapic_assembly_gradle_pkg" ); len (rules ) > 0 {
57+ if len (rules ) > 1 {
58+ log .Printf ("Warning: multiple java_gapic_assembly_gradle_pkg in %s/BUILD.bazel, using first" , dir )
59+ }
60+ rule := rules [0 ]
61+ info .NoSamples = rule .AttrLiteral ("include_samples" ) == "False"
62+ }
63+ // 3. From proto_library_with_info
64+ if rules := file .Rules ("proto_library_with_info" ); len (rules ) > 0 {
65+ if len (rules ) > 1 {
66+ log .Printf ("Warning: multiple proto_library_with_info in %s/BUILD.bazel, using first" , dir )
67+ }
68+ rule := rules [0 ]
69+ // Search for specific common resource targets in deps
70+ if deps := rule .AttrStrings ("deps" ); len (deps ) > 0 {
71+ protoMappings := map [string ]string {
72+ "//google/cloud:common_resources_proto" : "google/cloud/common_resources.proto" ,
73+ "//google/cloud/location:location_proto" : "google/cloud/location/locations.proto" ,
74+ "//google/iam/v1:iam_policy_proto" : "google/iam/v1/iam_policy.proto" ,
75+ }
76+ for _ , dep := range deps {
77+ if protoPath , ok := protoMappings [dep ]; ok {
78+ info .AdditionalProtos = append (info .AdditionalProtos , protoPath )
79+ }
80+ }
81+ }
82+ }
83+ return info , nil
84+ }
85+
3286// GAPICConfig represents the GAPIC configuration in generation_config.yaml.
3387type GAPICConfig struct {
3488 ProtoPath string `yaml:"proto_path"`
@@ -73,10 +127,17 @@ func runJavaMigration(ctx context.Context, repoPath string) error {
73127 if err != nil {
74128 return err
75129 }
76- cfg := buildConfig (gen )
130+ src , err := fetchSource (ctx )
131+ if err != nil {
132+ return errFetchSource
133+ }
134+ cfg := buildConfig (gen , src .Dir )
77135 if cfg == nil {
78136 return fmt .Errorf ("no libraries found to migrate" )
79137 }
138+ // The directory name in Googleapis is present for migration code to look
139+ // up API details. It shouldn't be persisted.
140+ cfg .Sources .Googleapis .Dir = ""
80141 if err := librarian .RunTidyOnConfig (ctx , cfg ); err != nil {
81142 return errTidyFailed
82143 }
@@ -89,18 +150,36 @@ func readGenerationConfig(path string) (*GenerationConfig, error) {
89150}
90151
91152// buildConfig converts a GenerationConfig to a Librarian Config.
92- func buildConfig (gen * GenerationConfig ) * config.Config {
153+ func buildConfig (gen * GenerationConfig , googleapisDir string ) * config.Config {
93154 var libs []* config.Library
94155 for _ , l := range gen .Libraries {
95156 name := l .LibraryName
96157 if name == "" {
97158 name = l .APIShortName
98159 }
99160 var apis []* config.API
161+ var javaAPIs []* config.JavaAPI
100162 for _ , g := range l .GAPICs {
101- if g .ProtoPath != "" {
102- apis = append (apis , & config.API {Path : g .ProtoPath })
163+ if g .ProtoPath == "" {
164+ continue
165+ }
166+ apis = append (apis , & config.API {Path : g .ProtoPath })
167+
168+ info , err := parseJavaBazel (googleapisDir , g .ProtoPath )
169+ if err != nil {
170+ log .Printf ("Warning: failed to parse BUILD.bazel for %s: %v" , g .ProtoPath , err )
171+ continue
172+ }
173+ if info == nil {
174+ continue
175+ }
176+ javaAPI := & config.JavaAPI {
177+ Path : g .ProtoPath ,
178+ NoRestNumericEnums : info .NoRestNumericEnums ,
179+ AdditionalProtos : info .AdditionalProtos ,
180+ NoSamples : info .NoSamples ,
103181 }
182+ javaAPIs = append (javaAPIs , javaAPI )
104183 }
105184 libs = append (libs , & config.Library {
106185 Name : name ,
@@ -119,6 +198,7 @@ func buildConfig(gen *GenerationConfig) *config.Config {
119198 ExcludedDependencies : l .ExcludedDependencies ,
120199 ExcludedPoms : l .ExcludedPoms ,
121200 ExtraVersionedModules : l .ExtraVersionedModules ,
201+ JavaAPIs : javaAPIs ,
122202 GroupID : l .GroupID ,
123203 IssueTrackerOverride : l .IssueTracker ,
124204 LibraryTypeOverride : l .LibraryType ,
@@ -139,8 +219,7 @@ func buildConfig(gen *GenerationConfig) *config.Config {
139219 Language : "java" ,
140220 Default : & config.Default {},
141221 Sources : & config.Sources {
142- // hardcoded for local testing
143- Googleapis : & config.Source {Dir : "../../googleapis" },
222+ Googleapis : & config.Source {Dir : googleapisDir },
144223 },
145224 Libraries : libs ,
146225 }
0 commit comments