Remove uses of NUSHELL_FOLDER

This commit is contained in:
Piotr Kufel 2024-07-27 17:44:12 -07:00
parent 9d24c5efd4
commit 75dc8a6ed1
7 changed files with 29 additions and 62 deletions

View File

@ -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<Spanned<String>>,
storage_path: &str,
) {
pub fn read_plugin_file(engine_state: &mut EngineState, plugin_file: Option<Spanned<String>>) {
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<Spanned<String>>,
storage_path: &str,
) {
pub fn add_plugin_file(engine_state: &mut EngineState, plugin_file: Option<Spanned<String>>) {
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<PathBuf> {
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;
};

View File

@ -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<AbsolutePathBuf> {
dirs::home_dir().and_then(|home| AbsolutePathBuf::try_from(home).ok())
}
@ -39,8 +34,7 @@ pub fn config_dir() -> Option<AbsolutePathBuf> {
/// Return the nushell config directory.
pub fn nu_config_dir() -> Option<AbsolutePathBuf> {
config_dir().map(|mut p| {
#[allow(deprecated)]
p.push(NUSHELL_FOLDER);
p.push("nushell");
p
})
}

View File

@ -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;

View File

@ -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;

View File

@ -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,

View File

@ -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)

View File

@ -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();