-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathRunScript.cs
More file actions
140 lines (123 loc) · 3.85 KB
/
Copy pathRunScript.cs
File metadata and controls
140 lines (123 loc) · 3.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
using System.Collections;
using BlocklyNet.Core.Model;
using BlocklyNet.Extensions.Builder;
using BlocklyNet.Scripting;
using BlocklyNet.Scripting.Definition;
using BlocklyNet.Scripting.Engine;
using BlocklyNet.Scripting.Generic;
using Microsoft.Extensions.DependencyInjection;
namespace BlocklyNet.Extensions;
/// <summary>
/// Run a user defined script.
/// </summary>
[CustomBlock(
"run_script_by_name",
"Scripts",
@"{
""message0"": ""RunScript %1 Display name %2 Id %3 Parameters %4 Do not execute %5"",
""args0"": [
{
""type"": ""input_dummy""
},
{
""type"": ""input_value"",
""name"": ""NAME"",
""check"": ""String""
},
{
""type"": ""input_value"",
""name"": ""ID"",
""check"": ""String""
},
{
""type"": ""input_value"",
""name"": ""ARGS"",
""check"": [""Array(run_script_parameter)"", ""Array""]
},
{
""type"": ""input_value"",
""name"": ""BUILDONLY"",
""check"": ""Boolean""
}
],
""output"": null,
""colour"": 230,
""tooltip"": ""Run a script by name"",
""helpUrl"": """"
}",
@"{
""inputs"": {
""NAME"": {
""shadow"": {
""type"": ""text"",
""fields"": {
""TEXT"": """"
}
}
},
""BUILDONLY"": {
""shadow"": {
""type"": ""logic_boolean"",
""fields"": {
""BOOL"": ""FALSE""
}
}
}
}
}"
)]
public class RunScript : Block
{
/// <summary>
///
/// </summary>
/// <param name="context"></param>
/// <returns></returns>
private async Task<StartScript> ReadConfigurationAsync(Context context)
{
/* Find the script by its name - character casing is ignored. */
var store = context.ServiceProvider.GetRequiredService<IScriptDefinitionStorage>();
var byName = await Values.EvaluateAsync<string>("NAME", context);
var byId = await Values.EvaluateAsync<string?>("ID", context, false);
var script =
(string.IsNullOrEmpty(byId) ? null : await store.GetAsync(byId))
?? await store.FindAsync(byName)
?? throw new ArgumentException($"script '{byName}' not found");
/* Prepare to run generic script. */
var config = context.ServiceProvider.GetService<IGenericScriptFactory>()?.Create() ?? new StartGenericScript();
config.Name = script.Name;
config.ScriptId = script.Id;
config.ResultType = script.ResultType;
/* Fill presets - just copy indicated variables with the same name. */
var copies = await Values.EvaluateAsync<IEnumerable>("ARGS", context, false);
if (copies != null)
foreach (RunScriptParameter parameter in copies)
config.Presets.Add(new() { Key = parameter.VariableName, Value = parameter.Value });
return (StartScript)config;
}
/// <inheritdoc/>
protected override async Task<object?> EvaluateAsync(Context context)
{
/* Convert the configuration. */
var config = await ReadConfigurationAsync(context);
/* We are prepared to be run in parallel to other scripts. */
if (context.ParallelMode > 0) return config;
/* Or we are hust building. */
var buildOnly = await Values.EvaluateAsync<bool?>("BUILDONLY", context, false);
if (buildOnly == true) return config;
/* Install debugging context tree. */
IDebugScriptSite? debugSite = context.Engine as IDebugScriptSite;
var oldContext = debugSite?.CallerContext;
debugSite?.CallerContext = context;
/* Run the script and report the result - in a new isolated environment. */
try
{
var result = await context.Engine.RunAsync<GenericResult>(config);
return result.Result;
}
finally
{
debugSite?.CallerContext = oldContext;
}
}
}