@@ -275,12 +275,20 @@ def update_bom_pom(filename: str, modules: List[module.Module]):
275275 tree .write (filename , pretty_print = True , xml_declaration = True , encoding = "utf-8" )
276276
277277def main ():
278+ print (f"working directory: { os .getcwd ()} " )
278279 with open (".repo-metadata.json" , "r" ) as fp :
279280 repo_metadata = json .load (fp )
280-
281281 group_id , artifact_id = repo_metadata ["distribution_name" ].split (":" )
282282 name = repo_metadata ["name_pretty" ]
283283 existing_modules = load_versions ("versions.txt" , group_id )
284+ monorepo = False
285+ if not existing_modules :
286+ # For single-component Release Please setup, the root versions.txt
287+ # manages the versions of all submodules.
288+ existing_modules = load_versions ("../versions.txt" , group_id )
289+ if existing_modules :
290+ monorepo = True
291+ print (f"monorepo? { monorepo } " )
284292
285293 # extra modules that need to be manages in versions.txt
286294 if "extra_versioned_modules" in repo_metadata :
@@ -300,6 +308,7 @@ def main():
300308 else :
301309 excluded_poms_list = ""
302310
311+ # Missing Case 1: When this library ('java-XXX' module) is new.
303312 if artifact_id not in existing_modules :
304313 existing_modules [artifact_id ] = module .Module (
305314 group_id = group_id ,
@@ -322,9 +331,18 @@ def main():
322331
323332 required_dependencies = {}
324333 for dependency_module in existing_modules :
325- if dependency_module not in excluded_dependencies_list :
326- required_dependencies [dependency_module ] = existing_modules [dependency_module ]
334+ if dependency_module in excluded_dependencies_list :
335+ continue
336+ dep_artifact_id = existing_modules [dependency_module ].artifact_id
337+ if monorepo and not os .path .isdir (dep_artifact_id ):
338+ # In monorepo, existing_modules are loaded from the root
339+ # versions.txt and thus includes irrelevant artifacts
340+ continue
341+ required_dependencies [dependency_module ] = existing_modules [dependency_module ]
327342
343+ # Missing Case 2: There's a new proto-XXX and grpc-XXX directory. It's a new
344+ # version in the proto file to a library. Both a new library and existing
345+ # library.
328346 for path in glob .glob ("proto-google-*" ):
329347 if not path in existing_modules :
330348 existing_modules [path ] = module .Module (
@@ -463,10 +481,10 @@ def main():
463481 name = name ,
464482 )
465483
466- if os . path . isfile ( "versions.txt" ):
467- print ( "updating modules in versions.txt" )
468- else :
469- print ("creating missing versions.txt " )
484+ # For monorepo, we use the versions.txt at the root. The "./" is needed
485+ # for the templates.render(), which tries to create a directory.
486+ versions_txt_file = "../versions.txt" if monorepo else "./versions.txt"
487+ print (f"updating modules in { versions_txt_file } " )
470488 existing_modules .pop (parent_artifact_id )
471489
472490 # add extra modules to versions.txt
@@ -479,7 +497,7 @@ def main():
479497 release_version = main_module .release_version ,
480498 )
481499 templates .render (
482- template_name = "versions.txt.j2" , output_name = "./versions.txt" , modules = existing_modules .values (),
500+ template_name = "versions.txt.j2" , output_name = versions_txt_file , modules = existing_modules .values (),
483501 )
484502
485503if __name__ == "__main__" :
0 commit comments