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

new command to support more options #5710

Closed
wants to merge 23 commits into from
Closed
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
f5e625c
new command to support more options
juhoautio May 27, 2022
e4e036d
Fix mypy error
juhoautio May 30, 2022
5e82630
Fix the new test
juhoautio May 30, 2022
73c0cfb
Docs: add a missing bracket
juhoautio May 30, 2022
cd0bee9
Docs: add a missing bracket
juhoautio May 30, 2022
125e301
Docs: add a missing colon
juhoautio May 30, 2022
f4a190a
Try to fix the test on Windows
juhoautio May 31, 2022
7d578a1
Drop -l as a short version of --license
juhoautio Jun 7, 2022
a09c47b
Extract helpers.requirements, don't inherit InitCommand
juhoautio Jun 7, 2022
16caa17
Merge branch 'master' into new_command_args
juhoautio Jun 7, 2022
03e272c
Merge branch 'master' into new_command_args
juhoautio Sep 12, 2022
16d52ee
Merge branch 'master' into new_command_args
juhoautio Sep 12, 2022
efb1336
Extract function: create_pool
juhoautio Sep 12, 2022
ad7e353
Drop --dev-dependencies from `new`
juhoautio Sep 24, 2022
d2a1baf
Merge branch 'master' into new_command_args
juhoautio Sep 24, 2022
e5a6ea9
Merge branch 'master' into new_command_args
juhoautio Sep 24, 2022
c456461
Merge branch 'master' into new_command_args
juhoautio Oct 10, 2022
865c652
Move pool creation inline
juhoautio Oct 10, 2022
b3e5364
Pool -> AbstractRepository
juhoautio Oct 10, 2022
b5ccc78
Merge branch 'master' into new_command_args
juhoautio Oct 10, 2022
4341009
Revert "Pool -> AbstractRepository"
juhoautio Oct 10, 2022
b9ba428
Merge branch 'master' into new_command_args
juhoautio Oct 11, 2022
f3b66b3
Merge branch 'master' into new_command_args
juhoautio Oct 17, 2022
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
13 changes: 10 additions & 3 deletions docs/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,13 @@ my-package
### Options

* `--name`: Set the resulting package name.
* `--description`: Description of the package.
* `--package-version`: Set the version of the package.
* `--author`: Author of the package.
* `--python`: Compatible Python versions.
* `--license`: License of the package.
* `--dependency`: Package to require with an optional version constraint, e.g. `requests:^2.10.0` or `requests=2.11.1`. (see [add]({{< relref "#add" >}})).
* `--dev-dependency`: Development requirements, see `--dependency`.
* `--src`: Use the src layout for the project.
* `--readme`: Specify the readme file format. One of `md` (default) or `rst`.

Expand All @@ -122,9 +129,9 @@ poetry init
* `--name`: Name of the package.
* `--description`: Description of the package.
* `--author`: Author of the package.
* `--python` Compatible Python versions.
* `--dependency`: Package to require with a version constraint. Should be in format `foo:1.0.0`.
* `--dev-dependency`: Development requirements, see `--require`.
* `--python`: Compatible Python versions.
* `--dependency`: Package to require with an optional version constraint, e.g. `requests:^2.10.0` or `requests=2.11.1`. (see [add]({{< relref "#add" >}})).
* `--dev-dependency`: Development requirements, see `--dependency`.


## install
Expand Down
67 changes: 59 additions & 8 deletions src/poetry/console/commands/new.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,47 @@
import sys

from contextlib import suppress
from typing import TYPE_CHECKING

from cleo.helpers import argument
from cleo.helpers import option

from poetry.console.commands.command import Command
from poetry.console.commands.init import InitCommand


class NewCommand(Command):
if TYPE_CHECKING:
from poetry.console.commands.init import Requirements


class NewCommand(InitCommand):

name = "new"
description = "Creates a new Python project at <path>."

arguments = [argument("path", "The path to create the project at.")]
options = [
option("name", None, "Set the resulting package name.", flag=False),
option("description", None, "Description of the package.", flag=False),
option("package-version", None, "Set the version of the package.", flag=False),
option("author", None, "Author name of the package.", flag=False),
option("python", None, "Compatible Python versions.", flag=False),
option(
"dependency",
None,
"Package to require, with an optional version constraint, "
"e.g. requests:^2.10.0 or requests=2.11.1.",
flag=False,
multiple=True,
),
option(
"dev-dependency",
None,
"Package to require for development, with an optional version constraint, "
"e.g. requests:^2.10.0 or requests=2.11.1.",
flag=False,
multiple=True,
),
option("license", "l", "License of the package.", flag=False),
option("src", None, "Use the src layout for the project."),
option(
"readme",
Expand All @@ -27,7 +53,7 @@ class NewCommand(Command):
),
]

