forked from extern/nushell
Overhaul the plugin cache file with a new msgpack+brotli format (#12579)
# Description - Plugin signatures are now saved to `plugin.msgpackz`, which is brotli-compressed MessagePack. - The file is updated incrementally, rather than writing all plugin commands in the engine every time. - The file always contains the result of the `Signature` call to the plugin, even if commands were removed. - Invalid data for a particular plugin just causes an error to be reported, but the rest of the plugins can still be parsed # User-Facing Changes - The plugin file has a different filename, and it's not a nushell script. - The default `plugin.nu` file will be automatically migrated the first time, but not other plugin config files. - We don't currently provide any utilities that could help edit this file, beyond `plugin add` and `plugin rm` - `from msgpackz`, `to msgpackz` could also help - New commands: `plugin add`, `plugin rm` # Tests + Formatting Tests added for the format and for the invalid handling. - 🟢 `toolkit fmt` - 🟢 `toolkit clippy` - 🟢 `toolkit test` - 🟢 `toolkit test stdlib` # After Submitting - [ ] Check for documentation changes - [ ] Definitely needs release notes
This commit is contained in:
50
crates/nu-cmd-plugin/src/util.rs
Normal file
50
crates/nu-cmd-plugin/src/util.rs
Normal file
@ -0,0 +1,50 @@
|
||||
use std::fs::{self, File};
|
||||
|
||||
use nu_engine::{command_prelude::*, current_dir};
|
||||
use nu_protocol::PluginCacheFile;
|
||||
|
||||
pub(crate) fn modify_plugin_file(
|
||||
engine_state: &EngineState,
|
||||
stack: &mut Stack,
|
||||
span: Span,
|
||||
custom_path: Option<Spanned<String>>,
|
||||
operate: impl FnOnce(&mut PluginCacheFile) -> Result<(), ShellError>,
|
||||
) -> Result<(), ShellError> {
|
||||
let cwd = current_dir(engine_state, stack)?;
|
||||
|
||||
let plugin_cache_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(),
|
||||
msg: "pass --plugin-config explicitly here".into(),
|
||||
span: Some(span),
|
||||
help: Some("you may be running `nu` with --no-config-file".into()),
|
||||
inner: vec![],
|
||||
})?
|
||||
};
|
||||
|
||||
// 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).map_err(|err| err.into_spanned(span))?,
|
||||
Some(span),
|
||||
)?
|
||||
} else {
|
||||
PluginCacheFile::default()
|
||||
};
|
||||
|
||||
// Do the operation
|
||||
operate(&mut contents)?;
|
||||
|
||||
// Save the modified file on success
|
||||
contents.write_to(
|
||||
File::create(&plugin_cache_file_path).map_err(|err| err.into_spanned(span))?,
|
||||
Some(span),
|
||||
)?;
|
||||
|
||||
Ok(())
|
||||
}
|
Reference in New Issue
Block a user