Skip to content

Commit

Permalink
💡 Add methods for C# scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
jxnkwlp committed Feb 5, 2024
1 parent 3982c8c commit 204702e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
using Elsa.Services.Models;
using Elsa.Services.WorkflowStorage;
using MediatR;
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json;
using NodaTime;
using Passingwind.Abp.ElsaModule.CSharp;
using Passingwind.Abp.ElsaModule.Scripting.CSharp.Messages;
using Passingwind.CSharpScriptEngine;
using Volo.Abp.Json;
Expand Down Expand Up @@ -71,7 +71,12 @@ private async Task ConfigEngineGlobalAsync(CSharpScriptEvaluationGlobal global,
global.Context.WorkflowInstance = context.WorkflowExecutionContext.WorkflowInstance;
global.Context.WorkflowContext = context.GetWorkflowContext();

// Core functions
global.Context.GetService = (Action<Type>)((Type type) => context.ServiceProvider.GetService(type));
global.Context.GetRequiredService = (Action<Type>)((Type type) => context.ServiceProvider.GetRequiredService(type));

// Global functions.
global.Context.AddJournalData = (Action<string, object>)((string key, object value) => context.JournalData.Add(key, value));
global.Context.GetTransientVariable = (Func<string, object>)((string name) => context.GetTransientVariable(name));
global.Context.SetTransientVariable = (Action<string, object>)((string name, object value) => context.SetTransientVariable(name, value));
global.Context.GetVariable = (Func<string, object>)((string name) => context.GetVariable(name));
Expand All @@ -87,12 +92,15 @@ private async Task ConfigEngineGlobalAsync(CSharpScriptEvaluationGlobal global,
global.Context.getWorkflowDefinitionIdByTag = (Func<string, string>)(tag => GetWorkflowDefinitionIdByTag(context, tag));
global.Context.GetActivity = (Func<string, object>)(idOrName => GetActivityModel(context, idOrName));
global.Context.GetActivityId = (Func<string, string>)(activityName => GetActivityId(context, activityName));
global.Context.AddEntry = (Action<string, string, object>)((string eventName, string message, object data) => context.AddEntry(eventName, message, data));

// Workflow variables.
var variables = context.WorkflowExecutionContext.GetMergedVariables();

foreach (var variable in variables.Data)
{
global.AddVariable(variable.Key, variable.Value);
}

// activity output
await AddActivityOutputAsync(global, context, cancellationToken);
Expand Down Expand Up @@ -128,28 +136,28 @@ private async Task AddActivityOutputAsync(CSharpScriptEvaluationGlobal global, A
global.Context.ActivitityData = activities;
}

private string GetWorkflowDefinitionIdByTag(ActivityExecutionContext activityExecutionContext, string tag)
private static string GetWorkflowDefinitionIdByTag(ActivityExecutionContext activityExecutionContext, string tag)
{
var workflowRegistry = activityExecutionContext.GetService<IWorkflowRegistry>();
var workflowBlueprint = workflowRegistry.FindByTagAsync(tag, VersionOptions.Published).Result;
return workflowBlueprint?.Id;
}

private string GetWorkflowDefinitionIdByName(ActivityExecutionContext activityExecutionContext, string name)
private static string GetWorkflowDefinitionIdByName(ActivityExecutionContext activityExecutionContext, string name)
{
var workflowRegistry = activityExecutionContext.GetService<IWorkflowRegistry>();
var workflowBlueprint = workflowRegistry.FindByNameAsync(name, VersionOptions.Published).Result;
return workflowBlueprint?.Id;
}

private string GetActivityId(ActivityExecutionContext context, string activityName)
private static string GetActivityId(ActivityExecutionContext context, string activityName)
{
var workflowExecutionContext = context.WorkflowExecutionContext;
var activity = workflowExecutionContext.GetActivityBlueprintByName(activityName);
return activity?.Id;
}

private object GetActivityModel(ActivityExecutionContext context, string idOrName)
private static object GetActivityModel(ActivityExecutionContext context, string idOrName)
{
var workflowExecutionContext = context.WorkflowExecutionContext;
var activity = workflowExecutionContext.GetActivityBlueprintByName(idOrName) ?? workflowExecutionContext.GetActivityBlueprintById(idOrName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,12 @@ public Task Handle(CSharpTypeDefinitionNotification notification, CancellationTo
public static ").Append(typeof(WorkflowExecutionContext).FullName).AppendLine(@" WorkflowExecutionContext => throw new System.NotImplementedException();
public static object WorkflowContext => throw new System.NotImplementedException();
// Core functions
public static object? GetService(Type type) => throw new System.NotImplementedException();
public static object GetRequiredService(Type type) => throw new System.NotImplementedException();
// methods
public static void AddJournalData(string key, object? value) => throw new System.NotImplementedException();
public static object GetWorkflowContext() => throw new System.NotImplementedException();
public static object GetTransientVariable(string name) => throw new System.NotImplementedException();
public static void SetTransientVariable(string name, object value) => throw new System.NotImplementedException();
Expand All @@ -41,6 +46,7 @@ public Task Handle(CSharpTypeDefinitionNotification notification, CancellationTo
public static dynamic JsonDecode(string value) => throw new System.NotImplementedException();
public static string GetWorkflowDefinitionIdByName(string name) => throw new System.NotImplementedException();
public static string GetWorkflowDefinitionIdByTag(string tag) => throw new System.NotImplementedException();
public static void AddEntry(string eventName, string message, object? data) => throw new System.NotImplementedException();
// activities
public static IDictionary<string, object> GetActivity(string name) => throw new System.NotImplementedException();
Expand Down

0 comments on commit 204702e

Please sign in to comment.