Skip to content

Commit

Permalink
Split the plugin crate (#12563)
Browse files Browse the repository at this point in the history
# 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`
  • Loading branch information
devyn committed Apr 27, 2024
1 parent 884d531 commit 0c4d533
Show file tree
Hide file tree
Showing 74 changed files with 3,403 additions and 2,999 deletions.
65 changes: 52 additions & 13 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 8 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ members = [
"crates/nu-pretty-hex",
"crates/nu-protocol",
"crates/nu-plugin",
"crates/nu-plugin-core",
"crates/nu-plugin-engine",
"crates/nu-plugin-protocol",
"crates/nu-plugin-test-support",
"crates/nu_plugin_inc",
"crates/nu_plugin_gstat",
Expand Down Expand Up @@ -90,6 +93,7 @@ heck = "0.5.0"
human-date-parser = "0.1.1"
indexmap = "2.2"
indicatif = "0.17"
interprocess = "1.2.1"
is_executable = "1.0"
itertools = "0.12"
libc = "0.2"
Expand Down Expand Up @@ -184,7 +188,7 @@ nu-explore = { path = "./crates/nu-explore", version = "0.92.3" }
nu-lsp = { path = "./crates/nu-lsp/", version = "0.92.3" }
nu-parser = { path = "./crates/nu-parser", version = "0.92.3" }
nu-path = { path = "./crates/nu-path", version = "0.92.3" }
nu-plugin = { path = "./crates/nu-plugin", optional = true, version = "0.92.3" }
nu-plugin-engine = { path = "./crates/nu-plugin-engine", optional = true, version = "0.92.3" }
nu-protocol = { path = "./crates/nu-protocol", version = "0.92.3" }
nu-std = { path = "./crates/nu-std", version = "0.92.3" }
nu-system = { path = "./crates/nu-system", version = "0.92.3" }
Expand Down Expand Up @@ -218,6 +222,8 @@ nix = { workspace = true, default-features = false, features = [

[dev-dependencies]
nu-test-support = { path = "./crates/nu-test-support", version = "0.92.3" }
nu-plugin-protocol = { path = "./crates/nu-plugin-protocol", version = "0.92.3" }
nu-plugin-core = { path = "./crates/nu-plugin-core", version = "0.92.3" }
assert_cmd = "2.0"
dirs-next = { workspace = true }
divan = "0.1.14"
Expand All @@ -228,7 +234,7 @@ tempfile = { workspace = true }

[features]
plugin = [
"nu-plugin",
"nu-plugin-engine",
"nu-cmd-plugin",
"nu-cli/plugin",
"nu-parser/plugin",
Expand Down
3 changes: 2 additions & 1 deletion benches/benchmarks.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use nu_cli::{eval_source, evaluate_commands};
use nu_parser::parse;
use nu_plugin::{Encoder, EncodingType, PluginCallResponse, PluginOutput};
use nu_plugin_core::{Encoder, EncodingType};
use nu_plugin_protocol::{PluginCallResponse, PluginOutput};
use nu_protocol::{
engine::{EngineState, Stack},
eval_const::create_nu_constant,
Expand Down
4 changes: 2 additions & 2 deletions crates/nu-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ nu-cmd-base = { path = "../nu-cmd-base", version = "0.92.3" }
nu-engine = { path = "../nu-engine", version = "0.92.3" }
nu-path = { path = "../nu-path", version = "0.92.3" }
nu-parser = { path = "../nu-parser", version = "0.92.3" }
nu-plugin = { path = "../nu-plugin", version = "0.92.3", optional = true }
nu-plugin-engine = { path = "../nu-plugin-engine", version = "0.92.3", optional = true }
nu-protocol = { path = "../nu-protocol", version = "0.92.3" }
nu-utils = { path = "../nu-utils", version = "0.92.3" }
nu-color-config = { path = "../nu-color-config", version = "0.92.3" }
Expand All @@ -45,5 +45,5 @@ uuid = { workspace = true, features = ["v4"] }
which = { workspace = true }

[features]
plugin = ["nu-plugin"]
plugin = ["nu-plugin-engine"]
system-clipboard = ["reedline/system_clipboard"]
2 changes: 1 addition & 1 deletion crates/nu-cli/src/config_files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ pub fn read_plugin_file(

let mut working_set = StateWorkingSet::new(engine_state);

nu_plugin::load_plugin_file(&mut working_set, &contents, span);
nu_plugin_engine::load_plugin_file(&mut working_set, &contents, span);

if let Err(err) = engine_state.merge_delta(working_set.render()) {
report_error_new(engine_state, &err);
Expand Down
2 changes: 1 addition & 1 deletion crates/nu-cmd-plugin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ version = "0.92.3"
nu-engine = { path = "../nu-engine", version = "0.92.3" }
nu-path = { path = "../nu-path", version = "0.92.3" }
nu-protocol = { path = "../nu-protocol", version = "0.92.3", features = ["plugin"] }
nu-plugin = { path = "../nu-plugin", version = "0.92.3" }
nu-plugin-engine = { path = "../nu-plugin-engine", version = "0.92.3" }

itertools = { workspace = true }

Expand Down
2 changes: 1 addition & 1 deletion crates/nu-cmd-plugin/src/commands/plugin/add.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::sync::Arc;

use nu_engine::{command_prelude::*, current_dir};
use nu_plugin::{GetPlugin, PersistentPlugin};
use nu_plugin_engine::{GetPlugin, PersistentPlugin};
use nu_protocol::{PluginGcConfig, PluginIdentity, PluginRegistryItem, RegisteredPlugin};

use crate::util::{get_plugin_dirs, modify_plugin_file};
Expand Down
4 changes: 2 additions & 2 deletions crates/nu-parser/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ bench = false
[dependencies]
nu-engine = { path = "../nu-engine", version = "0.92.3" }
nu-path = { path = "../nu-path", version = "0.92.3" }
nu-plugin = { path = "../nu-plugin", optional = true, version = "0.92.3" }
nu-plugin-engine = { path = "../nu-plugin-engine", optional = true, version = "0.92.3" }
nu-protocol = { path = "../nu-protocol", version = "0.92.3" }

bytesize = { workspace = true }
Expand All @@ -27,4 +27,4 @@ serde_json = { workspace = true }
rstest = { workspace = true, default-features = false }

[features]
plugin = ["nu-plugin"]
plugin = ["nu-plugin-engine"]
26 changes: 15 additions & 11 deletions crates/nu-parser/src/parse_keywords.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3555,7 +3555,7 @@ pub fn parse_where(working_set: &mut StateWorkingSet, lite_command: &LiteCommand
/// `register` is deprecated and will be removed in 0.94. Use `plugin add` and `plugin use` instead.
#[cfg(feature = "plugin")]
pub fn parse_register(working_set: &mut StateWorkingSet, lite_command: &LiteCommand) -> Pipeline {
use nu_plugin::{get_signature, PluginDeclaration};
use nu_plugin_engine::PluginDeclaration;
use nu_protocol::{
engine::Stack, ErrSpan, ParseWarning, PluginIdentity, PluginRegistryItem, PluginSignature,
RegisteredPlugin,
Expand Down Expand Up @@ -3714,7 +3714,7 @@ pub fn parse_register(working_set: &mut StateWorkingSet, lite_command: &LiteComm
// Create the plugin identity. This validates that the plugin name starts with `nu_plugin_`
let identity = PluginIdentity::new(path, shell).err_span(path_span)?;

let plugin = nu_plugin::add_plugin_to_working_set(working_set, &identity)
let plugin = nu_plugin_engine::add_plugin_to_working_set(working_set, &identity)
.map_err(|err| err.wrap(working_set, call.head))?;

let signatures = signature.map_or_else(
Expand All @@ -3731,14 +3731,18 @@ pub fn parse_register(working_set: &mut StateWorkingSet, lite_command: &LiteComm
)
})?;

let signatures = get_signature(plugin.clone(), get_envs).map_err(|err| {
log::warn!("Error getting signatures: {err:?}");
ParseError::LabeledError(
"Error getting signatures".into(),
err.to_string(),
spans[0],
)
});
let signatures = plugin
.clone()
.get(get_envs)
.and_then(|p| p.get_signature())
.map_err(|err| {
log::warn!("Error getting signatures: {err:?}");
ParseError::LabeledError(
"Error getting signatures".into(),
err.to_string(),
spans[0],
)
});

if let Ok(ref signatures) = signatures {
// Add the loaded plugin to the delta
Expand Down Expand Up @@ -3863,7 +3867,7 @@ pub fn parse_plugin_use(working_set: &mut StateWorkingSet, call: Box<Call>) -> P
})?;

// Now add the signatures to the working set
nu_plugin::load_plugin_registry_item(working_set, plugin_item, Some(call.head))
nu_plugin_engine::load_plugin_registry_item(working_set, plugin_item, Some(call.head))
.map_err(|err| err.wrap(working_set, call.head))?;

Ok(())
Expand Down
28 changes: 28 additions & 0 deletions crates/nu-plugin-core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
[package]
authors = ["The Nushell Project Developers"]
description = "Shared internal functionality to support Nushell plugins"
repository = "https://github.com/nushell/nushell/tree/main/crates/nu-plugin-core"
edition = "2021"
license = "MIT"
name = "nu-plugin-core"
version = "0.92.3"

[lib]
bench = false

[dependencies]
nu-protocol = { path = "../nu-protocol", version = "0.92.3" }
nu-plugin-protocol = { path = "../nu-plugin-protocol", version = "0.92.3" }

rmp-serde = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
log = { workspace = true }
interprocess = { workspace = true, optional = true }

[features]
default = ["local-socket"]
local-socket = ["interprocess"]

[target.'cfg(target_os = "windows")'.dependencies]
windows = { workspace = true }
21 changes: 21 additions & 0 deletions crates/nu-plugin-core/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2019 - 2023 The Nushell Project Developers

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
3 changes: 3 additions & 0 deletions crates/nu-plugin-core/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# nu-plugin-core

This crate provides functionality that is shared by the [Nushell](https://nushell.sh/) engine and plugins.
Loading

0 comments on commit 0c4d533

Please sign in to comment.