mirror of
https://github.com/nushell/nushell.git
synced 2025-04-12 07:18:19 +02:00
# Description Adds a new keyword, `plugin use`. Unlike `register`, this merely loads the signatures from the plugin cache file. The file is configurable with the `--plugin-config` option either to `nu` or to `plugin use` itself, just like the other `plugin` family of commands. At the REPL, one might do this to replace `register`: ```nushell > plugin add ~/.cargo/bin/nu_plugin_foo > plugin use foo ``` This will not work in a script, because `plugin use` is a keyword and `plugin add` does not evaluate at parse time (intentionally). This means we no longer run random binaries during parse. The `--plugins` option has been added to allow running `nu` with certain plugins in one step. This is used especially for the `nu_with_plugins!` test macro, but I'd imagine is generally useful. The only weird quirk is that it has to be a list, and we don't really do this for any of our other CLI args at the moment. `register` now prints a deprecation parse warning. This should fix #11923, as we now have a complete alternative to `register`. # User-Facing Changes - Add `plugin use` command - Deprecate `register` - Add `--plugins` option to `nu` to replace a common use of `register` # Tests + Formatting I think I've tested it thoroughly enough and every existing test passes. Testing nu CLI options and alternate config files is a little hairy and I wish there were some more generic helpers for this, so this will go on my TODO list for refactoring. - 🟢 `toolkit fmt` - 🟢 `toolkit clippy` - 🟢 `toolkit test` - 🟢 `toolkit test stdlib` # After Submitting - [ ] Update plugins sections of book - [ ] Release notes
150 lines
4.8 KiB
Rust
150 lines
4.8 KiB
Rust
use std::path::{Path, PathBuf};
|
|
|
|
use crate::{ParseError, ShellError, Spanned};
|
|
|
|
/// Error when an invalid plugin filename was encountered.
|
|
#[derive(Debug, Clone)]
|
|
pub struct InvalidPluginFilename(PathBuf);
|
|
|
|
impl std::fmt::Display for InvalidPluginFilename {
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
f.write_str("invalid plugin filename")
|
|
}
|
|
}
|
|
|
|
impl From<Spanned<InvalidPluginFilename>> for ParseError {
|
|
fn from(error: Spanned<InvalidPluginFilename>) -> ParseError {
|
|
ParseError::LabeledError(
|
|
"Invalid plugin filename".into(),
|
|
"must start with `nu_plugin_`".into(),
|
|
error.span,
|
|
)
|
|
}
|
|
}
|
|
|
|
impl From<Spanned<InvalidPluginFilename>> for ShellError {
|
|
fn from(error: Spanned<InvalidPluginFilename>) -> ShellError {
|
|
ShellError::GenericError {
|
|
error: format!("Invalid plugin filename: {}", error.item.0.display()),
|
|
msg: "not a valid plugin filename".into(),
|
|
span: Some(error.span),
|
|
help: Some("valid Nushell plugin filenames must start with `nu_plugin_`".into()),
|
|
inner: vec![],
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
|
pub struct PluginIdentity {
|
|
/// The filename used to start the plugin
|
|
filename: PathBuf,
|
|
/// The shell used to start the plugin, if required
|
|
shell: Option<PathBuf>,
|
|
/// The friendly name of the plugin (e.g. `inc` for `C:\nu_plugin_inc.exe`)
|
|
name: String,
|
|
}
|
|
|
|
impl PluginIdentity {
|
|
/// Create a new plugin identity from a path to plugin executable and shell option.
|
|
///
|
|
/// The `filename` must be an absolute path. Canonicalize before trying to construct the
|
|
/// [`PluginIdentity`].
|
|
pub fn new(
|
|
filename: impl Into<PathBuf>,
|
|
shell: Option<PathBuf>,
|
|
) -> Result<PluginIdentity, InvalidPluginFilename> {
|
|
let filename: PathBuf = filename.into();
|
|
|
|
// Must pass absolute path.
|
|
if filename.is_relative() {
|
|
return Err(InvalidPluginFilename(filename));
|
|
}
|
|
|
|
let name = filename
|
|
.file_stem()
|
|
.map(|stem| stem.to_string_lossy().into_owned())
|
|
.and_then(|stem| stem.strip_prefix("nu_plugin_").map(|s| s.to_owned()))
|
|
.ok_or_else(|| InvalidPluginFilename(filename.clone()))?;
|
|
|
|
Ok(PluginIdentity {
|
|
filename,
|
|
shell,
|
|
name,
|
|
})
|
|
}
|
|
|
|
/// The filename of the plugin executable.
|
|
pub fn filename(&self) -> &Path {
|
|
&self.filename
|
|
}
|
|
|
|
/// The shell command used by the plugin.
|
|
pub fn shell(&self) -> Option<&Path> {
|
|
self.shell.as_deref()
|
|
}
|
|
|
|
/// The name of the plugin, determined by the part of the filename after `nu_plugin_` excluding
|
|
/// the extension.
|
|
///
|
|
/// - `C:\nu_plugin_inc.exe` becomes `inc`
|
|
/// - `/home/nu/.cargo/bin/nu_plugin_inc` becomes `inc`
|
|
pub fn name(&self) -> &str {
|
|
&self.name
|
|
}
|
|
|
|
/// Create a fake identity for testing.
|
|
#[cfg(windows)]
|
|
#[doc(hidden)]
|
|
pub fn new_fake(name: &str) -> PluginIdentity {
|
|
PluginIdentity::new(format!(r"C:\fake\path\nu_plugin_{name}.exe"), None)
|
|
.expect("fake plugin identity path is invalid")
|
|
}
|
|
|
|
/// Create a fake identity for testing.
|
|
#[cfg(not(windows))]
|
|
#[doc(hidden)]
|
|
pub fn new_fake(name: &str) -> PluginIdentity {
|
|
PluginIdentity::new(format!(r"/fake/path/nu_plugin_{name}"), None)
|
|
.expect("fake plugin identity path is invalid")
|
|
}
|
|
|
|
/// A command that could be used to add the plugin, for suggesting in errors.
|
|
pub fn add_command(&self) -> String {
|
|
if let Some(shell) = self.shell() {
|
|
format!(
|
|
"plugin add --shell '{}' '{}'",
|
|
shell.display(),
|
|
self.filename().display(),
|
|
)
|
|
} else {
|
|
format!("plugin add '{}'", self.filename().display())
|
|
}
|
|
}
|
|
|
|
/// A command that could be used to reload the plugin, for suggesting in errors.
|
|
pub fn use_command(&self) -> String {
|
|
format!("plugin use '{}'", self.name())
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn parses_name_from_path() {
|
|
assert_eq!("test", PluginIdentity::new_fake("test").name());
|
|
assert_eq!("test_2", PluginIdentity::new_fake("test_2").name());
|
|
let absolute_path = if cfg!(windows) {
|
|
r"C:\path\to\nu_plugin_foo.sh"
|
|
} else {
|
|
"/path/to/nu_plugin_foo.sh"
|
|
};
|
|
assert_eq!(
|
|
"foo",
|
|
PluginIdentity::new(absolute_path, Some("sh".into()))
|
|
.expect("should be valid")
|
|
.name()
|
|
);
|
|
// Relative paths should be invalid
|
|
PluginIdentity::new("nu_plugin_foo.sh", Some("sh".into())).expect_err("should be invalid");
|
|
PluginIdentity::new("other", None).expect_err("should be invalid");
|
|
PluginIdentity::new("", None).expect_err("should be invalid");
|
|
}
|