Rename plugin cache file ⇒ plugin registry file (#12634)

# 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.
This commit is contained in:
Devyn Cairns
2024-04-24 15:40:39 -07:00
committed by GitHub
parent 9996e4a1f8
commit 25cbcb511d
22 changed files with 165 additions and 159 deletions

View File

@ -24,7 +24,7 @@ use std::{
type PoisonDebuggerError<'a> = PoisonError<MutexGuard<'a, Box<dyn Debugger>>>;
#[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<PluginCacheItem>,
updated_items: Vec<PluginRegistryItem>,
) -> 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(),

View File

@ -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<Arc<dyn RegisteredPlugin>>,
#[cfg(feature = "plugin")]
pub(super) plugin_cache_items: Vec<PluginCacheItem>,
pub(super) plugin_registry_items: Vec<PluginRegistryItem>,
}
impl StateDelta {
@ -50,7 +50,7 @@ impl StateDelta {
#[cfg(feature = "plugin")]
plugins: vec![],
#[cfg(feature = "plugin")]
plugin_cache_items: vec![],
plugin_registry_items: vec![],
}
}

View File

@ -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<DeclId> {

View File

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

View File

@ -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<Span>,
#[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,
},

View File

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

View File

@ -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<PluginCacheItem>,
pub plugins: Vec<PluginRegistryItem>,
}
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<Span>,
) -> Result<PluginCacheFile, ShellError> {
) -> Result<PluginRegistryFile, ShellError> {
// 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<PluginSignature>) -> PluginCacheItem {
impl PluginRegistryItem {
/// Create a [`PluginRegistryItem`] from an identity and signatures.
pub fn new(
identity: &PluginIdentity,
mut commands: Vec<PluginSignature>,
) -> 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<PluginSignature>,

View File

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