-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathProgram.cs
More file actions
203 lines (172 loc) · 5.9 KB
/
Program.cs
File metadata and controls
203 lines (172 loc) · 5.9 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
using System;
using System.IO;
using System.Text;
using Waher.Events;
using Waher.Persistence;
using Waher.Persistence.Files;
using Waher.Persistence.Serialization;
using Waher.Runtime.Console;
using Waher.Runtime.Inventory;
using Waher.Script;
namespace Waher.Utility.RunScript
{
/// <summary>
/// Allows you to execute script.
///
/// Command line switches:
///
/// -i SCRIPT_FILE Points to the script file to execute.
/// -d APP_DATA_FOLDER Points to the application data folder.
/// If specified, a connection to a files
/// object database (Waher.Persistence.Files)
/// will be established.
/// -e If encryption is used by the database.
/// -bs BLOCK_SIZE Block size, in bytes. Default=8192.
/// -bbs BLOB_BLOCK_SIZE BLOB block size, in bytes. Default=8192.
/// -? Help.
/// </summary>
class Program
{
static int Main(string[] args)
{
FilesProvider FilesProvider = null;
Encoding Encoding = Encoding.UTF8;
string ProgramDataFolder = null;
string ScriptFile = null;
string s;
int BlockSize = 8192;
int BlobBlockSize = 8192;
int i = 0;
int c = args.Length;
bool Help = false;
bool Encryption = false;
try
{
while (i < c)
{
s = args[i++].ToLower();
switch (s)
{
case "-i":
if (i >= c)
throw new Exception("Missing script file.");
if (string.IsNullOrEmpty(ScriptFile))
ScriptFile = args[i++];
else
throw new Exception("Only one script file allowed.");
break;
case "-d":
if (i >= c)
throw new Exception("Missing program data folder.");
if (string.IsNullOrEmpty(ProgramDataFolder))
ProgramDataFolder = args[i++];
else
throw new Exception("Only one program data folder allowed.");
break;
case "-bs":
if (i >= c)
throw new Exception("Block size missing.");
if (!int.TryParse(args[i++], out BlockSize))
throw new Exception("Invalid block size");
break;
case "-bbs":
if (i >= c)
throw new Exception("Blob Block size missing.");
if (!int.TryParse(args[i++], out BlobBlockSize))
throw new Exception("Invalid blob block size");
break;
case "-enc":
if (i >= c)
throw new Exception("Text encoding missing.");
Encoding = Encoding.GetEncoding(args[i++]);
break;
case "-e":
Encryption = true;
break;
case "-?":
Help = true;
break;
default:
throw new Exception("Unrecognized switch: " + s);
}
}
if (Help || c == 0)
{
ConsoleOut.WriteLine("Allows you to execute script.");
ConsoleOut.WriteLine();
ConsoleOut.WriteLine("Command line switches:");
ConsoleOut.WriteLine();
ConsoleOut.WriteLine("-i SCRIPT_FILE Points to the script file to execute.");
ConsoleOut.WriteLine("-d APP_DATA_FOLDER Points to the application data folder.");
ConsoleOut.WriteLine(" If specified, a connection to a files");
ConsoleOut.WriteLine(" object database (Waher.Persistence.Files)");
ConsoleOut.WriteLine(" will be established.");
ConsoleOut.WriteLine("-e If encryption is used by the database.");
ConsoleOut.WriteLine("-bs BLOCK_SIZE Block size, in bytes. Default=8192.");
ConsoleOut.WriteLine("-bbs BLOB_BLOCK_SIZE BLOB block size, in bytes. Default=8192.");
ConsoleOut.WriteLine("-? Help.");
return 0;
}
if (string.IsNullOrEmpty(ScriptFile))
throw new Exception("No script file provided.");
Types.Initialize(
typeof(Log).Assembly,
typeof(Expression).Assembly,
typeof(Database).Assembly,
typeof(FilesProvider).Assembly,
typeof(ObjectSerializer).Assembly,
typeof(Content.InternetContent).Assembly,
typeof(Content.Html.HtmlDocument).Assembly,
typeof(Content.Images.ImageCodec).Assembly,
typeof(Content.Xml.XML).Assembly,
typeof(Content.Xsl.XSL).Assembly,
typeof(Events.Console.ConsoleEventSink).Assembly,
typeof(Script.Content.Functions.Encoding.Base64Decode).Assembly,
typeof(Script.Cryptography.Functions.RandomBytes).Assembly,
typeof(Script.Fractals.FractalGraph).Assembly,
typeof(Script.Graphs.Graph).Assembly,
typeof(Script.Graphs3D.Canvas3D).Assembly,
typeof(Script.FullTextSearch.Functions.AddFtsProperties).Assembly,
typeof(Script.Networking.Functions.Dns).Assembly,
typeof(Script.Persistence.Functions.DeleteObject).Assembly,
typeof(Script.Statistics.StatMath).Assembly,
typeof(Script.System.Functions.ShellExecute).Assembly,
typeof(Script.Xml.XmlOutput).Assembly);
string Script = File.ReadAllText(ScriptFile);
Expression Parsed = new(Script);
Log.Register(new Events.Console.ConsoleEventSink());
if (!string.IsNullOrEmpty(ProgramDataFolder))
{
if (!Directory.Exists(ProgramDataFolder))
throw new Exception("Program data folder does not exist.");
FilesProvider = FilesProvider.CreateAsync(ProgramDataFolder, "Default", BlockSize, 10000, BlobBlockSize, Encoding, 3600000, Encryption, false).Result;
Database.Register(FilesProvider);
FilesProvider.RepairIfInproperShutdown(string.Empty).Wait();
FilesProvider.Start().Wait();
}
Variables Variables = new()
{
ConsoleOut = ConsoleOut.Writer
};
object Result = Parsed.EvaluateAsync(Variables).Result;
ConsoleOut.WriteLine(Expression.ToString(Result));
return 0;
}
catch (Exception ex)
{
ConsoleOut.WriteLine(ex.Message);
return -1;
}
finally
{
ConsoleOut.Flush(true);
Log.TerminateAsync().Wait();
if (FilesProvider is not null)
{
FilesProvider.Stop().Wait();
FilesProvider?.DisposeAsync().Wait();
}
}
}
}
}