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 )
3
+ _repo_attr = (
4
+ "repo_name" if hasattr (Label ("//:all" ), "repo_name" ) else "workspace_name"
5
+ )
20
6
21
7
def apparent_repo_name (label_or_name ):
22
8
"""Return a repository's apparent repository name.
@@ -30,7 +16,7 @@ def apparent_repo_name(label_or_name):
30
16
Returns:
31
17
The apparent repository name
32
18
"""
33
- repo_name = _repo_name ( label_or_name ).lstrip ("@" )
19
+ repo_name = getattr ( label_or_name , _repo_attr , label_or_name ).lstrip ("@" )
34
20
delimiter_indices = []
35
21
36
22
# Bazed on this pattern from the Bazel source:
@@ -49,44 +35,3 @@ def apparent_repo_name(label_or_name):
49
35
return repo_name [:delimiter_indices [0 ]]
50
36
51
37
return repo_name [delimiter_indices [- 1 ] + 1 :]
52
-
53
- def apparent_repo_label_string (label ):
54
- """Return a Label string starting with its apparent repo name.
55
-
56
- Args:
57
- label: a Label instance
58
-
59
- Returns:
60
- str(label) with its canonical repository name replaced with its apparent
61
- repository name
62
- """
63
- repo_name = _repo_name (label )
64
- if len (repo_name ) == 0 :
65
- return str (label )
66
-
67
- label_str = "@" + str (label ).lstrip ("@" )
68
- return label_str .replace (label .repo_name , apparent_repo_name (label ))
69
-
70
- _IS_BZLMOD_ENABLED = str (Label ("//:all" )).startswith ("@@" )
71
-
72
- _MAIN_REPO_PREFIX = "@@//" if _IS_BZLMOD_ENABLED else "@//"
73
-
74
- def adjust_main_repo_prefix (target_pattern ):
75
- """Updates the main repo prefix to match the current Bazel version.
76
-
77
- The main repo prefix will be "@//" for Bazel < 7.1.0, and "@@//" for Bazel
78
- >= 7.1.0 under Bzlmod. This macro automatically updates strings representing
79
- include/exclude target patterns so that they match actual main repository
80
- target Labels correctly.
81
-
82
- Args:
83
- target_pattern: a string used to match a BUILD target pattern
84
-
85
- Returns:
86
- the string with any main repository prefix updated to match the current
87
- Bazel version
88
- """
89
- if target_pattern .startswith ("@//" ) or target_pattern .startswith ("@@//" ):
90
- return _MAIN_REPO_PREFIX + target_pattern .lstrip ("@/" )
91
-
92
- return target_pattern
0 commit comments