From a29efe28f75a383a8089e5dd4a243a2cb40d26b3 Mon Sep 17 00:00:00 2001 From: Devyn Cairns Date: Tue, 19 Mar 2024 10:36:46 -0700 Subject: [PATCH] Merge stream_example into example plugin and clean up names (#12234) # Description As suggested by @WindSoilder, since plugins can now contain both simple commands that produce `Value` and commands that produce `PipelineData` without having to choose one or the other for the whole plugin, this change merges `stream_example` into `example`. # User-Facing Changes All of the example plugins are renamed. # Tests + Formatting - :green_circle: `toolkit fmt` - :green_circle: `toolkit clippy` - :green_circle: `toolkit test` - :green_circle: `toolkit test stdlib` # After Submitting - [ ] Check nushell/nushell.github.io for any docs that match the command names changed --- Cargo.lock | 8 --- Cargo.toml | 1 - crates/nu_plugin_example/README.md | 2 +- .../src/commands/collect_external.rs | 12 ++-- .../{nu_example_config.rs => config.rs} | 6 +- ...nu_example_disable_gc.rs => disable_gc.rs} | 6 +- .../commands/{nu_example_env.rs => env.rs} | 6 +- .../src/commands/for_each.rs | 12 ++-- .../src/commands/generate.rs | 15 ++--- crates/nu_plugin_example/src/commands/main.rs | 41 ++++++++++++ crates/nu_plugin_example/src/commands/mod.rs | 46 +++++++++---- .../src/commands/{nu_example_1.rs => one.rs} | 10 +-- .../src/commands/seq.rs | 12 ++-- .../src/commands/sum.rs | 12 ++-- .../commands/{nu_example_3.rs => three.rs} | 6 +- .../src/commands/{nu_example_2.rs => two.rs} | 6 +- crates/nu_plugin_example/src/lib.rs | 21 ++++-- crates/nu_plugin_stream_example/Cargo.toml | 19 ------ crates/nu_plugin_stream_example/README.md | 62 ------------------ .../src/commands/mod.rs | 11 ---- crates/nu_plugin_stream_example/src/lib.rs | 50 --------------- crates/nu_plugin_stream_example/src/main.rs | 30 --------- tests/plugin_persistence/mod.rs | 18 +++--- tests/plugins/config.rs | 6 +- tests/plugins/env.rs | 10 +-- tests/plugins/register.rs | 6 +- tests/plugins/stream.rs | 64 +++++++++---------- wix/main.wxs | 4 +- 28 files changed, 197 insertions(+), 305 deletions(-) rename crates/{nu_plugin_stream_example => nu_plugin_example}/src/commands/collect_external.rs (84%) rename crates/nu_plugin_example/src/commands/{nu_example_config.rs => config.rs} (90%) rename crates/nu_plugin_example/src/commands/{nu_example_disable_gc.rs => disable_gc.rs} (92%) rename crates/nu_plugin_example/src/commands/{nu_example_env.rs => env.rs} (95%) rename crates/{nu_plugin_stream_example => nu_plugin_example}/src/commands/for_each.rs (82%) rename crates/{nu_plugin_stream_example => nu_plugin_example}/src/commands/generate.rs (86%) create mode 100644 crates/nu_plugin_example/src/commands/main.rs rename crates/nu_plugin_example/src/commands/{nu_example_1.rs => one.rs} (86%) rename crates/{nu_plugin_stream_example => nu_plugin_example}/src/commands/seq.rs (86%) rename crates/{nu_plugin_stream_example => nu_plugin_example}/src/commands/sum.rs (91%) rename crates/nu_plugin_example/src/commands/{nu_example_3.rs => three.rs} (92%) rename crates/nu_plugin_example/src/commands/{nu_example_2.rs => two.rs} (93%) delete mode 100644 crates/nu_plugin_stream_example/Cargo.toml delete mode 100644 crates/nu_plugin_stream_example/README.md delete mode 100644 crates/nu_plugin_stream_example/src/commands/mod.rs delete mode 100644 crates/nu_plugin_stream_example/src/lib.rs delete mode 100644 crates/nu_plugin_stream_example/src/main.rs diff --git a/Cargo.lock b/Cargo.lock index 1a03df41fa..78af3e2935 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3228,14 +3228,6 @@ dependencies = [ "sxd-xpath", ] -[[package]] -name = "nu_plugin_stream_example" -version = "0.91.1" -dependencies = [ - "nu-plugin", - "nu-protocol", -] - [[package]] name = "num" version = "0.4.1" diff --git a/Cargo.toml b/Cargo.toml index 956b52cb31..7c4a6d2ef6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -43,7 +43,6 @@ members = [ "crates/nu_plugin_inc", "crates/nu_plugin_gstat", "crates/nu_plugin_example", - "crates/nu_plugin_stream_example", "crates/nu_plugin_query", "crates/nu_plugin_custom_values", "crates/nu_plugin_formats", diff --git a/crates/nu_plugin_example/README.md b/crates/nu_plugin_example/README.md index bb26b0c7f2..d4bc6dbe29 100644 --- a/crates/nu_plugin_example/README.md +++ b/crates/nu_plugin_example/README.md @@ -29,6 +29,6 @@ $env.config = { To list plugin values run: ```nushell -nu-example-config +example config ``` diff --git a/crates/nu_plugin_stream_example/src/commands/collect_external.rs b/crates/nu_plugin_example/src/commands/collect_external.rs similarity index 84% rename from crates/nu_plugin_stream_example/src/commands/collect_external.rs rename to crates/nu_plugin_example/src/commands/collect_external.rs index d603a568ad..999f39bc42 100644 --- a/crates/nu_plugin_stream_example/src/commands/collect_external.rs +++ b/crates/nu_plugin_example/src/commands/collect_external.rs @@ -1,16 +1,16 @@ use nu_plugin::{EngineInterface, EvaluatedCall, LabeledError, PluginCommand}; use nu_protocol::{Category, PipelineData, PluginExample, PluginSignature, RawStream, Type, Value}; -use crate::StreamExample; +use crate::Example; -/// `> | stream_example collect-external` +/// `> | example collect-external` pub struct CollectExternal; impl PluginCommand for CollectExternal { - type Plugin = StreamExample; + type Plugin = Example; fn signature(&self) -> PluginSignature { - PluginSignature::build("stream_example collect-external") + PluginSignature::build("example collect-external") .usage("Example transformer to raw external stream") .search_terms(vec!["example".into()]) .input_output_types(vec![ @@ -18,7 +18,7 @@ impl PluginCommand for CollectExternal { (Type::List(Type::Binary.into()), Type::Binary), ]) .plugin_examples(vec![PluginExample { - example: "[a b] | stream_example collect-external".into(), + example: "[a b] | example collect-external".into(), description: "collect strings into one stream".into(), result: Some(Value::test_string("ab")), }]) @@ -27,7 +27,7 @@ impl PluginCommand for CollectExternal { fn run( &self, - _plugin: &StreamExample, + _plugin: &Example, _engine: &EngineInterface, call: &EvaluatedCall, input: PipelineData, diff --git a/crates/nu_plugin_example/src/commands/nu_example_config.rs b/crates/nu_plugin_example/src/commands/config.rs similarity index 90% rename from crates/nu_plugin_example/src/commands/nu_example_config.rs rename to crates/nu_plugin_example/src/commands/config.rs index 1bbb11b7b4..5e7ffd571f 100644 --- a/crates/nu_plugin_example/src/commands/nu_example_config.rs +++ b/crates/nu_plugin_example/src/commands/config.rs @@ -3,13 +3,13 @@ use nu_protocol::{Category, PluginSignature, Type, Value}; use crate::Example; -pub struct NuExampleConfig; +pub struct Config; -impl SimplePluginCommand for NuExampleConfig { +impl SimplePluginCommand for Config { type Plugin = Example; fn signature(&self) -> PluginSignature { - PluginSignature::build("nu-example-config") + PluginSignature::build("example config") .usage("Show plugin configuration") .extra_usage("The configuration is set under $env.config.plugins.example") .category(Category::Experimental) diff --git a/crates/nu_plugin_example/src/commands/nu_example_disable_gc.rs b/crates/nu_plugin_example/src/commands/disable_gc.rs similarity index 92% rename from crates/nu_plugin_example/src/commands/nu_example_disable_gc.rs rename to crates/nu_plugin_example/src/commands/disable_gc.rs index 681e221d7d..98ca12387e 100644 --- a/crates/nu_plugin_example/src/commands/nu_example_disable_gc.rs +++ b/crates/nu_plugin_example/src/commands/disable_gc.rs @@ -3,13 +3,13 @@ use nu_protocol::{Category, PluginSignature, Value}; use crate::Example; -pub struct NuExampleDisableGc; +pub struct DisableGc; -impl SimplePluginCommand for NuExampleDisableGc { +impl SimplePluginCommand for DisableGc { type Plugin = Example; fn signature(&self) -> PluginSignature { - PluginSignature::build("nu-example-disable-gc") + PluginSignature::build("example disable-gc") .usage("Disable the plugin garbage collector for `example`") .extra_usage( "\ diff --git a/crates/nu_plugin_example/src/commands/nu_example_env.rs b/crates/nu_plugin_example/src/commands/env.rs similarity index 95% rename from crates/nu_plugin_example/src/commands/nu_example_env.rs rename to crates/nu_plugin_example/src/commands/env.rs index 1d239779cc..85d5276da4 100644 --- a/crates/nu_plugin_example/src/commands/nu_example_env.rs +++ b/crates/nu_plugin_example/src/commands/env.rs @@ -3,13 +3,13 @@ use nu_protocol::{Category, PluginSignature, SyntaxShape, Type, Value}; use crate::Example; -pub struct NuExampleEnv; +pub struct Env; -impl SimplePluginCommand for NuExampleEnv { +impl SimplePluginCommand for Env { type Plugin = Example; fn signature(&self) -> PluginSignature { - PluginSignature::build("nu-example-env") + PluginSignature::build("example env") .usage("Get environment variable(s)") .extra_usage("Returns all environment variables if no name provided") .category(Category::Experimental) diff --git a/crates/nu_plugin_stream_example/src/commands/for_each.rs b/crates/nu_plugin_example/src/commands/for_each.rs similarity index 82% rename from crates/nu_plugin_stream_example/src/commands/for_each.rs rename to crates/nu_plugin_example/src/commands/for_each.rs index 3b1f4245c6..9b9feeadee 100644 --- a/crates/nu_plugin_stream_example/src/commands/for_each.rs +++ b/crates/nu_plugin_example/src/commands/for_each.rs @@ -1,16 +1,16 @@ use nu_plugin::{EngineInterface, EvaluatedCall, LabeledError, PluginCommand}; use nu_protocol::{Category, PipelineData, PluginExample, PluginSignature, SyntaxShape, Type}; -use crate::StreamExample; +use crate::Example; -/// ` | stream_example for-each { |value| ... }` +/// ` | example for-each { |value| ... }` pub struct ForEach; impl PluginCommand for ForEach { - type Plugin = StreamExample; + type Plugin = Example; fn signature(&self) -> PluginSignature { - PluginSignature::build("stream_example for-each") + PluginSignature::build("example for-each") .usage("Example execution of a closure with a stream") .extra_usage("Prints each value the closure returns to stderr") .input_output_type(Type::ListStream, Type::Nothing) @@ -20,7 +20,7 @@ impl PluginCommand for ForEach { "The closure to run for each input value", ) .plugin_examples(vec![PluginExample { - example: "ls | get name | stream_example for-each { |f| ^file $f }".into(), + example: "ls | get name | example for-each { |f| ^file $f }".into(), description: "example with an external command".into(), result: None, }]) @@ -29,7 +29,7 @@ impl PluginCommand for ForEach { fn run( &self, - _plugin: &StreamExample, + _plugin: &Example, engine: &EngineInterface, call: &EvaluatedCall, input: PipelineData, diff --git a/crates/nu_plugin_stream_example/src/commands/generate.rs b/crates/nu_plugin_example/src/commands/generate.rs similarity index 86% rename from crates/nu_plugin_stream_example/src/commands/generate.rs rename to crates/nu_plugin_example/src/commands/generate.rs index 1bedbe85a5..f40fb7ae9e 100644 --- a/crates/nu_plugin_stream_example/src/commands/generate.rs +++ b/crates/nu_plugin_example/src/commands/generate.rs @@ -4,16 +4,16 @@ use nu_protocol::{ SyntaxShape, Type, Value, }; -use crate::StreamExample; +use crate::Example; -/// `stream_example generate { |previous| {out: ..., next: ...} }` +/// `example generate { |previous| {out: ..., next: ...} }` pub struct Generate; impl PluginCommand for Generate { - type Plugin = StreamExample; + type Plugin = Example; fn signature(&self) -> PluginSignature { - PluginSignature::build("stream_example generate") + PluginSignature::build("example generate") .usage("Example execution of a closure to produce a stream") .extra_usage("See the builtin `generate` command") .input_output_type(Type::Nothing, Type::ListStream) @@ -28,9 +28,8 @@ impl PluginCommand for Generate { "The closure to run to generate values", ) .plugin_examples(vec![PluginExample { - example: - "stream_example generate 0 { |i| if $i <= 10 { {out: $i, next: ($i + 2)} } }" - .into(), + example: "example generate 0 { |i| if $i <= 10 { {out: $i, next: ($i + 2)} } }" + .into(), description: "Generate a sequence of numbers".into(), result: Some(Value::test_list( [0, 2, 4, 6, 8, 10] @@ -44,7 +43,7 @@ impl PluginCommand for Generate { fn run( &self, - _plugin: &StreamExample, + _plugin: &Example, engine: &EngineInterface, call: &EvaluatedCall, _input: PipelineData, diff --git a/crates/nu_plugin_example/src/commands/main.rs b/crates/nu_plugin_example/src/commands/main.rs new file mode 100644 index 0000000000..dcce189891 --- /dev/null +++ b/crates/nu_plugin_example/src/commands/main.rs @@ -0,0 +1,41 @@ +use nu_plugin::{EngineInterface, EvaluatedCall, LabeledError, SimplePluginCommand}; +use nu_protocol::{Category, PluginSignature, Value}; + +use crate::Example; + +pub struct Main; + +impl SimplePluginCommand for Main { + type Plugin = Example; + + fn signature(&self) -> PluginSignature { + PluginSignature::build("example") + .usage("Example commands for Nushell plugins") + .extra_usage( + r#" +The `example` plugin demonstrates usage of the Nushell plugin API. + +Several commands provided to test and demonstrate different capabilities of +plugins exposed through the API. None of these commands are intended to be +particularly useful. +"# + .trim(), + ) + .search_terms(vec!["example".into()]) + .category(Category::Experimental) + } + + fn run( + &self, + _plugin: &Self::Plugin, + _engine: &EngineInterface, + call: &EvaluatedCall, + _input: &Value, + ) -> Result { + Err(LabeledError { + label: "No subcommand provided".into(), + msg: "add --help to see a list of subcommands".into(), + span: Some(call.head), + }) + } +} diff --git a/crates/nu_plugin_example/src/commands/mod.rs b/crates/nu_plugin_example/src/commands/mod.rs index 38371a335b..0acffeb566 100644 --- a/crates/nu_plugin_example/src/commands/mod.rs +++ b/crates/nu_plugin_example/src/commands/mod.rs @@ -1,13 +1,35 @@ -mod nu_example_1; -mod nu_example_2; -mod nu_example_3; -mod nu_example_config; -mod nu_example_disable_gc; -mod nu_example_env; +// `example` command - just suggests to call --help +mod main; -pub use nu_example_1::NuExample1; -pub use nu_example_2::NuExample2; -pub use nu_example_3::NuExample3; -pub use nu_example_config::NuExampleConfig; -pub use nu_example_disable_gc::NuExampleDisableGc; -pub use nu_example_env::NuExampleEnv; +pub use main::Main; + +// Basic demos +mod one; +mod three; +mod two; + +pub use one::One; +pub use three::Three; +pub use two::Two; + +// Engine interface demos +mod config; +mod disable_gc; +mod env; + +pub use config::Config; +pub use disable_gc::DisableGc; +pub use env::Env; + +// Stream demos +mod collect_external; +mod for_each; +mod generate; +mod seq; +mod sum; + +pub use collect_external::CollectExternal; +pub use for_each::ForEach; +pub use generate::Generate; +pub use seq::Seq; +pub use sum::Sum; diff --git a/crates/nu_plugin_example/src/commands/nu_example_1.rs b/crates/nu_plugin_example/src/commands/one.rs similarity index 86% rename from crates/nu_plugin_example/src/commands/nu_example_1.rs rename to crates/nu_plugin_example/src/commands/one.rs index 6741358379..bb6900c65a 100644 --- a/crates/nu_plugin_example/src/commands/nu_example_1.rs +++ b/crates/nu_plugin_example/src/commands/one.rs @@ -3,17 +3,17 @@ use nu_protocol::{Category, PluginExample, PluginSignature, SyntaxShape, Value}; use crate::Example; -pub struct NuExample1; +pub struct One; -impl SimplePluginCommand for NuExample1 { +impl SimplePluginCommand for One { type Plugin = Example; fn signature(&self) -> PluginSignature { // The signature defines the usage of the command inside Nu, and also automatically // generates its help page. - PluginSignature::build("nu-example-1") + PluginSignature::build("example one") .usage("PluginSignature test 1 for plugin. Returns Value::Nothing") - .extra_usage("Extra usage for nu-example-1") + .extra_usage("Extra usage for example one") .search_terms(vec!["example".into()]) .required("a", SyntaxShape::Int, "required integer value") .required("b", SyntaxShape::String, "required string value") @@ -22,7 +22,7 @@ impl SimplePluginCommand for NuExample1 { .named("named", SyntaxShape::String, "named string", Some('n')) .rest("rest", SyntaxShape::String, "rest value string") .plugin_examples(vec![PluginExample { - example: "nu-example-1 3 bb".into(), + example: "example one 3 bb".into(), description: "running example with an int value and string value".into(), result: None, }]) diff --git a/crates/nu_plugin_stream_example/src/commands/seq.rs b/crates/nu_plugin_example/src/commands/seq.rs similarity index 86% rename from crates/nu_plugin_stream_example/src/commands/seq.rs rename to crates/nu_plugin_example/src/commands/seq.rs index 2190ffb122..f7dc7160f2 100644 --- a/crates/nu_plugin_stream_example/src/commands/seq.rs +++ b/crates/nu_plugin_example/src/commands/seq.rs @@ -3,23 +3,23 @@ use nu_protocol::{ Category, ListStream, PipelineData, PluginExample, PluginSignature, SyntaxShape, Type, Value, }; -use crate::StreamExample; +use crate::Example; -/// `stream_example seq ` +/// `example seq ` pub struct Seq; impl PluginCommand for Seq { - type Plugin = StreamExample; + type Plugin = Example; fn signature(&self) -> PluginSignature { - PluginSignature::build("stream_example seq") + PluginSignature::build("example seq") .usage("Example stream generator for a list of values") .search_terms(vec!["example".into()]) .required("first", SyntaxShape::Int, "first number to generate") .required("last", SyntaxShape::Int, "last number to generate") .input_output_type(Type::Nothing, Type::List(Type::Int.into())) .plugin_examples(vec![PluginExample { - example: "stream_example seq 1 3".into(), + example: "example seq 1 3".into(), description: "generate a sequence from 1 to 3".into(), result: Some(Value::test_list(vec![ Value::test_int(1), @@ -32,7 +32,7 @@ impl PluginCommand for Seq { fn run( &self, - _plugin: &StreamExample, + _plugin: &Example, _engine: &EngineInterface, call: &EvaluatedCall, _input: PipelineData, diff --git a/crates/nu_plugin_stream_example/src/commands/sum.rs b/crates/nu_plugin_example/src/commands/sum.rs similarity index 91% rename from crates/nu_plugin_stream_example/src/commands/sum.rs rename to crates/nu_plugin_example/src/commands/sum.rs index 68add17a05..4652c76ed7 100644 --- a/crates/nu_plugin_stream_example/src/commands/sum.rs +++ b/crates/nu_plugin_example/src/commands/sum.rs @@ -1,16 +1,16 @@ use nu_plugin::{EngineInterface, EvaluatedCall, LabeledError, PluginCommand}; use nu_protocol::{Category, PipelineData, PluginExample, PluginSignature, Span, Type, Value}; -use crate::StreamExample; +use crate::Example; -/// ` | stream_example sum` +/// ` | example sum` pub struct Sum; impl PluginCommand for Sum { - type Plugin = StreamExample; + type Plugin = Example; fn signature(&self) -> PluginSignature { - PluginSignature::build("stream_example sum") + PluginSignature::build("example sum") .usage("Example stream consumer for a list of values") .search_terms(vec!["example".into()]) .input_output_types(vec![ @@ -18,7 +18,7 @@ impl PluginCommand for Sum { (Type::List(Type::Float.into()), Type::Float), ]) .plugin_examples(vec![PluginExample { - example: "seq 1 5 | stream_example sum".into(), + example: "seq 1 5 | example sum".into(), description: "sum values from 1 to 5".into(), result: Some(Value::test_int(15)), }]) @@ -27,7 +27,7 @@ impl PluginCommand for Sum { fn run( &self, - _plugin: &StreamExample, + _plugin: &Example, _engine: &EngineInterface, call: &EvaluatedCall, input: PipelineData, diff --git a/crates/nu_plugin_example/src/commands/nu_example_3.rs b/crates/nu_plugin_example/src/commands/three.rs similarity index 92% rename from crates/nu_plugin_example/src/commands/nu_example_3.rs rename to crates/nu_plugin_example/src/commands/three.rs index da4f2c7352..69e702f7f1 100644 --- a/crates/nu_plugin_example/src/commands/nu_example_3.rs +++ b/crates/nu_plugin_example/src/commands/three.rs @@ -3,15 +3,15 @@ use nu_protocol::{Category, PluginSignature, SyntaxShape, Value}; use crate::Example; -pub struct NuExample3; +pub struct Three; -impl SimplePluginCommand for NuExample3 { +impl SimplePluginCommand for Three { type Plugin = Example; fn signature(&self) -> PluginSignature { // The signature defines the usage of the command inside Nu, and also automatically // generates its help page. - PluginSignature::build("nu-example-3") + PluginSignature::build("example three") .usage("PluginSignature test 3 for plugin. Returns labeled error") .required("a", SyntaxShape::Int, "required integer value") .required("b", SyntaxShape::String, "required string value") diff --git a/crates/nu_plugin_example/src/commands/nu_example_2.rs b/crates/nu_plugin_example/src/commands/two.rs similarity index 93% rename from crates/nu_plugin_example/src/commands/nu_example_2.rs rename to crates/nu_plugin_example/src/commands/two.rs index b209375e4d..6c315338ad 100644 --- a/crates/nu_plugin_example/src/commands/nu_example_2.rs +++ b/crates/nu_plugin_example/src/commands/two.rs @@ -3,15 +3,15 @@ use nu_protocol::{record, Category, PluginSignature, SyntaxShape, Value}; use crate::Example; -pub struct NuExample2; +pub struct Two; -impl SimplePluginCommand for NuExample2 { +impl SimplePluginCommand for Two { type Plugin = Example; fn signature(&self) -> PluginSignature { // The signature defines the usage of the command inside Nu, and also automatically // generates its help page. - PluginSignature::build("nu-example-2") + PluginSignature::build("example two") .usage("PluginSignature test 2 for plugin. Returns list of records") .required("a", SyntaxShape::Int, "required integer value") .required("b", SyntaxShape::String, "required string value") diff --git a/crates/nu_plugin_example/src/lib.rs b/crates/nu_plugin_example/src/lib.rs index 11c901b13a..f2af897286 100644 --- a/crates/nu_plugin_example/src/lib.rs +++ b/crates/nu_plugin_example/src/lib.rs @@ -13,12 +13,21 @@ impl Plugin for Example { // // If it doesn't appear on this list, it won't be added. vec![ - Box::new(NuExample1), - Box::new(NuExample2), - Box::new(NuExample3), - Box::new(NuExampleConfig), - Box::new(NuExampleEnv), - Box::new(NuExampleDisableGc), + Box::new(Main), + // Basic demos + Box::new(One), + Box::new(Two), + Box::new(Three), + // Engine interface demos + Box::new(Config), + Box::new(Env), + Box::new(DisableGc), + // Stream demos + Box::new(CollectExternal), + Box::new(ForEach), + Box::new(Generate), + Box::new(Seq), + Box::new(Sum), ] } } diff --git a/crates/nu_plugin_stream_example/Cargo.toml b/crates/nu_plugin_stream_example/Cargo.toml deleted file mode 100644 index 6bced74784..0000000000 --- a/crates/nu_plugin_stream_example/Cargo.toml +++ /dev/null @@ -1,19 +0,0 @@ -[package] -authors = ["The Nushell Project Developers"] -description = "An example of stream handling in nushell plugins" -repository = "https://github.com/nushell/nushell/tree/main/crates/nu_plugin_stream_example" -edition = "2021" -license = "MIT" -name = "nu_plugin_stream_example" -version = "0.91.1" - -[[bin]] -name = "nu_plugin_stream_example" -bench = false - -[lib] -bench = false - -[dependencies] -nu-plugin = { path = "../nu-plugin", version = "0.91.1" } -nu-protocol = { path = "../nu-protocol", version = "0.91.1", features = ["plugin"] } diff --git a/crates/nu_plugin_stream_example/README.md b/crates/nu_plugin_stream_example/README.md deleted file mode 100644 index f155d6a969..0000000000 --- a/crates/nu_plugin_stream_example/README.md +++ /dev/null @@ -1,62 +0,0 @@ -# Streaming Plugin Example - -Crate with a simple example of a plugin with commands that produce streams - -## `stream_example seq` - -This command demonstrates generating list streams. It generates numbers from the first argument -to the second argument just like the builtin `seq` command does. - -Examples: - -> ```nushell -> stream_example seq 1 10 -> ``` - - [1 2 3 4 5 6 7 8 9 10] - -> ```nushell -> stream_example seq 1 10 | describe -> ``` - - list (stream) - -## `stream_example sum` - -This command demonstrates consuming list streams. It consumes a stream of numbers and calculates the -sum just like the builtin `math sum` command does. - -Examples: - -> ```nushell -> seq 1 5 | stream_example sum -> ``` - - 15 - -## `stream_example collect-external` - -This command demonstrates transforming streams into external streams. The list (or stream) of -strings on input will be concatenated into an external stream (raw input) on stdout. - -> ```nushell -> [Hello "\n" world how are you] | stream_example collect-external -> ```` - - Hello - worldhowareyou - -## `stream_example for-each` - -This command demonstrates executing closures on values in streams. Each value received on the input -will be printed to the plugin's stderr. This works even with external commands. - -> ```nushell -> ls | get name | stream_example for-each { |f| ^file $f } -> ``` - - CODE_OF_CONDUCT.md: ASCII text - - CONTRIBUTING.md: ASCII text, with very long lines (303) - - ... diff --git a/crates/nu_plugin_stream_example/src/commands/mod.rs b/crates/nu_plugin_stream_example/src/commands/mod.rs deleted file mode 100644 index 7fbd507183..0000000000 --- a/crates/nu_plugin_stream_example/src/commands/mod.rs +++ /dev/null @@ -1,11 +0,0 @@ -mod collect_external; -mod for_each; -mod generate; -mod seq; -mod sum; - -pub use collect_external::CollectExternal; -pub use for_each::ForEach; -pub use generate::Generate; -pub use seq::Seq; -pub use sum::Sum; diff --git a/crates/nu_plugin_stream_example/src/lib.rs b/crates/nu_plugin_stream_example/src/lib.rs deleted file mode 100644 index abec82c1ad..0000000000 --- a/crates/nu_plugin_stream_example/src/lib.rs +++ /dev/null @@ -1,50 +0,0 @@ -use nu_plugin::{ - EngineInterface, EvaluatedCall, LabeledError, Plugin, PluginCommand, SimplePluginCommand, -}; -use nu_protocol::{Category, PluginSignature, Value}; - -mod commands; -pub use commands::*; - -pub struct StreamExample; - -impl Plugin for StreamExample { - fn commands(&self) -> Vec>> { - vec![ - Box::new(Main), - Box::new(Seq), - Box::new(Sum), - Box::new(CollectExternal), - Box::new(ForEach), - Box::new(Generate), - ] - } -} - -/// `stream_example` -pub struct Main; - -impl SimplePluginCommand for Main { - type Plugin = StreamExample; - - fn signature(&self) -> PluginSignature { - PluginSignature::build("stream_example") - .usage("Examples for streaming plugins") - .search_terms(vec!["example".into()]) - .category(Category::Experimental) - } - - fn run( - &self, - _plugin: &StreamExample, - _engine: &EngineInterface, - call: &EvaluatedCall, - _input: &Value, - ) -> Result { - Err(LabeledError { - label: "No subcommand provided".into(), - msg: "add --help here to see usage".into(), - span: Some(call.head.past()), - }) - } -} diff --git a/crates/nu_plugin_stream_example/src/main.rs b/crates/nu_plugin_stream_example/src/main.rs deleted file mode 100644 index 726219de74..0000000000 --- a/crates/nu_plugin_stream_example/src/main.rs +++ /dev/null @@ -1,30 +0,0 @@ -use nu_plugin::{serve_plugin, MsgPackSerializer}; -use nu_plugin_stream_example::StreamExample; - -fn main() { - // When defining your plugin, you can select the Serializer that could be - // used to encode and decode the messages. The available options are - // MsgPackSerializer and JsonSerializer. Both are defined in the serializer - // folder in nu-plugin. - serve_plugin(&StreamExample {}, MsgPackSerializer {}) - - // Note - // When creating plugins in other languages one needs to consider how a plugin - // is added and used in nushell. - // The steps are: - // - The plugin is register. In this stage nushell calls the binary file of - // the plugin sending information using the encoded PluginCall::PluginSignature object. - // Use this encoded data in your plugin to design the logic that will return - // the encoded signatures. - // Nushell is expecting and encoded PluginResponse::PluginSignature with all the - // plugin signatures - // - When calling the plugin, nushell sends to the binary file the encoded - // PluginCall::CallInfo which has all the call information, such as the - // values of the arguments, the name of the signature called and the input - // from the pipeline. - // Use this data to design your plugin login and to create the value that - // will be sent to nushell - // Nushell expects an encoded PluginResponse::Value from the plugin - // - If an error needs to be sent back to nushell, one can encode PluginResponse::Error. - // This is a labeled error that nushell can format for pretty printing -} diff --git a/tests/plugin_persistence/mod.rs b/tests/plugin_persistence/mod.rs index efbf7b6b8a..41fb917de1 100644 --- a/tests/plugin_persistence/mod.rs +++ b/tests/plugin_persistence/mod.rs @@ -101,12 +101,12 @@ fn plugin_commands_run_without_error() { cwd: ".", plugins: [ ("nu_plugin_inc"), - ("nu_plugin_stream_example"), + ("nu_plugin_example"), ("nu_plugin_custom_values"), ], r#" "2.0.0" | inc -m | ignore - stream_example seq 1 10 | ignore + example seq 1 10 | ignore custom-value generate | ignore "# ); @@ -120,14 +120,14 @@ fn plugin_commands_run_multiple_times_without_error() { cwd: ".", plugins: [ ("nu_plugin_inc"), - ("nu_plugin_stream_example"), + ("nu_plugin_example"), ("nu_plugin_custom_values"), ], r#" ["2.0.0" "2.1.0" "2.2.0"] | each { inc -m } | print - stream_example seq 1 10 | ignore + example seq 1 10 | ignore custom-value generate | ignore - stream_example seq 1 20 | ignore + example seq 1 20 | ignore custom-value generate2 | ignore "# ); @@ -340,9 +340,9 @@ fn plugin_gc_can_be_disabled_by_plugin() { cwd: ".", plugin: ("nu_plugin_example"), r#" - nu-example-disable-gc + example disable-gc $env.config.plugin_gc = { default: { stop_after: 0sec } } - nu-example-1 1 foo | ignore # ensure we've run the plugin with the new config + example one 1 foo | ignore # ensure we've run the plugin with the new config sleep 100ms (plugin list | where name == example).0.is_running "# @@ -355,11 +355,11 @@ fn plugin_gc_can_be_disabled_by_plugin() { fn plugin_gc_does_not_stop_plugin_while_stream_output_is_active() { let out = nu_with_plugins!( cwd: ".", - plugin: ("nu_plugin_stream_example"), + plugin: ("nu_plugin_example"), r#" $env.config.plugin_gc = { default: { stop_after: 10ms } } # This would exceed the configured time - stream_example seq 1 500 | each { |n| sleep 1ms; $n } | length | print + example seq 1 500 | each { |n| sleep 1ms; $n } | length | print "# ); assert!(out.status.success()); diff --git a/tests/plugins/config.rs b/tests/plugins/config.rs index 656d092c5a..44f2797ceb 100644 --- a/tests/plugins/config.rs +++ b/tests/plugins/config.rs @@ -15,7 +15,7 @@ fn closure() { } } } - nu-example-config + example config "# ); @@ -27,7 +27,7 @@ fn none() { let actual = nu_with_plugins!( cwd: "tests", plugin: ("nu_plugin_example"), - "nu-example-config" + "example config" ); assert!(actual.err.contains("No config sent")); @@ -47,7 +47,7 @@ fn record() { } } } - nu-example-config + example config "# ); diff --git a/tests/plugins/env.rs b/tests/plugins/env.rs index 83774f15e7..c5a2ccc7af 100644 --- a/tests/plugins/env.rs +++ b/tests/plugins/env.rs @@ -7,9 +7,9 @@ fn get_env_by_name() { plugin: ("nu_plugin_example"), r#" $env.FOO = bar - nu-example-env FOO | print + example env FOO | print $env.FOO = baz - nu-example-env FOO | print + example env FOO | print "# ); assert!(result.status.success()); @@ -21,7 +21,7 @@ fn get_envs() { let result = nu_with_plugins!( cwd: ".", plugin: ("nu_plugin_example"), - "$env.BAZ = foo; nu-example-env | get BAZ" + "$env.BAZ = foo; example env | get BAZ" ); assert!(result.status.success()); assert_eq!("foo", result.out); @@ -37,7 +37,7 @@ fn get_current_dir() { let result = nu_with_plugins!( cwd: ".", plugin: ("nu_plugin_example"), - "cd tests; nu-example-env --cwd" + "cd tests; example env --cwd" ); assert!(result.status.success()); assert_eq!(cwd, result.out); @@ -48,7 +48,7 @@ fn set_env() { let result = nu_with_plugins!( cwd: ".", plugin: ("nu_plugin_example"), - "nu-example-env NUSHELL_OPINION --set=rocks; $env.NUSHELL_OPINION" + "example env NUSHELL_OPINION --set=rocks; $env.NUSHELL_OPINION" ); assert!(result.status.success()); assert_eq!("rocks", result.out); diff --git a/tests/plugins/register.rs b/tests/plugins/register.rs index 4e95c8eb85..7646ae6f23 100644 --- a/tests/plugins/register.rs +++ b/tests/plugins/register.rs @@ -7,11 +7,11 @@ fn help() { let actual = nu_with_plugins!( cwd: dirs.test(), plugin: ("nu_plugin_example"), - "nu-example-1 --help" + "example one --help" ); assert!(actual.out.contains("PluginSignature test 1")); - assert!(actual.out.contains("Extra usage for nu-example-1")); + assert!(actual.out.contains("Extra usage for example one")); }) } @@ -21,7 +21,7 @@ fn search_terms() { let actual = nu_with_plugins!( cwd: dirs.test(), plugin: ("nu_plugin_example"), - r#"help commands | where name == "nu-example-1" | echo $"search terms: ($in.search_terms)""# + r#"help commands | where name == "example one" | echo $"search terms: ($in.search_terms)""# ); assert!(actual.out.contains("search terms: [example]")); diff --git a/tests/plugins/stream.rs b/tests/plugins/stream.rs index 38aaa73c38..ee62703017 100644 --- a/tests/plugins/stream.rs +++ b/tests/plugins/stream.rs @@ -5,8 +5,8 @@ use pretty_assertions::assert_eq; fn seq_produces_stream() { let actual = nu_with_plugins!( cwd: "tests/fixtures/formats", - plugin: ("nu_plugin_stream_example"), - "stream_example seq 1 5 | describe" + plugin: ("nu_plugin_example"), + "example seq 1 5 | describe" ); assert_eq!(actual.out, "list (stream)"); @@ -20,8 +20,8 @@ fn seq_describe_no_collect_succeeds_without_error() { for _ in 0..10 { let actual = nu_with_plugins!( cwd: "tests/fixtures/formats", - plugin: ("nu_plugin_stream_example"), - "stream_example seq 1 5 | describe --no-collect" + plugin: ("nu_plugin_example"), + "example seq 1 5 | describe --no-collect" ); assert_eq!(actual.out, "stream"); @@ -33,16 +33,16 @@ fn seq_describe_no_collect_succeeds_without_error() { fn seq_stream_collects_to_correct_list() { let actual = nu_with_plugins!( cwd: "tests/fixtures/formats", - plugin: ("nu_plugin_stream_example"), - "stream_example seq 1 5 | to json --raw" + plugin: ("nu_plugin_example"), + "example seq 1 5 | to json --raw" ); assert_eq!(actual.out, "[1,2,3,4,5]"); let actual = nu_with_plugins!( cwd: "tests/fixtures/formats", - plugin: ("nu_plugin_stream_example"), - "stream_example seq 1 0 | to json --raw" + plugin: ("nu_plugin_example"), + "example seq 1 0 | to json --raw" ); assert_eq!(actual.out, "[]"); @@ -53,8 +53,8 @@ fn seq_big_stream() { // Testing big streams helps to ensure there are no deadlocking bugs let actual = nu_with_plugins!( cwd: "tests/fixtures/formats", - plugin: ("nu_plugin_stream_example"), - "stream_example seq 1 100000 | length" + plugin: ("nu_plugin_example"), + "example seq 1 100000 | length" ); assert_eq!(actual.out, "100000"); @@ -64,8 +64,8 @@ fn seq_big_stream() { fn sum_accepts_list_of_int() { let actual = nu_with_plugins!( cwd: "tests/fixtures/formats", - plugin: ("nu_plugin_stream_example"), - "[1 2 3] | stream_example sum" + plugin: ("nu_plugin_example"), + "[1 2 3] | example sum" ); assert_eq!(actual.out, "6"); @@ -75,8 +75,8 @@ fn sum_accepts_list_of_int() { fn sum_accepts_list_of_float() { let actual = nu_with_plugins!( cwd: "tests/fixtures/formats", - plugin: ("nu_plugin_stream_example"), - "[1.0 2.0 3.5] | stream_example sum" + plugin: ("nu_plugin_example"), + "[1.0 2.0 3.5] | example sum" ); assert_eq!(actual.out, "6.5"); @@ -86,8 +86,8 @@ fn sum_accepts_list_of_float() { fn sum_accepts_stream_of_int() { let actual = nu_with_plugins!( cwd: "tests/fixtures/formats", - plugin: ("nu_plugin_stream_example"), - "seq 1 5 | stream_example sum" + plugin: ("nu_plugin_example"), + "seq 1 5 | example sum" ); assert_eq!(actual.out, "15"); @@ -97,8 +97,8 @@ fn sum_accepts_stream_of_int() { fn sum_accepts_stream_of_float() { let actual = nu_with_plugins!( cwd: "tests/fixtures/formats", - plugin: ("nu_plugin_stream_example"), - "seq 1 5 | into float | stream_example sum" + plugin: ("nu_plugin_example"), + "seq 1 5 | into float | example sum" ); assert_eq!(actual.out, "15"); @@ -109,8 +109,8 @@ fn sum_big_stream() { // Testing big streams helps to ensure there are no deadlocking bugs let actual = nu_with_plugins!( cwd: "tests/fixtures/formats", - plugin: ("nu_plugin_stream_example"), - "seq 1 100000 | stream_example sum" + plugin: ("nu_plugin_example"), + "seq 1 100000 | example sum" ); assert_eq!(actual.out, "5000050000"); @@ -120,8 +120,8 @@ fn sum_big_stream() { fn collect_external_accepts_list_of_string() { let actual = nu_with_plugins!( cwd: "tests/fixtures/formats", - plugin: ("nu_plugin_stream_example"), - "[a b] | stream_example collect-external" + plugin: ("nu_plugin_example"), + "[a b] | example collect-external" ); assert_eq!(actual.out, "ab"); @@ -131,8 +131,8 @@ fn collect_external_accepts_list_of_string() { fn collect_external_accepts_list_of_binary() { let actual = nu_with_plugins!( cwd: "tests/fixtures/formats", - plugin: ("nu_plugin_stream_example"), - "[0x[41] 0x[42]] | stream_example collect-external" + plugin: ("nu_plugin_example"), + "[0x[41] 0x[42]] | example collect-external" ); assert_eq!(actual.out, "AB"); @@ -142,8 +142,8 @@ fn collect_external_accepts_list_of_binary() { fn collect_external_produces_raw_input() { let actual = nu_with_plugins!( cwd: "tests/fixtures/formats", - plugin: ("nu_plugin_stream_example"), - "[a b c] | stream_example collect-external | describe" + plugin: ("nu_plugin_example"), + "[a b c] | example collect-external | describe" ); assert_eq!(actual.out, "raw input"); @@ -155,12 +155,12 @@ fn collect_external_big_stream() { // time without deadlocking let actual = nu_with_plugins!( cwd: "tests/fixtures/formats", - plugin: ("nu_plugin_stream_example"), + plugin: ("nu_plugin_example"), r#"( seq 1 10000 | to text | each { into string } | - stream_example collect-external | + example collect-external | lines | length )"# @@ -173,8 +173,8 @@ fn collect_external_big_stream() { fn for_each_prints_on_stderr() { let actual = nu_with_plugins!( cwd: "tests/fixtures/formats", - plugin: ("nu_plugin_stream_example"), - "[a b c] | stream_example for-each { $in }" + plugin: ("nu_plugin_example"), + "[a b c] | example for-each { $in }" ); assert_eq!(actual.err, "a\nb\nc\n"); @@ -184,8 +184,8 @@ fn for_each_prints_on_stderr() { fn generate_sequence() { let actual = nu_with_plugins!( cwd: "tests/fixtures/formats", - plugin: ("nu_plugin_stream_example"), - "stream_example generate 0 { |i| if $i <= 10 { {out: $i, next: ($i + 2)} } } | to json --raw" + plugin: ("nu_plugin_example"), + "example generate 0 { |i| if $i <= 10 { {out: $i, next: ($i + 2)} } } | to json --raw" ); assert_eq!(actual.out, "[0,2,4,6,8,10]"); diff --git a/wix/main.wxs b/wix/main.wxs index a28a388e4b..d516aa6952 100644 --- a/wix/main.wxs +++ b/wix/main.wxs @@ -281,6 +281,7 @@ Source='target\$(var.Profile)\nu_plugin_gstat.exe' KeyPath='yes'/> + @@ -353,7 +355,7 @@ --> - +