Change PluginCommand API to be more like Command (#12279)

# Description

This is something that was discussed in the core team meeting last
Wednesday. @ayax79 is building `nu-plugin-polars` with all of the
dataframe commands into a plugin, and there are a lot of them, so it
would help to make the API more similar. At the same time, I think the
`Command` API is just better anyway. I don't think the difference is
justified, and the types for core commands have the benefit of requiring
less `.into()` because they often don't own their data

- Broke `signature()` up into `name()`, `usage()`, `extra_usage()`,
`search_terms()`, `examples()`
- `signature()` returns `nu_protocol::Signature`
- `examples()` returns `Vec<nu_protocol::Example>`
- `PluginSignature` and `PluginExample` no longer need to be used by
plugin developers

# User-Facing Changes
Breaking API for plugins yet again 😄
This commit is contained in:
Devyn Cairns
2024-03-27 03:59:57 -07:00
committed by GitHub
parent 03b5e9d853
commit 01d30a416b
45 changed files with 962 additions and 674 deletions

View File

@ -1,8 +1,7 @@
use crate::CustomValuePlugin;
use nu_plugin::{EngineInterface, EvaluatedCall, SimplePluginCommand};
use nu_protocol::{
record, Category, CustomValue, LabeledError, PluginSignature, ShellError, Span, SyntaxShape,
Value,
record, Category, CustomValue, LabeledError, ShellError, Signature, Span, SyntaxShape, Value,
};
use serde::{Deserialize, Serialize};
@ -59,9 +58,16 @@ pub struct DropCheck;
impl SimplePluginCommand for DropCheck {
type Plugin = CustomValuePlugin;
fn signature(&self) -> nu_protocol::PluginSignature {
PluginSignature::build("custom-value drop-check")
.usage("Generates a custom value that prints a message when dropped")
fn name(&self) -> &str {
"custom-value drop-check"
}
fn usage(&self) -> &str {
"Generates a custom value that prints a message when dropped"
}
fn signature(&self) -> Signature {
Signature::build(self.name())
.required("msg", SyntaxShape::String, "the message to print on drop")
.category(Category::Experimental)
}

View File

@ -1,21 +1,30 @@
use crate::{cool_custom_value::CoolCustomValue, CustomValuePlugin};
use nu_plugin::{EngineInterface, EvaluatedCall, SimplePluginCommand};
use nu_protocol::{Category, LabeledError, PluginExample, PluginSignature, Span, Value};
use nu_protocol::{Category, Example, LabeledError, Signature, Span, Value};
pub struct Generate;
impl SimplePluginCommand for Generate {
type Plugin = CustomValuePlugin;
fn signature(&self) -> PluginSignature {
PluginSignature::build("custom-value generate")
.usage("PluginSignature for a plugin that generates a custom value")
.category(Category::Experimental)
.plugin_examples(vec![PluginExample {
example: "custom-value generate".into(),
description: "Generate a new CoolCustomValue".into(),
result: Some(CoolCustomValue::new("abc").into_value(Span::test_data())),
}])
fn name(&self) -> &str {
"custom-value generate"
}
fn usage(&self) -> &str {
"PluginSignature for a plugin that generates a custom value"
}
fn signature(&self) -> Signature {
Signature::build(self.name()).category(Category::Experimental)
}
fn examples(&self) -> Vec<Example> {
vec![Example {
example: "custom-value generate",
description: "Generate a new CoolCustomValue",
result: Some(CoolCustomValue::new("abc").into_value(Span::test_data())),
}]
}
fn run(

View File

@ -1,35 +1,43 @@
use crate::{second_custom_value::SecondCustomValue, CustomValuePlugin};
use nu_plugin::{EngineInterface, EvaluatedCall, SimplePluginCommand};
use nu_protocol::{
Category, LabeledError, PluginExample, PluginSignature, Span, SyntaxShape, Value,
};
use nu_protocol::{Category, Example, LabeledError, Signature, Span, SyntaxShape, Value};
pub struct Generate2;
impl SimplePluginCommand for Generate2 {
type Plugin = CustomValuePlugin;
fn signature(&self) -> PluginSignature {
PluginSignature::build("custom-value generate2")
.usage("PluginSignature for a plugin that generates a different custom value")
fn name(&self) -> &str {
"custom-value generate2"
}
fn usage(&self) -> &str {
"PluginSignature for a plugin that generates a different custom value"
}
fn signature(&self) -> Signature {
Signature::build(self.name())
.optional(
"closure",
SyntaxShape::Closure(Some(vec![SyntaxShape::Any])),
"An optional closure to pass the custom value to",
)
.category(Category::Experimental)
.plugin_examples(vec![
PluginExample {
example: "custom-value generate2".into(),
description: "Generate a new SecondCustomValue".into(),
result: Some(SecondCustomValue::new("xyz").into_value(Span::test_data())),
},
PluginExample {
example: "custom-value generate2 { print }".into(),
description: "Generate a new SecondCustomValue and pass it to a closure".into(),
result: None,
},
])
}
fn examples(&self) -> Vec<Example> {
vec![
Example {
example: "custom-value generate2",
description: "Generate a new SecondCustomValue",
result: Some(SecondCustomValue::new("xyz").into_value(Span::test_data())),
},
Example {
example: "custom-value generate2 { print }",
description: "Generate a new SecondCustomValue and pass it to a closure",
result: None,
},
]
}
fn run(

View File

@ -2,31 +2,38 @@ use crate::{
cool_custom_value::CoolCustomValue, second_custom_value::SecondCustomValue, CustomValuePlugin,
};
use nu_plugin::{EngineInterface, EvaluatedCall, SimplePluginCommand};
use nu_protocol::{
Category, LabeledError, PluginExample, PluginSignature, ShellError, Span, Value,
};
use nu_protocol::{Category, Example, LabeledError, ShellError, Signature, Span, Value};
pub struct Update;
impl SimplePluginCommand for Update {
type Plugin = CustomValuePlugin;
fn signature(&self) -> PluginSignature {
PluginSignature::build("custom-value update")
.usage("PluginSignature for a plugin that updates a custom value")
.category(Category::Experimental)
.plugin_examples(vec![
PluginExample {
example: "custom-value generate | custom-value update".into(),
description: "Update a CoolCustomValue".into(),
result: Some(CoolCustomValue::new("abcxyz").into_value(Span::test_data())),
},
PluginExample {
example: "custom-value generate2 | custom-value update".into(),
description: "Update a SecondCustomValue".into(),
result: Some(SecondCustomValue::new("xyzabc").into_value(Span::test_data())),
},
])
fn name(&self) -> &str {
"custom-value update"
}
fn usage(&self) -> &str {
"PluginSignature for a plugin that updates a custom value"
}
fn signature(&self) -> Signature {
Signature::build(self.name()).category(Category::Experimental)
}
fn examples(&self) -> Vec<Example> {
vec![
Example {
example: "custom-value generate | custom-value update",
description: "Update a CoolCustomValue",
result: Some(CoolCustomValue::new("abcxyz").into_value(Span::test_data())),
},
Example {
example: "custom-value generate2 | custom-value update",
description: "Update a SecondCustomValue",
result: Some(SecondCustomValue::new("xyzabc").into_value(Span::test_data())),
},
]
}
fn run(

View File

@ -1,15 +1,22 @@
use crate::{update::Update, CustomValuePlugin};
use nu_plugin::{EngineInterface, EvaluatedCall, SimplePluginCommand};
use nu_protocol::{Category, LabeledError, PluginSignature, SyntaxShape, Value};
use nu_protocol::{Category, LabeledError, Signature, SyntaxShape, Value};
pub struct UpdateArg;
impl SimplePluginCommand for UpdateArg {
type Plugin = CustomValuePlugin;
fn signature(&self) -> PluginSignature {
PluginSignature::build("custom-value update-arg")
.usage("PluginSignature for a plugin that updates a custom value as an argument")
fn name(&self) -> &str {
"custom-value update-arg"
}
fn usage(&self) -> &str {
"Updates a custom value as an argument"
}
fn signature(&self) -> Signature {
Signature::build(self.name())
.required(
"custom_value",
SyntaxShape::Any,