@@ -50,17 +50,18 @@ static class Verbs
5050 public const string New = "new" ;
5151 public const string ClearCache = "clear-cache" ;
5252 public const string InitVSCode = "init-vscode" ;
53+ public const string ListRunning = "list-running" ;
5354 }
5455
55- enum ErrorCodes : uint
56+ enum ErrorCodes
5657 {
5758 OK = 0 ,
58- ErrorMask = 0xFF000000 ,
59- ScriptReturnRangeConflict = 0x01_00_00_00 ,
6059 GenericError ,
60+ ScriptReturnRangeConflict ,
6161 UnrecognizedArgument ,
6262 ScriptFileDoesNotExist ,
6363 ScriptCompilationFailed ,
64+ Reserved = 0xFF
6465 }
6566
6667 [ STAThread ]
@@ -76,7 +77,7 @@ static async Task<ErrorCodes> MainAsync(string[] args)
7677 var dir = Path . Combine ( Path . GetDirectoryName ( thisProcess . MainModule . FileName ) , "running" ) ;
7778 Directory . CreateDirectory ( dir ) ;
7879
79- using var file = new FileStream ( Path . Combine ( dir , thisProcess . Id . ToString ( ) ) , FileMode . Create , FileAccess . ReadWrite , FileShare . Read , 512 , FileOptions . DeleteOnClose | FileOptions . Asynchronous ) ;
80+ using var file = new FileStream ( Path . Combine ( dir , thisProcess . Id . ToString ( ) ) , FileMode . Create , FileAccess . Write , FileShare . Read , 512 , FileOptions . DeleteOnClose | FileOptions . Asynchronous ) ;
8081 using ( var writer = new StreamWriter ( file , null , - 1 , true ) )
8182 await writer . WriteLineAsync ( Environment . CommandLine ) ;
8283
@@ -98,6 +99,7 @@ static async Task<ErrorCodes> MainAsync(string[] args)
9899 case Verbs . New : CreateNew ( args . Length > 1 ? args [ 1 ] : null ) ; break ;
99100 case Verbs . ClearCache : await ClearCache ( ) ; break ;
100101 case Verbs . InitVSCode : InitVSCode ( ) ; break ;
102+ case Verbs . ListRunning : await ListRunning ( ) ; break ;
101103 default : return await RunScript ( args ) ;
102104 }
103105 }
@@ -129,7 +131,15 @@ static void PrintHelp()
129131 Console . WriteLine ( $ " Initializes VS Code debugging support (creates .vscode directory)") ;
130132 Console . WriteLine ( $ "{ exe } { Verbs . ClearCache } ") ;
131133 Console . WriteLine ( $ " Cleares the cache of previously compiled scripts.") ;
132- }
134+ Console . WriteLine ( $ "{ exe } { Verbs . ListRunning } ") ;
135+ Console . WriteLine ( $ " Lists the currently running script engine instances.") ;
136+ Console . WriteLine ( $ "Returned error codes:") ;
137+ foreach ( var code in Enum . GetValues < ErrorCodes > ( ) . Where ( x => x != ErrorCodes . Reserved ) )
138+ Console . WriteLine ( $ " - { code , 3 : D} { code } ") ;
139+ Console . WriteLine ( $ " - Value returned by script.") ;
140+ Console . WriteLine ( $ " Values { ErrorCodes . OK + 1 : D} - { ErrorCodes . Reserved : D} are reserved by the engine.") ;
141+ Console . WriteLine ( $ " If a script returns a value in the reserved range, { ErrorCodes . ScriptReturnRangeConflict } will be returned instead.") ;
142+ }
133143
134144 static void WriteLine ( string line , ConsoleColor color = ( ConsoleColor ) ( - 1 ) )
135145 {
@@ -138,5 +148,38 @@ static void WriteLine(string line, ConsoleColor color = (ConsoleColor)(-1))
138148 Console . WriteLine ( line ) ;
139149 Console . ResetColor ( ) ;
140150 }
151+
152+ static async Task ListRunning ( )
153+ {
154+ WriteLine ( "Process ID RT Arguments" ) ;
155+ WriteLine ( "---------- --- ---------" ) ;
156+ using var thisProcess = Process . GetCurrentProcess ( ) ;
157+ var runtimesDir = Path . GetDirectoryName ( thisProcess . MainModule . FileName ) ; // bin
158+ runtimesDir = Path . GetDirectoryName ( runtimesDir ) ; // x64
159+ runtimesDir = Path . GetDirectoryName ( runtimesDir ) ; // vX.Y.Z
160+ foreach ( var runtimeDir in Directory . EnumerateDirectories ( runtimesDir ) )
161+ {
162+ var dir = Path . Combine ( runtimeDir , "bin" , "running" ) ;
163+ if ( ! Directory . Exists ( dir ) )
164+ continue ;
165+
166+ var rt = Path . GetFileName ( runtimeDir ) ;
167+
168+ foreach ( var file in Directory . EnumerateFiles ( dir ) )
169+ {
170+ var processId = Path . GetFileNameWithoutExtension ( file ) ;
171+ if ( thisProcess . Id == int . Parse ( processId ) )
172+ continue ;
173+
174+ using var reader = new StreamReader ( new FileStream ( file , FileMode . Open , FileAccess . Read , FileShare . ReadWrite | FileShare . Delete ) ) ;
175+ var args = await reader . ReadToEndAsync ( ) . ConfigureAwait ( false ) ;
176+ if ( args . StartsWith ( '"' ) )
177+ args = args . Substring ( args . IndexOf ( '"' , 1 ) + 1 ) . TrimStart ( ) ;
178+ else
179+ args = args . Split ( ' ' , 2 ) . Last ( ) ;
180+ WriteLine ( $ "{ processId , 10 } { rt , - 3 } { args } ") ;
181+ }
182+ }
183+ }
141184 }
142185}
0 commit comments