Skip to content

Commit 1e9e43e

Browse files
committed
Update to have csharp as an executor.
0 parents  commit 1e9e43e

6 files changed

Lines changed: 134 additions & 0 deletions

File tree

‎Dockerfile‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Bring in from Mono docker image
2+
FROM mcr.microsoft.com/dotnet/core/sdk:2.2
3+
4+
# Copy over our .NET C# solution skeleton
5+
COPY ./src /opt/executor
6+
7+
# Set working directory to our /opt/executor location
8+
WORKDIR /opt/executor
9+
10+
# Install required
11+
#RUN nuget install Newtonsoft.Json -Version 12.0.1
12+
#dotnet add package Newtonsoft.Json
13+

‎README.md‎

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# executor-php
2+
Script Task Executor Engine with Mono Runtime to support C#
3+
4+
This docker image provides a sandboxed protected environment to run custom C# "scripts" that are written in ProcessMaker BPM.
5+
User created script tasks should be isolated however have utilities available to them in order to get most common tasks done. This
6+
.net core environment has a C# Solution Skeleton that includes the following libraries:
7+
8+
- Newtonsoft.Json 12.0.2
9+
- TODO : Identify most common libraries used for existing script tasks
10+
11+
## How to use
12+
The execution requires a data.json, config.json and an output.json file be present on the host system. The data.json represents the
13+
Request instance data. The config.json represents configuration specific for this Script Task. And the output.json should be a blank
14+
file that will be populated by the successful output of the script task. The script task is represented by a Script.cs file.
15+
It is the responsibility of the caller to have these files prepared before executing the engine via command line (or docker API).
16+
17+
## Script Task design
18+
When writing a Script Task, three variables are available. They are:
19+
20+
- data - A dynamic object that represents the current Request data
21+
- config - A dynamic object that represents the current Script Task configuration
22+
- output - A dynamic object you can populate with your desired JSON changes. See: https://www.newtonsoft.com/json/help/html/CreateJsonDynamic.htm
23+
24+
Your script must extend the BaseScript class. The method signature is:
25+
```csharp
26+
public abstract void Execute(dynamic data, dynamic config, dynamic output)
27+
```
28+
Your script can read JSON properties from data aond config. Then it can populate the dynamic output object with whatever JSON data you wish to output.
29+
Once the script is complete, the final output to console will be used and converted to JSON which
30+
will be stored in the output.json file. Once the docker execution is complete, you should evaluate the return code of the docker execution.
31+
If the code is 0, then the script task executed successfully and you can read output.json for the valid output. If it is non-zero,
32+
then you should review STDERR to see the error that was displayed during execution.
33+
34+
### Example data.json
35+
```json
36+
{
37+
"firstname": "Taylor"
38+
}
39+
```
40+
41+
### Example Script Task
42+
```csharp
43+
public class Script : BaseScript
44+
{
45+
public override void Execute(dynamic data, dynamic config, dynamic output)
46+
{
47+
output.firstname = ((string)data.firstname).ToUpper();
48+
}
49+
}
50+
```
51+
52+
### Example output.json
53+
```json
54+
{"firstname":"TAYLOR"}
55+
```
56+
57+
## Command Line Usage
58+
```bash
59+
$ docker run -v <path to local data.json>:/opt/executor/data.json \
60+
-v <path to local config.json>:/opt/executor/config.json \
61+
-v <path to local Script.cs>:/opt/executor/Script.cs \
62+
-v <path to local output.json>:/opt/executor/output.json \
63+
processmaker/executor:csharp \
64+
"dotnet run"
65+
```

‎src/BaseScript.cs‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using Newtonsoft.Json.Linq;
2+
3+
/**
4+
BaseScript is the base class that a custom script should inherit. It has one single
5+
method to execute the script, passing in the data, config as well as a prepared output
6+
object to populate.
7+
*/
8+
public abstract class BaseScript
9+
{
10+
public abstract void Execute(dynamic data, dynamic config, dynamic output);
11+
}

‎src/NuGet.Config‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<configuration>
3+
<config>
4+
<add key="repositoryPath" value="./packages" />
5+
</config>
6+
<packageRestore>
7+
<!-- The 'enabled' key is True when the "Allow NuGet to download missing packages" checkbox is set.
8+
Clearing the box sets this to False, disabling command-line, automatic, and MSBuild-Integrated restore. -->
9+
<add key="enabled" value="True" />
10+
</packageRestore>
11+
</configuration>

‎src/ScriptRunner.cs‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using System;
2+
using System.IO;
3+
using Newtonsoft.Json.Linq;
4+
5+
/*
6+
Our quick and simple .net c sharp script runner that prepares reading
7+
json data from a local file as well as config. Prepares a dynamic object
8+
to be utilized for dynamic json population then writes the contents of the
9+
object to stdout.
10+
*/
11+
public class ScriptRunner
12+
{
13+
static public void Main ()
14+
{
15+
dynamic data = JObject.Parse(File.ReadAllText(@"data.json"));
16+
dynamic config = JObject.Parse(File.ReadAllText(@"config.json"));
17+
dynamic output = new JObject();
18+
Script script = new Script();
19+
script.Execute(data, config, output);
20+
File.WriteAllText(@"output.json", output.ToString());
21+
}
22+
}

‎src/ScriptRunner.csproj‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp2.2</TargetFramework>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
10+
</ItemGroup>
11+
12+
</Project>

0 commit comments

Comments
 (0)