2025-12-31 18:12:18 +03:00
using System.Reflection ;
using System.Text.Json ;
2025-12-31 18:04:54 +03:00
using s8n_runtime.ViewModels ;
string workflowViewFile = Environment . GetEnvironmentVariable ( "WORKFLOW_FILE" ) ! ;
string projectDirectory = Environment . GetEnvironmentVariable ( "PROJECT_DIR" ) ? ? $"/sources" ;
2025-12-31 18:12:18 +03:00
if ( string . IsNullOrEmpty ( workflowViewFile ) | | ! Directory . Exists ( projectDirectory ) )
{
AssemblyName a_name = Assembly . GetExecutingAssembly ( ) ! . GetName ( ) ;
Console . WriteLine ( "S8N Compiler, version: {0}.\nSpecify environment variables: WORKFLOW_FILE, PROJECT_DIR.\nCLR: {1}" , a_name . Version , Environment . Version ) ;
return ;
}
2025-12-31 18:04:54 +03:00
var workflow = JsonSerializer . Deserialize < S8nWorkflow > ( File . OpenRead ( workflowViewFile ) , JsonSerializerOptions . Web ) ! ;
2026-01-01 22:33:48 +03:00
var edgesFile = Path . Combine ( projectDirectory , $"{workflow.Name}.Edges.cs" ) ;
var nodesFile = Path . Combine ( projectDirectory , $"{workflow.Name}.Nodes.cs" ) ;
2025-12-31 18:04:54 +03:00
// Linking
const string templateEdges = "" "
// !Warning! Please, don't change this file. Code autogenerated
#pragma warning disable CS8622 // Nullability of reference types in type of parameter doesn't match the target delegate (possibly because of nullability attributes).
public partial class { 0 }
{ {
public void Connect ( )
{ {
// edges connect here >>>
{ 1 }
} }
public void Disconnect ( )
{ {
// edges disconnect here >>>
{ 2 }
} }
} }
"" ";
const string templateNodes = "" "
// !Warning! Please, don't change this file. Code autogenerated
public partial class { 0 }
{ {
// nodes here >>>
{ 1 }
} }
"" ";
static string GetConnection ( WorkflowEdge edge ) = >
$"{edge.Source}{(string.IsNullOrEmpty(edge.SourceHandle) ? string.Empty : " . ")}{edge.SourceHandle ?? string.Empty} {(
edge . IsEvent ? "+" : string . Empty
) } = { edge . Target } { ( string . IsNullOrEmpty ( edge . TargetHandle ) ? string . Empty : "." ) } { edge . TargetHandle ? ? string . Empty } ";
string edgesConnections = string . Join ( "\n" , workflow . Edges . Select ( e = > $"// {e.Id}\n {GetConnection(e)};" ) ) ;
var edges = string . Format ( templateEdges , workflow . Name , edgesConnections , edgesConnections . Replace ( '+' , '-' ) ) ;
string nodesFields = string . Join ( "\n " , workflow . Nodes . Select ( n = > $"// {n.Data?.Label}\n public {n.Class} {n.Id};" ) ) ;
var nodes = string . Format ( templateNodes , workflow . Name , nodesFields ) ;
await File . WriteAllTextAsync ( edgesFile , edges ) ;
await File . WriteAllTextAsync ( nodesFile , nodes ) ;