Skip to content

Commit

Permalink
Allow uv crate to be used as a library
Browse files Browse the repository at this point in the history
  • Loading branch information
zanieb committed Jun 29, 2024
1 parent 3a627f3 commit 0022fe7
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 72 deletions.
7 changes: 7 additions & 0 deletions crates/uv/src/bin/uv.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use std::process::ExitCode;

use uv::main as uv_main;

fn main() -> ExitCode {
uv_main(std::env::args_os())
}
151 changes: 79 additions & 72 deletions crates/uv/src/main.rs → crates/uv/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::env;
use std::ffi::OsString;
use std::fmt::Write;
use std::io::stdout;
use std::path::PathBuf;
Expand Down Expand Up @@ -47,76 +48,15 @@ static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
#[global_allocator]
static GLOBAL: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc;

mod commands;
mod logging;
mod printer;
mod settings;
mod shell;
mod version;

#[instrument]
async fn run() -> Result<ExitStatus> {
let cli = match Cli::try_parse() {
Ok(cli) => cli,
Err(mut err) => {
if let Some(ContextValue::String(subcommand)) = err.get(ContextKind::InvalidSubcommand)
{
match subcommand.as_str() {
"compile" | "lock" => {
err.insert(
ContextKind::SuggestedSubcommand,
ContextValue::String("uv pip compile".to_string()),
);
}
"sync" => {
err.insert(
ContextKind::SuggestedSubcommand,
ContextValue::String("uv pip sync".to_string()),
);
}
"install" | "add" => {
err.insert(
ContextKind::SuggestedSubcommand,
ContextValue::String("uv pip install".to_string()),
);
}
"uninstall" | "remove" => {
err.insert(
ContextKind::SuggestedSubcommand,
ContextValue::String("uv pip uninstall".to_string()),
);
}
"freeze" => {
err.insert(
ContextKind::SuggestedSubcommand,
ContextValue::String("uv pip freeze".to_string()),
);
}
"list" => {
err.insert(
ContextKind::SuggestedSubcommand,
ContextValue::String("uv pip list".to_string()),
);
}
"show" => {
err.insert(
ContextKind::SuggestedSubcommand,
ContextValue::String("uv pip show".to_string()),
);
}
"tree" => {
err.insert(
ContextKind::SuggestedSubcommand,
ContextValue::String("uv pip tree".to_string()),
);
}
_ => {}
}
}
err.exit()
}
};
pub(crate) mod commands;
pub(crate) mod logging;
pub(crate) mod printer;
pub(crate) mod settings;
pub(crate) mod shell;
pub(crate) mod version;

#[instrument(skip_all)]
async fn run(cli: Cli) -> Result<ExitStatus> {
// enable flag to pick up warnings generated by workspace loading.
if !cli.global_args.quiet {
uv_warnings::enable();
Expand Down Expand Up @@ -903,7 +843,74 @@ async fn run() -> Result<ExitStatus> {
}
}

fn main() -> ExitCode {
pub fn main<I, T>(args: I) -> ExitCode
where
I: IntoIterator<Item = T>,
T: Into<OsString> + Clone,
{
// `std::env::args` is not `Send` so we parse before passing to our runtime
// https://github.com/rust-lang/rust/pull/48005
let cli = match Cli::try_parse_from(args) {
Ok(cli) => cli,
Err(mut err) => {
if let Some(ContextValue::String(subcommand)) = err.get(ContextKind::InvalidSubcommand)
{
match subcommand.as_str() {
"compile" | "lock" => {
err.insert(
ContextKind::SuggestedSubcommand,
ContextValue::String("uv pip compile".to_string()),
);
}
"sync" => {
err.insert(
ContextKind::SuggestedSubcommand,
ContextValue::String("uv pip sync".to_string()),
);
}
"install" | "add" => {
err.insert(
ContextKind::SuggestedSubcommand,
ContextValue::String("uv pip install".to_string()),
);
}
"uninstall" | "remove" => {
err.insert(
ContextKind::SuggestedSubcommand,
ContextValue::String("uv pip uninstall".to_string()),
);
}
"freeze" => {
err.insert(
ContextKind::SuggestedSubcommand,
ContextValue::String("uv pip freeze".to_string()),
);
}
"list" => {
err.insert(
ContextKind::SuggestedSubcommand,
ContextValue::String("uv pip list".to_string()),
);
}
"show" => {
err.insert(
ContextKind::SuggestedSubcommand,
ContextValue::String("uv pip show".to_string()),
);
}
"tree" => {
err.insert(
ContextKind::SuggestedSubcommand,
ContextValue::String("uv pip tree".to_string()),
);
}
_ => {}
}
}
err.exit()
}
};

let result = if let Ok(stack_size) = env::var("UV_STACK_SIZE") {
// Artificially limit the stack size to test for stack overflows. Windows has a default stack size of 1MB,
// which is lower than the linux and mac default.
Expand All @@ -915,7 +922,7 @@ fn main() -> ExitCode {
.thread_stack_size(stack_size)
.build()
.expect("Failed building the Runtime")
.block_on(run())
.block_on(run(cli))
};
std::thread::Builder::new()
.stack_size(stack_size)
Expand All @@ -928,7 +935,7 @@ fn main() -> ExitCode {
.enable_all()
.build()
.expect("Failed building the Runtime")
.block_on(run())
.block_on(run(cli))
};

match result {
Expand Down

0 comments on commit 0022fe7

Please sign in to comment.