diff --git a/CHANGELOG.md b/CHANGELOG.md index 9eeffc951b0..5b0462d4440 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/docs/docs/cli.md b/docs/docs/cli.md index 7aa5a64b9c5..0558a5a5e4a 100644 --- a/docs/docs/cli.md +++ b/docs/docs/cli.md @@ -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: @@ -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. diff --git a/poetry/console/commands/run.py b/poetry/console/commands/run.py index 8bba1ba367f..37919900f82 100644 --- a/poetry/console/commands/run.py +++ b/poetry/console/commands/run.py @@ -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 diff --git a/poetry/console/commands/script.py b/poetry/console/commands/script.py index 8108ad21b93..4bfed2e90bf 100644 --- a/poetry/console/commands/script.py +++ b/poetry/console/commands/script.py @@ -1,12 +1,9 @@ -import sys - -from ...masonry.utils.module import Module from .venv_command import VenvCommand class ScriptCommand(VenvCommand): """ - Executes a script defined in pyproject.toml + Executes a script defined in pyproject.toml. (Deprecated) script { script-name : The name of the script to execute } @@ -14,6 +11,9 @@ class ScriptCommand(VenvCommand): """ def handle(self): + self.line('script is deprecated use run instead.') + self.line('') + script = self.argument('script-name') argv = [script] + self.argument('args') @@ -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