Skip to content

Commit

Permalink
add package-mode option to init and new commands
Browse files Browse the repository at this point in the history
  • Loading branch information
rHuggler committed Aug 12, 2024
1 parent b6bad9c commit 9d2a620
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 0 deletions.
4 changes: 4 additions & 0 deletions docs/basic-usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ Therefore, it is not possible to build a distribution or publish the project to
Further, when running `poetry install`, Poetry does not try to install the project itself,
but only its dependencies (same as `poetry install --no-root`).


You can also create a new project or 'initialise' a pre-populated directory in **non-package mode**
using the option `--package-mode false` when running `poetry new` or `poetry init` commands

{{% note %}}
In the [pyproject section]({{< relref "pyproject" >}}) you can see which fields are required in package mode.
{{% /note %}}
Expand Down
10 changes: 10 additions & 0 deletions src/poetry/console/commands/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ class InitCommand(Command):
multiple=True,
),
option("license", "l", "License of the package.", flag=False),
option(
"package-mode",
None,
"Operating mode of the project.",
flag=False,
default="true",
),
]

help = """\
Expand Down Expand Up @@ -145,6 +152,8 @@ def _init_pyproject(
)
version = self.ask(question)

package_mode = self.option("package-mode", default="true").lower() != "false"

description = self.option("description") or ""
if not description and is_interactive:
description = self.ask(self.create_question("Description []: ", default=""))
Expand Down Expand Up @@ -248,6 +257,7 @@ def _init_pyproject(
python=python,
dependencies=requirements,
dev_dependencies=dev_requirements,
package_mode=package_mode,
)

create_layout = not project_path.exists()
Expand Down
1 change: 1 addition & 0 deletions src/poetry/console/commands/new.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class NewCommand(InitCommand):
"dependency",
"dev-dependency",
"license",
"package-mode",
}
],
]
Expand Down
6 changes: 6 additions & 0 deletions src/poetry/layouts/layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ def __init__(
python: str = "*",
dependencies: Mapping[str, str | Mapping[str, Any]] | None = None,
dev_dependencies: Mapping[str, str | Mapping[str, Any]] | None = None,
package_mode: bool = True,
) -> None:
self._project = canonicalize_name(project)
self._package_path_relative = Path(
Expand All @@ -64,6 +65,7 @@ def __init__(

self._license = license
self._python = python
self._package_mode = package_mode
self._dependencies = dependencies or {}
self._dev_dependencies = dev_dependencies or {}

Expand Down Expand Up @@ -134,6 +136,10 @@ def generate_poetry_content(self) -> TOMLDocument:
poetry_content.remove("license")

poetry_content["readme"] = f"README.{self._readme_format}"

if not self._package_mode:
poetry_content["package-mode"] = self._package_mode

packages = self.get_package_include()
if packages:
poetry_content["packages"].append(packages)
Expand Down
31 changes: 31 additions & 0 deletions tests/console/commands/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,37 @@ def test_python_option(tester: CommandTester) -> None:
assert expected in tester.io.fetch_output()


def test_package_mode_option(tester: CommandTester) -> None:
inputs = [
"my-package", # Package name
"1.2.3", # Version
"This is a description", # Description
"n", # Author
"MIT", # License
"^3.8", # Python
"n", # Interactive packages
"n", # Interactive dev packages
"\n", # Generate
]
tester.execute("--package-mode false", inputs="\n".join(inputs))

expected = """\
[tool.poetry]
name = "my-package"
version = "1.2.3"
description = "This is a description"
authors = ["Your Name <[email protected]>"]
license = "MIT"
readme = "README.md"
package-mode = false
[tool.poetry.dependencies]
python = "^3.8"
"""

assert expected in tester.io.fetch_output()


def test_predefined_dependency(tester: CommandTester, repo: TestRepository) -> None:
repo.add_package(get_package("pendulum", "2.0.0"))

Expand Down

0 comments on commit 9d2a620

Please sign in to comment.