https://github.com/myty/nu-plugin-lib
A .Net Standard class library for the sole purpose of building plugins for Nu Shell, https://github.com/nushell/nushell
https://github.com/myty/nu-plugin-lib
csharp nushell
Last synced: 3 months ago
JSON representation
A .Net Standard class library for the sole purpose of building plugins for Nu Shell, https://github.com/nushell/nushell
- Host: GitHub
- URL: https://github.com/myty/nu-plugin-lib
- Owner: myty
- License: mit
- Created: 2020-02-21T04:03:12.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2024-07-09T21:18:32.000Z (over 1 year ago)
- Last Synced: 2025-09-26T11:02:38.546Z (5 months ago)
- Topics: csharp, nushell
- Language: C#
- Homepage:
- Size: 104 KB
- Stars: 16
- Watchers: 3
- Forks: 2
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Nu.Plugin (.Net Standard 2.0)
A .Net Standard class library for the sole purpose of building plugins for Nu Shell, https://github.com/nushell/nushell
A Nu Shell plugin is any executable that macthes the pattern of `nu_plugin_*` so in essence, a .Net plugin for Nu Shell is a console application. At it's simplest form, a nu plugin is an application that reads the standard input stream and writes to the standard output stream. The communication protocol is done via [JSON-RPC](https://www.jsonrpc.org/). For more information: [Nu Plugins - Discovery](https://github.com/nushell/contributor-book/blob/master/en/plugins.md#discovery)
## Motivations
To see how feasible it would be to create a plugin for Nu Shell in a language I am familiar with as well enjoy working in, C#. The biggest hurdle was figuring out how to communicate with the correct Json structure protocol. I personally like the vision for Nu Shell and am looking for ways to explore various avenues of getting others involved in the ecosystem.
I used the [sample Python plugin](https://github.com/nushell/contributor-book/blob/master/en/plugins.md#creating-a-plugin-in-python) as the starting point and went from there.
Eventually I abstracted the plugin bits into their own class library, which should make it much easier for others to consume and begin making .Net Core Nu Shell plugins.
## A Walk Through Creating Your First Plugin
This will go through the steps that would have gone into creating this "len" plugin
From the command line:
```cmd
dotnet new console -o Nu.Plugin.Len
cd Nu.Plugin.Len
dotnet add package Nu.Plugin
```
Open `Program.cs` and update to the following:
```csharp
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Nu.Plugin.Interfaces;
namespace Nu.Plugin.Len
{
class Program : INuPluginFilter
{
static async Task Main(string[] args) => await NuPlugin.Create()
.Name("len")
.Usage("Return the length of a string")
.IsFilter()
.RunAsync();
public object BeginFilter() => Array.Empty();
public JsonRpcParams Filter(JsonRpcParams requestParams)
{
var stringLength = requestParams.Value.Primitive["String"].ToString().Length;
requestParams.Value.Primitive = new Dictionary{
{"Int", stringLength}
};
return requestParams;
}
public object EndFilter() => Array.Empty();
}
}
```
Open `Nu.Plugin.Len.csproj` and add the `AssemblyName` element to the `PropertyGroup` like so:
```xml
nu_plugin_len
Exe
...
```
The key takeaway is that Nu searches for any executable file within the path named `nu_plugin_*` to be included as plugins. By updating the assembly name to this pattern, it will create an executable with the name, `nu_plugin_len`, For more information: [Nu Plugins - Discovery](https://github.com/nushell/contributor-book/blob/master/en/plugins.md#discovery)
Build the project:
```cmd
dotnet build
```
** Note: Update your OS settings to include the PATH to the debug directory where the `nu_plugin_len` executable was created. Next time you start Nu Shell, it will now discover your newly created .Net Core executable as Nu plugin.
Run from within Nu shell:
```shell
echo "Hello, world" | len
```
