From 25cbcb511dc69e4334eaf311a299cc522cee95b8 Mon Sep 17 00:00:00 2001 From: Devyn Cairns Date: Wed, 24 Apr 2024 15:40:39 -0700 Subject: [PATCH] =?UTF-8?q?Rename=20plugin=20cache=20file=20=E2=87=92=20pl?= =?UTF-8?q?ugin=20registry=20file=20(#12634)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit # Description So far this seems like the winner of my poll on what the name should be. I'll take this off draft once the poll expires, if this is indeed the winner. --- crates/nu-cli/src/config_files.rs | 22 ++++---- .../nu-cmd-plugin/src/commands/plugin/add.rs | 14 ++--- .../nu-cmd-plugin/src/commands/plugin/mod.rs | 5 +- .../nu-cmd-plugin/src/commands/plugin/rm.rs | 12 ++--- .../nu-cmd-plugin/src/commands/plugin/use_.rs | 14 ++--- crates/nu-cmd-plugin/src/util.rs | 18 +++---- crates/nu-parser/src/parse_keywords.rs | 20 +++---- crates/nu-plugin/src/lib.rs | 4 +- crates/nu-plugin/src/plugin/mod.rs | 20 +++---- crates/nu-protocol/src/engine/engine_state.rs | 12 ++--- crates/nu-protocol/src/engine/state_delta.rs | 6 +-- .../src/engine/state_working_set.rs | 6 +-- crates/nu-protocol/src/errors/parse_error.rs | 4 +- crates/nu-protocol/src/errors/shell_error.rs | 10 ++-- crates/nu-protocol/src/plugin/mod.rs | 4 +- .../{cache_file => registry_file}/mod.rs | 49 +++++++++-------- .../{cache_file => registry_file}/tests.rs | 38 ++++++------- crates/nu-test-support/src/macros.rs | 2 +- src/command.rs | 4 +- src/tests/test_parser.rs | 4 +- tests/plugins/mod.rs | 2 +- .../{cache_file.rs => registry_file.rs} | 54 +++++++++---------- 22 files changed, 165 insertions(+), 159 deletions(-) rename crates/nu-protocol/src/plugin/{cache_file => registry_file}/mod.rs (77%) rename crates/nu-protocol/src/plugin/{cache_file => registry_file}/tests.rs (72%) rename tests/plugins/{cache_file.rs => registry_file.rs} (89%) diff --git a/crates/nu-cli/src/config_files.rs b/crates/nu-cli/src/config_files.rs index 760a3f7e97..746e12dbf2 100644 --- a/crates/nu-cli/src/config_files.rs +++ b/crates/nu-cli/src/config_files.rs @@ -6,7 +6,7 @@ use nu_protocol::{ report_error, HistoryFileFormat, PipelineData, }; #[cfg(feature = "plugin")] -use nu_protocol::{ParseError, PluginCacheFile, Spanned}; +use nu_protocol::{ParseError, PluginRegistryFile, Spanned}; #[cfg(feature = "plugin")] use nu_utils::utils::perf; use std::path::PathBuf; @@ -51,7 +51,7 @@ pub fn read_plugin_file( } let mut start_time = std::time::Instant::now(); - // Reading signatures from plugin cache file + // 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); perf( @@ -89,7 +89,7 @@ pub fn read_plugin_file( engine_state, &ShellError::GenericError { error: format!( - "Error while opening plugin cache file: {}", + "Error while opening plugin registry file: {}", plugin_path.display() ), msg: "plugin path defined here".into(), @@ -113,15 +113,15 @@ pub fn read_plugin_file( } // Read the contents of the plugin file - let contents = match PluginCacheFile::read_from(&mut file, span) { + let contents = match PluginRegistryFile::read_from(&mut file, span) { Ok(contents) => contents, Err(err) => { - log::warn!("Failed to read plugin cache file: {err:?}"); + log::warn!("Failed to read plugin registry file: {err:?}"); report_error_new( engine_state, &ShellError::GenericError { error: format!( - "Error while reading plugin cache file: {}", + "Error while reading plugin registry file: {}", plugin_path.display() ), msg: "plugin path defined here".into(), @@ -265,8 +265,8 @@ pub(crate) fn get_history_path(storage_path: &str, mode: HistoryFileFormat) -> O #[cfg(feature = "plugin")] pub fn migrate_old_plugin_file(engine_state: &EngineState, storage_path: &str) -> bool { use nu_protocol::{ - report_error_new, PluginCacheItem, PluginCacheItemData, PluginExample, PluginIdentity, - PluginSignature, ShellError, + report_error_new, PluginExample, PluginIdentity, PluginRegistryItem, + PluginRegistryItemData, PluginSignature, ShellError, }; use std::collections::BTreeMap; @@ -318,7 +318,7 @@ pub fn migrate_old_plugin_file(engine_state: &EngineState, storage_path: &str) - } // Now that the plugin commands are loaded, we just have to generate the file - let mut contents = PluginCacheFile::new(); + let mut contents = PluginRegistryFile::new(); let mut groups = BTreeMap::>::new(); @@ -339,11 +339,11 @@ pub fn migrate_old_plugin_file(engine_state: &EngineState, storage_path: &str) - } for (identity, commands) in groups { - contents.upsert_plugin(PluginCacheItem { + contents.upsert_plugin(PluginRegistryItem { name: identity.name().to_owned(), filename: identity.filename().to_owned(), shell: identity.shell().map(|p| p.to_owned()), - data: PluginCacheItemData::Valid { commands }, + data: PluginRegistryItemData::Valid { commands }, }); } diff --git a/crates/nu-cmd-plugin/src/commands/plugin/add.rs b/crates/nu-cmd-plugin/src/commands/plugin/add.rs index 99d72b23ab..30b56879c1 100644 --- a/crates/nu-cmd-plugin/src/commands/plugin/add.rs +++ b/crates/nu-cmd-plugin/src/commands/plugin/add.rs @@ -2,7 +2,7 @@ use std::sync::Arc; use nu_engine::{command_prelude::*, current_dir}; use nu_plugin::{GetPlugin, PersistentPlugin}; -use nu_protocol::{PluginCacheItem, PluginGcConfig, PluginIdentity, RegisteredPlugin}; +use nu_protocol::{PluginGcConfig, PluginIdentity, PluginRegistryItem, RegisteredPlugin}; use crate::util::{get_plugin_dirs, modify_plugin_file}; @@ -21,7 +21,7 @@ impl Command for PluginAdd { .named( "plugin-config", SyntaxShape::Filepath, - "Use a plugin cache file other than the one set in `$nu.plugin-path`", + "Use a plugin registry file other than the one set in `$nu.plugin-path`", None, ) .named( @@ -39,7 +39,7 @@ impl Command for PluginAdd { } fn usage(&self) -> &str { - "Add a plugin to the plugin cache file." + "Add a plugin to the plugin registry file." } fn extra_usage(&self) -> &str { @@ -47,8 +47,8 @@ impl Command for PluginAdd { This does not load the plugin commands into the scope - see `register` for that. Instead, it runs the plugin to get its command signatures, and then edits the -plugin cache file (by default, `$nu.plugin-path`). The changes will be -apparent the next time `nu` is next launched with that plugin cache file. +plugin registry file (by default, `$nu.plugin-path`). The changes will be +apparent the next time `nu` is next launched with that plugin registry file. "# .trim() } @@ -66,7 +66,7 @@ apparent the next time `nu` is next launched with that plugin cache file. }, Example { example: "plugin add --plugin-config polars.msgpackz nu_plugin_polars", - description: "Run the `nu_plugin_polars` plugin from the current directory or $env.NU_PLUGIN_DIRS, and install its signatures to the \"polars.msgpackz\" plugin cache file.", + description: "Run the `nu_plugin_polars` plugin from the current directory or $env.NU_PLUGIN_DIRS, and install its signatures to the \"polars.msgpackz\" plugin registry file.", result: None, }, ] @@ -122,7 +122,7 @@ apparent the next time `nu` is next launched with that plugin cache file. modify_plugin_file(engine_state, stack, call.head, custom_path, |contents| { // Update the file with the received signatures - let item = PluginCacheItem::new(plugin.identity(), commands); + let item = PluginRegistryItem::new(plugin.identity(), commands); contents.upsert_plugin(item); Ok(()) })?; diff --git a/crates/nu-cmd-plugin/src/commands/plugin/mod.rs b/crates/nu-cmd-plugin/src/commands/plugin/mod.rs index 14a5f32ee5..87daa5a328 100644 --- a/crates/nu-cmd-plugin/src/commands/plugin/mod.rs +++ b/crates/nu-cmd-plugin/src/commands/plugin/mod.rs @@ -60,8 +60,9 @@ impl Command for PluginCommand { Example { example: "plugin use inc", description: " -Load (or reload) the `inc` plugin from the plugin cache file and put its commands in scope. -The plugin must already be in the cache file at parse time. +Load (or reload) the `inc` plugin from the plugin registry file and put its +commands in scope. The plugin must already be in the registry file at parse +time. " .trim(), result: None, diff --git a/crates/nu-cmd-plugin/src/commands/plugin/rm.rs b/crates/nu-cmd-plugin/src/commands/plugin/rm.rs index 6c3147e52b..a00060e27a 100644 --- a/crates/nu-cmd-plugin/src/commands/plugin/rm.rs +++ b/crates/nu-cmd-plugin/src/commands/plugin/rm.rs @@ -17,7 +17,7 @@ impl Command for PluginRm { .named( "plugin-config", SyntaxShape::Filepath, - "Use a plugin cache file other than the one set in `$nu.plugin-path`", + "Use a plugin registry file other than the one set in `$nu.plugin-path`", None, ) .switch( @@ -34,15 +34,15 @@ impl Command for PluginRm { } fn usage(&self) -> &str { - "Remove a plugin from the plugin cache file." + "Remove a plugin from the plugin registry file." } fn extra_usage(&self) -> &str { r#" This does not remove the plugin commands from the current scope or from `plugin list` in the current shell. It instead removes the plugin from the plugin -cache file (by default, `$nu.plugin-path`). The changes will be apparent the -next time `nu` is launched with that plugin cache file. +registry file (by default, `$nu.plugin-path`). The changes will be apparent the +next time `nu` is launched with that plugin registry file. This can be useful for removing an invalid plugin signature, if it can't be fixed with `plugin add`. @@ -68,7 +68,7 @@ fixed with `plugin add`. }, Example { example: "plugin rm --plugin-config polars.msgpackz polars", - description: "Remove the installed signatures for the `polars` plugin from the \"polars.msgpackz\" plugin cache file.", + description: "Remove the installed signatures for the `polars` plugin from the \"polars.msgpackz\" plugin registry file.", result: None, }, ] @@ -100,7 +100,7 @@ fixed with `plugin add`. } else { Err(ShellError::GenericError { error: format!("Failed to remove the `{}` plugin", name.item), - msg: "couldn't find a plugin with this name in the cache file".into(), + msg: "couldn't find a plugin with this name in the registry file".into(), span: Some(name.span), help: None, inner: vec![], diff --git a/crates/nu-cmd-plugin/src/commands/plugin/use_.rs b/crates/nu-cmd-plugin/src/commands/plugin/use_.rs index a5ff8ae043..e5997efcf0 100644 --- a/crates/nu-cmd-plugin/src/commands/plugin/use_.rs +++ b/crates/nu-cmd-plugin/src/commands/plugin/use_.rs @@ -9,7 +9,7 @@ impl Command for PluginUse { } fn usage(&self) -> &str { - "Load a plugin from the plugin cache file into scope." + "Load a plugin from the plugin registry file into scope." } fn signature(&self) -> nu_protocol::Signature { @@ -18,7 +18,7 @@ impl Command for PluginUse { .named( "plugin-config", SyntaxShape::Filepath, - "Use a plugin cache file other than the one set in `$nu.plugin-path`", + "Use a plugin registry file other than the one set in `$nu.plugin-path`", None, ) .required( @@ -34,13 +34,13 @@ impl Command for PluginUse { This command is a parser keyword. For details, check: https://www.nushell.sh/book/thinking_in_nu.html -The plugin definition must be available in the plugin cache file at parse time. -Run `plugin add` first in the REPL to do this, or from a script consider -preparing a plugin cache file and passing `--plugin-config`, or using the +The plugin definition must be available in the plugin registry file at parse +time. Run `plugin add` first in the REPL to do this, or from a script consider +preparing a plugin registry file and passing `--plugin-config`, or using the `--plugin` option to `nu` instead. If the plugin was already loaded, this will reload the latest definition from -the cache file into scope. +the registry file into scope. Note that even if the plugin filename is specified, it will only be loaded if it was already previously registered with `plugin add`. @@ -80,7 +80,7 @@ it was already previously registered with `plugin add`. }, Example { description: - "Load the commands for the `query` plugin from a custom plugin cache file", + "Load the commands for the `query` plugin from a custom plugin registry file", example: r#"plugin use --plugin-config local-plugins.msgpackz query"#, result: None, }, diff --git a/crates/nu-cmd-plugin/src/util.rs b/crates/nu-cmd-plugin/src/util.rs index bfbcd5309e..85818e0564 100644 --- a/crates/nu-cmd-plugin/src/util.rs +++ b/crates/nu-cmd-plugin/src/util.rs @@ -4,25 +4,25 @@ use std::{ }; use nu_engine::{command_prelude::*, current_dir}; -use nu_protocol::{engine::StateWorkingSet, PluginCacheFile}; +use nu_protocol::{engine::StateWorkingSet, PluginRegistryFile}; pub(crate) fn modify_plugin_file( engine_state: &EngineState, stack: &mut Stack, span: Span, custom_path: Option>, - operate: impl FnOnce(&mut PluginCacheFile) -> Result<(), ShellError>, + operate: impl FnOnce(&mut PluginRegistryFile) -> Result<(), ShellError>, ) -> Result<(), ShellError> { let cwd = current_dir(engine_state, stack)?; - let plugin_cache_file_path = if let Some(ref custom_path) = custom_path { + let plugin_registry_file_path = if let Some(ref custom_path) = custom_path { nu_path::expand_path_with(&custom_path.item, cwd, true) } else { engine_state .plugin_path .clone() .ok_or_else(|| ShellError::GenericError { - error: "Plugin cache file not set".into(), + error: "Plugin registry file not set".into(), msg: "pass --plugin-config explicitly here".into(), span: Some(span), help: Some("you may be running `nu` with --no-config-file".into()), @@ -31,13 +31,13 @@ pub(crate) fn modify_plugin_file( }; // Try to read the plugin file if it exists - let mut contents = if fs::metadata(&plugin_cache_file_path).is_ok_and(|m| m.len() > 0) { - PluginCacheFile::read_from( - File::open(&plugin_cache_file_path).err_span(span)?, + let mut contents = if fs::metadata(&plugin_registry_file_path).is_ok_and(|m| m.len() > 0) { + PluginRegistryFile::read_from( + File::open(&plugin_registry_file_path).err_span(span)?, Some(span), )? } else { - PluginCacheFile::default() + PluginRegistryFile::default() }; // Do the operation @@ -45,7 +45,7 @@ pub(crate) fn modify_plugin_file( // Save the modified file on success contents.write_to( - File::create(&plugin_cache_file_path).err_span(span)?, + File::create(&plugin_registry_file_path).err_span(span)?, Some(span), )?; diff --git a/crates/nu-parser/src/parse_keywords.rs b/crates/nu-parser/src/parse_keywords.rs index 4f594148e8..4269e5b5d8 100644 --- a/crates/nu-parser/src/parse_keywords.rs +++ b/crates/nu-parser/src/parse_keywords.rs @@ -3557,7 +3557,7 @@ pub fn parse_where(working_set: &mut StateWorkingSet, lite_command: &LiteCommand pub fn parse_register(working_set: &mut StateWorkingSet, lite_command: &LiteCommand) -> Pipeline { use nu_plugin::{get_signature, PluginDeclaration}; use nu_protocol::{ - engine::Stack, ErrSpan, ParseWarning, PluginCacheItem, PluginIdentity, PluginSignature, + engine::Stack, ErrSpan, ParseWarning, PluginIdentity, PluginRegistryItem, PluginSignature, RegisteredPlugin, }; @@ -3742,8 +3742,10 @@ pub fn parse_register(working_set: &mut StateWorkingSet, lite_command: &LiteComm if let Ok(ref signatures) = signatures { // Add the loaded plugin to the delta - working_set - .update_plugin_cache(PluginCacheItem::new(&identity, signatures.clone())); + working_set.update_plugin_registry(PluginRegistryItem::new( + &identity, + signatures.clone(), + )); } signatures @@ -3776,7 +3778,7 @@ pub fn parse_register(working_set: &mut StateWorkingSet, lite_command: &LiteComm #[cfg(feature = "plugin")] pub fn parse_plugin_use(working_set: &mut StateWorkingSet, call: Box) -> Pipeline { - use nu_protocol::{FromValue, PluginCacheFile}; + use nu_protocol::{FromValue, PluginRegistryFile}; let cwd = working_set.get_cwd(); @@ -3827,8 +3829,8 @@ pub fn parse_plugin_use(working_set: &mut StateWorkingSet, call: Box) -> P .plugin_path .as_ref() .ok_or_else(|| ParseError::LabeledErrorWithHelp { - error: "Plugin cache file not set".into(), - label: "can't load plugin without cache file".into(), + error: "Plugin registry file not set".into(), + label: "can't load plugin without registry file".into(), span: call.head, help: "pass --plugin-config to `plugin use` when $nu.plugin-path is not set" @@ -3840,14 +3842,14 @@ pub fn parse_plugin_use(working_set: &mut StateWorkingSet, call: Box) -> P let file = plugin_config_path.open(working_set).map_err(|err| { ParseError::LabeledError( - "Plugin cache file can't be opened".into(), + "Plugin registry file can't be opened".into(), err.to_string(), plugin_config.as_ref().map(|p| p.span).unwrap_or(call.head), ) })?; // The file is now open, so we just have to parse the contents and find the plugin - let contents = PluginCacheFile::read_from(file, Some(call.head)) + let contents = PluginRegistryFile::read_from(file, Some(call.head)) .map_err(|err| err.wrap(working_set, call.head))?; let plugin_item = contents @@ -3861,7 +3863,7 @@ pub fn parse_plugin_use(working_set: &mut StateWorkingSet, call: Box) -> P })?; // Now add the signatures to the working set - nu_plugin::load_plugin_cache_item(working_set, plugin_item, Some(call.head)) + nu_plugin::load_plugin_registry_item(working_set, plugin_item, Some(call.head)) .map_err(|err| err.wrap(working_set, call.head))?; Ok(()) diff --git a/crates/nu-plugin/src/lib.rs b/crates/nu-plugin/src/lib.rs index b5ecd57b61..6b9571b79f 100644 --- a/crates/nu-plugin/src/lib.rs +++ b/crates/nu-plugin/src/lib.rs @@ -78,8 +78,8 @@ pub use serializers::{json::JsonSerializer, msgpack::MsgPackSerializer}; // Used by other nu crates. #[doc(hidden)] pub use plugin::{ - add_plugin_to_working_set, create_plugin_signature, get_signature, load_plugin_cache_item, - load_plugin_file, serve_plugin_io, EngineInterfaceManager, GetPlugin, Interface, + add_plugin_to_working_set, create_plugin_signature, get_signature, load_plugin_file, + load_plugin_registry_item, serve_plugin_io, EngineInterfaceManager, GetPlugin, Interface, InterfaceManager, PersistentPlugin, PluginDeclaration, PluginExecutionCommandContext, PluginExecutionContext, PluginInterface, PluginInterfaceManager, PluginSource, ServePluginError, diff --git a/crates/nu-plugin/src/plugin/mod.rs b/crates/nu-plugin/src/plugin/mod.rs index 9dcc5f6a8a..63c3ca3cf6 100644 --- a/crates/nu-plugin/src/plugin/mod.rs +++ b/crates/nu-plugin/src/plugin/mod.rs @@ -24,8 +24,8 @@ use std::{ use nu_engine::documentation::get_flags_section; use nu_protocol::{ ast::Operator, engine::StateWorkingSet, report_error_new, CustomValue, IntoSpanned, - LabeledError, PipelineData, PluginCacheFile, PluginCacheItem, PluginCacheItemData, - PluginIdentity, PluginSignature, RegisteredPlugin, ShellError, Span, Spanned, Value, + LabeledError, PipelineData, PluginIdentity, PluginRegistryFile, PluginRegistryItem, + PluginRegistryItemData, PluginSignature, RegisteredPlugin, ShellError, Span, Spanned, Value, }; use thiserror::Error; @@ -925,12 +925,12 @@ pub fn get_plugin_encoding( #[doc(hidden)] pub fn load_plugin_file( working_set: &mut StateWorkingSet, - plugin_cache_file: &PluginCacheFile, + plugin_registry_file: &PluginRegistryFile, span: Option, ) { - for plugin in &plugin_cache_file.plugins { + for plugin in &plugin_registry_file.plugins { // Any errors encountered should just be logged. - if let Err(err) = load_plugin_cache_item(working_set, plugin, span) { + if let Err(err) = load_plugin_registry_item(working_set, plugin, span) { report_error_new(working_set.permanent_state, &err) } } @@ -938,15 +938,15 @@ pub fn load_plugin_file( /// Load a definition from the plugin file into the engine state #[doc(hidden)] -pub fn load_plugin_cache_item( +pub fn load_plugin_registry_item( working_set: &mut StateWorkingSet, - plugin: &PluginCacheItem, + plugin: &PluginRegistryItem, span: Option, ) -> Result, ShellError> { let identity = PluginIdentity::new(plugin.filename.clone(), plugin.shell.clone()).map_err(|_| { ShellError::GenericError { - error: "Invalid plugin filename in plugin cache file".into(), + error: "Invalid plugin filename in plugin registry file".into(), msg: "loaded from here".into(), span, help: Some(format!( @@ -959,7 +959,7 @@ pub fn load_plugin_cache_item( })?; match &plugin.data { - PluginCacheItemData::Valid { commands } => { + PluginRegistryItemData::Valid { commands } => { let plugin = add_plugin_to_working_set(working_set, &identity)?; // Ensure that the plugin is reset. We're going to load new signatures, so we want to @@ -974,7 +974,7 @@ pub fn load_plugin_cache_item( } Ok(plugin) } - PluginCacheItemData::Invalid => Err(ShellError::PluginCacheDataInvalid { + PluginRegistryItemData::Invalid => Err(ShellError::PluginRegistryDataInvalid { plugin_name: identity.name().to_owned(), span, add_command: identity.add_command(), diff --git a/crates/nu-protocol/src/engine/engine_state.rs b/crates/nu-protocol/src/engine/engine_state.rs index a870c99b44..287b4c4e27 100644 --- a/crates/nu-protocol/src/engine/engine_state.rs +++ b/crates/nu-protocol/src/engine/engine_state.rs @@ -24,7 +24,7 @@ use std::{ type PoisonDebuggerError<'a> = PoisonError>>; #[cfg(feature = "plugin")] -use crate::{PluginCacheFile, PluginCacheItem, RegisteredPlugin}; +use crate::{PluginRegistryFile, PluginRegistryItem, RegisteredPlugin}; pub static PWD_ENV: &str = "PWD"; @@ -267,10 +267,10 @@ impl EngineState { } #[cfg(feature = "plugin")] - if !delta.plugin_cache_items.is_empty() { + if !delta.plugin_registry_items.is_empty() { // Update the plugin file with the new signatures. if self.plugin_path.is_some() { - self.update_plugin_file(std::mem::take(&mut delta.plugin_cache_items))?; + self.update_plugin_file(std::mem::take(&mut delta.plugin_registry_items))?; } } @@ -482,7 +482,7 @@ impl EngineState { #[cfg(feature = "plugin")] pub fn update_plugin_file( &self, - updated_items: Vec, + updated_items: Vec, ) -> Result<(), ShellError> { // Updating the signatures plugin file with the added signatures use std::fs::File; @@ -500,10 +500,10 @@ impl EngineState { // Read the current contents of the plugin file if it exists let mut contents = match File::open(plugin_path.as_path()) { - Ok(mut plugin_file) => PluginCacheFile::read_from(&mut plugin_file, None), + Ok(mut plugin_file) => PluginRegistryFile::read_from(&mut plugin_file, None), Err(err) => { if err.kind() == std::io::ErrorKind::NotFound { - Ok(PluginCacheFile::default()) + Ok(PluginRegistryFile::default()) } else { Err(ShellError::GenericError { error: "Failed to open plugin file".into(), diff --git a/crates/nu-protocol/src/engine/state_delta.rs b/crates/nu-protocol/src/engine/state_delta.rs index 657c10ee11..1e283bade8 100644 --- a/crates/nu-protocol/src/engine/state_delta.rs +++ b/crates/nu-protocol/src/engine/state_delta.rs @@ -9,7 +9,7 @@ use crate::{ use std::sync::Arc; #[cfg(feature = "plugin")] -use crate::{PluginCacheItem, RegisteredPlugin}; +use crate::{PluginRegistryItem, RegisteredPlugin}; /// A delta (or change set) between the current global state and a possible future global state. Deltas /// can be applied to the global state to update it to contain both previous state and the state held @@ -26,7 +26,7 @@ pub struct StateDelta { #[cfg(feature = "plugin")] pub(super) plugins: Vec>, #[cfg(feature = "plugin")] - pub(super) plugin_cache_items: Vec, + pub(super) plugin_registry_items: Vec, } impl StateDelta { @@ -50,7 +50,7 @@ impl StateDelta { #[cfg(feature = "plugin")] plugins: vec![], #[cfg(feature = "plugin")] - plugin_cache_items: vec![], + plugin_registry_items: vec![], } } diff --git a/crates/nu-protocol/src/engine/state_working_set.rs b/crates/nu-protocol/src/engine/state_working_set.rs index 9070fea925..1ef4ba5e05 100644 --- a/crates/nu-protocol/src/engine/state_working_set.rs +++ b/crates/nu-protocol/src/engine/state_working_set.rs @@ -15,7 +15,7 @@ use std::{ }; #[cfg(feature = "plugin")] -use crate::{PluginCacheItem, PluginIdentity, RegisteredPlugin}; +use crate::{PluginIdentity, PluginRegistryItem, RegisteredPlugin}; /// A temporary extension to the global state. This handles bridging between the global state and the /// additional declarations and scope changes that are not yet part of the global scope. @@ -182,8 +182,8 @@ impl<'a> StateWorkingSet<'a> { } #[cfg(feature = "plugin")] - pub fn update_plugin_cache(&mut self, item: PluginCacheItem) { - self.delta.plugin_cache_items.push(item); + pub fn update_plugin_registry(&mut self, item: PluginRegistryItem) { + self.delta.plugin_registry_items.push(item); } pub fn merge_predecl(&mut self, name: &[u8]) -> Option { diff --git a/crates/nu-protocol/src/errors/parse_error.rs b/crates/nu-protocol/src/errors/parse_error.rs index 8f94bc5d52..7e39fe1ef6 100644 --- a/crates/nu-protocol/src/errors/parse_error.rs +++ b/crates/nu-protocol/src/errors/parse_error.rs @@ -442,13 +442,13 @@ pub enum ParseError { #[error("Plugin not found")] #[diagnostic( code(nu::parser::plugin_not_found), - help("plugins need to be added to the plugin cache file before your script is run (see `plugin add`)"), + help("plugins need to be added to the plugin registry file before your script is run (see `plugin add`)"), )] PluginNotFound { name: String, #[label("Plugin not found: {name}")] name_span: Span, - #[label("in this cache file")] + #[label("in this registry file")] plugin_config_span: Option, }, diff --git a/crates/nu-protocol/src/errors/shell_error.rs b/crates/nu-protocol/src/errors/shell_error.rs index 51c857a252..48d2ec88e8 100644 --- a/crates/nu-protocol/src/errors/shell_error.rs +++ b/crates/nu-protocol/src/errors/shell_error.rs @@ -750,18 +750,18 @@ pub enum ShellError { span: Span, }, - /// The cached plugin data for a plugin is invalid. + /// The registered plugin data for a plugin is invalid. /// /// ## Resolution /// /// `plugin add` the plugin again to update the data, or remove it with `plugin rm`. - #[error("The cached plugin data for `{plugin_name}` is invalid")] - #[diagnostic(code(nu::shell::plugin_cache_data_invalid))] - PluginCacheDataInvalid { + #[error("The registered plugin data for `{plugin_name}` is invalid")] + #[diagnostic(code(nu::shell::plugin_registry_data_invalid))] + PluginRegistryDataInvalid { plugin_name: String, #[label("plugin `{plugin_name}` loaded here")] span: Option, - #[help("the format in the plugin cache file is not compatible with this version of Nushell.\n\nTry adding the plugin again with `{}`")] + #[help("the format in the plugin registry file is not compatible with this version of Nushell.\n\nTry adding the plugin again with `{}`")] add_command: String, }, diff --git a/crates/nu-protocol/src/plugin/mod.rs b/crates/nu-protocol/src/plugin/mod.rs index 46bdbc2fd1..b266f8ebac 100644 --- a/crates/nu-protocol/src/plugin/mod.rs +++ b/crates/nu-protocol/src/plugin/mod.rs @@ -1,9 +1,9 @@ -mod cache_file; mod identity; mod registered; +mod registry_file; mod signature; -pub use cache_file::*; pub use identity::*; pub use registered::*; +pub use registry_file::*; pub use signature::*; diff --git a/crates/nu-protocol/src/plugin/cache_file/mod.rs b/crates/nu-protocol/src/plugin/registry_file/mod.rs similarity index 77% rename from crates/nu-protocol/src/plugin/cache_file/mod.rs rename to crates/nu-protocol/src/plugin/registry_file/mod.rs index c77b3e5c23..d3eb4a9d02 100644 --- a/crates/nu-protocol/src/plugin/cache_file/mod.rs +++ b/crates/nu-protocol/src/plugin/registry_file/mod.rs @@ -15,34 +15,34 @@ const COMPRESSION_QUALITY: u32 = 1; const WIN_SIZE: u32 = 20; // recommended 20-22 #[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] -pub struct PluginCacheFile { +pub struct PluginRegistryFile { /// The Nushell version that last updated the file. pub nushell_version: String, /// The installed plugins. - pub plugins: Vec, + pub plugins: Vec, } -impl Default for PluginCacheFile { +impl Default for PluginRegistryFile { fn default() -> Self { Self::new() } } -impl PluginCacheFile { - /// Create a new, empty plugin cache file. - pub fn new() -> PluginCacheFile { - PluginCacheFile { +impl PluginRegistryFile { + /// Create a new, empty plugin registry file. + pub fn new() -> PluginRegistryFile { + PluginRegistryFile { nushell_version: env!("CARGO_PKG_VERSION").to_owned(), plugins: vec![], } } - /// Read the plugin cache file from a reader, e.g. [`File`](std::fs::File). + /// Read the plugin registry file from a reader, e.g. [`File`](std::fs::File). pub fn read_from( reader: impl Read, error_span: Option, - ) -> Result { + ) -> Result { // Format is brotli compressed messagepack let brotli_reader = brotli::Decompressor::new(reader, BUFFER_SIZE); @@ -57,7 +57,7 @@ impl PluginCacheFile { }) } - /// Write the plugin cache file to a writer, e.g. [`File`](std::fs::File). + /// Write the plugin registry file to a writer, e.g. [`File`](std::fs::File). /// /// The `nushell_version` will be updated to the current version before writing. pub fn write_to( @@ -84,8 +84,8 @@ impl PluginCacheFile { }) } - /// Insert or update a plugin in the plugin cache file. - pub fn upsert_plugin(&mut self, item: PluginCacheItem) { + /// Insert or update a plugin in the plugin registry file. + pub fn upsert_plugin(&mut self, item: PluginRegistryItem) { if let Some(existing_item) = self.plugins.iter_mut().find(|p| p.name == item.name) { *existing_item = item; } else { @@ -98,12 +98,12 @@ impl PluginCacheFile { } } -/// A single plugin definition from a [`PluginCacheFile`]. +/// A single plugin definition from a [`PluginRegistryFile`]. /// /// Contains the information necessary for the [`PluginIdentity`], as well as possibly valid data -/// about the plugin including the cached command signatures. +/// about the plugin including the registered command signatures. #[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] -pub struct PluginCacheItem { +pub struct PluginRegistryItem { /// The name of the plugin, as would show in `plugin list`. This does not include the file /// extension or the `nu_plugin_` prefix. pub name: String, @@ -117,29 +117,32 @@ pub struct PluginCacheItem { /// Additional data that might be invalid so that we don't fail to load the whole plugin file /// if there's a deserialization error. #[serde(flatten)] - pub data: PluginCacheItemData, + pub data: PluginRegistryItemData, } -impl PluginCacheItem { - /// Create a [`PluginCacheItem`] from an identity and signatures. - pub fn new(identity: &PluginIdentity, mut commands: Vec) -> PluginCacheItem { +impl PluginRegistryItem { + /// Create a [`PluginRegistryItem`] from an identity and signatures. + pub fn new( + identity: &PluginIdentity, + mut commands: Vec, + ) -> PluginRegistryItem { // Sort the commands for consistency commands.sort_by(|cmd1, cmd2| cmd1.sig.name.cmp(&cmd2.sig.name)); - PluginCacheItem { + PluginRegistryItem { name: identity.name().to_owned(), filename: identity.filename().to_owned(), shell: identity.shell().map(|p| p.to_owned()), - data: PluginCacheItemData::Valid { commands }, + data: PluginRegistryItemData::Valid { commands }, } } } -/// Possibly valid data about a plugin in a [`PluginCacheFile`]. If deserialization fails, it will +/// Possibly valid data about a plugin in a [`PluginRegistryFile`]. If deserialization fails, it will /// be `Invalid`. #[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] #[serde(untagged)] -pub enum PluginCacheItemData { +pub enum PluginRegistryItemData { Valid { /// Signatures and examples for each command provided by the plugin. commands: Vec, diff --git a/crates/nu-protocol/src/plugin/cache_file/tests.rs b/crates/nu-protocol/src/plugin/registry_file/tests.rs similarity index 72% rename from crates/nu-protocol/src/plugin/cache_file/tests.rs rename to crates/nu-protocol/src/plugin/registry_file/tests.rs index 56ded46948..0d34ecca1c 100644 --- a/crates/nu-protocol/src/plugin/cache_file/tests.rs +++ b/crates/nu-protocol/src/plugin/registry_file/tests.rs @@ -1,16 +1,16 @@ -use super::{PluginCacheFile, PluginCacheItem, PluginCacheItemData}; +use super::{PluginRegistryFile, PluginRegistryItem, PluginRegistryItemData}; use crate::{ Category, PluginExample, PluginSignature, ShellError, Signature, SyntaxShape, Type, Value, }; use pretty_assertions::assert_eq; use std::io::Cursor; -fn foo_plugin() -> PluginCacheItem { - PluginCacheItem { +fn foo_plugin() -> PluginRegistryItem { + PluginRegistryItem { name: "foo".into(), filename: "/path/to/nu_plugin_foo".into(), shell: None, - data: PluginCacheItemData::Valid { + data: PluginRegistryItemData::Valid { commands: vec![PluginSignature { sig: Signature::new("foo") .input_output_type(Type::Int, Type::List(Box::new(Type::Int))) @@ -30,12 +30,12 @@ fn foo_plugin() -> PluginCacheItem { } } -fn bar_plugin() -> PluginCacheItem { - PluginCacheItem { +fn bar_plugin() -> PluginRegistryItem { + PluginRegistryItem { name: "bar".into(), filename: "/path/to/nu_plugin_bar".into(), shell: None, - data: PluginCacheItemData::Valid { + data: PluginRegistryItemData::Valid { commands: vec![PluginSignature { sig: Signature::new("bar") .usage("overwrites files with random data") @@ -54,48 +54,48 @@ fn bar_plugin() -> PluginCacheItem { #[test] fn roundtrip() -> Result<(), ShellError> { - let mut plugin_cache_file = PluginCacheFile { + let mut plugin_registry_file = PluginRegistryFile { nushell_version: env!("CARGO_PKG_VERSION").to_owned(), plugins: vec![foo_plugin(), bar_plugin()], }; let mut output = vec![]; - plugin_cache_file.write_to(&mut output, None)?; + plugin_registry_file.write_to(&mut output, None)?; - let read_file = PluginCacheFile::read_from(Cursor::new(&output[..]), None)?; + let read_file = PluginRegistryFile::read_from(Cursor::new(&output[..]), None)?; - assert_eq!(plugin_cache_file, read_file); + assert_eq!(plugin_registry_file, read_file); Ok(()) } #[test] fn roundtrip_invalid() -> Result<(), ShellError> { - let mut plugin_cache_file = PluginCacheFile { + let mut plugin_registry_file = PluginRegistryFile { nushell_version: env!("CARGO_PKG_VERSION").to_owned(), - plugins: vec![PluginCacheItem { + plugins: vec![PluginRegistryItem { name: "invalid".into(), filename: "/path/to/nu_plugin_invalid".into(), shell: None, - data: PluginCacheItemData::Invalid, + data: PluginRegistryItemData::Invalid, }], }; let mut output = vec![]; - plugin_cache_file.write_to(&mut output, None)?; + plugin_registry_file.write_to(&mut output, None)?; - let read_file = PluginCacheFile::read_from(Cursor::new(&output[..]), None)?; + let read_file = PluginRegistryFile::read_from(Cursor::new(&output[..]), None)?; - assert_eq!(plugin_cache_file, read_file); + assert_eq!(plugin_registry_file, read_file); Ok(()) } #[test] fn upsert_new() { - let mut file = PluginCacheFile::new(); + let mut file = PluginRegistryFile::new(); file.plugins.push(foo_plugin()); @@ -106,7 +106,7 @@ fn upsert_new() { #[test] fn upsert_replace() { - let mut file = PluginCacheFile::new(); + let mut file = PluginRegistryFile::new(); file.plugins.push(foo_plugin()); diff --git a/crates/nu-test-support/src/macros.rs b/crates/nu-test-support/src/macros.rs index 1f8d797eb9..7e513a1a4a 100644 --- a/crates/nu-test-support/src/macros.rs +++ b/crates/nu-test-support/src/macros.rs @@ -334,7 +334,7 @@ where temp_file }); - // We don't have to write the plugin cache file, it's ok for it to not exist + // We don't have to write the plugin registry file, it's ok for it to not exist let temp_plugin_file = temp.path().join("plugin.msgpackz"); crate::commands::ensure_plugins_built(); diff --git a/src/command.rs b/src/command.rs index f23907d179..0d9aaf925f 100644 --- a/src/command.rs +++ b/src/command.rs @@ -392,13 +392,13 @@ impl Command for Nu { .named( "plugin-config", SyntaxShape::Filepath, - "start with an alternate plugin cache file", + "start with an alternate plugin registry file", None, ) .named( "plugins", SyntaxShape::List(Box::new(SyntaxShape::Filepath)), - "list of plugin executable files to load, separately from the cache file", + "list of plugin executable files to load, separately from the registry file", None, ) } diff --git a/src/tests/test_parser.rs b/src/tests/test_parser.rs index 0468437a7e..d9a0bc794c 100644 --- a/src/tests/test_parser.rs +++ b/src/tests/test_parser.rs @@ -598,7 +598,7 @@ register $file fn plugin_use_with_string_literal() -> TestResult { fail_test( r#"plugin use 'nu-plugin-math'"#, - "Plugin cache file not set", + "Plugin registry file not set", ) } @@ -609,7 +609,7 @@ const file = 'nu-plugin-math' plugin use $file "; // should not fail with `not a constant` - fail_test(input, "Plugin cache file not set") + fail_test(input, "Plugin registry file not set") } #[test] diff --git a/tests/plugins/mod.rs b/tests/plugins/mod.rs index 26db8376a9..605f78b564 100644 --- a/tests/plugins/mod.rs +++ b/tests/plugins/mod.rs @@ -1,4 +1,3 @@ -mod cache_file; mod config; mod core_inc; mod custom_values; @@ -6,5 +5,6 @@ mod env; mod formats; mod nu_plugin_nu_example; mod register; +mod registry_file; mod stream; mod stress_internals; diff --git a/tests/plugins/cache_file.rs b/tests/plugins/registry_file.rs similarity index 89% rename from tests/plugins/cache_file.rs rename to tests/plugins/registry_file.rs index d8132955ce..674be48f15 100644 --- a/tests/plugins/cache_file.rs +++ b/tests/plugins/registry_file.rs @@ -1,6 +1,6 @@ use std::{fs::File, path::PathBuf}; -use nu_protocol::{PluginCacheFile, PluginCacheItem, PluginCacheItemData}; +use nu_protocol::{PluginRegistryFile, PluginRegistryItem, PluginRegistryItemData}; use nu_test_support::{fs::Stub, nu, nu_with_plugins, playground::Playground}; fn example_plugin_path() -> PathBuf { @@ -120,7 +120,7 @@ fn plugin_add_to_custom_path() { assert!(result.status.success()); - let contents = PluginCacheFile::read_from( + let contents = PluginRegistryFile::read_from( File::open(dirs.test().join("test-plugin-file.msgpackz")) .expect("failed to open plugin file"), None, @@ -143,21 +143,21 @@ fn plugin_rm_then_restart_nu() { let file = File::create(dirs.test().join("test-plugin-file.msgpackz")) .expect("failed to create file"); - let mut contents = PluginCacheFile::new(); + let mut contents = PluginRegistryFile::new(); - contents.upsert_plugin(PluginCacheItem { + contents.upsert_plugin(PluginRegistryItem { name: "example".into(), filename: example_plugin_path, shell: None, - data: PluginCacheItemData::Valid { commands: vec![] }, + data: PluginRegistryItemData::Valid { commands: vec![] }, }); - contents.upsert_plugin(PluginCacheItem { + contents.upsert_plugin(PluginRegistryItem { name: "foo".into(), // this doesn't exist, but it should be ok filename: dirs.test().join("nu_plugin_foo"), shell: None, - data: PluginCacheItemData::Valid { commands: vec![] }, + data: PluginRegistryItemData::Valid { commands: vec![] }, }); contents @@ -219,21 +219,21 @@ fn plugin_rm_from_custom_path() { Playground::setup("plugin rm from custom path", |dirs, _playground| { let file = File::create(dirs.test().join("test-plugin-file.msgpackz")) .expect("failed to create file"); - let mut contents = PluginCacheFile::new(); + let mut contents = PluginRegistryFile::new(); - contents.upsert_plugin(PluginCacheItem { + contents.upsert_plugin(PluginRegistryItem { name: "example".into(), filename: example_plugin_path, shell: None, - data: PluginCacheItemData::Valid { commands: vec![] }, + data: PluginRegistryItemData::Valid { commands: vec![] }, }); - contents.upsert_plugin(PluginCacheItem { + contents.upsert_plugin(PluginRegistryItem { name: "foo".into(), // this doesn't exist, but it should be ok filename: dirs.test().join("nu_plugin_foo"), shell: None, - data: PluginCacheItemData::Valid { commands: vec![] }, + data: PluginRegistryItemData::Valid { commands: vec![] }, }); contents @@ -248,7 +248,7 @@ fn plugin_rm_from_custom_path() { assert!(result.err.trim().is_empty()); // Check the contents after running - let contents = PluginCacheFile::read_from( + let contents = PluginRegistryFile::read_from( File::open(dirs.test().join("test-plugin-file.msgpackz")).expect("failed to open file"), None, ) @@ -267,21 +267,21 @@ fn plugin_rm_using_filename() { Playground::setup("plugin rm using filename", |dirs, _playground| { let file = File::create(dirs.test().join("test-plugin-file.msgpackz")) .expect("failed to create file"); - let mut contents = PluginCacheFile::new(); + let mut contents = PluginRegistryFile::new(); - contents.upsert_plugin(PluginCacheItem { + contents.upsert_plugin(PluginRegistryItem { name: "example".into(), filename: example_plugin_path.clone(), shell: None, - data: PluginCacheItemData::Valid { commands: vec![] }, + data: PluginRegistryItemData::Valid { commands: vec![] }, }); - contents.upsert_plugin(PluginCacheItem { + contents.upsert_plugin(PluginRegistryItem { name: "foo".into(), // this doesn't exist, but it should be ok filename: dirs.test().join("nu_plugin_foo"), shell: None, - data: PluginCacheItemData::Valid { commands: vec![] }, + data: PluginRegistryItemData::Valid { commands: vec![] }, }); contents @@ -299,7 +299,7 @@ fn plugin_rm_using_filename() { assert!(result.err.trim().is_empty()); // Check the contents after running - let contents = PluginCacheFile::read_from( + let contents = PluginRegistryFile::read_from( File::open(dirs.test().join("test-plugin-file.msgpackz")).expect("failed to open file"), None, ) @@ -325,21 +325,21 @@ fn warning_on_invalid_plugin_item() { let file = File::create(dirs.test().join("test-plugin-file.msgpackz")) .expect("failed to create file"); - let mut contents = PluginCacheFile::new(); + let mut contents = PluginRegistryFile::new(); - contents.upsert_plugin(PluginCacheItem { + contents.upsert_plugin(PluginRegistryItem { name: "example".into(), filename: example_plugin_path, shell: None, - data: PluginCacheItemData::Valid { commands: vec![] }, + data: PluginRegistryItemData::Valid { commands: vec![] }, }); - contents.upsert_plugin(PluginCacheItem { + contents.upsert_plugin(PluginRegistryItem { name: "badtest".into(), // this doesn't exist, but it should be ok filename: dirs.test().join("nu_plugin_badtest"), shell: None, - data: PluginCacheItemData::Invalid, + data: PluginRegistryItemData::Invalid, }); contents @@ -372,7 +372,7 @@ fn warning_on_invalid_plugin_item() { // The "example" plugin should be unaffected assert_eq!(r#"["example"]"#, out); // The warning should be in there - assert!(err.contains("cached plugin data")); + assert!(err.contains("registered plugin data")); assert!(err.contains("badtest")); }) } @@ -388,9 +388,9 @@ fn plugin_use_error_not_found() { // Make an empty msgpackz let file = File::create(dirs.test().join("plugin.msgpackz")) .expect("failed to open plugin.msgpackz"); - PluginCacheFile::default() + PluginRegistryFile::default() .write_to(file, None) - .expect("failed to write empty cache file"); + .expect("failed to write empty registry file"); let output = assert_cmd::Command::new(nu_test_support::fs::executable_path()) .current_dir(dirs.test())