1
1
"""Utilities for working with Bazel modules"""
2
2
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
+
3
21
def apparent_repo_name (label_or_name ):
4
22
"""Return a repository's apparent repository name.
5
23
@@ -12,7 +30,7 @@ def apparent_repo_name(label_or_name):
12
30
Returns:
13
31
The apparent repository name
14
32
"""
15
- repo_name = getattr ( label_or_name , "repo_name" , label_or_name ).lstrip ("@" )
33
+ repo_name = _repo_name ( label_or_name ).lstrip ("@" )
16
34
delimiter_indices = []
17
35
18
36
# Bazed on this pattern from the Bazel source:
@@ -42,13 +60,14 @@ def apparent_repo_label_string(label):
42
60
str(label) with its canonical repository name replaced with its apparent
43
61
repository name
44
62
"""
45
- if len (label .repo_name ) == 0 :
63
+ repo_name = _repo_name (label )
64
+ if len (repo_name ) == 0 :
46
65
return str (label )
47
66
48
67
label_str = "@" + str (label ).lstrip ("@" )
49
68
return label_str .replace (label .repo_name , apparent_repo_name (label ))
50
69
51
- _MAIN_REPO_PREFIX = str (Label ("@@ //:all" )).split (":" )[0 ]
70
+ _MAIN_REPO_PREFIX = str (Label ("//:all" )).split (":" )[0 ]
52
71
53
72
def adjust_main_repo_prefix (target_pattern ):
54
73
"""Updates the main repo prefix to match the current Bazel version.
0 commit comments