new.py (poetry-1.1.15) | : | new.py (poetry-1.2.0) | ||
---|---|---|---|---|
from __future__ import annotations | ||||
import sys | import sys | |||
from cleo import argument | from contextlib import suppress | |||
from cleo import option | ||||
from poetry.utils.helpers import module_name | from cleo.helpers import argument | |||
from cleo.helpers import option | ||||
from .command import Command | from poetry.console.commands.command import Command | |||
class NewCommand(Command): | class NewCommand(Command): | |||
name = "new" | name = "new" | |||
description = "Creates a new Python project at <path>." | description = "Creates a new Python project at <path>." | |||
arguments = [argument("path", "The path to create the project at.")] | arguments = [argument("path", "The path to create the project at.")] | |||
options = [ | options = [ | |||
option("name", None, "Set the resulting package name.", flag=False), | option("name", None, "Set the resulting package name.", flag=False), | |||
option("src", None, "Use the src layout for the project."), | option("src", None, "Use the src layout for the project."), | |||
option( | ||||
"readme", | ||||
None, | ||||
"Specify the readme file format. One of md (default) or rst", | ||||
flag=False, | ||||
), | ||||
] | ] | |||
def handle(self): | def handle(self) -> int: | |||
from poetry.core.semver import parse_constraint | from pathlib import Path | |||
from poetry.core.vcs.git import GitConfig | from poetry.core.vcs.git import GitConfig | |||
from poetry.layouts import layout | from poetry.layouts import layout | |||
from poetry.utils._compat import Path | ||||
from poetry.utils.env import SystemEnv | from poetry.utils.env import SystemEnv | |||
if self.option("src"): | if self.option("src"): | |||
layout_ = layout("src") | layout_cls = layout("src") | |||
else: | else: | |||
layout_ = layout("standard") | layout_cls = layout("standard") | |||
path = Path(self.argument("path")) | ||||
if not path.is_absolute(): | ||||
# we do not use resolve here due to compatibility issues | ||||
# for path.resolve(strict=False) | ||||
path = Path.cwd().joinpath(path) | ||||
path = Path.cwd() / Path(self.argument("path")) | ||||
name = self.option("name") | name = self.option("name") | |||
if not name: | if not name: | |||
name = path.name | name = path.name | |||
if path.exists(): | if path.exists() and list(path.glob("*")): | |||
if list(path.glob("*")): | # Directory is not empty. Aborting. | |||
# Directory is not empty. Aborting. | raise RuntimeError( | |||
raise RuntimeError( | f"Destination <fg=yellow>{path}</> exists and is not empty" | |||
"Destination <fg=yellow>{}</> " | ) | |||
"exists and is not empty".format(path) | ||||
) | ||||
readme_format = "rst" | readme_format = self.option("readme") or "md" | |||
config = GitConfig() | config = GitConfig() | |||
author = None | author = None | |||
if config.get("user.name"): | if config.get("user.name"): | |||
author = config["user.name"] | author = config["user.name"] | |||
author_email = config.get("user.email") | author_email = config.get("user.email") | |||
if author_email: | if author_email: | |||
author += " <{}>".format(author_email) | author += f" <{author_email}>" | |||
current_env = SystemEnv(Path(sys.executable)) | current_env = SystemEnv(Path(sys.executable)) | |||
default_python = "^{}".format( | default_python = "^" + ".".join(str(v) for v in current_env.version_info | |||
".".join(str(v) for v in current_env.version_info[:2]) | [:2]) | |||
) | ||||
dev_dependencies = {} | ||||
python_constraint = parse_constraint(default_python) | ||||
if parse_constraint("<3.5").allows_any(python_constraint): | ||||
dev_dependencies["pytest"] = "^4.6" | ||||
if parse_constraint(">=3.5").allows_all(python_constraint): | ||||
dev_dependencies["pytest"] = "^5.2" | ||||
layout_ = layout_( | layout_ = layout_cls( | |||
name, | name, | |||
"0.1.0", | "0.1.0", | |||
author=author, | author=author, | |||
readme_format=readme_format, | readme_format=readme_format, | |||
python=default_python, | python=default_python, | |||
dev_dependencies=dev_dependencies, | ||||
) | ) | |||
layout_.create(path) | layout_.create(path) | |||
path = path.resolve() | ||||
with suppress(ValueError): | ||||
path = path.relative_to(Path.cwd()) | ||||
self.line( | self.line( | |||
"Created package <info>{}</> in <fg=blue>{}</>".format( | f"Created package <info>{layout_._package_name}</> in" | |||
module_name(name), path.relative_to(Path.cwd()) | f" <fg=blue>{path.as_posix()}</>" | |||
) | ||||
) | ) | |||
return 0 | ||||
End of changes. 21 change blocks. | ||||
35 lines changed or deleted | 41 lines changed or added |