Skip to content
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

Add a "setup:" setting to allow running setup scripts before any command? #204

Open
jooola opened this issue May 16, 2019 · 16 comments
Open

Comments

@jooola
Copy link

jooola commented May 16, 2019

I'm looking for a tool that can run source venv/bin/activate and run the commands in that same shell. It is not cleanly doable using make, and so I tried Task.

It appears we can't do this using Task. Is there a way to have a workaround or add an option to run all commands in the same shell ?

@andreynering
Copy link
Member

Hi @jooola,

Can you explain more why you think this'd be useful? Perhaps an example...

@jooola
Copy link
Author

jooola commented May 19, 2019

When developing a Python project within a Virtual environment with all its dependencies, we always need to activate this virtual environment in order to run project tasks like testing, linting, building.

To set up the environment by hand, we run the following commands (where requirements.txt lists all the dependencies):

python3 -m venv venv
source venv/bin/activate
pip -q install --upgrade -r requirements.txt

Since the source command is altering the shell to activate it, we need to stay in it to execute the next commands. So I suggested to allow to run all the commands within the same shell.

The option should also all to run all the dependencies in the same shell.

Sorry for my previous message, I clearly wasn't describing my problem enough.

@smyrman
Copy link
Contributor

smyrman commented May 20, 2019

You can do this today as a multi-line command, but you need to somehow duplicate the setup of venv to each command, e.g. as a template variable, or just copy-paste, which ever is easier.

@jooola
Copy link
Author

jooola commented May 20, 2019

It would be something like this I guess ?

vars:
  VENV: |-
    test -d venv || python3 -m venv venv
    source venv/bin/activate


tasks:
  setup:
    cmds:
      - |
        {{.VENV}}
        pip -q install --upgrade -r requirements.txt

But dependencies inheritance cannot work in this case, that was the point of chaining dependencies. But this work around could work, I'll try it out. Thanks

@smyrman
Copy link
Contributor

smyrman commented May 20, 2019

👍

Remember to also add a set -e on of multi-line commands to ensure they exit on error.

vars:
  VENV: |-
    test -d venv || python3 -m venv venv
    source venv/bin/activate


tasks:
  setup:
    cmds:
    - |
      set -e
      {{.VENV}}
      pip -q install --upgrade -r requirements.txt

@smyrman
Copy link
Contributor

smyrman commented May 20, 2019

But dependencies inheritance cannot work in this case, that was the point of chaining dependencies.

No it can't. Dependencies, or any other cmd, can never change the environment in task, which is what would be needed for that to work. The environment now, is evaluated/generated from scratch every-time before a task is run.

At an early stage of task, there was a "set" property, that could set the environment to the output of a command, but it was deprecated with good reason.

@andreynering
Copy link
Member

As @smyrman said, multi-line strings should solve this use case. The downside is that you have to concat the setup stage in each needed command.

Maybe we could add a setup flag to task/global to setup stuff before all commands?

version: '2'

setup:
  - set -e
  - rbenv init

tasks:
  # ...

Sharing state between command calls for a single task would be possible, but it'd be likely confusing.

@stephencheng

This comment was marked as off-topic.

@andreynering
Copy link
Member

@stephencheng Interesting idea, worth studying the possibility. Thanks for sharing.

@andreynering andreynering changed the title Add ability to source venv/bin/activate Add a "setup:" setting to allow running setup scripts before any command? Sep 7, 2019
@andreynering andreynering added type: feature A new feature or functionality. proposal labels Sep 7, 2019
@ProfessorManhattan
Copy link

This is ideal but while we're waiting on this feature is there a file like ~/.bashrc that we can put functions in that we would like to be able to access globally from any task?

@andreynering
Copy link
Member

@ProfessorManhattan Interesting idea, but no, we do not support that.

This was referenced Jan 29, 2022
@ssbarnea
Copy link
Contributor

I really like the idea to allow a configurable list of commands to run before each shell task, set -e being an obvious use case.

@ProfessorManhattan
Copy link

ProfessorManhattan commented May 24, 2022 via email

@andreynering
Copy link
Member

Idea: just like the setup: proposal above, having a cleanup: setting (needs to think if it's the best name) to run after each task could make sense as well.

@andreynering
Copy link
Member

#1115 is a complement to this. Both should probably be implemented together.

@cweagans
Copy link

cleanup would be really great. My use case is that I need to do something like this:

tasks:
  foo:
    cmds:
      - echo "do something"
    preconditions:
      - op document get "TLS Cert" --output=./test.crt --force && step certificate needs-renewal --expires-in 48h --bundle test.crt

This leaves a test.crt sitting around - I'll never want that after the task runs, so having some way to clean things like that up at the very end of task execution would be helpful! I'd definitely be able to use setup: as well.

@pd93 pd93 removed the type: feature A new feature or functionality. label Dec 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants