-
-
Notifications
You must be signed in to change notification settings - Fork 198
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
Allow finer control over execution of tasks #240
Comments
Yes, I think that it should work similar to migrations: pre and post. We could even have pre-copy, pre-update, post-copy, post-update. That should be enough, right? |
@baurmatt had a good idea at #238 (comment): add IMHO the path to follow. |
I'd love to have those indeed. Would this be accomplished with both
|
Yes, well... definitely this couldn't be backwards-compatible after all. |
I'm OK with implementing this incrementally if you'd like to make it backwards-compatible first, then add functionality like more stages. |
Hey @yajo, I'd like to start tackling this. Basically, I want to add an Do you already have an idea of how this could be implemented? run_tasks(
conf, render, [{"task": t, "extra_env": {"STAGE": "task", "OPERATION": os.environ["OPERATION"]}} for t in conf.tasks]
) ...but I'm not sure because it's like using a global variable 😅 What do you think? |
What if we apply a logic like the one explained at #326 but for tasks too? |
Isn't it exactly like what is discussed here 🙂 ? Passing everything needed (env vars) to tasks when executing them? |
Lol yes... I've got pretty clear now that copier 6 will break all things. It's gonna be fun 😆 |
Is there a preferred way of handling things in terms running commands only on creation of a new project vs. updating them now we're up on copier 7? For example: Wanting to just run git init and doing an initial commit on creation, but not on any updates afterwards? |
FWIW if you Other than that, right now there's no official way to distinguish it. Thus, this issue exists. 😁 You have to use your imagination. One workaround for you would be to check if |
That is true. I just gave it as an example. Considering I might then want to do an initial commit, and add a second or third branch, it seems like something that might get a bit iffy. I will probably do it with a python script that checks the state before running anything. |
You may consider also pushing a PR here to enhance this feature and close the issue. 😃 |
Hi all, first thank you for making and maintaining this awesome project <3. I'm having this same question:
And I've thought of creating a tag copier copy -r 0.0.0 src dest
copier update It will run over all the migrations steps you make in the future. A way to tackle this is to eventually release a As I'm new to the tool I was wondering if you think this is a good idea, or if it can backfire Thanks! PS: Our paths cross again @pawamoy :) |
Hey @lyz-code, happy to cross paths again 😄 |
Thanks @pawamoy for your quick answer. The next "solution" I can think of is to embed inside the generated project's Makefile an
Not ideal but it can be a workaround until we have the |
What is the status of this issue? Any news so far? |
Also, I would honestly call it |
For those who need a workaround, you might look into my template—https://github.com/bswck/skeleton/blob/master/handle-task-event.sh. I made a template shell script that is rendered and run every time copier operates. It's included in It has some caveats and requires Redis to communicate between task stages aka "operations", but maybe that works for you at the moment. Not sure if my shell script interacts well with existing projects, but I will be fixing it to incorporate the template into all of my Python projects. |
Thanks for providing that workaround. For the time being, the only progress is that now we require the |
Newcomer here, wondering if this suggestion is naive or preposterous... But in the interest of preserving backwards-compatibility... Could we simply add the new operations/stages as new categories? We could add a pre-run, pre-update, post-run, post-update... and then simply alias tasks to post-run and post-update? This would maintain backwards compatibility and have the benefit of not having overly complex logic branches in the tasks yaml, given many combinations of stage and operation. It would allow the yaml document to flow nicely as well, allowing users to order the headers as they want. For what it's worth, I'd be extremely eager to work on this. It's the only thing preventing me from migrating to copier. I need a pre-check that will prevent the project from being generated if the user doesn't have a certain tool installed. |
My favorite approach would be a single declarative Python script that would allow to register callbacks for each event; with overall appearance similar to # hooks.py
from copier import after_copy, after_update, session
@after_update
def teardown_update() -> None:
session.run("somecmd")
@after_copy
def create_repo() -> None:
should_create_repo: bool = ...
if should_create_repo:
session.run("gh", "repo", "create", ...) |
@bswck That would only work for Python scripts, but tasks can use executables written in any language. |
Why? I just showed an example of invoking the GitHub CLI. If you're referring to rendering scripts and then running them, A Python layer for registering callbacks would not only make things easier, but also extend the overall ability to control tasks. If you are, however, opposing the idea of having to define a Python script, I would argue then that the potential benefit justifies the trade-off. |
@bswck, @sisp meant that providing this feature through Python decorators like |
FWIW I was scared away from yeoman because I didn't want to write js just for a template. In the same fashion I don't want to scare away js devs by requiring writing hooks in Python. The truth is that we can just use env variables and command calls, and that just gives the dev all possibilities. Copier has remained as lang-agnostic as possible and I wouldn't like to loose that. |
in case it's helpful for others, I am testing this workaround which seems to work to only execute the tasks during the first execution of copier to initialize my project (create the required gitlab repos and extra stuff I don't want to repeat when doing in _tasks:
- bash .project_init.sh and in #!/bin/bash
if [[ $PWD == /tmp* ]]; then
echo "We are /tmp/. This is copier update. Exiting"
exit 0
fi
if [ -d .git/ ]; then
echo "This project is already initialized. Exiting"
exit 0
fi
git init
git add .
git commit -a -m 'First commit by copier'
# here more initilization stuff Script .project_init.sh` is another template in my copier project. I need to do more testing but so far it seems to work for me |
I just saw the 'good for newbies' label and could potentially set aside some time to work on this. Has there been any progress yet, or would it pretty much be a clean slate to work from |
@sevdesk-ryanp thanks for offering your help! There is an unfinished PR here: #1511, if you want to take a look (and maybe take over). |
Another workaround I have been using to avoid running intialization tasks on
And then the extension defines
The This will require the |
For small tasks I use this: _tasks:
- test "${PWD#'/tmp'}" = "$PWD" && git init # on first time only |
I don't know if this goes here. I was looking for a way to populate some of the defaults by "proding" the system. Either directly in the questions, or as a task recording variables to be used during the questions. email:
type: str
help: What is your email address?
default_cmd:
command: git config user.email |
@KalleDK You can create a Jinja extension for this. See, e.g., https://github.com/pawamoy/copier-pdm/blob/adff9b64887d0b4c9ec0b42de1698b34858a511e/extensions.py#L23-L27 & https://github.com/pawamoy/copier-pdm/blob/adff9b64887d0b4c9ec0b42de1698b34858a511e/copier.yml#L11. |
Thank you - works like a charm.. Just needed to inject the extension loader :) |
I don't think this issue should be closed. Seems like #1733 didn't fully resolve this issue. The good thing is that the good work in #1510 and #1733 paved the way to implement a PR for this more easily. #1510 gave Tasks the ability to use the As a result, a way to resolve this issue would be:
I can make a PR for this some time this week. However this has the implication that |
I would like to discuss about a way to improve the usability of tasks. In short, I think it would be great to have control over when (and if) a task is executed.
Related to #196 and #238.
The text was updated successfully, but these errors were encountered: