@@ -22,28 +22,59 @@ def qualify_package(label):
2222 else :
2323 return "@" + label .workspace_name + "//" + label .package
2424
25- def current_qualified_package ():
25+ def current_qualified_package (ctx = None ):
2626 """
2727 Gets the current package as "qualified package".
2828 """
29- label_str = "//" + native . package_name ()
30- repository = native . repository_name ()
29+ label_str = "//" + package_name (ctx )
30+ repository = repository_name (ctx )
3131 if repository == "@" :
32- return "//" + native . package_name ()
32+ return "//" + package_name (ctx )
3333 else :
34- return repository + "//" + native . package_name ()
34+ return repository + "//" + package_name (ctx )
3535
36- def to_label (label_str ):
36+ def to_label (label_str , ctx = None ):
3737 """
3838 Converts any label string to a Label instance.
39+
3940 Normally, Label() requires absolute paths, so this wraps the constructor to
4041 allow automatic scoping.
42+
4143 This can be used to parse / canonicalize labels.
4244 """
45+ if type (label_str ) == "Label" :
46+ return label_str
4347 if label_str .startswith (":" ):
44- label_str = current_qualified_package () + label_str
48+ label_str = current_qualified_package (ctx ) + label_str
4549 return Label (label_str )
4650
51+ def label_to_str (label , ctx = None ):
52+ """
53+ Converts label to abbreviated string.
54+
55+ For some reason, Bazel likes showing the more verbose label.
56+ """
57+ if in_current_package (label , ctx = ctx ):
58+ return ":" + label .name
59+ else :
60+ if label .workspace_name == "" and repository_name (ctx ) == "" :
61+ repository_prefix = ""
62+ else :
63+ repository_prefix = "@" + label .workspace_name
64+
65+ if label .package == "" and label .name == label .workspace_name :
66+ return repository_prefix
67+ elif (
68+ label .package == label .name or
69+ label .package .endswith ("/" + label .name )
70+ ):
71+ return repository_prefix + "//" + label .package
72+ else :
73+ return str (label )
74+
75+ def labels_to_str_list (labels , ctx = None ):
76+ return [label_to_str (label , ctx ) for label in labels ]
77+
4778def in_workspace (label , workspace ):
4879 """True if given Label is in the specified workspace / repository."""
4980 if workspace .startswith ("@" ):
@@ -54,24 +85,56 @@ def in_root_workspace(label):
5485 """True if given Label is in source workspace."""
5586 return in_workspace (label , "" )
5687
57- def workspace_name ():
88+ def repository_name (ctx = None ):
89+ if ctx == None :
90+ return native .repository_name ()
91+ else :
92+ return ctx .attr .repository_name
93+
94+ def package_name (ctx = None ):
95+ if ctx == None :
96+ return native .package_name ()
97+ else :
98+ return ctx .attr .package_name
99+
100+ def workspace_name (ctx = None ):
58101 """Repository name, but without leading @."""
59- return strip_prefix ("@" , native . repository_name ())
102+ return strip_prefix ("@" , repository_name (ctx ))
60103
61- def in_current_package (label ):
104+ def in_current_package (label , ctx = None ):
62105 """True if given label is in the current package."""
63106 return (
64- label .workspace_name == workspace_name () and
65- label .package == native . package_name ()
107+ label .workspace_name == workspace_name (ctx ) and
108+ label .package == package_name (ctx )
66109 )
67110
111+ def in_same_package (a , b ):
112+ return (
113+ a .workspace_name == b .workspace_name and
114+ a .package == b .package
115+ )
116+
117+ # Attributes for using these functions in analysis phase, where you cannot use
118+ # native.repostiory_name() or native.package_name().
119+ attrs = {
120+ "repository_name" : attr .string (mandatory = True ),
121+ "package_name" : attr .string (mandatory = True ),
122+ }
123+
124+ """Rolled-up access."""
68125labels = struct (
69126 strip_prefix = strip_prefix ,
70127 qualify_package = qualify_package ,
71128 current_qualified_package = current_qualified_package ,
72129 to_label = to_label ,
130+ label_to_str = label_to_str ,
131+ labels_to_str_list = labels_to_str_list ,
73132 in_workspace = in_workspace ,
74133 in_root_workspace = in_root_workspace ,
134+ package_name = package_name ,
135+ repository_name = repository_name ,
75136 workspace_name = workspace_name ,
76137 in_current_package = in_current_package ,
138+ in_same_package = in_same_package ,
139+ attrs = attrs ,
77140)
0 commit comments