@@ -59,6 +59,7 @@ def _setup_deps(deps, tla_code_libraries = {}, ext_code_libraries = {}, transiti
59
59
deps: List of deps labels from ctx.attr.deps.
60
60
tla_code_libraries: Dict of labels to names from ctx.attr.tla_code_files.
61
61
ext_code_libraries: List of deps labels from ctx.attr.ext_code_files.
62
+ transitive_extvars: Dict of extvar to values build from _make_extvar_dict
62
63
63
64
Returns:
64
65
Returns a struct containing the following fields:
@@ -69,6 +70,9 @@ def _setup_deps(deps, tla_code_libraries = {}, ext_code_libraries = {}, transiti
69
70
short_imports: Depset of Strings containing import flags set by
70
71
transitive dependency targets, when invoking Jsonnet as part
71
72
of a test where dependencies are stored in runfiles.
73
+ transitive_extvars: Dict of extvar to values that has merged the
74
+ input value with all extvars of its depdencies.
75
+
72
76
"""
73
77
transitive_sources = []
74
78
imports = []
@@ -115,42 +119,72 @@ def _make_extvar_dict(
115
119
116
120
Returns:
117
121
Dictionary with keys are variable names, and values a dict containing
118
- type: The kind of extvar it is from and maps to a ctx.attr, e.g. string, code, string_file, etc.
122
+ type: The type of extvar it will be in jsonnet: string or code
119
123
value: The string, code, or File depending on type
120
124
sources: List of labels that define the extvar
121
125
"""
122
126
extvars = dict ()
123
127
label = str (label )
124
- for key , code in ext_code .items ():
125
- _make_extvar_dict_update (extvars , "code" , key , code , label )
126
- for key in ext_code_envs :
127
- _make_extvar_dict_update (extvars , "code_env" , key , None , label )
128
- for file , key in ext_code_files .items ():
129
- _make_extvar_dict_update (extvars , "code_file" , key , file , label )
130
- for file , key in ext_code_libraries .items ():
131
- _make_extvar_dict_update (extvars , "code_library" , key , file , label )
132
- for key in ext_str_envs :
133
- _make_extvar_dict_update (extvars , "string_env" , key , None , label )
134
- for val , key in ext_str_files .items ():
135
- _make_extvar_dict_update (extvars , "string_file" , key , val , label )
136
- for key , val in ext_strs .items ():
128
+
129
+ # extvar_lists is a list of tuple (extvar: str, value: None | str | File | JsonnetInfo, extvar_type: str)
130
+ # The `None` value are used by environment
131
+ # Collect all the Code extvars
132
+ # ext_code, dict[extvar, str_value]
133
+ extvar_code_lists = zip (ext_code .keys (), ext_code .values ())
134
+
135
+ # ext_code_envs, list[extvar]
136
+ extvar_code_lists .extend (zip (ext_code_envs , [None ] * len (ext_code_envs )))
137
+
138
+ # ext_code_files, dict[label, extvar]
139
+ extvar_code_lists .extend (zip (ext_code_files .values (), ext_code_files .keys ()))
140
+
141
+ # ext_code_libraries, dict[label, extvar]
142
+ extvar_code_lists .extend (zip (ext_code_libraries .values (), ext_code_libraries .keys ()))
143
+
144
+ for key , val in extvar_code_lists :
145
+ _make_extvar_dict_update (extvars , "code" , key , val , label )
146
+
147
+ # Collect all of the String extvars
148
+ # ext_str_envs, list[extvar]
149
+ extvar_str_lists = zip (ext_str_envs , [None ] * len (ext_str_envs ))
150
+
151
+ # ext_str_files, dict[label, extvar]
152
+ extvar_str_lists .extend (zip (ext_str_files .values (), ext_str_files .keys ()))
153
+
154
+ # ext_strs, dict[extvar, str]
155
+ extvar_str_lists .extend (zip (ext_strs .keys (), ext_strs .values ()))
156
+
157
+ for key , val in extvar_str_lists :
137
158
_make_extvar_dict_update (extvars , "string" , key , val , label )
159
+
138
160
return extvars
139
161
140
- def _make_extvar_dict_update (extvars , extvar_type , key , val , label ):
141
- if key in extvars :
162
+ def _make_extvar_dict_update (extvars , extvar_type , extvar_name , value , label ):
163
+ """Adds an entry to a given extrvars dict and validates its uniqueness
164
+
165
+ Args:
166
+ extvars: Dict of extvars to be added to
167
+ extvar_type: String of either "string" or "code"
168
+ extvar_name: String of the extvar variable name
169
+ value: Either a None, string, File or Target
170
+ label: String of the package this extvar is defined in
171
+
172
+ Returns:
173
+ None, modifies the given extvars input in-place
174
+ """
175
+ if extvar_name in extvars :
142
176
fail ("duplicate extvar '{}' of type {} and {}"
143
- .format (key , extvar_type , extvars [key ]["type" ]))
177
+ .format (extvar_name , extvar_type , extvars [extvar_name ]["type" ]))
144
178
145
- if type (val ) == "string" or type (val ) == "File" or val == None :
179
+ if type (value ) == "string" or type (value ) == "File" or value == None :
146
180
pass
147
- elif type (val ) == "Target" :
148
- val = val [DefaultInfo ].files .to_list ()[0 ]
181
+ elif type (value ) == "Target" :
182
+ value = value [DefaultInfo ].files .to_list ()[0 ]
149
183
else :
150
- fail ("unknown type of value {} for {} in {}" .format (type (val ), key , label ))
184
+ fail ("unknown type of value {} for {} in {}" .format (type (value ), extvar_name , label ))
151
185
152
- extvars .update ([[key , {
153
- "value" : val ,
186
+ extvars .update ([[extvar_name , {
187
+ "value" : value ,
154
188
"type" : extvar_type ,
155
189
"sources" : [label ],
156
190
}]])
@@ -167,22 +201,24 @@ def _extvar_to_arguments(transitive_extvars, short_path = False):
167
201
"""
168
202
args = []
169
203
for key , val in transitive_extvars .items ():
170
- if val ["type" ] == "string" :
171
- args .append ("--ext-str %s=%s" % (_quote (key ), _quote (val ["value" ])))
172
- elif val ["type" ] == "string_env" :
173
- args .append ("--ext-str %s" % _quote (key ))
174
- elif val ["type" ] == "string_file" :
175
- file = val ["value" ]
176
- args .append ("--ext-str-file %s=%s" % (_quote (key ), _quote (file .short_path if short_path else file .path )))
177
- elif val ["type" ] == "code" :
178
- args .append ("--ext-code %s=%s" % (_quote (key ), _quote (val ["value" ])))
179
- elif val ["type" ] == "code_env" :
180
- args .append ("--ext-code %s" % _quote (key ))
181
- elif val ["type" ] == "code_library" or val ["type" ] == "code_file" :
204
+ # The --ext-str-* and --ext-code-* flag families are interchangable,
205
+ # so the `type` is used to determine which to use.
206
+ flag_type = "str" if val ["type" ] == "string" else val ["type" ]
207
+
208
+ # Each different type of value is formatted in the flags differently
209
+ if val ["value" ] == None :
210
+ # Environment flags
211
+ args .append ("--ext-%s %s" % (flag_type , _quote (key )))
212
+ elif type (val ["value" ]) == "string" :
213
+ # String flags
214
+ args .append ("--ext-%s %s=%s" % (flag_type , _quote (key ), _quote (val ["value" ])))
215
+ elif type (val ["value" ]) == "File" :
216
+ # Files and library flags
182
217
file = val ["value" ]
183
- args .append ("--ext-code-file %s=%s" % (_quote (key ), _quote (file .short_path if short_path else file .path )))
218
+ file_path = file .short_path if short_path else file .path
219
+ args .append ("--ext-%s-file %s=%s" % (flag_type , _quote (key ), _quote (file_path )))
184
220
else :
185
- fail ("The {} key has an unknown extvar type {}: {}" .format (key , val ["type" ] , val ["sources" ]))
221
+ fail ("The {} key has an unknown extvar type {}: {}" .format (key , type ( val ["value" ]) , val ["sources" ]))
186
222
187
223
return args
188
224
0 commit comments