diff --git a/docs/basic-usage.md b/docs/basic-usage.md index 425742e9fc2..191be089ff9 100644 --- a/docs/basic-usage.md +++ b/docs/basic-usage.md @@ -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 %}} diff --git a/src/poetry/console/commands/init.py b/src/poetry/console/commands/init.py index d1b9438650e..cf501f6613e 100644 --- a/src/poetry/console/commands/init.py +++ b/src/poetry/console/commands/init.py @@ -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 = """\ @@ -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="")) @@ -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() diff --git a/src/poetry/console/commands/new.py b/src/poetry/console/commands/new.py index bb0fefad961..39a31ff662b 100644 --- a/src/poetry/console/commands/new.py +++ b/src/poetry/console/commands/new.py @@ -47,6 +47,7 @@ class NewCommand(InitCommand): "dependency", "dev-dependency", "license", + "package-mode", } ], ] diff --git a/src/poetry/layouts/layout.py b/src/poetry/layouts/layout.py index f5174ba3951..106c8281681 100644 --- a/src/poetry/layouts/layout.py +++ b/src/poetry/layouts/layout.py @@ -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( @@ -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 {} @@ -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) diff --git a/tests/console/commands/test_init.py b/tests/console/commands/test_init.py index edb5ad57e17..1acbb6ef788 100644 --- a/tests/console/commands/test_init.py +++ b/tests/console/commands/test_init.py @@ -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 "] +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"))