From 8b01b53d8981367d2d627c0b28ccf5d34f2fde69 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Sat, 7 Jan 2023 22:01:20 -0500 Subject: [PATCH] Move RUFF_CACHE_DIR to Clap's env support (#1733) --- README.md | 2 +- src/cache.rs | 18 +++++++----------- src/cli.rs | 2 +- src/commands.rs | 6 +++--- 4 files changed, 12 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 1baf27c1823d5..c5268ea5cb412 100644 --- a/README.md +++ b/README.md @@ -366,7 +366,7 @@ Options: --stdin-filename The name of the file when passing it through stdin --cache-dir - Path to the cache directory + Path to the cache directory [env: RUFF_CACHE_DIR=] --show-source Show violations with source code --respect-gitignore diff --git a/src/cache.rs b/src/cache.rs index a3ba0020046b1..2eab3a66f18dd 100644 --- a/src/cache.rs +++ b/src/cache.rs @@ -1,6 +1,5 @@ use std::collections::hash_map::DefaultHasher; use std::fs; -use std::fs::{create_dir_all, File, Metadata}; use std::hash::{Hash, Hasher}; use std::io::Write; use std::path::{Path, PathBuf}; @@ -8,16 +7,15 @@ use std::path::{Path, PathBuf}; use anyhow::Result; use filetime::FileTime; use log::error; -use once_cell::sync::Lazy; use path_absolutize::Absolutize; use serde::{Deserialize, Serialize}; use crate::message::Message; use crate::settings::{flags, Settings}; +pub const CACHE_DIR_NAME: &str = ".ruff_cache"; + const CARGO_PKG_VERSION: &str = env!("CARGO_PKG_VERSION"); -static CACHE_DIR: Lazy> = Lazy::new(|| std::env::var("RUFF_CACHE_DIR").ok()); -pub const DEFAULT_CACHE_DIR_NAME: &str = ".ruff_cache"; #[derive(Serialize, Deserialize)] struct CacheMetadata { @@ -39,9 +37,7 @@ struct CheckResult { /// Return the cache directory for a given project root. Defers to the /// `RUFF_CACHE_DIR` environment variable, if set. pub fn cache_dir(project_root: &Path) -> PathBuf { - CACHE_DIR - .as_ref() - .map_or_else(|| project_root.join(DEFAULT_CACHE_DIR_NAME), PathBuf::from) + project_root.join(CACHE_DIR_NAME) } fn content_dir() -> &'static Path { @@ -60,7 +56,7 @@ fn cache_key>(path: P, settings: &Settings, autofix: flags::Autof /// Initialize the cache at the specified `Path`. pub fn init(path: &Path) -> Result<()> { // Create the cache directories. - create_dir_all(path.join(content_dir()))?; + fs::create_dir_all(path.join(content_dir()))?; // Add the CACHEDIR.TAG. if !cachedir::is_tagged(path)? { @@ -70,7 +66,7 @@ pub fn init(path: &Path) -> Result<()> { // Add the .gitignore. let gitignore_path = path.join(".gitignore"); if !gitignore_path.exists() { - let mut file = File::create(gitignore_path)?; + let mut file = fs::File::create(gitignore_path)?; file.write_all(b"*")?; } @@ -91,7 +87,7 @@ fn read_sync(cache_dir: &Path, key: u64) -> Result, std::io::Error> { /// Get a value from the cache. pub fn get>( path: P, - metadata: &Metadata, + metadata: &fs::Metadata, settings: &Settings, autofix: flags::Autofix, ) -> Option> { @@ -115,7 +111,7 @@ pub fn get>( /// Set a value in the cache. pub fn set>( path: P, - metadata: &Metadata, + metadata: &fs::Metadata, settings: &Settings, autofix: flags::Autofix, messages: &[Message], diff --git a/src/cli.rs b/src/cli.rs index fa2b8628dd5a3..3b79ba238448b 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -99,7 +99,7 @@ pub struct Cli { #[arg(long)] pub stdin_filename: Option, /// Path to the cache directory. - #[arg(long)] + #[arg(long, env = "RUFF_CACHE_DIR")] pub cache_dir: Option, /// Show violations with source code. #[arg(long, overrides_with("no_show_source"))] diff --git a/src/commands.rs b/src/commands.rs index eb34b9082c24c..ed5d1ce1fd658 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -16,7 +16,7 @@ use serde::Serialize; use walkdir::WalkDir; use crate::autofix::fixer; -use crate::cache::DEFAULT_CACHE_DIR_NAME; +use crate::cache::CACHE_DIR_NAME; use crate::cli::Overrides; use crate::iterators::par_iter; use crate::linter::{add_noqa_to_path, lint_path, lint_stdin, Diagnostics}; @@ -340,10 +340,10 @@ pub fn explain(code: &CheckCode, format: &SerializationFormat) -> Result<()> { pub fn clean(level: &LogLevel) -> Result<()> { for entry in WalkDir::new(&*path_dedot::CWD) .into_iter() - .filter_map(std::result::Result::ok) + .filter_map(Result::ok) .filter(|entry| entry.file_type().is_dir()) { - let cache = entry.path().join(DEFAULT_CACHE_DIR_NAME); + let cache = entry.path().join(CACHE_DIR_NAME); if cache.is_dir() { if level >= &LogLevel::Default { eprintln!("Removing cache at: {}", fs::relativize_path(&cache).bold());