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 tasks #591

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions docs/docs/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,22 @@ poetry run my-script

Note that this command has no option.

## task
The `task` command executes the given task inside the project's virtualenv according to its definition in `pyproject.toml`. So if you have some tasks defined like this:
```toml
[tool.poetry.tasks]
lint = "black --check ."
test = "pytest tests/"
clean = "rm -rf temp/"
```

You can execute them like so:
```bash
poetry task lint
poetry task test
poetry task clean
```

## shell

The `shell` command spawns a shell,
Expand Down
14 changes: 14 additions & 0 deletions docs/docs/pyproject.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,20 @@ poetry = 'poetry:console.run'

Here, we will have the `poetry` script installed which will execute `console.run` in the `poetry` package.

## `tasks`

This section describes the tasks that may be run when developing the project inside its virtualenv

```toml
[tool.poetry.tasks]
test = 'pytest tests/'
clean = 'rm -rf temp/'
```

Here we have two tasks, `test` which will run the project tests and `clean` which will wipe a temporary directory from the project.

These tasks may be used in development only, they will _not_ be available to the users that have installed your package.

## `extras`

Poetry supports extras to allow expression of:
Expand Down
2 changes: 2 additions & 0 deletions poetry/console/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from .commands import SearchCommand
from .commands import ShellCommand
from .commands import ShowCommand
from .commands import TaskCommand
from .commands import UpdateCommand
from .commands import VersionCommand

Expand Down Expand Up @@ -81,6 +82,7 @@ def get_default_commands(self): # type: () -> list
SearchCommand(),
ShellCommand(),
ShowCommand(),
TaskCommand(),
UpdateCommand(),
VersionCommand(),
]
Expand Down
1 change: 1 addition & 0 deletions poetry/console/commands/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@
from .search import SearchCommand
from .shell import ShellCommand
from .show import ShowCommand
from .task import TaskCommand
from .update import UpdateCommand
from .version import VersionCommand
20 changes: 20 additions & 0 deletions poetry/console/commands/task.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from .env_command import EnvCommand


class TaskCommand(EnvCommand):
"""
Runs a task in the appropriate environment.

task
{ task : The task to run.}
"""

def handle(self):
task = self.argument("task")
tasks = self.poetry.local_config.get("tasks")

if tasks and task in tasks:
return self.run_task(tasks[task])

def run_task(self, task):
return self.env.run(*task.split(), shell=True, call=True)
michaeldel marked this conversation as resolved.
Show resolved Hide resolved
22 changes: 22 additions & 0 deletions tests/console/commands/test_task.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from cleo.testers import CommandTester

from poetry.utils._compat import Path
from poetry.poetry import Poetry


def test_task(app):
app._poetry = Poetry.create(
Path(__file__).parent.parent.parent / "fixtures" / "project_with_tasks"
)
command = app.find("task")
tester = CommandTester(command)

tester.execute("task echo")

path = Path(".temp/poetry_test_task.txt")
with path.open("r") as f:
assert "Hello World!" in f.read()

# cleanup
Path.unlink(path)
Path.rmdir(path.parent)
9 changes: 9 additions & 0 deletions tests/fixtures/project_with_tasks/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[tool.poetry]
name = "project-with-tasks"
version = "1.2.3"
description = "This is a description"
authors = ["Your Name <you@example.com>"]
license = "MIT"

[tool.poetry.tasks]
echo = "mkdir .temp && echo 'Hello World!' > .temp/poetry_test_task.txt"