|
11 | 11 | // SPDX-License-Identifier: Apache-2.0 |
12 | 12 | // |
13 | 13 | //===----------------------------------------------------------------------===// |
| 14 | +import * as child_process from "child_process"; |
14 | 15 | import type * as nodePty from "node-pty"; |
15 | 16 | import * as vscode from "vscode"; |
16 | 17 |
|
@@ -201,3 +202,105 @@ export class SwiftPtyProcess implements SwiftProcess { |
201 | 202 |
|
202 | 203 | onDidClose: vscode.Event<number | void> = this.closeHandler.event; |
203 | 204 | } |
| 205 | + |
| 206 | +/** |
| 207 | + * A {@link SwiftProcess} that spawns a child process and does not bind to stdio. |
| 208 | + * |
| 209 | + * Use this for Swift tasks that do not need to accept input, as its lighter weight and |
| 210 | + * less error prone than using a spawned node-pty process. |
| 211 | + * |
| 212 | + * Specifically node-pty on Linux suffers from a long standing issue where the last chunk |
| 213 | + * of output before a program exits is sometimes dropped, especially if that program produces |
| 214 | + * a lot of output immediately before exiting. See https://github.com/microsoft/node-pty/issues/72 |
| 215 | + */ |
| 216 | +export class ReadOnlySwiftProcess implements SwiftProcess { |
| 217 | + private readonly spawnEmitter: vscode.EventEmitter<void> = new vscode.EventEmitter<void>(); |
| 218 | + private readonly writeEmitter: vscode.EventEmitter<string> = new vscode.EventEmitter<string>(); |
| 219 | + private readonly errorEmitter: vscode.EventEmitter<Error> = new vscode.EventEmitter<Error>(); |
| 220 | + private readonly closeHandler: CloseHandler = new CloseHandler(); |
| 221 | + private disposables: vscode.Disposable[] = []; |
| 222 | + |
| 223 | + private spawnedProcess: child_process.ChildProcessWithoutNullStreams | undefined; |
| 224 | + |
| 225 | + constructor( |
| 226 | + public readonly command: string, |
| 227 | + public readonly args: string[], |
| 228 | + private readonly options: vscode.ProcessExecutionOptions = {} |
| 229 | + ) { |
| 230 | + this.disposables.push( |
| 231 | + this.spawnEmitter, |
| 232 | + this.writeEmitter, |
| 233 | + this.errorEmitter, |
| 234 | + this.closeHandler |
| 235 | + ); |
| 236 | + } |
| 237 | + |
| 238 | + spawn(): void { |
| 239 | + try { |
| 240 | + this.spawnedProcess = child_process.spawn(this.command, this.args, { |
| 241 | + cwd: this.options.cwd, |
| 242 | + env: { ...process.env, ...this.options.env }, |
| 243 | + }); |
| 244 | + this.spawnEmitter.fire(); |
| 245 | + |
| 246 | + this.spawnedProcess.stdout.on("data", data => { |
| 247 | + this.writeEmitter.fire(data.toString()); |
| 248 | + this.closeHandler.reset(); |
| 249 | + }); |
| 250 | + |
| 251 | + this.spawnedProcess.stderr.on("data", data => { |
| 252 | + this.writeEmitter.fire(data.toString()); |
| 253 | + this.closeHandler.reset(); |
| 254 | + }); |
| 255 | + |
| 256 | + this.spawnedProcess.on("error", error => { |
| 257 | + this.errorEmitter.fire(new Error(`${error}`)); |
| 258 | + this.closeHandler.handle(); |
| 259 | + }); |
| 260 | + |
| 261 | + this.spawnedProcess.once("exit", code => { |
| 262 | + this.closeHandler.handle(code ?? undefined); |
| 263 | + }); |
| 264 | + |
| 265 | + this.disposables.push( |
| 266 | + this.onDidClose(() => { |
| 267 | + this.dispose(); |
| 268 | + }) |
| 269 | + ); |
| 270 | + } catch (error) { |
| 271 | + this.errorEmitter.fire(new Error(`${error}`)); |
| 272 | + this.closeHandler.handle(); |
| 273 | + } |
| 274 | + } |
| 275 | + |
| 276 | + handleInput(_s: string): void { |
| 277 | + // Do nothing |
| 278 | + } |
| 279 | + |
| 280 | + terminate(signal?: NodeJS.Signals): void { |
| 281 | + if (!this.spawnedProcess) { |
| 282 | + return; |
| 283 | + } |
| 284 | + this.spawnedProcess.kill(signal); |
| 285 | + this.dispose(); |
| 286 | + } |
| 287 | + |
| 288 | + setDimensions(_dimensions: vscode.TerminalDimensions): void { |
| 289 | + // Do nothing |
| 290 | + } |
| 291 | + |
| 292 | + dispose(): void { |
| 293 | + this.spawnedProcess?.stdout.removeAllListeners(); |
| 294 | + this.spawnedProcess?.stderr.removeAllListeners(); |
| 295 | + this.spawnedProcess?.removeAllListeners(); |
| 296 | + this.disposables.forEach(d => d.dispose()); |
| 297 | + } |
| 298 | + |
| 299 | + onDidSpawn: vscode.Event<void> = this.spawnEmitter.event; |
| 300 | + |
| 301 | + onDidWrite: vscode.Event<string> = this.writeEmitter.event; |
| 302 | + |
| 303 | + onDidThrowError: vscode.Event<Error> = this.errorEmitter.event; |
| 304 | + |
| 305 | + onDidClose: vscode.Event<number | void> = this.closeHandler.event; |
| 306 | +} |
0 commit comments