From 8ce6898f99b6308ef479b1d5130830ded021f9e4 Mon Sep 17 00:00:00 2001 From: Ulrik Sverdrup Date: Tue, 24 Sep 2024 20:51:50 +0200 Subject: [PATCH] init: Use main as default function in packaged app Create a function main as the default for a packaged app. Configure the default executable as: example-packaged-app = "example_packaged_app:main" Which is often what you want - the executable has the same name as the app. --- crates/uv/src/commands/project/init.rs | 4 ++-- crates/uv/tests/init.rs | 10 +++++----- docs/concepts/projects.md | 6 +++--- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/crates/uv/src/commands/project/init.rs b/crates/uv/src/commands/project/init.rs index 34ca18e3269e..d4b4cbe08355 100644 --- a/crates/uv/src/commands/project/init.rs +++ b/crates/uv/src/commands/project/init.rs @@ -460,7 +460,7 @@ impl InitProjectKind { if package { // Since it'll be packaged, we can add a `[project.scripts]` entry pyproject.push('\n'); - pyproject.push_str(&pyproject_project_scripts(name, "hello", "hello")); + pyproject.push_str(&pyproject_project_scripts(name, name.as_str(), "main")); // Add a build system pyproject.push('\n'); @@ -479,7 +479,7 @@ impl InitProjectKind { fs_err::write( init_py, indoc::formatdoc! {r#" - def hello() -> None: + def main() -> None: print("Hello from {name}!") "#}, )?; diff --git a/crates/uv/tests/init.rs b/crates/uv/tests/init.rs index 02891fa2e19e..afbfba1aa5b8 100644 --- a/crates/uv/tests/init.rs +++ b/crates/uv/tests/init.rs @@ -279,7 +279,7 @@ fn init_application_package() -> Result<()> { dependencies = [] [project.scripts] - hello = "foo:hello" + foo = "foo:main" [build-system] requires = ["hatchling"] @@ -294,7 +294,7 @@ fn init_application_package() -> Result<()> { }, { assert_snapshot!( init, @r###" - def hello() -> None: + def main() -> None: print("Hello from foo!") "### ); @@ -1423,7 +1423,7 @@ fn init_virtual_project() -> Result<()> { dependencies = [] [project.scripts] - hello = "foo:hello" + foo = "foo:main" [build-system] requires = ["hatchling"] @@ -1457,7 +1457,7 @@ fn init_virtual_project() -> Result<()> { dependencies = [] [project.scripts] - hello = "foo:hello" + foo = "foo:main" [build-system] requires = ["hatchling"] @@ -1552,7 +1552,7 @@ fn init_nested_virtual_workspace() -> Result<()> { dependencies = [] [project.scripts] - hello = "foo:hello" + foo = "foo:main" [build-system] requires = ["hatchling"] diff --git a/docs/concepts/projects.md b/docs/concepts/projects.md index 2bf6f67055f8..39a38481aad5 100644 --- a/docs/concepts/projects.md +++ b/docs/concepts/projects.md @@ -231,7 +231,7 @@ example-packaged-app But the module defines a CLI function: ```python title="__init__.py" -def hello() -> None: +def main() -> None: print("Hello from example-packaged-app!") ``` @@ -247,7 +247,7 @@ requires-python = ">=3.11" dependencies = [] [project.scripts] -hello = "example_packaged_app:hello" +example-packaged-app = "example_packaged_app:hello" [build-system] requires = ["hatchling"] @@ -257,7 +257,7 @@ build-backend = "hatchling.build" Which can be executed with `uv run`: ```console -$ uv run hello +$ uv run example-packaged-app Hello from example-packaged-app! ```