diff --git a/crates/uv/src/commands/pip_compile.rs b/crates/uv/src/commands/pip_compile.rs index 8afd94c855d2..808b4f0d2a3c 100644 --- a/crates/uv/src/commands/pip_compile.rs +++ b/crates/uv/src/commands/pip_compile.rs @@ -56,6 +56,7 @@ pub(crate) async fn pip_compile( no_emit_packages: Vec, include_annotations: bool, include_header: bool, + custom_compile_command: Option, include_index_url: bool, include_find_links: bool, index_locations: IndexLocations, @@ -369,7 +370,15 @@ pub(crate) async fn pip_compile( writeln!( writer, "{}", - format!("# {}", cmd(include_index_url, include_find_links,)).green() + format!( + "# {}", + cmd( + include_index_url, + include_find_links, + custom_compile_command + ) + ) + .green() )?; } @@ -435,7 +444,14 @@ pub(crate) async fn pip_compile( /// Format the `uv` command used to generate the output file. #[allow(clippy::fn_params_excessive_bools)] -fn cmd(include_index_url: bool, include_find_links: bool) -> String { +fn cmd( + include_index_url: bool, + include_find_links: bool, + custom_compile_command: Option, +) -> String { + if let Some(cmd_str) = custom_compile_command { + return cmd_str; + } let args = env::args_os() .skip(1) .map(|arg| arg.simplified_display().to_string()) diff --git a/crates/uv/src/main.rs b/crates/uv/src/main.rs index ee99de892001..2404e11733d1 100644 --- a/crates/uv/src/main.rs +++ b/crates/uv/src/main.rs @@ -324,6 +324,10 @@ struct PipCompileArgs { #[clap(long)] no_header: bool, + /// Change header comment to reflect custom command wrapping `uv pip compile`. + #[clap(long, env = "UV_CUSTOM_COMPILE_COMMAND")] + custom_compile_command: Option, + /// Run offline, i.e., without accessing the network. #[arg( global = true, @@ -1507,6 +1511,7 @@ async fn run() -> Result { args.no_emit_package, !args.no_annotate, !args.no_header, + args.custom_compile_command, args.emit_index_url, args.emit_find_links, index_urls, diff --git a/crates/uv/tests/pip_compile.rs b/crates/uv/tests/pip_compile.rs index 9430c6f1fec2..9efdbde77a82 100644 --- a/crates/uv/tests/pip_compile.rs +++ b/crates/uv/tests/pip_compile.rs @@ -3482,6 +3482,68 @@ fn no_header() -> Result<()> { Ok(()) } +/// Include custom compile command in the header. +#[test] +fn custom_compile_command() -> Result<()> { + let context = TestContext::new("3.12"); + let requirements_in = context.temp_dir.child("requirements.in"); + requirements_in.write_str("black==23.10.1")?; + + uv_snapshot!(context.compile() + .arg("requirements.in") + .arg("--custom-compile-command") + .arg("./custom-uv-compile.sh"), @r###" + success: true + exit_code: 0 + ----- stdout ----- + # This file was autogenerated by uv via the following command: + # ./custom-uv-compile.sh + black==23.10.1 + click==8.1.7 + # via black + mypy-extensions==1.0.0 + # via black + packaging==23.2 + # via black + pathspec==0.11.2 + # via black + platformdirs==4.0.0 + # via black + + ----- stderr ----- + Resolved 6 packages in [TIME] + "### + ); + + // with env var + uv_snapshot!(context.compile() + .arg("requirements.in") + .env("UV_CUSTOM_COMPILE_COMMAND", "./custom-uv-compile.sh"), @r###" + success: true + exit_code: 0 + ----- stdout ----- + # This file was autogenerated by uv via the following command: + # ./custom-uv-compile.sh + black==23.10.1 + click==8.1.7 + # via black + mypy-extensions==1.0.0 + # via black + packaging==23.2 + # via black + pathspec==0.11.2 + # via black + platformdirs==4.0.0 + # via black + + ----- stderr ----- + Resolved 6 packages in [TIME] + "### + ); + + Ok(()) +} + /// Emit warnings when users pass redundant options from `pip-compile`. #[test] fn allow_unsafe() -> Result<()> {