Skip to content

Commit

Permalink
Merge run and script
Browse files Browse the repository at this point in the history
  • Loading branch information
sdispater committed May 21, 2018
1 parent 1dd12f7 commit 06a2bd1
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
### Changed

- Improved the `show` command to make it easier to check if packages are properly installed.
- The `script` command has been deprecated, use `run` instead.
- Expanded version constraints now keep the original version's precision.

### Fixed
Expand Down
8 changes: 2 additions & 6 deletions docs/docs/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -289,11 +289,7 @@ The `run` command executes the given command inside the project's virtualenv.
poetry run python -V
```

Note that this command has no option.

## script

The `script` executes one of the scripts defined in `pyproject.toml`.
It can also executes one of the scripts defined in `pyproject.toml`.

So, if you have a script defined like this:

Expand All @@ -305,7 +301,7 @@ my-script = "my_module:main"
You can execute it like so:

```bash
poetry script my-script
poetry run my-script
```

Note that this command has no option.
Expand Down
36 changes: 36 additions & 0 deletions poetry/console/commands/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,47 @@ class RunCommand(VenvCommand):

def handle(self):
args = self.argument('args')
script = args[0]
scripts = self.poetry.local_config.get('scripts')

if scripts and script in scripts:
return self.run_script(scripts[script], args)

venv = self.venv

return venv.execute(*args)

def run_script(self, script, args):
module, callable_ = script.split(':')

src_in_sys_path = 'sys.path.append(\'src\'); ' \
if self._module.is_in_src() else ''

cmd = ['python', '-c']

cmd += [
'"import sys; '
'from importlib import import_module; '
'sys.argv = {!r}; {}'
'import_module(\'{}\').{}()"'.format(
args, src_in_sys_path, module, callable_
)
]

return self.venv.run(*cmd, shell=True, call=True)

@property
def _module(self):
from ...masonry.utils.module import Module

poetry = self.poetry
package = poetry.package
path = poetry.file.parent
module = Module(
package.name, path.as_posix()
)
return module

def merge_application_definition(self, merge_args=True):
if self._application is None \
or (self._application_definition_merged
Expand Down
10 changes: 6 additions & 4 deletions poetry/console/commands/script.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import sys

from ...masonry.utils.module import Module
from .venv_command import VenvCommand


class ScriptCommand(VenvCommand):
"""
Executes a script defined in <comment>pyproject.toml</comment>
Executes a script defined in <comment>pyproject.toml</comment>. (<error>Deprecated</error>)
script
{ script-name : The name of the script to execute }
{ args?* : The command and arguments/options to pass to the script. }
"""

def handle(self):
self.line('<warning>script is deprecated use run instead.</warning>')
self.line('')

script = self.argument('script-name')
argv = [script] + self.argument('args')

Expand Down Expand Up @@ -44,6 +44,8 @@ def handle(self):

@property
def _module(self):
from ...masonry.utils.module import Module

poetry = self.poetry
package = poetry.package
path = poetry.file.parent
Expand Down

0 comments on commit 06a2bd1

Please sign in to comment.