Fix issue live preview#2747
Conversation
There was a problem hiding this comment.
I don't think the current implementation is a correct solution to the problem.
- It is insufficient to run
Worker._render_templateon source file changes – this ignores changes in the questionnaire, which likely affects the render context, and doesn't (re)run tasks, which may be essential to the render output. - It doesn't remove files that were previously rendered but are no longer rendered after a template change – files are only added or replaced.
I imagine a (more) correct solution might involve making a full copy (using run_copy) in a temporary destination, computing the filetree diff between the temporary copy and the user-specified destination, and syncing the latter to the former. But this is just the first idea that has come to my mind right now. I imagine the devil is in the details. For example, for good UX you'll likely want to ask only questions that were changed in copier.yml, but a template might not render an answers file which is what copier update --skip-answered uses.
Currently, template developers have no way to preview how their template renders
without going through a slow manual cycle of committing changes, pushing to
GitHub, switching to a downstream project, and runningcopier update. This
makes template development extremely tedious and slow, especially for small
iterative changes.
I think serious template authors need a test suite to test their templates, this manual cycle is not sustainable for non-trivial templates. FWIW, there's a trick that lets you avoid pushing to a Git platform when testing a local change on an existing Copier-managed project.
There was a problem hiding this comment.
I don't understand this file, it doesn't belong in this repo.
Updated contribution status and added maintainer feedback.
|
Thank you for the detailed feedback @sisp! I understand the issues with the current approach. Before I attempt a revised implementation, I have a few questions to make sure I'm on the right track:
I want to make sure I implement this correctly before writing more code. Thanks for your patience with a first-time contributor! |
What does this PR do?
Adds a new
copier previewcommand that allows template developers to previewhow their template renders locally with hot-reloading. When a developer runs
copier preview <template_src> <destination_path>, Copier renders the templateimmediately and then watches the template directory for any file changes,
automatically re-rendering whenever a change is saved.
Why was this PR needed?
Currently, template developers have no way to preview how their template renders
without going through a slow manual cycle of committing changes, pushing to
GitHub, switching to a downstream project, and running
copier update. Thismakes template development extremely tedious and slow, especially for small
iterative changes.
This PR implements the feature requested in issue #1451 by adding a
run_previewfunction that wraps the existing_render_templatelogic in awatchfilesfile watcher loop, and exposes it as acopier previewCLIsubcommand.
What are the relevant issue numbers?
Closes #1451
Does this PR meet the acceptance criteria?
this change)