mirror of
https://github.com/nushell/nushell.git
synced 2025-08-10 20:24:24 +02:00
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:
@ -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::*;
|
||||
|
@ -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>,
|
@ -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());
|
||||
|
Reference in New Issue
Block a user