BadScript 2
Loading...
Searching...
No Matches
BadRunSystem.cs
Go to the documentation of this file.
1using System.Diagnostics;
2using System.Net;
3using System.Net.Sockets;
4
10using BadScript2.IO;
14
16
20public class BadRunSystem : BadConsoleSystem<BadRunSystemSettings>
21{
26 public BadRunSystem(BadRuntime runtime) : base(runtime) { }
27
32 private static string StartupDirectory
33 {
34 get
35 {
36 string? s = BadSettingsProvider.RootSettings.FindProperty<string>("Subsystems.Run.StartupDirectory");
37
38 if (s == null)
39 {
40 throw new BadRuntimeException("Subsystems.Run.StartupDirectory not set");
41 }
42
44
45 return s;
46 }
47 }
48
50 public override string Name => "run";
51
52
54 protected override async Task<int> Run(BadRunSystemSettings settings)
55 {
56 BadNetworkConsoleHost? host = null;
57
58 if (settings.RemotePort != -1)
59 {
60 host = new BadNetworkConsoleHost(new TcpListener(IPAddress.Any, settings.RemotePort));
61 host.Start();
62 Runtime.UseConsole(host);
63 }
64
65 Runtime.UseStartupArguments(settings.Args);
66 IEnumerable<string> files = BadFileSystem.Instance.GetFiles(
68 $".{BadRuntimeSettings.Instance.FileExtension}",
69 true
70 )
71 .Concat(settings.Files);
72
73 if (settings.Interactive)
74 {
75 if (settings.Benchmark)
76 {
77 BadLogger.Warn("Benchmarking is not supported in interactive mode");
78 }
79
80 await Runtime.RunInteractiveAsync(files);
81
82 return 0;
83 }
84
85 Stopwatch? sw = null;
86
87 if (settings.Benchmark)
88 {
89 sw = Stopwatch.StartNew();
90 }
91
92 if (settings.Debug)
93 {
94 Runtime.UseScriptDebugger();
95 }
96
97 foreach (string file in files)
98 {
99 Runtime.ExecuteFile(file);
100 }
101
102 if (settings.Benchmark)
103 {
104 sw?.Stop();
105 BadLogger.Log($"Execution Time: {sw?.ElapsedMilliseconds ?? 0}ms", "Benchmark");
106 }
107
108 host?.Stop();
109
110 return 0;
111 }
112}
Exposes the BadScript Runtime Functionality to Consumers.
Definition BadRuntime.cs:28
Public facing interface for a logger.
Definition BadLogger.cs:7
static void Log(string message)
Writes a Log to the Message Handler.
Definition BadLogger.cs:26
static void Warn(string message)
Writes a Warning to the Message Handler.
Definition BadLogger.cs:56
Implements a Console System that uses a settings object of Type T.
Runs one or more BadScript scripts.
static string StartupDirectory
The Startup Directory where all containing scripts will be loaded at every execution.
BadRunSystem(BadRuntime runtime)
Creates a new BadRunSystem instance.
override async Task< int > Run(BadRunSystemSettings settings)
bool Benchmark
If Enabled, the execution time will be printed to the console.
int RemotePort
If specified, the Run System will try to host a remote shell on the specified port.
IEnumerable< string > Files
The Files that will be executed.
bool Interactive
If Enabled, the Console will be started in interactive mode.
IEnumerable< string > Args
The Commandline Arguments for the Scripts.
bool Debug
If enabled, the debugger will be attached to the process.
Public interface for the filesystem abstraction of the BadScript Engine.
static IFileSystem Instance
File System implementation.
BadSettings? FindProperty(string propertyPath)
Finds a property based on the property path relative to this object.
Helper class that can be used to automatically load a settings object from a file.
static BadSettings RootSettings
Returns the Root Settings Object.
void CreateDirectory(string path, bool recursive=false)
Creates a new directory.
IEnumerable< string > GetFiles(string path, string extension, bool recursive)
Returns all files in the given directory that match the specified extension.
Contains Logging system for the BadScript Runtime.
Definition BadLog.cs:6
Contains the Implementation of the Remote Console Abstraction over TCP.
Contains the 'run' console command implementation.
Contains the debugger implementations for the BadScript2 Runtime.
Contains IO Implementation for the BadScript2 Runtime.
Contains the interactive console Implementation.
Contains Common Interop Extensions and APIs for the BadScript2 Runtime.
Contains the Error Objects for the BadScript2 Language.
Contains Runtime Settings Objects.
Contains the Settings Implementation.