UnifyWeaver provides robust support for integrating C# code into your Prolog-to-Bash/PowerShell pipelines. This guide covers the compilation strategies, architecture, and integration with external libraries like LiteDB.
UnifyWeaver supports two primary strategies for compiling C# code, automatically selected based on your environment and needs.
external_compile)This is the recommended strategy for robust development. It uses the .NET SDK (dotnet build) to compile C# code into DLLs, which are then loaded by PowerShell.
Benefits:
<PackageReference>) and local DLL references.Requirements:
dotnet command available).How it works:
%TEMP%/unifyweaver_dotnet_build/Build_MySource_123456)..csproj file with necessary references.dotnet build to produce a DLL.Add-Type -Path ....pre_compile)This is a fallback strategy that uses PowerShell’s built-in Add-Type cmdlet to compile C# code on the fly.
Benefits:
Limitations:
When to use:
UnifyWeaver’s dotnet_source module automatically selects the best strategy:
dotnet is available in the system PATH.external_compile.pre_compile.You can override the default selection when necessary:
:- source(dotnet, my_predicate, [
external_compile(false), % Force Add-Type even if dotnet is available
pre_compile(true)
]).
:- source(dotnet, other_predicate, [
external_compile(true) % Force dotnet build even if auto-detect fails
]).
UnifyWeaver includes built-in support for LiteDB, a lightweight, serverless NoSQL document store. This allows you to stream data into a structured database and query it efficiently.
Use the included setup scripts to download the LiteDB DLL:
Bash/WSL:
bash scripts/setup/setup_litedb.sh
PowerShell:
.\scripts\setup\setup_litedb.ps1
This places LiteDB.dll in the lib/ directory.
Here is how to define a source that loads data into LiteDB:
:- source(dotnet, load_products, [
arity(0),
target(powershell),
csharp_inline('
using LiteDB;
using System.IO;
namespace UnifyWeaver.Generated {
public class Handler {
public string Process() {
using (var db = new LiteDatabase("data.db")) {
var col = db.GetCollection("products");
col.Insert(new BsonDocument { ["name"] = "Widget", ["price"] = 9.99 });
return "Inserted";
}
}
}
}
'),
references(['lib/LiteDB.dll']) % Reference the DLL
]).
A common issue in PowerShell development is that once a DLL is loaded, the file is locked by the process and cannot be overwritten. This makes iterative development (compile -> run -> fix -> compile) difficult.
UnifyWeaver’s Solution:
When using external_compile, UnifyWeaver generates a unique build directory for every compilation (e.g., using a timestamp).
tmp/Build_Source_A_1001/bin/Debug/net8.0/Source_A.dlltmp/Build_Source_A_1002/bin/Debug/net8.0/Source_A.dllBecause the file path changes, PowerShell treats it as a new assembly, avoiding the lock on the previous file. The template automatically cleans up old build artifacts to prevent disk clutter.