def handle(self) -> None:
def handle(self) -> int:
from pathlib import Path

from poetry.core.vcs.git import GitConfig
Expand All @@ -50,6 +76,10 @@ def handle(self) -> None:
if not name:
name = path.name

description = self.option("description") or ""
license = self.option("license") or ""
version = self.option("package-version") or "0.1.0"

if path.exists() and list(path.glob("*")):
# Directory is not empty. Aborting.
raise RuntimeError(
Expand All @@ -59,22 +89,41 @@ def handle(self) -> None:
readme_format = self.option("readme") or "md"

config = GitConfig()
author = None
if config.get("user.name"):
author = self.option("author")
if not author and config.get("user.name"):
author = config["user.name"]
author_email = config.get("user.email")
if author_email:
author += f" <{author_email}>"

current_env = SystemEnv(Path(sys.executable))
default_python = "^" + ".".join(str(v) for v in current_env.version_info[:2])

python = self.option("python")
if not python:
python = "^" + ".".join(str(v) for v in current_env.version_info[:2])

requirements: Requirements = {}
if self.option("dependency"):
requirements = self._format_requirements(
self._determine_requirements(self.option("dependency"))
)

dev_requirements: Requirements = {}
if self.option("dev-dependency"):
dev_requirements = self._format_requirements(
self._determine_requirements(self.option("dev-dependency"))
)

layout_ = layout_cls(
name,
"0.1.0",
version,
description=description,
author=author,
license=license,
python=python,
dependencies=requirements,
dev_dependencies=dev_requirements,
readme_format=readme_format,
python=default_python,
)
layout_.create(path)

Expand All @@ -87,3 +136,5 @@ def handle(self) -> None:
f"Created package <info>{layout_._package_name}</> in"
f" <fg=blue>{path.as_posix()}</>"
)

return 0
49 changes: 48 additions & 1 deletion tests/console/commands/test_new.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
import pytest

from poetry.factory import Factory
from tests.helpers import get_package


if TYPE_CHECKING:
from cleo.testers.command_tester import CommandTester

from poetry.poetry import Poetry
from tests.helpers import TestRepository
from tests.types import CommandTesterFactory


Expand All @@ -21,7 +23,10 @@ def tester(command_tester_factory: CommandTesterFactory) -> CommandTester:


def verify_project_directory(
path: Path, package_name: str, package_path: str, include_from: str | None = None
path: Path,
package_name: str,
package_path: str,
include_from: str | None = None,
) -> Poetry:
package_path = Path(package_path)
assert path.is_dir()
Expand Down Expand Up @@ -170,3 +175,45 @@ def test_command_new_with_readme(fmt: str | None, tester: CommandTester, tmp_dir

poetry = verify_project_directory(path, package, package, None)
assert poetry.local_config.get("readme") == f"README.{fmt or 'md'}"


def test_command_new_with_dependencies(
tester: CommandTester, repo: TestRepository, tmp_dir: str
):
repo.add_package(get_package("pendulum", "2.0.0"))
repo.add_package(get_package("pytest", "3.6.0"))

package_path = "custompackage"
path = Path(tmp_dir) / package_path
options = [
path.as_posix(),
"--name custompackage",
"--package-version 1.2.3",
"--description 'My Description'",
"--author 'Your Name <[email protected]>' ",
"--license 'My License'",
"--python '^3.8' ",
"--dependency pendulum",
"--dev-dependency pytest",
]
tester.execute(" ".join(options))
poetry = verify_project_directory(path, "custompackage", package_path, None)

expected = """\
[tool.poetry]
name = "custompackage"
version = "1.2.3"
description = "My Description"
authors = ["Your Name <[email protected]>"]
license = "My License"
readme = "README.md"

[tool.poetry.dependencies]
python = "^3.8"
pendulum = "^2.0.0"

[tool.poetry.group.dev.dependencies]
pytest = "^3.6.0"
"""
# Replacing possible \r\n because as_string adds them on Windows
assert expected in poetry.pyproject.data.as_string().replace("\r\n", "\n")