Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion crates/uv-cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3309,7 +3309,9 @@ pub struct InitArgs {
///
/// Disables creating extra files like `README.md`, the `src/` tree, `.python-version` files,
/// etc.
#[arg(long, conflicts_with = "script")]
///
/// When combined with `--script`, the script will only contain the inline metadata header.
#[arg(long)]
pub bare: bool,

/// Create a virtual project, rather than a package.
Expand Down
3 changes: 3 additions & 0 deletions crates/uv-scripts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ impl Pep723Script {
file: impl AsRef<Path>,
requires_python: &VersionSpecifiers,
existing_contents: Option<Vec<u8>>,
bare: bool,
) -> Result<(), Pep723Error> {
let file = file.as_ref();

Expand Down Expand Up @@ -305,6 +306,8 @@ impl Pep723Script {
indoc::formatdoc! {r"
{shebang}{metadata}
{contents}" }
} else if bare {
metadata
} else {
indoc::formatdoc! {r#"
{metadata}
Expand Down
10 changes: 6 additions & 4 deletions crates/uv/src/commands/project/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ pub(crate) async fn init(

init_script(
path,
bare,
python,
install_mirrors,
client_builder,
Expand Down Expand Up @@ -168,7 +169,7 @@ pub(crate) async fn init(
.await?;

// Create the `README.md` if it does not already exist.
if !no_readme {
if !no_readme && !bare {
let readme = path.join("README.md");
if !readme.exists() {
fs_err::write(readme, String::new())?;
Expand Down Expand Up @@ -201,6 +202,7 @@ pub(crate) async fn init(
#[allow(clippy::fn_params_excessive_bools)]
async fn init_script(
script_path: &Path,
bare: bool,
python: Option<String>,
install_mirrors: PythonInstallMirrors,
client_builder: &BaseClientBuilder<'_>,
Expand Down Expand Up @@ -275,7 +277,7 @@ async fn init_script(
fs_err::tokio::create_dir_all(parent).await?;
}

Pep723Script::create(script_path, requires_python.specifiers(), content).await?;
Pep723Script::create(script_path, requires_python.specifiers(), content, bare).await?;

Ok(())
}
Expand Down Expand Up @@ -829,7 +831,7 @@ impl InitProjectKind {
author.as_ref(),
description,
no_description,
no_readme,
no_readme || bare,
);

// Include additional project configuration for packaged applications
Expand Down Expand Up @@ -908,7 +910,7 @@ impl InitProjectKind {
author.as_ref(),
description,
no_description,
no_readme,
no_readme || bare,
);

// Always include a build system if the project is packaged.
Expand Down
2 changes: 1 addition & 1 deletion crates/uv/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ impl InitSettings {
no_description,
vcs: vcs.or(bare.then_some(VersionControlSystem::None)),
build_backend,
no_readme: no_readme || bare,
no_readme,
author_from,
pin_python: flag(pin_python, no_pin_python, "pin-python").unwrap_or(!bare),
no_workspace,
Expand Down
36 changes: 36 additions & 0 deletions crates/uv/tests/it/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -700,6 +700,42 @@ fn init_script() -> Result<()> {
Ok(())
}

/// Using `--bare` with `--script` omits the default script content.
#[test]
fn init_script_bare() -> Result<()> {
let context = TestContext::new("3.12");

let child = context.temp_dir.child("foo");
child.create_dir_all()?;

let script = child.join("main.py");

uv_snapshot!(context.filters(), context.init().current_dir(&child).arg("--script").arg("--bare").arg("main.py"), @r###"
success: true
exit_code: 0
----- stdout -----

----- stderr -----
Initialized script at `main.py`
"###);

let script = fs_err::read_to_string(&script)?;
insta::with_settings!({
filters => context.filters(),
}, {
assert_snapshot!(
script, @r###"
# /// script
# requires-python = ">=3.12"
# dependencies = []
# ///
"###
);
});

Ok(())
}

// Ensure python versions passed as arguments are present in file metadata
#[test]
fn init_script_python_version() -> Result<()> {
Expand Down
Loading