2
2
for updating and testing the Gazelle manifest file.
3
3
"""
4
4
5
- load ("@io_bazel_rules_go//go:def.bzl" , "go_binary" )
5
+ load ("@io_bazel_rules_go//go:def.bzl" , "GoSource" , " go_binary" )
6
6
7
7
def gazelle_python_manifest (
8
8
name ,
@@ -38,7 +38,11 @@ def gazelle_python_manifest(
38
38
update_target = "{}.update" .format (name )
39
39
update_target_label = "//{}:{}" .format (native .package_name (), update_target )
40
40
41
+ manifest_generator_hash = Label ("//gazelle/manifest/generate:generate_lib_sources_hash" )
42
+
41
43
update_args = [
44
+ "--manifest-generator-hash" ,
45
+ "$(rootpath {})" .format (manifest_generator_hash ),
42
46
"--requirements" ,
43
47
"$(rootpath {})" .format (requirements ),
44
48
"--pip-repository-name" ,
@@ -55,11 +59,12 @@ def gazelle_python_manifest(
55
59
56
60
go_binary (
57
61
name = update_target ,
58
- embed = ["@rules_python //gazelle/manifest/generate:generate_lib" ],
62
+ embed = [Label ( " //gazelle/manifest/generate:generate_lib") ],
59
63
data = [
60
64
manifest ,
61
65
modules_mapping ,
62
66
requirements ,
67
+ manifest_generator_hash ,
63
68
],
64
69
args = update_args ,
65
70
visibility = ["//visibility:private" ],
@@ -70,21 +75,23 @@ def gazelle_python_manifest(
70
75
71
76
go_binary (
72
77
name = test_binary ,
73
- embed = ["@rules_python //gazelle/manifest/test:test_lib" ],
78
+ embed = [Label ( " //gazelle/manifest/test:test_lib") ],
74
79
visibility = ["//visibility:private" ],
75
80
)
76
81
77
82
native .sh_test (
78
83
name = "{}.test" .format (name ),
79
- srcs = ["@rules_python //gazelle/manifest/test:run.sh" ],
84
+ srcs = [Label ( " //gazelle/manifest/test:run.sh") ],
80
85
data = [
81
86
":{}" .format (test_binary ),
82
87
manifest ,
83
88
requirements ,
89
+ manifest_generator_hash ,
84
90
],
85
91
env = {
86
92
"_TEST_BINARY" : "$(rootpath :{})" .format (test_binary ),
87
93
"_TEST_MANIFEST" : "$(rootpath {})" .format (manifest ),
94
+ "_TEST_MANIFEST_GENERATOR_HASH" : "$(rootpath {})" .format (manifest_generator_hash ),
88
95
"_TEST_REQUIREMENTS" : "$(rootpath {})" .format (requirements ),
89
96
},
90
97
visibility = ["//visibility:private" ],
@@ -97,3 +104,56 @@ def gazelle_python_manifest(
97
104
tags = ["manual" ],
98
105
visibility = ["//visibility:public" ],
99
106
)
107
+
108
+ # buildifier: disable=provider-params
109
+ AllSourcesInfo = provider (fields = {"all_srcs" : "All sources collected from the target and dependencies." })
110
+
111
+ _rules_python_workspace = Label ("//:WORKSPACE" )
112
+
113
+ def _get_all_sources_impl (target , ctx ):
114
+ is_rules_python = target .label .workspace_name == _rules_python_workspace .workspace_name
115
+ if not is_rules_python :
116
+ # Avoid adding third-party dependency files to the checksum of the srcs.
117
+ return AllSourcesInfo (all_srcs = depset ())
118
+ srcs = depset (
119
+ target [GoSource ].orig_srcs ,
120
+ transitive = [dep [AllSourcesInfo ].all_srcs for dep in ctx .rule .attr .deps ],
121
+ )
122
+ return [AllSourcesInfo (all_srcs = srcs )]
123
+
124
+ _get_all_sources = aspect (
125
+ implementation = _get_all_sources_impl ,
126
+ attr_aspects = ["deps" ],
127
+ )
128
+
129
+ def _sources_hash_impl (ctx ):
130
+ all_srcs = ctx .attr .go_library [AllSourcesInfo ].all_srcs
131
+ hash_file = ctx .actions .declare_file (ctx .attr .name + ".hash" )
132
+ args = ctx .actions .args ()
133
+ args .add (hash_file )
134
+ args .add_all (all_srcs )
135
+ ctx .actions .run (
136
+ outputs = [hash_file ],
137
+ inputs = all_srcs ,
138
+ arguments = [args ],
139
+ executable = ctx .executable ._hasher ,
140
+ )
141
+ return [DefaultInfo (
142
+ files = depset ([hash_file ]),
143
+ runfiles = ctx .runfiles ([hash_file ]),
144
+ )]
145
+
146
+ sources_hash = rule (
147
+ _sources_hash_impl ,
148
+ attrs = {
149
+ "go_library" : attr .label (
150
+ aspects = [_get_all_sources ],
151
+ providers = [GoSource ],
152
+ ),
153
+ "_hasher" : attr .label (
154
+ cfg = "exec" ,
155
+ default = Label ("//gazelle/manifest/hasher" ),
156
+ executable = True ,
157
+ ),
158
+ },
159
+ )
0 commit comments