mirror of
https://github.com/nushell/nushell.git
synced 2025-04-27 22:58:20 +02:00
# Description This breaks `nu-plugin` up into four crates: - `nu-plugin-protocol`: just the type definitions for the protocol, no I/O. If someone wanted to wire up something more bare metal, maybe for async I/O, they could use this. - `nu-plugin-core`: the shared stuff between engine/plugin. Less stable interface. - `nu-plugin-engine`: everything required for the engine to talk to plugins. Less stable interface. - `nu-plugin`: everything required for the plugin to talk to the engine, what plugin developers use. Should be the most stable interface. No changes are made to the interface exposed by `nu-plugin` - it should all still be there. Re-exports from `nu-plugin-protocol` or `nu-plugin-core` are used as required. Plugins shouldn't ever have to use those crates directly. This should be somewhat faster to compile as `nu-plugin-engine` and `nu-plugin` can compile in parallel, and the engine doesn't need `nu-plugin` and plugins don't need `nu-plugin-engine` (except for test support), so that should reduce what needs to be compiled too. The only significant change here other than splitting stuff up was to break the `source` out of `PluginCustomValue` and create a new `PluginCustomValueWithSource` type that contains that instead. One bonus of that is we get rid of the option and it's now more type-safe, but it also means that the logic for that stuff (actually running the plugin for custom value ops) can live entirely within the `nu-plugin-engine` crate. # User-Facing Changes - New crates. - Added `local-socket` feature for `nu` to try to make it possible to compile without that support if needed. # Tests + Formatting - 🟢 `toolkit fmt` - 🟢 `toolkit clippy` - 🟢 `toolkit test` - 🟢 `toolkit test stdlib`
127 lines
3.5 KiB
Rust
127 lines
3.5 KiB
Rust
use nu_engine::{command_prelude::*, get_eval_expression};
|
|
use nu_plugin_protocol::{CallInfo, EvaluatedCall};
|
|
use nu_protocol::{PluginIdentity, PluginSignature};
|
|
use std::sync::Arc;
|
|
|
|
use crate::{GetPlugin, PluginExecutionCommandContext, PluginSource};
|
|
|
|
/// The command declaration proxy used within the engine for all plugin commands.
|
|
#[derive(Clone)]
|
|
pub struct PluginDeclaration {
|
|
name: String,
|
|
signature: PluginSignature,
|
|
source: PluginSource,
|
|
}
|
|
|
|
impl PluginDeclaration {
|
|
pub fn new(plugin: Arc<dyn GetPlugin>, signature: PluginSignature) -> Self {
|
|
Self {
|
|
name: signature.sig.name.clone(),
|
|
signature,
|
|
source: PluginSource::new(plugin),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl Command for PluginDeclaration {
|
|
fn name(&self) -> &str {
|
|
&self.name
|
|
}
|
|
|
|
fn signature(&self) -> Signature {
|
|
self.signature.sig.clone()
|
|
}
|
|
|
|
fn usage(&self) -> &str {
|
|
self.signature.sig.usage.as_str()
|
|
}
|
|
|
|
fn extra_usage(&self) -> &str {
|
|
self.signature.sig.extra_usage.as_str()
|
|
}
|
|
|
|
fn search_terms(&self) -> Vec<&str> {
|
|
self.signature
|
|
.sig
|
|
.search_terms
|
|
.iter()
|
|
.map(|term| term.as_str())
|
|
.collect()
|
|
}
|
|
|
|
fn examples(&self) -> Vec<Example> {
|
|
let mut res = vec![];
|
|
for e in self.signature.examples.iter() {
|
|
res.push(Example {
|
|
example: &e.example,
|
|
description: &e.description,
|
|
result: e.result.clone(),
|
|
})
|
|
}
|
|
res
|
|
}
|
|
|
|
fn run(
|
|
&self,
|
|
engine_state: &EngineState,
|
|
stack: &mut Stack,
|
|
call: &Call,
|
|
input: PipelineData,
|
|
) -> Result<PipelineData, ShellError> {
|
|
let eval_expression = get_eval_expression(engine_state);
|
|
|
|
// Create the EvaluatedCall to send to the plugin first - it's best for this to fail early,
|
|
// before we actually try to run the plugin command
|
|
let evaluated_call =
|
|
EvaluatedCall::try_from_call(call, engine_state, stack, eval_expression)?;
|
|
|
|
// Get the engine config
|
|
let engine_config = nu_engine::get_config(engine_state, stack);
|
|
|
|
// Get, or start, the plugin.
|
|
let plugin = self
|
|
.source
|
|
.persistent(None)
|
|
.and_then(|p| {
|
|
// Set the garbage collector config from the local config before running
|
|
p.set_gc_config(engine_config.plugin_gc.get(p.identity().name()));
|
|
p.get_plugin(Some((engine_state, stack)))
|
|
})
|
|
.map_err(|err| {
|
|
let decl = engine_state.get_decl(call.decl_id);
|
|
ShellError::GenericError {
|
|
error: format!("Unable to spawn plugin for `{}`", decl.name()),
|
|
msg: err.to_string(),
|
|
span: Some(call.head),
|
|
help: None,
|
|
inner: vec![],
|
|
}
|
|
})?;
|
|
|
|
// Create the context to execute in - this supports engine calls and custom values
|
|
let mut context = PluginExecutionCommandContext::new(
|
|
self.source.identity.clone(),
|
|
engine_state,
|
|
stack,
|
|
call,
|
|
);
|
|
|
|
plugin.run(
|
|
CallInfo {
|
|
name: self.name.clone(),
|
|
call: evaluated_call,
|
|
input,
|
|
},
|
|
&mut context,
|
|
)
|
|
}
|
|
|
|
fn is_plugin(&self) -> bool {
|
|
true
|
|
}
|
|
|
|
fn plugin_identity(&self) -> Option<&PluginIdentity> {
|
|
Some(&self.source.identity)
|
|
}
|
|
}
|