Making Copier generate a complete project #908
Replies: 6 comments 32 replies
-
Copier accepts arbitrary data as answers (JSON, YAML), so users could definitely paste their OpenAPI schema there. However, you won't be able to create complex file trees as you cannot use Jinja loops for directories/files creation. Still, Copier has the concept of tasks, which can be Python scripts, and which you could use to create the complex file tree, but you'd probably lose some of the update system cleverness. Definitely worth a try though ^^ |
Beta Was this translation helpful? Give feedback.
-
In the worst scenario, I don't think you need to fork copier. You can use it as a library to render projects. That way you can add any custom automation on top of it while benefiting from all its good things. It's not copier's main target use case, but it's supported. You can use that to download the API spec and pass it to copier as an answer, like @pawamoy said. Or to apply different templates on different directories in loop. |
Beta Was this translation helpful? Give feedback.
-
Thank you @pawamoy and @yajo for your answers.
Yes, that was the idea. I already have a program to do all those things, just wanted to replace the rendering phase. I think my issue boils down to a couple of questions.
|
Beta Was this translation helpful? Give feedback.
-
Turns out it wasn't that difficult. it adds two new special variables: _items and _item I'm just about to try it in my project, but I thought I'd get some early feedback from you. Let me know if you like to merge it at some point, and if so, if I should change anything before that. Otherwise I keep it as a fork. |
Beta Was this translation helpful? Give feedback.
-
I have another idea how to implement "looped templates" which IMO generalizes to a broader range of use cases. It will allow to handle nested loops and offers granular control over whether a new loop context should be created or whether an item in the current loop context should be accessed. Let me elaborate. A loop context can be created in three places and this context is propagated down in the filesystem hierarchy:
This may be a bit abstract, so let's get more concrete. With the following variables available in the Jinja context strings = ['a', 'b', 'c']
integers = [1, 2, 3]
dicts = [
{
'folder_name': 'folder_1',
'file_name': 'file_1',
},
{
'folder_name': 'folder_2',
'file_name': 'file_2',
},
{
'folder_name': 'folder_3',
'file_name': 'file_3',
},
] a Copier template
would generate the project
where in the file content of In addition, if a
would generate the project:
And similar to So what's happening here? And just like with regular Jinja Where these iterables come from is another possibly separate topic for discussion. Above, the examples by @rafalkrupinski showed a new special variable _items: # or `_data` or ...
strings:
- a
- b
- c
integers:
- 1
- 2
- 3
dicts:
- folder_name: folder_1
file_name: file_1
# ... And as also shown above, these lists could be populated dynamically with rendered answers from the questionnaire. Answers to YAML questions could be lists as well, or once I've implemented #750, list answers would also be more native to Copier. I haven't done a proof of concept implementation, but I imagine this should be possible. WDYT, @rafalkrupinski @yajo @pawamoy? |
Beta Was this translation helpful? Give feedback.
-
I've written a POC in a side project inspired by @sisp idea. It turned out quite simple really (at least as a side project, not sure about integration with copier).
The name template might look like this; I guess The project is here and currently uses Mako templates. |
Beta Was this translation helpful? Give feedback.
-
Hi Copier Community,
I'm the author of Lapidary, an API client library and generator.
The generator generates function stubs and a complete model (actual function calls are handled by the library at run-time) required to talk to a JSON API.
I was wondering, would it be hard to change Copier in such way, to be able to generate a complete project, like Lapidary does? If it's possible without large architectural changes, I'd make a PR or just fork the project.
The difference is that, AFAICT, Copier accepts only a single value for a key used to generate package or module, while Lapidary accepts a model structured as a tree. It generates a variable number of packages, each containing a set of modules, each containing a variable number of classes. All that based on an OpenAPI document.
Beta Was this translation helpful? Give feedback.
All reactions