From 75dc8a6ed1536979b9d3198e0252c3fec99e54af Mon Sep 17 00:00:00 2001 From: Piotr Kufel Date: Sat, 27 Jul 2024 17:44:12 -0700 Subject: [PATCH] Remove uses of NUSHELL_FOLDER --- crates/nu-cli/src/config_files.rs | 37 ++++++----------------- crates/nu-path/src/helpers.rs | 8 +---- crates/nu-path/src/lib.rs | 3 -- crates/nu-protocol/src/config/reedline.rs | 10 ++++++ crates/nu-protocol/src/eval_const.rs | 7 ++--- src/config_files.rs | 18 +++-------- src/run.rs | 8 ++--- 7 files changed, 29 insertions(+), 62 deletions(-) diff --git a/crates/nu-cli/src/config_files.rs b/crates/nu-cli/src/config_files.rs index 5842b6b0ce..a74fcc2502 100644 --- a/crates/nu-cli/src/config_files.rs +++ b/crates/nu-cli/src/config_files.rs @@ -16,15 +16,8 @@ const PLUGIN_FILE: &str = "plugin.msgpackz"; #[cfg(feature = "plugin")] const OLD_PLUGIN_FILE: &str = "plugin.nu"; -const HISTORY_FILE_TXT: &str = "history.txt"; -const HISTORY_FILE_SQLITE: &str = "history.sqlite3"; - #[cfg(feature = "plugin")] -pub fn read_plugin_file( - engine_state: &mut EngineState, - plugin_file: Option>, - storage_path: &str, -) { +pub fn read_plugin_file(engine_state: &mut EngineState, plugin_file: Option>) { use nu_protocol::ShellError; use std::path::Path; @@ -52,7 +45,7 @@ pub fn read_plugin_file( let mut start_time = std::time::Instant::now(); // Reading signatures from plugin registry file // The plugin.msgpackz file stores the parsed signature collected from each registered plugin - add_plugin_file(engine_state, plugin_file.clone(), storage_path); + add_plugin_file(engine_state, plugin_file.clone()); perf!( "add plugin file to engine_state", start_time, @@ -70,8 +63,7 @@ pub fn read_plugin_file( log::warn!("Plugin file not found: {}", plugin_path.display()); // Try migration of an old plugin file if this wasn't a custom plugin file - if plugin_file.is_none() && migrate_old_plugin_file(engine_state, storage_path) - { + if plugin_file.is_none() && migrate_old_plugin_file(engine_state) { let Ok(file) = std::fs::File::open(&plugin_path) else { log::warn!("Failed to load newly migrated plugin file"); return; @@ -159,11 +151,7 @@ pub fn read_plugin_file( } #[cfg(feature = "plugin")] -pub fn add_plugin_file( - engine_state: &mut EngineState, - plugin_file: Option>, - storage_path: &str, -) { +pub fn add_plugin_file(engine_state: &mut EngineState, plugin_file: Option>) { use std::path::Path; let working_set = StateWorkingSet::new(engine_state); @@ -189,9 +177,8 @@ pub fn add_plugin_file( ), ); } - } else if let Some(mut plugin_path) = nu_path::config_dir() { + } else if let Some(plugin_path) = nu_path::nu_config_dir() { // Path to store plugins signatures - plugin_path.push(storage_path); let mut plugin_path = canonicalize_with(&plugin_path, &cwd).unwrap_or(plugin_path.into()); plugin_path.push(PLUGIN_FILE); @@ -243,16 +230,13 @@ pub fn eval_config_contents( pub(crate) fn get_history_path(mode: HistoryFileFormat) -> Option { nu_path::nu_config_dir().map(|mut history_path| { - history_path.push(match mode { - HistoryFileFormat::PlainText => HISTORY_FILE_TXT, - HistoryFileFormat::Sqlite => HISTORY_FILE_SQLITE, - }); + history_path.push(mode.default_file_name()); history_path.into() }) } #[cfg(feature = "plugin")] -pub fn migrate_old_plugin_file(engine_state: &EngineState, storage_path: &str) -> bool { +pub fn migrate_old_plugin_file(engine_state: &EngineState) -> bool { use nu_protocol::{ PluginExample, PluginIdentity, PluginRegistryItem, PluginRegistryItemData, PluginSignature, ShellError, @@ -265,10 +249,9 @@ pub fn migrate_old_plugin_file(engine_state: &EngineState, storage_path: &str) - return false; }; - let Some(config_dir) = nu_path::config_dir().and_then(|mut dir| { - dir.push(storage_path); - nu_path::canonicalize_with(dir, &cwd).ok() - }) else { + let Some(config_dir) = + nu_path::nu_config_dir().and_then(|dir| nu_path::canonicalize_with(dir, &cwd).ok()) + else { return false; }; diff --git a/crates/nu-path/src/helpers.rs b/crates/nu-path/src/helpers.rs index 5a0e3aadd6..fc3113a32f 100644 --- a/crates/nu-path/src/helpers.rs +++ b/crates/nu-path/src/helpers.rs @@ -1,10 +1,5 @@ use crate::AbsolutePathBuf; -#[deprecated( - note = "prefer using nu_config_dir() instead of config_dir() joined with NUSHELL_FOLDER" -)] -pub const NUSHELL_FOLDER: &str = "nushell"; - pub fn home_dir() -> Option { dirs::home_dir().and_then(|home| AbsolutePathBuf::try_from(home).ok()) } @@ -39,8 +34,7 @@ pub fn config_dir() -> Option { /// Return the nushell config directory. pub fn nu_config_dir() -> Option { config_dir().map(|mut p| { - #[allow(deprecated)] - p.push(NUSHELL_FOLDER); + p.push("nushell"); p }) } diff --git a/crates/nu-path/src/lib.rs b/crates/nu-path/src/lib.rs index 51ca5ae812..d85f7bebd4 100644 --- a/crates/nu-path/src/lib.rs +++ b/crates/nu-path/src/lib.rs @@ -15,6 +15,3 @@ pub use helpers::{cache_dir, config_dir, data_dir, home_dir, nu_config_dir}; pub use path::*; pub use tilde::expand_tilde; pub use trailing_slash::{has_trailing_slash, strip_trailing_slash}; - -#[allow(deprecated)] -pub use helpers::NUSHELL_FOLDER; diff --git a/crates/nu-protocol/src/config/reedline.rs b/crates/nu-protocol/src/config/reedline.rs index 7032e74087..86177812cb 100644 --- a/crates/nu-protocol/src/config/reedline.rs +++ b/crates/nu-protocol/src/config/reedline.rs @@ -79,6 +79,16 @@ pub enum HistoryFileFormat { PlainText, } +impl HistoryFileFormat { + pub fn default_file_name(self) -> std::path::PathBuf { + match self { + HistoryFileFormat::PlainText => "history.txt", + HistoryFileFormat::Sqlite => "history.sqlite3", + } + .into() + } +} + impl FromStr for HistoryFileFormat { type Err = &'static str; diff --git a/crates/nu-protocol/src/eval_const.rs b/crates/nu-protocol/src/eval_const.rs index 1ee7802799..106bd00c7f 100644 --- a/crates/nu-protocol/src/eval_const.rs +++ b/crates/nu-protocol/src/eval_const.rs @@ -33,11 +33,8 @@ pub(crate) fn create_nu_constant(engine_state: &EngineState, span: Span) -> Valu let mut record = Record::new(); - let config_path = match nu_path::config_dir() { - Some(mut path) => { - path.push("nushell"); - Ok(canonicalize_path(engine_state, path.as_ref())) - } + let config_path = match nu_path::nu_config_dir() { + Some(path) => Ok(canonicalize_path(engine_state, path.as_ref())), None => Err(Value::error( ShellError::ConfigDirNotFound { span: Some(span) }, span, diff --git a/src/config_files.rs b/src/config_files.rs index dcc4c5da76..d36c18d501 100644 --- a/src/config_files.rs +++ b/src/config_files.rs @@ -3,8 +3,6 @@ use log::warn; use nu_cli::read_plugin_file; use nu_cli::{eval_config_contents, eval_source}; use nu_path::canonicalize_with; -#[allow(deprecated)] -use nu_path::NUSHELL_FOLDER; use nu_protocol::{ engine::{EngineState, Stack, StateWorkingSet}, report_error, report_error_new, Config, ParseError, PipelineData, Spanned, @@ -50,10 +48,7 @@ pub(crate) fn read_config_file( report_error(&working_set, &e); } } - } else if let Some(mut config_path) = nu_path::config_dir() { - #[allow(deprecated)] - config_path.push(NUSHELL_FOLDER); - + } else if let Some(mut config_path) = nu_path::nu_config_dir() { // Create config directory if it does not exist if !config_path.exists() { if let Err(err) = std::fs::create_dir_all(&config_path) { @@ -136,9 +131,7 @@ pub(crate) fn read_loginshell_file(engine_state: &mut EngineState, stack: &mut S ); // read and execute loginshell file if exists - if let Some(mut config_path) = nu_path::config_dir() { - #[allow(deprecated)] - config_path.push(NUSHELL_FOLDER); + if let Some(mut config_path) = nu_path::nu_config_dir() { config_path.push(LOGINSHELL_FILE); warn!("loginshell_file: {}", config_path.display()); @@ -271,8 +264,7 @@ pub(crate) fn setup_config( ); let result = catch_unwind(AssertUnwindSafe(|| { #[cfg(feature = "plugin")] - #[allow(deprecated)] - read_plugin_file(engine_state, plugin_file, NUSHELL_FOLDER); + read_plugin_file(engine_state, plugin_file); read_config_file(engine_state, stack, env_file, true); read_config_file(engine_state, stack, config_file, false); @@ -304,9 +296,7 @@ pub(crate) fn set_config_path( ); let config_path = match config_file { Some(s) => canonicalize_with(&s.item, cwd).ok(), - None => nu_path::config_dir().map(|mut p| { - #[allow(deprecated)] - p.push(NUSHELL_FOLDER); + None => nu_path::nu_config_dir().map(|p| { let mut p = canonicalize_with(&p, cwd).unwrap_or(p.into()); p.push(default_config_name); canonicalize_with(&p, cwd).unwrap_or(p) diff --git a/src/run.rs b/src/run.rs index 652cc05b44..1ec0c3eb7e 100644 --- a/src/run.rs +++ b/src/run.rs @@ -7,8 +7,6 @@ use log::trace; #[cfg(feature = "plugin")] use nu_cli::read_plugin_file; use nu_cli::{evaluate_commands, evaluate_file, evaluate_repl, EvaluateCommandsOpts}; -#[allow(deprecated)] -use nu_path::NUSHELL_FOLDER; use nu_protocol::{ engine::{EngineState, Stack}, report_error_new, PipelineData, Spanned, @@ -38,8 +36,7 @@ pub(crate) fn run_commands( // if the --no-config-file(-n) flag is passed, do not load plugin, env, or config files if parsed_nu_cli_args.no_config_file.is_none() { #[cfg(feature = "plugin")] - #[allow(deprecated)] - read_plugin_file(engine_state, parsed_nu_cli_args.plugin_file, NUSHELL_FOLDER); + read_plugin_file(engine_state, parsed_nu_cli_args.plugin_file); perf!("read plugins", start_time, use_color); @@ -127,8 +124,7 @@ pub(crate) fn run_file( if parsed_nu_cli_args.no_config_file.is_none() { let start_time = std::time::Instant::now(); #[cfg(feature = "plugin")] - #[allow(deprecated)] - read_plugin_file(engine_state, parsed_nu_cli_args.plugin_file, NUSHELL_FOLDER); + read_plugin_file(engine_state, parsed_nu_cli_args.plugin_file); perf!("read plugins", start_time, use_color); let start_time = std::time::Instant::now();