Skip to content

feat: add pwsh support #130

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,17 @@ Then, add the [Extism.Sdk NuGet package](https://www.nuget.org/packages/Extism.S
dotnet add package Extism.Sdk
```

### PowerShell

Open a PowerShell console and detect the Common Language Runtime (CLR) major version with the command
```
[System.Environment]::Version
```

Download the [Extism.Sdk NuGet package](https://www.nuget.org/packages/Extism.Sdk) and change the extension from nupkg to zip. Open the zip file and go into the lib folder. Choose the net folder in dependency of the CLR major version and open it. Copy the file Extism.sdk.dll in your PowerShell script directory.

Download the [Extism native runtime package](https://www.nuget.org/packages/Extism.runtime.all#dependencies-body-tab) in dependency of your operating system and change the extension from nupkg to zip. Open the zip file and go into the runtimes folder. At the end of the path you will find a file with the name libextism.so (shared object) or extism.dll (dynamic link library). Copy this file in your PowerShell script directory.

## Getting Started

This guide should walk you through some of the concepts in Extism and this .NET library.
Expand All @@ -36,6 +47,13 @@ open System
open Extism.Sdk
```

### PowerShell
```powershell
[System.String]$LibDir = $($PSScriptRoot)
[System.String]$Extism = $($LibDir) + "/Extism.Sdk.dll"
Add-Type -Path $Extism
```

## Creating A Plug-in

The primary concept in Extism is the [plug-in](https://extism.org/docs/concepts/plug-in). You can think of a plug-in as a code module stored in a `.wasm` file.
Expand All @@ -57,6 +75,22 @@ let manifest = Manifest(new UrlWasmSource(uri))
let plugin = new Plugin(manifest, Array.Empty<HostFunction>(), withWasi = true)
```

PowerShell:
```powershell
$Manifest = [Extism.Sdk.Manifest]::new(
[Extism.Sdk.UrlWasmSource]::new(
"https://github.com/extism/plugins/releases/latest/download/count_vowels.wasm"
)
)

$HostFunctionArray = [Extism.Sdk.HostFunction[]]::new(0)

$Options = [Extism.Sdk.PluginIntializationOptions]::new()
$Options.WithWasi = $True

$Plugin = [Extism.Sdk.Plugin]::new($Manifest, $HostFunctionArray, $Options)
```

> **Note**: The schema for this manifest can be found here: https://extism.org/docs/concepts/manifest/

### Calling A Plug-in's Exports
Expand All @@ -77,6 +111,13 @@ printfn "%s" output
// => {"count": 3, "total": 3, "vowels": "aeiouAEIOU"}
```

PowerShell:
```powershell
$output = $Plugin.Call("count_vowels", "Hello, World!")
Write-Host $output
# => {"count": 3, "total": 3, "vowels": "aeiouAEIOU"}
```

All exports have a simple interface of optional bytes in, and optional bytes out. This plug-in happens to take a string and return a JSON encoded string with a report of results.

## Precompiling plugins
Expand Down
13 changes: 10 additions & 3 deletions src/Extism.Sdk/Extism.Sdk.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard2.1;net7.0;net8.0;net9.0</TargetFrameworks>
<TargetFrameworks>netstandard2.1;net8.0;net9.0</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
Expand Down Expand Up @@ -30,12 +30,19 @@
<None Include="README.md" Pack="true" PackagePath="\" />
</ItemGroup>

<ItemGroup>
<!-- For netstandard2.1 target (still needs package references) -->
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="6.1.0" Condition="'$(TargetFramework)' == 'netstandard2.1'" />
<PackageReference Include="System.Text.Json" Version="9.0.0" Condition="'$(TargetFramework)' == 'netstandard2.1'" />

<!-- For net7.0+ targets, these libraries are part of the shared framework -->
<FrameworkReference Include="Microsoft.NETCore.App" Condition="'$(TargetFramework)' != 'netstandard2.1'" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="MinVer" Version="6.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="6.1.0" />
<PackageReference Include="System.Text.Json" Version="9.0.2" />
</ItemGroup>
</Project>
Loading