Skip to content

Commit 996a745

Browse files
authored
feat: add pwsh support (#130)
Thanks to @StSchnell for writing the docs and examples
1 parent f3102a1 commit 996a745

File tree

2 files changed

+51
-3
lines changed

2 files changed

+51
-3
lines changed

README.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,17 @@ Then, add the [Extism.Sdk NuGet package](https://www.nuget.org/packages/Extism.S
1616
dotnet add package Extism.Sdk
1717
```
1818

19+
### PowerShell
20+
21+
Open a PowerShell console and detect the Common Language Runtime (CLR) major version with the command
22+
```
23+
[System.Environment]::Version
24+
```
25+
26+
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.
27+
28+
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.
29+
1930
## Getting Started
2031

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

50+
### PowerShell
51+
```powershell
52+
[System.String]$LibDir = $($PSScriptRoot)
53+
[System.String]$Extism = $($LibDir) + "/Extism.Sdk.dll"
54+
Add-Type -Path $Extism
55+
```
56+
3957
## Creating A Plug-in
4058

4159
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.
@@ -57,6 +75,22 @@ let manifest = Manifest(new UrlWasmSource(uri))
5775
let plugin = new Plugin(manifest, Array.Empty<HostFunction>(), withWasi = true)
5876
```
5977

78+
PowerShell:
79+
```powershell
80+
$Manifest = [Extism.Sdk.Manifest]::new(
81+
[Extism.Sdk.UrlWasmSource]::new(
82+
"https://github.com/extism/plugins/releases/latest/download/count_vowels.wasm"
83+
)
84+
)
85+
86+
$HostFunctionArray = [Extism.Sdk.HostFunction[]]::new(0)
87+
88+
$Options = [Extism.Sdk.PluginIntializationOptions]::new()
89+
$Options.WithWasi = $True
90+
91+
$Plugin = [Extism.Sdk.Plugin]::new($Manifest, $HostFunctionArray, $Options)
92+
```
93+
6094
> **Note**: The schema for this manifest can be found here: https://extism.org/docs/concepts/manifest/
6195
6296
### Calling A Plug-in's Exports
@@ -77,6 +111,13 @@ printfn "%s" output
77111
// => {"count": 3, "total": 3, "vowels": "aeiouAEIOU"}
78112
```
79113

114+
PowerShell:
115+
```powershell
116+
$output = $Plugin.Call("count_vowels", "Hello, World!")
117+
Write-Host $output
118+
# => {"count": 3, "total": 3, "vowels": "aeiouAEIOU"}
119+
```
120+
80121
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.
81122

82123
## Precompiling plugins

src/Extism.Sdk/Extism.Sdk.csproj

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFrameworks>netstandard2.1;net7.0;net8.0;net9.0</TargetFrameworks>
4+
<TargetFrameworks>netstandard2.1;net8.0;net9.0</TargetFrameworks>
55
<ImplicitUsings>enable</ImplicitUsings>
66
<Nullable>enable</Nullable>
77
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
@@ -30,12 +30,19 @@
3030
<None Include="README.md" Pack="true" PackagePath="\" />
3131
</ItemGroup>
3232

33+
<ItemGroup>
34+
<!-- For netstandard2.1 target (still needs package references) -->
35+
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="6.1.0" Condition="'$(TargetFramework)' == 'netstandard2.1'" />
36+
<PackageReference Include="System.Text.Json" Version="9.0.0" Condition="'$(TargetFramework)' == 'netstandard2.1'" />
37+
38+
<!-- For net7.0+ targets, these libraries are part of the shared framework -->
39+
<FrameworkReference Include="Microsoft.NETCore.App" Condition="'$(TargetFramework)' != 'netstandard2.1'" />
40+
</ItemGroup>
41+
3342
<ItemGroup>
3443
<PackageReference Include="MinVer" Version="6.0.0">
3544
<PrivateAssets>all</PrivateAssets>
3645
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
3746
</PackageReference>
38-
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="6.1.0" />
39-
<PackageReference Include="System.Text.Json" Version="9.0.2" />
4047
</ItemGroup>
4148
</Project>

0 commit comments

Comments
 (0)