Skip to content

Commit

Permalink
Add tool dir and toolchain dir commands (#4695)
Browse files Browse the repository at this point in the history
## Summary

Resolves #4483 
Resolves #4484 

## Test Plan

`cargo test`

```sh
❯ cargo run -- toolchain dir
warning: `uv toolchain dir` is experimental and may change without warning.
/Users/ahmedilyas/Library/Application Support/uv/toolchains

❯ cargo run -- tool dir
warning: `uv tool dir` is experimental and may change without warning.
/Users/ahmedilyas/Library/Application Support/uv/tools
```
  • Loading branch information
blueraft committed Jul 1, 2024
1 parent 65cd676 commit 081f092
Show file tree
Hide file tree
Showing 11 changed files with 123 additions and 1 deletion.
5 changes: 5 additions & 0 deletions crates/uv-cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1889,6 +1889,8 @@ pub enum ToolCommand {
List(ToolListArgs),
/// Uninstall a tool.
Uninstall(ToolUninstallArgs),
/// Show the tools directory.
Dir,
}

#[derive(Args)]
Expand Down Expand Up @@ -2013,6 +2015,9 @@ pub enum ToolchainCommand {
/// Search for a toolchain
#[command(disable_version_flag = true)]
Find(ToolchainFindArgs),

/// Show the toolchains directory.
Dir,
}

#[derive(Args)]
Expand Down
2 changes: 1 addition & 1 deletion crates/uv/src/commands/cache_dir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ use uv_fs::Simplified;

/// Show the cache directory.
pub(crate) fn cache_dir(cache: &Cache) {
anstream::println!("{}", cache.root().user_display().cyan());
anstream::println!("{}", cache.root().simplified_display().cyan());
}
2 changes: 2 additions & 0 deletions crates/uv/src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@ pub(crate) use project::run::run;
pub(crate) use project::sync::sync;
#[cfg(feature = "self-update")]
pub(crate) use self_update::self_update;
pub(crate) use tool::dir::dir as tool_dir;
pub(crate) use tool::install::install as tool_install;
pub(crate) use tool::list::list as tool_list;
pub(crate) use tool::run::run as tool_run;
pub(crate) use tool::uninstall::uninstall as tool_uninstall;
pub(crate) use toolchain::dir::dir as toolchain_dir;
pub(crate) use toolchain::find::find as toolchain_find;
pub(crate) use toolchain::install::install as toolchain_install;
pub(crate) use toolchain::list::list as toolchain_list;
Expand Down
17 changes: 17 additions & 0 deletions crates/uv/src/commands/tool/dir.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use anyhow::Context;
use owo_colors::OwoColorize;
use uv_configuration::PreviewMode;
use uv_fs::Simplified;
use uv_tool::InstalledTools;
use uv_warnings::warn_user_once;

/// Show the tool directory.
pub(crate) fn dir(preview: PreviewMode) -> anyhow::Result<()> {
if preview.is_disabled() {
warn_user_once!("`uv tool dir` is experimental and may change without warning.");
}
let installed_tools =
InstalledTools::from_settings().context("Failed to initialize tools settings")?;
anstream::println!("{}", installed_tools.root().simplified_display().cyan());
Ok(())
}
1 change: 1 addition & 0 deletions crates/uv/src/commands/tool/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub(crate) mod dir;
pub(crate) mod install;
pub(crate) mod list;
pub(crate) mod run;
Expand Down
20 changes: 20 additions & 0 deletions crates/uv/src/commands/toolchain/dir.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use anyhow::Context;
use owo_colors::OwoColorize;
use uv_configuration::PreviewMode;
use uv_fs::Simplified;
use uv_toolchain::managed::InstalledToolchains;
use uv_warnings::warn_user_once;

/// Show the toolchain directory.
pub(crate) fn dir(preview: PreviewMode) -> anyhow::Result<()> {
if preview.is_disabled() {
warn_user_once!("`uv toolchain dir` is experimental and may change without warning.");
}
let installed_toolchains =
InstalledToolchains::from_settings().context("Failed to initialize toolchain settings")?;
anstream::println!(
"{}",
installed_toolchains.root().simplified_display().cyan()
);
Ok(())
}
1 change: 1 addition & 0 deletions crates/uv/src/commands/toolchain/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub(crate) mod dir;
pub(crate) mod find;
pub(crate) mod install;
pub(crate) mod list;
12 changes: 12 additions & 0 deletions crates/uv/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -847,6 +847,12 @@ async fn run() -> Result<ExitStatus> {

commands::tool_uninstall(args.name, globals.preview, printer).await
}
Commands::Tool(ToolNamespace {
command: ToolCommand::Dir,
}) => {
commands::tool_dir(globals.preview)?;
Ok(ExitStatus::Success)
}
Commands::Toolchain(ToolchainNamespace {
command: ToolchainCommand::List(args),
}) => {
Expand Down Expand Up @@ -907,6 +913,12 @@ async fn run() -> Result<ExitStatus> {
)
.await
}
Commands::Toolchain(ToolchainNamespace {
command: ToolchainCommand::Dir,
}) => {
commands::toolchain_dir(globals.preview)?;
Ok(ExitStatus::Success)
}
}
}

