Scaffold Python project
This commit is contained in:
parent
ab32474c29
commit
c5a88b01ea
7
Elsa.sln
7
Elsa.sln
|
|
@ -283,6 +283,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "storage", "storage", "{B818
|
|||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Elsa.CSharp", "src\modules\Elsa.CSharp\Elsa.CSharp.csproj", "{24331E82-D7AF-45B1-ACF0-CA6C3B0B77DC}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Elsa.Python", "src\modules\Elsa.Python\Elsa.Python.csproj", "{790E94F2-5393-47DF-AC52-D9247F5B243A}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
|
|
@ -709,6 +711,10 @@ Global
|
|||
{24331E82-D7AF-45B1-ACF0-CA6C3B0B77DC}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{24331E82-D7AF-45B1-ACF0-CA6C3B0B77DC}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{24331E82-D7AF-45B1-ACF0-CA6C3B0B77DC}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{790E94F2-5393-47DF-AC52-D9247F5B243A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{790E94F2-5393-47DF-AC52-D9247F5B243A}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{790E94F2-5393-47DF-AC52-D9247F5B243A}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{790E94F2-5393-47DF-AC52-D9247F5B243A}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
|
|
@ -839,6 +845,7 @@ Global
|
|||
{B818988E-639C-4E6E-85C1-B231BCAD9DAB} = {5BA4A8FA-F7F4-45B3-AEC8-8886D35AAC79}
|
||||
{732BF088-6AD7-4C4D-9A48-8074253596D4} = {B818988E-639C-4E6E-85C1-B231BCAD9DAB}
|
||||
{24331E82-D7AF-45B1-ACF0-CA6C3B0B77DC} = {6EF07978-A6D2-40EB-891D-7D70C5F37E76}
|
||||
{790E94F2-5393-47DF-AC52-D9247F5B243A} = {6EF07978-A6D2-40EB-891D-7D70C5F37E76}
|
||||
EndGlobalSection
|
||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||
SolutionGuid = {D4B5CEAA-7D70-4FCB-A68E-B03FBE5E0E5E}
|
||||
|
|
|
|||
|
|
@ -24,6 +24,6 @@ public static class ModuleExtensions
|
|||
/// </summary>
|
||||
public static IModule UseCSharp(this IModule module, Action<CSharpOptions> configureOptions)
|
||||
{
|
||||
return module.UseCSharp(csharp => csharp.RoslynOptions += configureOptions);
|
||||
return module.UseCSharp(csharp => csharp.CSharpOptions += configureOptions);
|
||||
}
|
||||
}
|
||||
|
|
@ -27,14 +27,14 @@ public class CSharpFeature : FeatureBase
|
|||
}
|
||||
|
||||
/// <summary>
|
||||
/// Configures the <see cref="RoslynOptions"/>.
|
||||
/// Configures the <see cref="CSharpOptions"/>.
|
||||
/// </summary>
|
||||
public Action<CSharpOptions> RoslynOptions { get; set; } = _ => { };
|
||||
public Action<CSharpOptions> CSharpOptions { get; set; } = _ => { };
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void Apply()
|
||||
{
|
||||
Services.Configure(RoslynOptions);
|
||||
Services.Configure(CSharpOptions);
|
||||
|
||||
// C# services.
|
||||
Services
|
||||
|
|
|
|||
25
src/modules/Elsa.Python/Contracts/IPythonEvaluator.cs
Normal file
25
src/modules/Elsa.Python/Contracts/IPythonEvaluator.cs
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
using Elsa.Expressions.Models;
|
||||
using JetBrains.Annotations;
|
||||
|
||||
namespace Elsa.Python.Contracts;
|
||||
|
||||
/// <summary>
|
||||
/// Evaluates C# expressions.
|
||||
/// </summary>
|
||||
[PublicAPI]
|
||||
public interface IPythonEvaluator
|
||||
{
|
||||
/// <summary>
|
||||
/// Evaluates a Python expression.
|
||||
/// </summary>
|
||||
/// <param name="expression">The expression to evaluate.</param>
|
||||
/// <param name="returnType">The type of the return value.</param>
|
||||
/// <param name="context">The context in which the expression is evaluated.</param>
|
||||
/// <param name="cancellationToken">An optional cancellation token.</param>
|
||||
/// <returns>The result of the evaluation.</returns>
|
||||
Task<object?> EvaluateAsync(
|
||||
string expression,
|
||||
Type returnType,
|
||||
ExpressionExecutionContext context,
|
||||
CancellationToken cancellationToken = default);
|
||||
}
|
||||
23
src/modules/Elsa.Python/Elsa.Python.csproj
Normal file
23
src/modules/Elsa.Python/Elsa.Python.csproj
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<Import Project="..\..\..\common.props" />
|
||||
<Import Project="..\..\..\configureawait.props" />
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFrameworks>net6.0;net7.0</TargetFrameworks>
|
||||
<Description>
|
||||
Provides a Python expression provider.
|
||||
</Description>
|
||||
<PackageTags>elsa module expressions scripting python</PackageTags>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Elsa.Expressions\Elsa.Expressions.csproj" />
|
||||
<ProjectReference Include="..\Elsa.Workflows.Management\Elsa.Workflows.Management.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="IronPython" Version="3.4.1" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
23
src/modules/Elsa.Python/Expressions/PythonExpression.cs
Normal file
23
src/modules/Elsa.Python/Expressions/PythonExpression.cs
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
using Elsa.Expressions.Contracts;
|
||||
|
||||
namespace Elsa.Python.Expressions;
|
||||
|
||||
/// <summary>
|
||||
/// A Python expression.
|
||||
/// </summary>
|
||||
public class PythonExpression : IExpression
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="PythonExpression"/> class.
|
||||
/// </summary>
|
||||
/// <param name="value">The Python expression.</param>
|
||||
public PythonExpression(string value)
|
||||
{
|
||||
Value = value;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the Python expression.
|
||||
/// </summary>
|
||||
public string Value { get; }
|
||||
}
|
||||
|
|
@ -0,0 +1,29 @@
|
|||
using Elsa.Expressions.Models;
|
||||
|
||||
namespace Elsa.Python.Expressions;
|
||||
|
||||
/// <summary>
|
||||
/// A reference to a Python expression.
|
||||
/// </summary>
|
||||
public class PythonExpressionBlockReference : MemoryBlockReference
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="PythonExpressionBlockReference"/> class.
|
||||
/// </summary>
|
||||
/// <param name="expression">The expression to reference.</param>
|
||||
public PythonExpressionBlockReference(PythonExpression expression)
|
||||
{
|
||||
Expression = expression;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the referenced expression.
|
||||
/// </summary>
|
||||
public PythonExpression Expression { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Declares the referenced expression.
|
||||
/// </summary>
|
||||
/// <returns>A memory block holding the expression.</returns>
|
||||
public override MemoryBlock Declare() => new();
|
||||
}
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
using Elsa.Expressions.Contracts;
|
||||
using Elsa.Expressions.Models;
|
||||
using Elsa.Python.Contracts;
|
||||
|
||||
namespace Elsa.Python.Expressions;
|
||||
|
||||
/// <summary>
|
||||
/// Evaluates C# expressions.
|
||||
/// </summary>
|
||||
public class PythonExpressionHandler : IExpressionHandler
|
||||
{
|
||||
private readonly IPythonEvaluator _evaluator;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="PythonExpressionHandler"/> class.
|
||||
/// </summary>
|
||||
public PythonExpressionHandler(IPythonEvaluator evaluator)
|
||||
{
|
||||
_evaluator = evaluator;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async ValueTask<object?> EvaluateAsync(IExpression expression, Type returnType, ExpressionExecutionContext context)
|
||||
{
|
||||
var pythonExpression = (PythonExpression)expression;
|
||||
return await _evaluator.EvaluateAsync(pythonExpression.Value, returnType, context);
|
||||
}
|
||||
}
|
||||
52
src/modules/Elsa.Python/Features/PythonFeature.cs
Normal file
52
src/modules/Elsa.Python/Features/PythonFeature.cs
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
using Elsa.Common.Features;
|
||||
using Elsa.Expressions.Contracts;
|
||||
using Elsa.Expressions.Features;
|
||||
using Elsa.Extensions;
|
||||
using Elsa.Features.Abstractions;
|
||||
using Elsa.Features.Attributes;
|
||||
using Elsa.Features.Services;
|
||||
using Elsa.Python.Contracts;
|
||||
using Elsa.Python.Expressions;
|
||||
using Elsa.Python.Options;
|
||||
using Elsa.Python.Providers;
|
||||
using Elsa.Python.Services;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace Elsa.Python.Features;
|
||||
|
||||
/// <summary>
|
||||
/// Installs C# integration.
|
||||
/// </summary>
|
||||
[DependsOn(typeof(MediatorFeature))]
|
||||
[DependsOn(typeof(ExpressionsFeature))]
|
||||
public class PythonFeature : FeatureBase
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public PythonFeature(IModule module) : base(module)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Configures the <see cref="PythonOptions"/>.
|
||||
/// </summary>
|
||||
public Action<PythonOptions> RoslynOptions { get; set; } = _ => { };
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void Apply()
|
||||
{
|
||||
Services.Configure(RoslynOptions);
|
||||
|
||||
// C# services.
|
||||
Services
|
||||
.AddSingleton<IExpressionSyntaxProvider, PythonExpressionSyntaxProvider>()
|
||||
.AddSingleton<IPythonEvaluator, IronPythonEvaluator>()
|
||||
.AddExpressionHandler<PythonExpressionHandler, PythonExpression>()
|
||||
;
|
||||
|
||||
// Handlers.
|
||||
Services.AddNotificationHandlersFrom<PythonFeature>();
|
||||
|
||||
// Activities.
|
||||
Module.AddActivitiesFrom<PythonFeature>();
|
||||
}
|
||||
}
|
||||
9
src/modules/Elsa.Python/Options/PythonOptions.cs
Normal file
9
src/modules/Elsa.Python/Options/PythonOptions.cs
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
namespace Elsa.Python.Options;
|
||||
|
||||
/// <summary>
|
||||
/// Options for the Python expression evaluator.
|
||||
/// </summary>
|
||||
public class PythonOptions
|
||||
{
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,36 @@
|
|||
using Elsa.Expressions.Contracts;
|
||||
using Elsa.Expressions.Models;
|
||||
using Elsa.Python.Expressions;
|
||||
|
||||
namespace Elsa.Python.Providers;
|
||||
|
||||
internal class PythonExpressionSyntaxProvider : IExpressionSyntaxProvider
|
||||
{
|
||||
private const string SyntaxName = "Python";
|
||||
|
||||
public ValueTask<IEnumerable<ExpressionSyntaxDescriptor>> GetDescriptorsAsync(CancellationToken cancellationToken = default)
|
||||
{
|
||||
var javaScript = CreatePythonDescriptor();
|
||||
|
||||
return ValueTask.FromResult<IEnumerable<ExpressionSyntaxDescriptor>>(new[] { javaScript });
|
||||
}
|
||||
|
||||
private ExpressionSyntaxDescriptor CreatePythonDescriptor() => new()
|
||||
{
|
||||
Syntax = SyntaxName,
|
||||
Type = typeof(PythonExpression),
|
||||
CreateExpression = CreateCSharpExpression,
|
||||
CreateBlockReference = context => new PythonExpressionBlockReference(context.GetExpression<PythonExpression>()),
|
||||
CreateSerializableObject = context => new
|
||||
{
|
||||
Type = SyntaxName,
|
||||
context.GetExpression<PythonExpression>().Value
|
||||
}
|
||||
};
|
||||
|
||||
private IExpression CreateCSharpExpression(ExpressionConstructorContext context)
|
||||
{
|
||||
var script = context.Element.TryGetProperty("value", out var p) ? p.ToString() : "";
|
||||
return new PythonExpression(script);
|
||||
}
|
||||
}
|
||||
19
src/modules/Elsa.Python/Services/IronPythonEvaluator.cs
Normal file
19
src/modules/Elsa.Python/Services/IronPythonEvaluator.cs
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
using Elsa.Expressions.Models;
|
||||
using Elsa.Python.Contracts;
|
||||
|
||||
namespace Elsa.Python.Services;
|
||||
|
||||
/// <summary>
|
||||
/// Evaluates Python expressions using IronPython.
|
||||
/// </summary>
|
||||
public class IronPythonEvaluator : IPythonEvaluator
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public async Task<object?> EvaluateAsync(string expression, Type returnType, ExpressionExecutionContext context, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var eng = IronPython.Hosting.Python.CreateEngine();
|
||||
var scope = eng.CreateScope();
|
||||
var result = eng.Execute(expression, scope);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue