2023-11-16 21:49:48 +01:00
|
|
|
use std::io;
|
|
|
|
|
use std::path::PathBuf;
|
|
|
|
|
|
|
|
|
|
use clap::Parser;
|
|
|
|
|
use directories::ProjectDirs;
|
2023-11-28 18:11:14 +01:00
|
|
|
|
|
|
|
|
use crate::Cache;
|
2023-11-16 21:49:48 +01:00
|
|
|
|
|
|
|
|
#[derive(Parser, Debug, Clone)]
|
2024-08-01 23:01:22 +08:00
|
|
|
#[command(next_help_heading = "Cache options")]
|
2023-11-16 21:49:48 +01:00
|
|
|
pub struct CacheArgs {
|
2024-07-16 16:50:04 -04:00
|
|
|
/// Avoid reading from or writing to the cache, instead using a temporary directory for the
|
|
|
|
|
/// duration of the operation.
|
2024-02-15 17:34:42 -06:00
|
|
|
#[arg(
|
|
|
|
|
global = true,
|
|
|
|
|
long,
|
|
|
|
|
short,
|
|
|
|
|
alias = "no-cache-dir",
|
2024-04-18 18:56:46 -04:00
|
|
|
env = "UV_NO_CACHE",
|
|
|
|
|
value_parser = clap::builder::BoolishValueParser::new(),
|
2024-02-15 17:34:42 -06:00
|
|
|
)]
|
2024-04-18 18:56:46 -04:00
|
|
|
pub no_cache: bool,
|
2023-11-16 21:49:48 +01:00
|
|
|
|
|
|
|
|
/// Path to the cache directory.
|
2024-03-09 16:47:46 -08:00
|
|
|
///
|
|
|
|
|
/// Defaults to `$HOME/Library/Caches/uv` on macOS, `$XDG_CACHE_HOME/uv` or `$HOME/.cache/uv` on
|
2024-04-26 22:28:00 -04:00
|
|
|
/// Linux, and `{FOLDERID_LocalAppData}\uv\cache` on Windows.
|
2024-02-15 12:49:27 -06:00
|
|
|
#[arg(global = true, long, env = "UV_CACHE_DIR")]
|
2024-04-17 13:03:29 -04:00
|
|
|
pub cache_dir: Option<PathBuf>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Cache {
|
|
|
|
|
/// Prefer, in order:
|
2024-08-19 15:51:45 -05:00
|
|
|
///
|
2024-04-17 13:03:29 -04:00
|
|
|
/// 1. A temporary cache directory, if the user requested `--no-cache`.
|
|
|
|
|
/// 2. The specific cache directory specified by the user via `--cache-dir` or `UV_CACHE_DIR`.
|
|
|
|
|
/// 3. The system-appropriate cache directory.
|
|
|
|
|
/// 4. A `.uv_cache` directory in the current working directory.
|
|
|
|
|
///
|
|
|
|
|
/// Returns an absolute cache dir.
|
2024-04-18 18:56:46 -04:00
|
|
|
pub fn from_settings(no_cache: bool, cache_dir: Option<PathBuf>) -> Result<Self, io::Error> {
|
|
|
|
|
if no_cache {
|
2024-04-17 13:03:29 -04:00
|
|
|
Cache::temp()
|
|
|
|
|
} else if let Some(cache_dir) = cache_dir {
|
2024-05-28 11:19:08 +02:00
|
|
|
Ok(Cache::from_path(cache_dir))
|
2024-04-17 13:03:29 -04:00
|
|
|
} else if let Some(project_dirs) = ProjectDirs::from("", "", "uv") {
|
2024-05-28 11:19:08 +02:00
|
|
|
Ok(Cache::from_path(project_dirs.cache_dir()))
|
2024-04-17 13:03:29 -04:00
|
|
|
} else {
|
2024-05-28 11:19:08 +02:00
|
|
|
Ok(Cache::from_path(".uv_cache"))
|
2024-04-17 13:03:29 -04:00
|
|
|
}
|
|
|
|
|
}
|
2023-11-16 21:49:48 +01:00
|
|
|
}
|
|
|
|
|
|
2023-11-28 18:11:14 +01:00
|
|
|
impl TryFrom<CacheArgs> for Cache {
|
2023-11-16 21:49:48 +01:00
|
|
|
type Error = io::Error;
|
|
|
|
|
|
|
|
|
|
fn try_from(value: CacheArgs) -> Result<Self, Self::Error> {
|
2024-04-17 13:03:29 -04:00
|
|
|
Cache::from_settings(value.no_cache, value.cache_dir)
|
2023-11-16 21:49:48 +01:00
|
|
|
}
|
|
|
|
|
}
|