7
7
from pathlib import Path
8
8
from typing import TYPE_CHECKING
9
9
10
+ import yaml
11
+
10
12
from ansible_creator .constants import GLOBAL_TEMPLATE_VARS
11
13
from ansible_creator .exceptions import CreatorError
12
14
from ansible_creator .templar import Templar
@@ -92,13 +94,33 @@ def unique_name_in_devfile(self) -> str:
92
94
final_uuid = str (uuid .uuid4 ())[:8 ]
93
95
return f"{ final_name } -{ final_uuid } "
94
96
97
+ def update_galaxy_dependency (self ) -> None :
98
+ """Update galaxy.yml file with the required dependency."""
99
+ galaxy_file = self ._add_path / "galaxy.yml"
100
+
101
+ # Load the galaxy.yml file
102
+ with galaxy_file .open ("r" , encoding = "utf-8" ) as file :
103
+ data = yaml .safe_load (file )
104
+
105
+ # Ensure the dependencies key exists
106
+ if "dependencies" not in data :
107
+ data ["dependencies" ] = {"ansible.utils" : "*" }
108
+
109
+ # Empty dependencies key or dependencies key without ansible.utils
110
+ elif not data ["dependencies" ] or "ansible.utils" not in data ["dependencies" ]:
111
+ data ["dependencies" ]["ansible.utils" ] = "*"
112
+
113
+ # Save the updated YAML back to the file
114
+ with galaxy_file .open ("w" , encoding = "utf-8" ) as file :
115
+ yaml .dump (data , file , sort_keys = False )
116
+
95
117
def _resource_scaffold (self ) -> None :
96
118
"""Scaffold the specified resource file based on the resource type.
97
119
98
120
Raises:
99
121
CreatorError: If unsupported resource type is given.
100
122
"""
101
- self .output .debug (f"Started copying { self ._project } resource to destination" )
123
+ self .output .debug (f"Started adding { self ._resource_type } to destination" )
102
124
103
125
# Call the appropriate scaffolding function based on the resource type
104
126
if self ._resource_type == "devfile" :
@@ -171,22 +193,66 @@ def _plugin_scaffold(self, plugin_path: Path) -> None:
171
193
Raises:
172
194
CreatorError: If unsupported plugin type is given.
173
195
"""
174
- self .output .debug (f"Started copying { self ._project } plugin to destination" )
196
+ self .output .debug (f"Started adding { self ._plugin_type } plugin to destination" )
175
197
176
198
# Call the appropriate scaffolding function based on the plugin type
177
- if self ._plugin_type in ("lookup" , "filter" ):
199
+ if self ._plugin_type == "action" :
200
+ self .update_galaxy_dependency ()
178
201
template_data = self ._get_plugin_template_data ()
202
+ self ._perform_action_plugin_scaffold (template_data , plugin_path )
203
+
204
+ elif self ._plugin_type == "filter" :
205
+ template_data = self ._get_plugin_template_data ()
206
+ self ._perform_filter_plugin_scaffold (template_data , plugin_path )
207
+
208
+ elif self ._plugin_type == "lookup" :
209
+ template_data = self ._get_plugin_template_data ()
210
+ self ._perform_lookup_plugin_scaffold (template_data , plugin_path )
179
211
180
212
else :
181
213
msg = f"Unsupported plugin type: { self ._plugin_type } "
182
214
raise CreatorError (msg )
183
215
184
- self ._perform_plugin_scaffold (template_data , plugin_path )
216
+ def _perform_action_plugin_scaffold (
217
+ self ,
218
+ template_data : TemplateData ,
219
+ plugin_path : Path ,
220
+ ) -> None :
221
+ resources = (
222
+ f"collection_project.plugins.{ self ._plugin_type } " ,
223
+ "collection_project.plugins.modules" ,
224
+ )
225
+ module_path = self ._add_path / "plugins" / "modules"
226
+ module_path .mkdir (parents = True , exist_ok = True )
227
+ final_plugin_path = [plugin_path , module_path ]
228
+ self ._perform_plugin_scaffold (resources , template_data , final_plugin_path )
229
+
230
+ def _perform_filter_plugin_scaffold (
231
+ self ,
232
+ template_data : TemplateData ,
233
+ plugin_path : Path ,
234
+ ) -> None :
235
+ resources = (f"collection_project.plugins.{ self ._plugin_type } " ,)
236
+ self ._perform_plugin_scaffold (resources , template_data , plugin_path )
185
237
186
- def _perform_plugin_scaffold (self , template_data : TemplateData , plugin_path : Path ) -> None :
238
+ def _perform_lookup_plugin_scaffold (
239
+ self ,
240
+ template_data : TemplateData ,
241
+ plugin_path : Path ,
242
+ ) -> None :
243
+ resources = (f"collection_project.plugins.{ self ._plugin_type } " ,)
244
+ self ._perform_plugin_scaffold (resources , template_data , plugin_path )
245
+
246
+ def _perform_plugin_scaffold (
247
+ self ,
248
+ resources : tuple [str , ...],
249
+ template_data : TemplateData ,
250
+ plugin_path : Path | list [Path ],
251
+ ) -> None :
187
252
"""Perform the actual scaffolding process using the provided template data.
188
253
189
254
Args:
255
+ resources: Tuple of resources.
190
256
template_data: TemplateData
191
257
plugin_path: Path where the plugin will be scaffolded.
192
258
@@ -195,7 +261,7 @@ def _perform_plugin_scaffold(self, template_data: TemplateData, plugin_path: Pat
195
261
destination directory contains files that will be overwritten.
196
262
"""
197
263
walker = Walker (
198
- resources = ( f"collection_project.plugins. { self . _plugin_type } " ,) ,
264
+ resources = resources ,
199
265
resource_id = self ._plugin_id ,
200
266
dest = plugin_path ,
201
267
output = self .output ,
@@ -213,6 +279,10 @@ def _perform_plugin_scaffold(self, template_data: TemplateData, plugin_path: Pat
213
279
)
214
280
raise CreatorError (msg )
215
281
282
+ # This check is for action plugins (having module file as an additional path)
283
+ if isinstance (plugin_path , list ):
284
+ plugin_path = plugin_path [0 ]
285
+
216
286
if not paths .has_conflicts () or self ._force or self ._overwrite :
217
287
copier .copy_containers (paths )
218
288
self .output .note (f"{ self ._plugin_type .capitalize ()} plugin added to { plugin_path } " )
@@ -270,10 +340,10 @@ def _get_devcontainer_template_data(self) -> TemplateData:
270
340
)
271
341
272
342
def _get_plugin_template_data (self ) -> TemplateData :
273
- """Get the template data for lookup plugin.
343
+ """Get the template data for plugin.
274
344
275
345
Returns:
276
- TemplateData: Data required for templating the lookup plugin.
346
+ TemplateData: Data required for templating the plugin.
277
347
"""
278
348
return TemplateData (
279
349
plugin_type = self ._plugin_type ,
@@ -282,10 +352,10 @@ def _get_plugin_template_data(self) -> TemplateData:
282
352
)
283
353
284
354
def _get_ee_template_data (self ) -> TemplateData :
285
- """Get the template data for lookup plugin.
355
+ """Get the template data for plugin.
286
356
287
357
Returns:
288
- TemplateData: Data required for templating the lookup plugin.
358
+ TemplateData: Data required for templating the plugin.
289
359
"""
290
360
return TemplateData (
291
361
resource_type = self ._resource_type ,
0 commit comments