-
-
Notifications
You must be signed in to change notification settings - Fork 210
Add inheritance and composition section in user docs #2165
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Add inheritance and composition section in user docs #2165
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #2165 +/- ##
=======================================
Coverage 98.03% 98.03%
=======================================
Files 55 55
Lines 6005 6005
=======================================
Hits 5887 5887
Misses 118 118
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
ffef11b
to
63b12a5
Compare
Co-authored-by: fpaterour-ippon <82809424+fpaterour-ippon@users.noreply.github.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for volunteering to write documentation on this complex topic, @francesco086! 🙏 I have a few suggestions, curious what you think.
Regarding solutions: Meta-templates is another one, although I don't know how relevant it actually is in practice.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about renaming the file to reusing.md
(intentionally as a gerund for consistency with some other files like configuring.md
) to keep the URL path concise and also less specific to a particular reuse approach?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My feeling is that reusing
is too generic, and it is especially confusing in the context of copier. The first thing that would come to my mind is "Of course! I have some code that I want to re-use by creating a template! Let's see how".
What about composing
, composing_templates
, or inheriting_and_composing_templates
(too long perhaps)? Alternatively, reusing_templates
may be explicit enough.
I leave the final decision to you / the maintainers, just let me know what do you think is best and I will apply 😄
@@ -0,0 +1,36 @@ | |||
# Template Inheritance and Composition |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For consistency with the renamed file:
# Template Inheritance and Composition | |
# Template Reuse |
@@ -0,0 +1,36 @@ | |||
# Template Inheritance and Composition | |||
|
|||
## What is Inheritance, what is Composition? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps this is more concise?
## What is Inheritance, what is Composition? | |
## Inheritance vs. Composition |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
True, but it miss the key point that this section is "What are we talking about", and not only comparing them.
In my head this section was the WHAT. Then there is the WHY, and finally HOW (solutions).
docs/inheritance_and_composition.md
Outdated
|
||
Template Inheritance is a way to create a new template based on an existing one. It | ||
allows you to reuse code and structure from the parent template while adding or | ||
overriding specific parts in the child template. This is useful for creating variations |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's mostly about extensions, not so much variations – perhaps a matter of definition. Variations – in my mind – can often be addressed by conditional questions and conditional rendering in the same template. WDYT?
overriding specific parts in the child template. This is useful for creating variations | |
overriding specific parts in the child template. This is useful for creating extensions |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with what is in your head, but this is exactly the reason why I think inheritance is useful, to avoid the ugly if/else.
By the way this is exactly what I did in my example, where we did not agree that it was inheritance.
I would like to mention that if you look carefully at the example you may notice that I did start with if/else (this is the key question), but it became a horror to maintain the template. Very complex, very hard to read, and very slow tests. This is why on the next flavor I had to add, I decided for this "inheritance" approach, which worked decently, except for the drawbacks you already clarified very well, especially when it comes to copier update
.
But you made a good point, in my opinion we should mention both. An alternative term for "variation" is "flavor" by the way.
Do you think we can agree to write "This is useful for creating extensions and variations"?
docs/inheritance_and_composition.md
Outdated
Template Composition, on the other hand, is a way to combine multiple templates, | ||
independent of each other, into a single new template. In this way you can join the | ||
functionalities of multiple templates, by adding some "glue code", and avoid repetition. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you have an approach like yours using tasks that run copier copy
in mind? I've mostly thought of "composition" along the lines of https://copier.readthedocs.io/en/stable/configuring/#applying-multiple-templates-to-the-same-subproject. There, template composition means generating output from multiple templates in the same target directory, but there is no new template that is a composition of other templates.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it is up to discussion!
The solution you mentioned is what I consider the "manual" solution. But of course leaves the problem of "what if it is very common to compose always the same 3 templates" and want to make it more convenient for a user to get the 3 together and don't need to manually check that they have compatible versions?
But yes, I think that using tasks could be a way, of course with the drawbacks already mentioned.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To avoid forgetting about it, I also added this to the solutions
|
||
## Solutions | ||
|
||
### Git submodules + jinja template inheritance |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
### Git submodules + jinja template inheritance | |
### Git submodules + Jinja template inheritance |
🤓
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🥷
@@ -12,6 +12,7 @@ nav: | |||
- Generating a project: "generating.md" | |||
- Updating a project: "updating.md" | |||
- Settings: "settings.md" | |||
- Template Inheritance and Compostion: "inheritance_and_composition.md" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See above:
- Template Inheritance and Compostion: "inheritance_and_composition.md" | |
- Reusing templates: "reusing.md" |
docs/inheritance_and_composition.md
Outdated
## Why copier does not natively support inheritance and composition? | ||
|
||
<!-- TODO: add content, re-use content from https://github.com/copier-org/copier/issues/934#issuecomment-1518964035 --> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I anticipate that a proper solution might require Copier to support it natively. At least for the fork-like workflow, I think that specified template versions in parent template migrations need to be mapped to child template versions – at the very least. But it's too early to make a definitive statement.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see that coming too! But I think @yajo had some very valid points. So, let's see where do we end up to, and let's try to keep what he said in mind.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reading carefully what he wrote once again, it seems to me that everything reduces to the UNIX philosophy of "do one thing well", or am I wrong?
I filled the WHY section
Right! I think it is not really a solution for inheritance and composition tbh, but I added it to the list, and we will discuss it at due time |
Co-authored-by: Sigurd Spieckermann <2206639+sisp@users.noreply.github.com>
Co-authored-by: Sigurd Spieckermann <2206639+sisp@users.noreply.github.com>
Co-authored-by: Sigurd Spieckermann <2206639+sisp@users.noreply.github.com>
Contributes to #934