-
Notifications
You must be signed in to change notification settings - Fork 97
Expand file tree
/
Copy pathAutoShell.cs
More file actions
189 lines (167 loc) · 6.53 KB
/
AutoShell.cs
File metadata and controls
189 lines (167 loc) · 6.53 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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Text.Json;
using autoShell.Logging;
using autoShell.Services.Interop;
namespace autoShell;
/// <summary>
/// Entry point for the autoShell Windows automation console application.
/// Reads JSON commands from stdin (interactive mode) or command-line arguments
/// and dispatches them to the appropriate handler via <see cref="ActionDispatcher"/>.
/// </summary>
/// <remarks>
/// Each JSON command is a single object where property names are command names
/// and values are parameters, e.g. <c>{"Volume":50}</c> or <c>{"Mute":true}</c>.
/// Multiple commands can be batched in one object: <c>{"Volume":50,"Mute":false}</c>.
/// The special command <c>"quit"</c> exits the application.
/// </remarks>
internal class AutoShell
{
#region P/Invoke
[DllImport(NativeDlls.Kernel32, CharSet = CharSet.Unicode)]
private static extern IntPtr GetCommandLineW();
#endregion P/Invoke
private static readonly ConsoleLogger s_logger = new();
private static readonly ActionDispatcher s_dispatcher = ActionDispatcher.Create(s_logger);
/// <summary>
/// Program entry point. Runs in one of two modes:
/// <list type="bullet">
/// <item><description>Command-line mode: executes the JSON command(s) passed as arguments and exits.</description></item>
/// <item><description>Interactive mode (no args): reads JSON commands from stdin in a loop until "quit" or EOF.</description></item>
/// </list>
/// </summary>
private static void Main(string[] args)
{
if (args.Length > 0)
{
RunFromCommandLine();
}
else
{
RunInteractive();
}
}
/// <summary>
/// Executes JSON command(s) from command-line arguments and exits.
/// Accepts a single JSON object (<c>{"Volume":50}</c>) or an array
/// (<c>[{"Volume":50},{"Mute":true}]</c>).
/// </summary>
/// <remarks>
/// Uses raw command line via P/Invoke to preserve original quoting and spacing,
/// since the CLR args array strips quotes and splits on spaces.
/// </remarks>
private static void RunFromCommandLine()
{
string rawCmdLine = Marshal.PtrToStringUni(GetCommandLineW());
string cmdLine = StripExecutableName(rawCmdLine);
try
{
using var doc = JsonDocument.Parse(cmdLine);
var root = doc.RootElement;
if (root.ValueKind == JsonValueKind.Array)
{
foreach (var element in root.EnumerateArray())
{
var result = ExecLine(element);
Console.WriteLine(JsonSerializer.Serialize(result));
}
}
else
{
var result = ExecLine(root);
Console.WriteLine(JsonSerializer.Serialize(result));
}
}
catch (Exception ex)
{
var result = ActionResult.Fail($"Failed to parse command line: {ex.Message}");
Console.WriteLine(JsonSerializer.Serialize(result));
}
}
/// <summary>
/// Reads JSON commands from stdin line by line until "quit" or EOF.
/// This is the primary mode when autoShell is launched as a child process
/// by the TypeAgent desktop connector.
/// </summary>
private static void RunInteractive()
{
while (true)
{
string line = null;
try
{
line = Console.ReadLine();
// Null means stdin was closed (e.g., parent process exited)
if (line == null)
{
break;
}
// Each line is a JSON object with one or more command keys
using var doc = JsonDocument.Parse(line);
var root = doc.RootElement;
ActionResult result = ExecLine(root);
// Pass through the request id if present
if (root.TryGetProperty("id", out JsonElement idElement))
{
result.Id = idElement.ToString();
}
Console.WriteLine(JsonSerializer.Serialize(result));
if (result.IsQuit)
{
break;
}
}
catch (Exception ex)
{
// Always write a JSON failure to stdout so the TS sendAction
// correlation doesn't hang waiting for a response.
var errorResult = ActionResult.Fail($"Error: {ex.Message}");
// Try to extract the request id from the raw line for correlation
try
{
using var errDoc = JsonDocument.Parse(line);
if (errDoc.RootElement.TryGetProperty("id", out JsonElement errorId))
{
errorResult.Id = errorId.ToString();
}
}
catch { /* line wasn't valid JSON — send response without id */ }
Console.WriteLine(JsonSerializer.Serialize(errorResult));
s_logger.Error(ex);
}
}
}
/// <summary>
/// Strips the executable name/path from the raw command line string,
/// leaving only the arguments portion.
/// </summary>
private static string StripExecutableName(string rawCmdLine)
{
// Try quoted path first: "C:\path\autoShell.exe"
string exe = $"\"{Environment.ProcessPath}\"";
string cmdLine = rawCmdLine.Replace(exe, "");
if (cmdLine.StartsWith(exe, StringComparison.OrdinalIgnoreCase))
{
return cmdLine[exe.Length..];
}
// Try unquoted filename: autoShell.exe
var processFileName = Path.GetFileName(Environment.ProcessPath);
if (cmdLine.StartsWith(processFileName, StringComparison.OrdinalIgnoreCase))
{
return cmdLine[processFileName.Length..];
}
// Try filename without extension: autoShell
var processFileNameNoExt = Path.GetFileNameWithoutExtension(processFileName);
return cmdLine.StartsWith(processFileNameNoExt, StringComparison.OrdinalIgnoreCase)
? cmdLine[processFileNameNoExt.Length..]
: cmdLine;
}
/// <summary>
/// Dispatches a parsed JSON command object to the appropriate handler.
/// </summary>
private static ActionResult ExecLine(JsonElement root)
=> s_dispatcher.Dispatch(root);
}