11"""Utilities for working with Bazel modules"""
22
3+ def _repo_name (label_or_name ):
4+ """Utility to provide Label compatibility with Bazel 5.
5+
6+ Under Bazel 5, calls `Label.workspace_name`. Otherwise calls
7+ `Label.repo_name`.
8+
9+ Args:
10+ label_or_name: a Label or repository name string
11+
12+ Returns:
13+ The repository name returned directly from the Label API, or the
14+ original string if not a Label
15+ """
16+ if hasattr (label_or_name , "repo_name" ):
17+ return label_or_name .repo_name
18+
19+ return getattr (label_or_name , "workspace_name" , label_or_name )
20+
321def apparent_repo_name (label_or_name ):
422 """Return a repository's apparent repository name.
523
@@ -12,7 +30,7 @@ def apparent_repo_name(label_or_name):
1230 Returns:
1331 The apparent repository name
1432 """
15- repo_name = getattr ( label_or_name , "repo_name" , label_or_name ).lstrip ("@" )
33+ repo_name = _repo_name ( label_or_name ).lstrip ("@" )
1634 delimiter_indices = []
1735
1836 # Bazed on this pattern from the Bazel source:
@@ -42,13 +60,14 @@ def apparent_repo_label_string(label):
4260 str(label) with its canonical repository name replaced with its apparent
4361 repository name
4462 """
45- if len (label .repo_name ) == 0 :
63+ repo_name = _repo_name (label )
64+ if len (repo_name ) == 0 :
4665 return str (label )
4766
4867 label_str = "@" + str (label ).lstrip ("@" )
4968 return label_str .replace (label .repo_name , apparent_repo_name (label ))
5069
51- _MAIN_REPO_PREFIX = str (Label ("@@ //:all" )).split (":" )[0 ]
70+ _MAIN_REPO_PREFIX = str (Label ("//:all" )).split (":" )[0 ]
5271
5372def adjust_main_repo_prefix (target_pattern ):
5473 """Updates the main repo prefix to match the current Bazel version.
0 commit comments