@@ -97,3 +97,86 @@ def integration_test(name, target, data):
9797 test_library = "%s_test" % target ,
9898 srcs = data ,
9999 )
100+
101+ def _overwrite_golden_impl (ctx ):
102+ # Extract the Java source files from the generated 3 srcjars from API bazel target,
103+ # and put them in the temporary folder `codegen_tmp`, zip as `goldens_output_zip`.
104+ # Overwrite the goldens folder e.g `test/integration/goldens/redis` with the
105+ # code generation in `goldens_output_zip`.
106+
107+ gapic_library = ctx .attr .gapic_library
108+ resource_name_library = ctx .attr .resource_name_library
109+ test_library = ctx .attr .test_library
110+ srcs = ctx .files .srcs
111+ # Convert the name of bazel rules e.g. `redis_update` to `redis`
112+ # because we will need to overwrite the goldens files in `redis` folder.
113+ api_name = "_" .join (ctx .attr .name .split ("_" )[:- 1 ])
114+ goldens_output_zip = ctx .outputs .goldens_output_zip
115+
116+ script = """
117+ mkdir codegen_tmp
118+ unzip -j {input} -d codegen_tmp
119+ unzip -j {input_resource_name} -d codegen_tmp
120+ unzip -j {input_test} -d codegen_tmp
121+ cd codegen_tmp
122+ # Remove unneeded non-Java files, like MANIFEST
123+ rm -rf $(find . -type f ! -name "*.java")
124+ zip -r ../{goldens_output_zip} .
125+ """ .format (
126+ goldens_output_zip = goldens_output_zip .path ,
127+ input = gapic_library [JavaInfo ].source_jars [0 ].path ,
128+ input_resource_name = resource_name_library [JavaInfo ].source_jars [0 ].path ,
129+ input_test = test_library [JavaInfo ].source_jars [0 ].path ,
130+ )
131+
132+ ctx .actions .run_shell (
133+ inputs = srcs + [
134+ gapic_library [JavaInfo ].source_jars [0 ],
135+ resource_name_library [JavaInfo ].source_jars [0 ],
136+ test_library [JavaInfo ].source_jars [0 ],
137+ ],
138+ outputs = [goldens_output_zip ],
139+ command = script ,
140+ )
141+
142+ # Overwrite the goldens.
143+ golden_update_script_content = """
144+ cd ${{BUILD_WORKSPACE_DIRECTORY}}
145+ unzip -ao {goldens_output_zip} -d test/integration/goldens/{api_name}
146+ """ .format (
147+ goldens_output_zip = goldens_output_zip .path ,
148+ api_name = api_name ,
149+ )
150+ ctx .actions .write (
151+ output = ctx .outputs .golden_update_script ,
152+ content = golden_update_script_content ,
153+ is_executable = True ,
154+ )
155+ return [DefaultInfo (executable = ctx .outputs .golden_update_script )]
156+
157+ overwrite_golden = rule (
158+ attrs = {
159+ "gapic_library" : attr .label (),
160+ "resource_name_library" : attr .label (),
161+ "test_library" : attr .label (),
162+ "srcs" : attr .label_list (
163+ allow_files = True ,
164+ mandatory = True ,
165+ ),
166+ },
167+ outputs = {
168+ "goldens_output_zip" : "%{name}.zip" ,
169+ "golden_update_script" : "%{name}.sh" ,
170+ },
171+ executable = True ,
172+ implementation = _overwrite_golden_impl ,
173+ )
174+
175+ def golden_update (name , target , data ):
176+ overwrite_golden (
177+ name = name ,
178+ gapic_library = target ,
179+ resource_name_library = "%s_resource_name" % target ,
180+ test_library = "%s_test" % target ,
181+ srcs = data ,
182+ )
0 commit comments