Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
InitScriptFile.cs
1using System;
2using System.Collections.Generic;
3using System.IO;
4using System.Reflection;
5using System.Threading.Tasks;
7using Waher.Script;
12
14{
20 {
21 private static readonly Dictionary<string, DateTime> lastExecuted = new Dictionary<string, DateTime>();
22
33 {
34 }
35
39 public override string FunctionName => nameof(InitScriptFile);
40
44 public override string[] DefaultArgumentNames => new string[] { "FileName" };
45
50 public override bool IsAsynchronous => true;
51
59 {
60 return this.EvaluateScalarAsync(Argument, Variables).Result;
61 }
62
69 public override async Task<IElement> EvaluateScalarAsync(string Argument, Variables Variables)
70 {
71 string Source = this.Expression.Source;
72 if (string.IsNullOrEmpty(Source))
73 throw new ScriptRuntimeException("Script has no source.", this);
74
75 Source = Path.Combine(Source, Argument);
76
77 if (await NeedsExecution(Source))
78 {
79 string Script = await Resources.ReadAllTextAsync(Source);
80 Expression Exp = new Expression(Script, Source);
81
82 return await Exp.Root.EvaluateAsync(Variables);
83 }
84 else
85 return ObjectValue.Null;
86 }
87
93 public static async Task<bool> NeedsExecution(string FileName)
94 {
95 DateTime Timestamp = File.GetLastWriteTime(FileName);
96 DateTime? LastExecuted;
97 Type RuntimeSettings = null;
98 MethodInfo GetAsync;
99 MethodInfo SetAsync;
100
101 lock (lastExecuted)
102 {
103 if (lastExecuted.TryGetValue(FileName, out DateTime TP))
104 LastExecuted = TP;
105 else
106 LastExecuted = null;
107 }
108
109 if (LastExecuted.HasValue && LastExecuted.Value >= Timestamp)
110 return false;
111
112 if (!LastExecuted.HasValue &&
113 !((RuntimeSettings = Types.GetType("Waher.Runtime.Settings.RuntimeSettings")) is null) &&
114 !((GetAsync = RuntimeSettings.GetRuntimeMethod("GetAsync", new Type[] { typeof(string), typeof(DateTime) })) is null) &&
115 GetAsync.ReturnType == typeof(Task<DateTime>))
116 {
117 LastExecuted = await (Task<DateTime>)GetAsync.Invoke(null, new object[] { FileName, DateTime.MinValue });
118
119 if (LastExecuted.HasValue && LastExecuted.Value >= Timestamp)
120 {
121 lock (lastExecuted)
122 {
123 lastExecuted[FileName] = LastExecuted.Value;
124 }
125
126 return false;
127 }
128 }
129
130 lock (lastExecuted)
131 {
132 lastExecuted[FileName] = Timestamp;
133 }
134
135 if (RuntimeSettings is null)
136 RuntimeSettings = Types.GetType("Waher.Runtime.Settings.RuntimeSettings");
137
138 if (!(RuntimeSettings is null) &&
139 !((SetAsync = RuntimeSettings.GetRuntimeMethod("SetAsync", new Type[] { typeof(string), typeof(DateTime) })) is null) &&
140 SetAsync.ReturnType == typeof(Task<bool>))
141 {
142 Task<bool> Result = (Task<bool>)SetAsync.Invoke(null, new object[] { FileName, Timestamp });
143 await Result;
144 }
145
146 return true;
147 }
148 }
149}
Executes script from a file, if not executed before, or if file timestamp has changed....
override string FunctionName
Name of the function
static async Task< bool > NeedsExecution(string FileName)
Checks if an init-file needs to be executed.
override bool IsAsynchronous
If the node (or its decendants) include asynchronous evaluation. Asynchronous nodes should be evaluat...
InitScriptFile(ScriptNode Argument, int Start, int Length, Expression Expression)
Executes script from a file, if not executed before, or if file timestamp has changed....
override string[] DefaultArgumentNames
Default Argument names
override IElement EvaluateScalar(string Argument, Variables Variables)
Evaluates the function on a scalar argument.
override async Task< IElement > EvaluateScalarAsync(string Argument, Variables Variables)
Evaluates the function on a scalar argument.
Static class managing loading of resources stored as embedded resources or in content files.
Definition: Resources.cs:15
static async Task< string > ReadAllTextAsync(string FileName)
Reads a text file asynchronously.
Definition: Resources.cs:205
Static class that dynamically manages types and interfaces available in the runtime environment.
Definition: Types.cs:14
static Type GetType(string FullName)
Gets a type, given its full name.
Definition: Types.cs:41
Class managing a script expression.
Definition: Expression.cs:39
ScriptNode Root
Root script node.
Definition: Expression.cs:4299
string Source
Source of script, or null if not defined.
Definition: Expression.cs:186
Base class for funcions of one scalar string variable.
ScriptNode Argument
Function argument.
Base class for all nodes in a parsed script tree.
Definition: ScriptNode.cs:69
int Length
Length of expression covered by node.
Definition: ScriptNode.cs:101
Expression Expression
Expression of which the node is a part.
Definition: ScriptNode.cs:177
int Start
Start position in script expression.
Definition: ScriptNode.cs:92
virtual Task< IElement > EvaluateAsync(Variables Variables)
Evaluates the node, using the variables provided in the Variables collection. This method should be ...
Definition: ScriptNode.cs:158
static readonly ObjectValue Null
Null value.
Definition: ObjectValue.cs:86
Collection of variables.
Definition: Variables.cs:25
Basic interface for all types of elements.
Definition: IElement.cs:20