Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add UV_CUSTOM_COMPILE_COMMAND support to uv pip compile #2554

Merged
merged 5 commits into from
Mar 20, 2024
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
20 changes: 18 additions & 2 deletions crates/uv/src/commands/pip_compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ pub(crate) async fn pip_compile(
no_emit_packages: Vec<PackageName>,
include_annotations: bool,
include_header: bool,
custom_compile_command: Option<String>,
include_index_url: bool,
include_find_links: bool,
index_locations: IndexLocations,
Expand Down Expand Up @@ -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
)
Comment on lines +375 to +379
Copy link
Contributor Author

@BakerNet BakerNet Mar 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternatively, if you prefer to have it inline:

Suggested change
cmd(
include_index_url,
include_find_links,
custom_compile_command
)
custom_compile_command.unwrap_or(cmd(include_index_url, include_find_links))

and revert the changes in cmd function

)
.green()
)?;
}

Expand Down Expand Up @@ -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>,
) -> String {
if let Some(cmd_str) = custom_compile_command {
return cmd_str;
}
Comment on lines +447 to +454
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If above suggestion, revert

Suggested change
fn cmd(
include_index_url: bool,
include_find_links: bool,
custom_compile_command: Option<String>,
) -> String {
if let Some(cmd_str) = custom_compile_command {
return cmd_str;
}
fn cmd(include_index_url: bool, include_find_links: bool) -> String {

let args = env::args_os()
.skip(1)
.map(|arg| arg.simplified_display().to_string())
Expand Down
5 changes: 5 additions & 0 deletions crates/uv/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,

/// Run offline, i.e., without accessing the network.
#[arg(
global = true,
Expand Down Expand Up @@ -1507,6 +1511,7 @@ async fn run() -> Result<ExitStatus> {
args.no_emit_package,
!args.no_annotate,
!args.no_header,
args.custom_compile_command,
args.emit_index_url,
args.emit_find_links,
index_urls,
Expand Down
62 changes: 62 additions & 0 deletions crates/uv/tests/pip_compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<()> {
Expand Down
Loading