Deprecate register and add plugin use (#12607)

# 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
This commit is contained in:
Devyn Cairns
2024-04-23 04:37:50 -07:00
committed by GitHub
parent 5c7f7883c8
commit 1f4131532d
27 changed files with 759 additions and 172 deletions

View File

@ -36,6 +36,8 @@ pub(crate) fn gather_commandline_args() -> (Vec<String>, String, Vec<String>) {
"--log-level" | "--log-target" | "--testbin" | "--threads" | "-t"
| "--include-path" | "--lsp" | "--ide-goto-def" | "--ide-hover" | "--ide-complete"
| "--ide-check" => args.next(),
#[cfg(feature = "plugin")]
"--plugins" => args.next(),
_ => None,
};
@ -87,6 +89,8 @@ pub(crate) fn parse_commandline_args(
let testbin = call.get_flag_expr("testbin");
#[cfg(feature = "plugin")]
let plugin_file = call.get_flag_expr("plugin-config");
#[cfg(feature = "plugin")]
let plugins = call.get_flag_expr("plugins");
let no_config_file = call.get_named_arg("no-config-file");
let no_history = call.get_named_arg("no-history");
let no_std_lib = call.get_named_arg("no-std-lib");
@ -131,17 +135,60 @@ pub(crate) fn parse_commandline_args(
}
}
fn extract_path(
expression: Option<&Expression>,
) -> Result<Option<Spanned<String>>, ShellError> {
if let Some(expr) = expression {
let tuple = expr.as_filepath();
if let Some((str, _)) = tuple {
Ok(Some(Spanned {
item: str,
span: expr.span,
}))
} else {
Err(ShellError::TypeMismatch {
err_message: "path".into(),
span: expr.span,
})
}
} else {
Ok(None)
}
}
let commands = extract_contents(commands)?;
let testbin = extract_contents(testbin)?;
#[cfg(feature = "plugin")]
let plugin_file = extract_contents(plugin_file)?;
let config_file = extract_contents(config_file)?;
let env_file = extract_contents(env_file)?;
let plugin_file = extract_path(plugin_file)?;
let config_file = extract_path(config_file)?;
let env_file = extract_path(env_file)?;
let log_level = extract_contents(log_level)?;
let log_target = extract_contents(log_target)?;
let execute = extract_contents(execute)?;
let include_path = extract_contents(include_path)?;
#[cfg(feature = "plugin")]
let plugins = plugins
.map(|expr| match &expr.expr {
Expr::List(list) => list
.iter()
.map(|item| {
item.expr()
.as_filepath()
.map(|(s, _)| s.into_spanned(item.expr().span))
.ok_or_else(|| ShellError::TypeMismatch {
err_message: "path".into(),
span: item.expr().span,
})
})
.collect::<Result<Vec<Spanned<String>>, _>>(),
_ => Err(ShellError::TypeMismatch {
err_message: "list<path>".into(),
span: expr.span,
}),
})
.transpose()?;
let help = call.has_flag(engine_state, &mut stack, "help")?;
if help {
@ -175,6 +222,8 @@ pub(crate) fn parse_commandline_args(
testbin,
#[cfg(feature = "plugin")]
plugin_file,
#[cfg(feature = "plugin")]
plugins,
no_config_file,
no_history,
no_std_lib,
@ -217,6 +266,8 @@ pub(crate) struct NushellCliArgs {
pub(crate) testbin: Option<Spanned<String>>,
#[cfg(feature = "plugin")]
pub(crate) plugin_file: Option<Spanned<String>>,
#[cfg(feature = "plugin")]
pub(crate) plugins: Option<Vec<Spanned<String>>>,
pub(crate) no_config_file: Option<Spanned<String>>,
pub(crate) no_history: Option<Spanned<String>>,
pub(crate) no_std_lib: Option<Spanned<String>>,
@ -294,13 +345,13 @@ impl Command for Nu {
.switch("version", "print the version", Some('v'))
.named(
"config",
SyntaxShape::String,
SyntaxShape::Filepath,
"start with an alternate config file",
None,
)
.named(
"env-config",
SyntaxShape::String,
SyntaxShape::Filepath,
"start with an alternate environment config file",
None,
)
@ -337,12 +388,19 @@ impl Command for Nu {
#[cfg(feature = "plugin")]
{
signature = signature.named(
"plugin-config",
SyntaxShape::String,
"start with an alternate plugin cache file",
None,
);
signature = signature
.named(
"plugin-config",
SyntaxShape::Filepath,
"start with an alternate plugin cache file",
None,
)
.named(
"plugins",
SyntaxShape::List(Box::new(SyntaxShape::Filepath)),
"list of plugin executable files to load, separately from the cache file",
None,
)
}
signature = signature