packaged app: use executable named for the project and main function (#7670)

## Summary

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.
The purpose is to more often hit what the user wants, so they don't have
to even rename the command to start developing.

## Test Plan

- existing tests are updated
This commit is contained in:
bluss 2024-10-01 00:19:36 +02:00 committed by GitHub
parent 04c79aff0a
commit 67769a4985
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 11 additions and 11 deletions

View File

@ -599,7 +599,7 @@ impl InitProjectKind {
if package { if package {
// Since it'll be packaged, we can add a `[project.scripts]` entry // Since it'll be packaged, we can add a `[project.scripts]` entry
pyproject.push('\n'); 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 // Add a build system
pyproject.push('\n'); pyproject.push('\n');
@ -618,7 +618,7 @@ impl InitProjectKind {
fs_err::write( fs_err::write(
init_py, init_py,
indoc::formatdoc! {r#" indoc::formatdoc! {r#"
def hello() -> None: def main() -> None:
print("Hello from {name}!") print("Hello from {name}!")
"#}, "#},
)?; )?;

View File

@ -282,7 +282,7 @@ fn init_application_package() -> Result<()> {
dependencies = [] dependencies = []
[project.scripts] [project.scripts]
hello = "foo:hello" foo = "foo:main"
[build-system] [build-system]
requires = ["hatchling"] requires = ["hatchling"]
@ -297,13 +297,13 @@ fn init_application_package() -> Result<()> {
}, { }, {
assert_snapshot!( assert_snapshot!(
init, @r###" init, @r###"
def hello() -> None: def main() -> None:
print("Hello from foo!") print("Hello from foo!")
"### "###
); );
}); });
uv_snapshot!(context.filters(), context.run().current_dir(&child).arg("hello"), @r###" uv_snapshot!(context.filters(), context.run().current_dir(&child).arg("foo"), @r###"
success: true success: true
exit_code: 0 exit_code: 0
----- stdout ----- ----- stdout -----
@ -1621,7 +1621,7 @@ fn init_virtual_project() -> Result<()> {
dependencies = [] dependencies = []
[project.scripts] [project.scripts]
hello = "foo:hello" foo = "foo:main"
[build-system] [build-system]
requires = ["hatchling"] requires = ["hatchling"]
@ -1655,7 +1655,7 @@ fn init_virtual_project() -> Result<()> {
dependencies = [] dependencies = []
[project.scripts] [project.scripts]
hello = "foo:hello" foo = "foo:main"
[build-system] [build-system]
requires = ["hatchling"] requires = ["hatchling"]
@ -1750,7 +1750,7 @@ fn init_nested_virtual_workspace() -> Result<()> {
dependencies = [] dependencies = []
[project.scripts] [project.scripts]
hello = "foo:hello" foo = "foo:main"
[build-system] [build-system]
requires = ["hatchling"] requires = ["hatchling"]

View File

@ -231,7 +231,7 @@ example-packaged-app
But the module defines a CLI function: But the module defines a CLI function:
```python title="__init__.py" ```python title="__init__.py"
def hello() -> None: def main() -> None:
print("Hello from example-packaged-app!") print("Hello from example-packaged-app!")
``` ```
@ -247,7 +247,7 @@ requires-python = ">=3.11"
dependencies = [] dependencies = []
[project.scripts] [project.scripts]
hello = "example_packaged_app:hello" example-packaged-app = "example_packaged_app:main"
[build-system] [build-system]
requires = ["hatchling"] requires = ["hatchling"]
@ -257,7 +257,7 @@ build-backend = "hatchling.build"
Which can be executed with `uv run`: Which can be executed with `uv run`:
```console ```console
$ uv run hello $ uv run example-packaged-app
Hello from example-packaged-app! Hello from example-packaged-app!
``` ```