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;
3using System.IO;
4using System.Reflection;
5using System.Threading.Tasks;
8using Waher.Script;
13
15{
21 {
22 private static readonly Dictionary<string, DateTime> lastExecuted = new Dictionary<string, DateTime>();
23
34 {
35 }
36
40 public override string FunctionName => nameof(InitScriptFile);
41
45 public override string[] DefaultArgumentNames => new string[] { "FileName" };
46
51 public override bool IsAsynchronous => true;
52
60 {
61 return this.EvaluateScalarAsync(Argument, Variables).Result;
62 }
63
70 public override async Task<IElement> EvaluateScalarAsync(string Argument, Variables Variables)
71 {
72 string Source = this.Expression.Source;
73 if (string.IsNullOrEmpty(Source))
74 throw new ScriptRuntimeException("Script has no source.", this);
75
76 Source = Path.Combine(Source, Argument);
77
78 if (await NeedsExecution(Source))
79 {
80 string Script = await Files.ReadAllTextAsync(Source);
81 Expression Exp = new Expression(Script, Source);
82
83 return await Exp.Root.EvaluateAsync(Variables);
84 }
85 else
86 return ObjectValue.Null;
87 }
88
94 public static async Task<bool> NeedsExecution(string FileName)
95 {
96 DateTime Timestamp = File.GetLastWriteTimeUtc(FileName);
97 DateTime? LastExecuted;
98 Type RuntimeSettings = null;
99 MethodInfo GetAsync;
100 MethodInfo SetAsync;
101
102 lock (lastExecuted)
103 {
104 if (lastExecuted.TryGetValue(FileName, out DateTime TP))
105 LastExecuted = TP;
106 else
107 LastExecuted = null;
108 }
109
110 if (LastExecuted.HasValue && LastExecuted.Value >= Timestamp)
111 return false;
112
113 if (!LastExecuted.HasValue &&
114 !((RuntimeSettings = Types.GetType("Waher.Runtime.Settings.RuntimeSettings")) is null) &&
115 !((GetAsync = RuntimeSettings.GetRuntimeMethod("GetAsync", new Type[] { typeof(string), typeof(DateTime) })) is null) &&
116 GetAsync.ReturnType == typeof(Task<DateTime>))
117 {
118 LastExecuted = await (Task<DateTime>)GetAsync.Invoke(null, new object[] { FileName, DateTime.MinValue });
119
120 if (LastExecuted.HasValue && LastExecuted.Value >= Timestamp)
121 {
122 lock (lastExecuted)
123 {
124 lastExecuted[FileName] = LastExecuted.Value;
125 }
126
127 return false;
128 }
129 }
130
131 lock (lastExecuted)
132 {
133 lastExecuted[FileName] = Timestamp;
134 }
135
136 if (RuntimeSettings is null)
137 RuntimeSettings = Types.GetType("Waher.Runtime.Settings.RuntimeSettings");
138
139 if (!(RuntimeSettings is null) &&
140 !((SetAsync = RuntimeSettings.GetRuntimeMethod("SetAsync", new Type[] { typeof(string), typeof(DateTime) })) is null) &&
141 SetAsync.ReturnType == typeof(Task<bool>))
142 {
143 Task<bool> Result = (Task<bool>)SetAsync.Invoke(null, new object[] { FileName, Timestamp });
144 await Result;
145 }
146
147 return true;
148 }
149 }
150}
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.
Contains static methods
Definition: Files.cs:14
static async Task< string > ReadAllTextAsync(string FileName)
Reads a text file asynchronously.
Definition: Files.cs:84
Static class that dynamically manages types and interfaces available in the runtime environment.
Definition: Types.cs:15
static Type GetType(string FullName)
Gets a type, given its full name.
Definition: Types.cs:42
Class managing a script expression.
Definition: Expression.cs:41
ScriptNode Root
Root script node.
Definition: Expression.cs:4496
string Source
Source of script, or null if not defined.
Definition: Expression.cs:212
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:88
Collection of variables.
Definition: Variables.cs:25
Basic interface for all types of elements.
Definition: IElement.cs:21