Expand Down
16 changes: 16 additions & 0 deletions crates/uv/tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,14 @@ impl TestContext {
command
}

/// Create a `uv toolchain dir` command with options shared across scenarios.
pub fn toolchain_dir(&self) -> Command {
let mut command = Command::new(get_bin());
command.arg("toolchain").arg("dir");
self.add_shared_args(&mut command);
command
}

/// Create a `uv run` command with options shared across scenarios.
pub fn run(&self) -> Command {
let mut command = Command::new(get_bin());
Expand Down Expand Up @@ -420,6 +428,14 @@ impl TestContext {
command
}

/// Create a `uv tool dir` command with options shared across scenarios.
pub fn tool_dir(&self) -> Command {
let mut command = Command::new(get_bin());
command.arg("tool").arg("dir");
self.add_shared_args(&mut command);
command
}

/// Create a `uv tool uninstall` command with options shared across scenarios.
pub fn tool_uninstall(&self) -> std::process::Command {
let mut command = std::process::Command::new(get_bin());
Expand Down
25 changes: 25 additions & 0 deletions crates/uv/tests/tool_dir.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#![cfg(all(feature = "python", feature = "pypi"))]

use assert_fs::fixture::PathChild;
use common::{uv_snapshot, TestContext};

mod common;

#[test]
fn tool_dir() {
let context = TestContext::new("3.12");
let tool_dir = context.temp_dir.child("tools");
let bin_dir = context.temp_dir.child("bin");

uv_snapshot!(context.filters(), context.tool_dir()
.env("UV_TOOL_DIR", tool_dir.as_os_str())
.env("XDG_BIN_HOME", bin_dir.as_os_str()), @r###"
success: true
exit_code: 0
----- stdout -----
[TEMP_DIR]/tools
----- stderr -----
warning: `uv tool dir` is experimental and may change without warning.
"###);
}
23 changes: 23 additions & 0 deletions crates/uv/tests/toolchain_dir.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#![cfg(all(feature = "python", feature = "pypi"))]

use assert_fs::fixture::PathChild;
use common::{uv_snapshot, TestContext};

mod common;

#[test]
fn toolchain_dir() {
let context = TestContext::new("3.12");

let toolchain_dir = context.temp_dir.child("toolchains");
uv_snapshot!(context.filters(), context.toolchain_dir()
.env("UV_TOOLCHAIN_DIR", toolchain_dir.as_os_str()), @r###"
success: true
exit_code: 0
----- stdout -----
[TEMP_DIR]/toolchains
----- stderr -----
warning: `uv toolchain dir` is experimental and may change without warning.
"###);
}

0 comments on commit 081f092

Please sign in to comment.