@noldova/terminal 0.0.0-stage → 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +10 -2
- package/menus.json +53 -0
- package/module.json +28 -0
- package/package.json +50 -4
- package/settings.json +83 -0
- package/src/api/index.d.ts +131 -0
- package/src/api/index.js +11 -0
- package/src/api/window.d.ts +467 -0
- package/src/api/window.js +8 -0
- package/src/enums/escape-state.js +15 -0
- package/src/enums/escape-stripper-state.js +15 -0
- package/src/enums/mark-kind.js +18 -0
- package/src/enums/shell-family.js +14 -0
- package/src/enums/system-requirement.js +11 -0
- package/src/exceptions/agent-terminal-restart.exception.js +16 -0
- package/src/exceptions/command-exited.exception.js +23 -0
- package/src/exceptions/command-failed.exception.js +13 -0
- package/src/exceptions/command-not-started.exception.js +17 -0
- package/src/exceptions/command-output-too-long.exception.js +18 -0
- package/src/exceptions/command-timed-out.exception.js +18 -0
- package/src/exceptions/command-variable-not-set.exception.js +18 -0
- package/src/exceptions/default-shell-not-found.exception.js +14 -0
- package/src/exceptions/environment-export-invalid.exception.js +14 -0
- package/src/exceptions/environment-key-unreadable.exception.js +17 -0
- package/src/exceptions/environment-unreadable.exception.js +13 -0
- package/src/exceptions/folder-not-found.exception.js +16 -0
- package/src/exceptions/host-ended.exception.js +14 -0
- package/src/exceptions/session-not-found.exception.js +18 -0
- package/src/exceptions/shell-kind-not-found.exception.js +16 -0
- package/src/exceptions/shell-not-found.exception.js +18 -0
- package/src/exceptions/shells-unreadable.exception.js +13 -0
- package/src/exceptions/stored-lines-damaged.exception.js +15 -0
- package/src/exceptions/stored-lines-unwritable.exception.js +15 -0
- package/src/exceptions/system-root-missing.exception.js +14 -0
- package/src/exceptions/terminal.exception.js +13 -0
- package/src/exceptions/unknown-method.exception.js +16 -0
- package/src/exceptions/unsupported-system.exception.js +18 -0
- package/src/exceptions/visual-studio-unreadable.exception.js +15 -0
- package/src/exceptions/wsl-distributions-unreadable.exception.js +15 -0
- package/src/interfaces/i-command-runner.js +8 -0
- package/src/interfaces/i-disposable.js +8 -0
- package/src/interfaces/i-environment-source.js +8 -0
- package/src/interfaces/i-file-system-reader.js +8 -0
- package/src/interfaces/i-file-system-writer.js +8 -0
- package/src/interfaces/i-flow-controller-listener.js +8 -0
- package/src/interfaces/i-folder-access.js +8 -0
- package/src/interfaces/i-line-process-listener.js +8 -0
- package/src/interfaces/i-line-process-starter.js +8 -0
- package/src/interfaces/i-line-process.js +8 -0
- package/src/interfaces/i-output-batcher-listener.js +8 -0
- package/src/interfaces/i-process-signaller.js +8 -0
- package/src/interfaces/i-pseudo-terminal-listener.js +8 -0
- package/src/interfaces/i-pty-exit-event.js +8 -0
- package/src/interfaces/i-pty-process.js +8 -0
- package/src/interfaces/i-pty-spawner.js +8 -0
- package/src/interfaces/i-terminal-mirror-listener.js +8 -0
- package/src/interfaces/i-terminal-session-listener.js +8 -0
- package/src/interfaces/i-waiting-options-request.js +8 -0
- package/src/models/command-listing.js +17 -0
- package/src/models/completion-candidate.js +19 -0
- package/src/models/environment-tools.js +23 -0
- package/src/models/environment-variable.js +15 -0
- package/src/models/folder-entry.js +15 -0
- package/src/models/hosted-terminal.js +116 -0
- package/src/models/mirror-line.js +27 -0
- package/src/models/mirror-snapshot.js +19 -0
- package/src/models/one-shot-launch.js +42 -0
- package/src/models/pane-progress.js +15 -0
- package/src/models/powershell-cmdlet.js +21 -0
- package/src/models/powershell-lookup.js +17 -0
- package/src/models/powershell-parameter.js +19 -0
- package/src/models/registry-value.js +15 -0
- package/src/models/request-client.js +23 -0
- package/src/models/saved-terminal.js +25 -0
- package/src/models/shell-launch.js +23 -0
- package/src/models/shell-line.js +21 -0
- package/src/models/shell-mark.js +19 -0
- package/src/models/shell-options-reply.js +17 -0
- package/src/models/shell.js +29 -0
- package/src/models/started-shell.js +21 -0
- package/src/models/system-shortfall.js +15 -0
- package/src/models/terminal-events.js +37 -0
- package/src/models/terminal-history-paths.js +15 -0
- package/src/models/terminal-prompt.js +28 -0
- package/src/models/tracked-command.js +26 -0
- package/src/models/typed-line.js +40 -0
- package/src/models/visual-studio-installation.js +22 -0
- package/src/models/waiting-run.js +25 -0
- package/src/resources.js +1725 -0
- package/src/runtime-part.js +84 -0
- package/src/services/agent-terminal.js +137 -0
- package/src/services/agent-terminals.js +55 -0
- package/src/services/bounded-reads.js +57 -0
- package/src/services/change-signal.js +33 -0
- package/src/services/command-history.js +20 -0
- package/src/services/command-runner.js +67 -0
- package/src/services/command-tracker.js +161 -0
- package/src/services/completion-source.js +316 -0
- package/src/services/context-pty-process.js +79 -0
- package/src/services/context-pty-spawner.js +26 -0
- package/src/services/deferred-pty-process.js +94 -0
- package/src/services/escape-sequence-tracker.js +126 -0
- package/src/services/escape-stripper.js +91 -0
- package/src/services/failure-text.js +15 -0
- package/src/services/file-system-writer.js +35 -0
- package/src/services/file-system.reader.js +54 -0
- package/src/services/flow-controller.js +102 -0
- package/src/services/folder-access.js +19 -0
- package/src/services/inherited-environment.js +20 -0
- package/src/services/line-process-starter.js +17 -0
- package/src/services/line-process.js +72 -0
- package/src/services/log-file.js +57 -0
- package/src/services/mac-environment.js +81 -0
- package/src/services/mac-folder-access.js +17 -0
- package/src/services/mark-scanner.js +94 -0
- package/src/services/one-shot-command.js +119 -0
- package/src/services/output-batcher.js +62 -0
- package/src/services/output-log.js +50 -0
- package/src/services/powershell-binding.js +61 -0
- package/src/services/process-signaller.js +19 -0
- package/src/services/pseudo-terminal.js +54 -0
- package/src/services/registry-export.js +108 -0
- package/src/services/resize-wakeup.js +39 -0
- package/src/services/saved-terminals.js +52 -0
- package/src/services/shared-reads.js +41 -0
- package/src/services/shell-command.js +90 -0
- package/src/services/shell-environment.js +20 -0
- package/src/services/shell-integration.js +207 -0
- package/src/services/shell-locator.js +177 -0
- package/src/services/shell-options-queue.js +34 -0
- package/src/services/shell-words.js +80 -0
- package/src/services/system-requirements.js +46 -0
- package/src/services/terminal-audience.js +83 -0
- package/src/services/terminal-history-folder.js +97 -0
- package/src/services/terminal-history-segment.js +109 -0
- package/src/services/terminal-history.js +130 -0
- package/src/services/terminal-host.js +931 -0
- package/src/services/terminal-line-reader.js +101 -0
- package/src/services/terminal-mirror.js +221 -0
- package/src/services/terminal-owner.service.js +27 -0
- package/src/services/terminal-session.js +398 -0
- package/src/services/visual-studio.reader.js +76 -0
- package/src/services/windows-environment.js +218 -0
- package/src/services/windows-folder-access.js +23 -0
- package/src/services/wsl-distribution.reader.js +49 -0
- package/src/services/xterm-headless-loader.js +14 -0
- package/src/tools/enums/command-status.js +13 -0
- package/src/tools/enums/run-method.js +11 -0
- package/src/tools/exceptions/abort-reason-unknown.exception.js +17 -0
- package/src/tools/exceptions/stale-terminal.exception.js +16 -0
- package/src/tools/interfaces/i-agent-command.js +8 -0
- package/src/tools/interfaces/i-agent-terminal.js +8 -0
- package/src/tools/interfaces/i-agent-terminals.js +8 -0
- package/src/tools/interfaces/i-output-source.js +8 -0
- package/src/tools/interfaces/i-terminal-prompt.js +8 -0
- package/src/tools/models/agent-terminal-access.js +25 -0
- package/src/tools/models/agent-terminal-opening.js +29 -0
- package/src/tools/models/command-line.js +28 -0
- package/src/tools/models/key-sequence.js +27 -0
- package/src/tools/models/output-chunk.js +18 -0
- package/src/tools/models/output-excerpt.js +31 -0
- package/src/tools/models/permission-ask.js +34 -0
- package/src/tools/models/read-cursor.js +26 -0
- package/src/tools/models/read-cursors.js +25 -0
- package/src/tools/models/terminal-open-input.js +34 -0
- package/src/tools/models/terminal-output-report.js +61 -0
- package/src/tools/models/terminal-read-input.js +39 -0
- package/src/tools/models/terminal-run-input.js +52 -0
- package/src/tools/models/terminal-target-input.js +34 -0
- package/src/tools/models/terminal-write-input.js +38 -0
- package/src/tools/models/tool-call-context.js +61 -0
- package/src/tools/services/abort-reason-reader.js +25 -0
- package/src/tools/services/permission-details.js +42 -0
- package/src/tools/services/permission-gate.js +35 -0
- package/src/tools/services/signal-race.js +24 -0
- package/src/tools/services/terminal-access-policy.js +57 -0
- package/src/tools/services/terminal-close-tool.js +57 -0
- package/src/tools/services/terminal-interrupt-tool.js +57 -0
- package/src/tools/services/terminal-list-tool.js +69 -0
- package/src/tools/services/terminal-open-tool.js +71 -0
- package/src/tools/services/terminal-read-tool.js +58 -0
- package/src/tools/services/terminal-run-tool.js +78 -0
- package/src/tools/services/terminal-text-escaper.js +40 -0
- package/src/tools/services/terminal-tool.js +47 -0
- package/src/tools/services/terminal-tools.js +60 -0
- package/src/tools/services/terminal-write-tool.js +75 -0
- package/src/tools/services/tool-failures.js +28 -0
- package/src/tools/services/tool-results.js +13 -0
|
@@ -0,0 +1,398 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright (c) Noldova.
|
|
4
|
+
*
|
|
5
|
+
* This source code is licensed under the license found in the
|
|
6
|
+
* LICENSE file in the root directory of this source tree.
|
|
7
|
+
*/
|
|
8
|
+
import { EndedBy, Resources as ProtocolResources, TerminalAttachment, TerminalCommandEnded, TerminalCwd, TerminalExit, TerminalExited, TerminalInputEdge, TerminalInputMark, TerminalOutput, TerminalPaneReference, TerminalReference, TerminalShellKind, TerminalSize, TerminalSummary, TerminalTitle } from "@noldova/terminal.protocol";
|
|
9
|
+
import { MarkKind } from "../enums/mark-kind.js";
|
|
10
|
+
import { ShellMark } from "../models/shell-mark.js";
|
|
11
|
+
import { ShellOptionsReply } from "../models/shell-options-reply.js";
|
|
12
|
+
import { TypedLine } from "../models/typed-line.js";
|
|
13
|
+
import { Resources } from "../resources.js";
|
|
14
|
+
import { ChangeSignal } from "./change-signal.js";
|
|
15
|
+
import { CommandTracker } from "./command-tracker.js";
|
|
16
|
+
import { FailureText } from "./failure-text.js";
|
|
17
|
+
import { FlowController } from "./flow-controller.js";
|
|
18
|
+
import { MarkScanner } from "./mark-scanner.js";
|
|
19
|
+
import { OutputBatcher } from "./output-batcher.js";
|
|
20
|
+
import { OutputLog } from "./output-log.js";
|
|
21
|
+
import { PseudoTerminal } from "./pseudo-terminal.js";
|
|
22
|
+
import { ResizeWakeup } from "./resize-wakeup.js";
|
|
23
|
+
import { ShellOptionsQueue } from "./shell-options-queue.js";
|
|
24
|
+
import { TerminalHistory } from "./terminal-history.js";
|
|
25
|
+
import { TerminalMirror } from "./terminal-mirror.js";
|
|
26
|
+
export class TerminalSession {
|
|
27
|
+
listener;
|
|
28
|
+
windowsPty;
|
|
29
|
+
history;
|
|
30
|
+
mirror;
|
|
31
|
+
batcher;
|
|
32
|
+
flow;
|
|
33
|
+
wakeup;
|
|
34
|
+
marksInput;
|
|
35
|
+
shellKind;
|
|
36
|
+
terminal;
|
|
37
|
+
scanner;
|
|
38
|
+
size;
|
|
39
|
+
exit = null;
|
|
40
|
+
isClosing = false;
|
|
41
|
+
receivedOffset = 0;
|
|
42
|
+
reportedCwd = null;
|
|
43
|
+
typedLines = new Set();
|
|
44
|
+
options = new ShellOptionsQueue();
|
|
45
|
+
isPromptNew = false;
|
|
46
|
+
disposal = new AbortController();
|
|
47
|
+
sessionId;
|
|
48
|
+
shellId;
|
|
49
|
+
commands = this.trackCommands();
|
|
50
|
+
text;
|
|
51
|
+
changes = new ChangeSignal();
|
|
52
|
+
hasIntegration;
|
|
53
|
+
promptLine = null;
|
|
54
|
+
constructor(sessionId, shellId, size, process, nonce, windowsPty, historyPaths, storedLimit, listener, textLimit, shellKind, marksInput) {
|
|
55
|
+
this.sessionId = sessionId;
|
|
56
|
+
this.shellKind = shellKind;
|
|
57
|
+
this.marksInput = marksInput;
|
|
58
|
+
this.shellId = shellId;
|
|
59
|
+
this.size = size;
|
|
60
|
+
this.windowsPty = windowsPty;
|
|
61
|
+
this.scanner = nonce === null ? null : new MarkScanner(nonce);
|
|
62
|
+
this.hasIntegration = nonce !== null;
|
|
63
|
+
this.listener = listener;
|
|
64
|
+
this.text = new OutputLog(textLimit);
|
|
65
|
+
this.text.subscribe(t => {
|
|
66
|
+
for (const line of this.typedLines)
|
|
67
|
+
line.read(t);
|
|
68
|
+
});
|
|
69
|
+
this.history = new TerminalHistory(historyPaths, storedLimit, () => this.flow.acknowledgeStoring(this.history.backlog));
|
|
70
|
+
this.mirror = new TerminalMirror(size, this.history, {
|
|
71
|
+
onReply: t => this.terminal.write(t),
|
|
72
|
+
onParsed: t => this.flow.acknowledgeMirror(t, this.history.backlog),
|
|
73
|
+
onTitleChanged: t => this.listener.onTitleChanged(new TerminalTitle(this.sessionId, t))
|
|
74
|
+
});
|
|
75
|
+
this.batcher = new OutputBatcher({
|
|
76
|
+
onBatch: (offset, data, inputs) => this.listener.onOutput(new TerminalOutput(this.sessionId, offset, data, this.history.stored, inputs))
|
|
77
|
+
});
|
|
78
|
+
this.flow = new FlowController({
|
|
79
|
+
onPause: () => this.terminal.pause(),
|
|
80
|
+
onResume: () => this.terminal.resume(),
|
|
81
|
+
onStalled: t => this.listener.onDetached(new TerminalPaneReference(this.sessionId, t))
|
|
82
|
+
});
|
|
83
|
+
this.wakeup = windowsPty !== null && shellKind === TerminalShellKind.Bash ? new ResizeWakeup(t => this.terminal.write(t)) : null;
|
|
84
|
+
this.terminal = this.startTerminal(process);
|
|
85
|
+
}
|
|
86
|
+
get pid() {
|
|
87
|
+
return this.terminal.pid;
|
|
88
|
+
}
|
|
89
|
+
readForegroundProcessGroupAsync() {
|
|
90
|
+
return this.terminal.readForegroundProcessGroupAsync();
|
|
91
|
+
}
|
|
92
|
+
get title() {
|
|
93
|
+
return this.mirror.title;
|
|
94
|
+
}
|
|
95
|
+
get cwd() {
|
|
96
|
+
return this.reportedCwd;
|
|
97
|
+
}
|
|
98
|
+
get shellPath() {
|
|
99
|
+
return this.commands.path;
|
|
100
|
+
}
|
|
101
|
+
get shellTemporaryFolder() {
|
|
102
|
+
return this.commands.temporaryFolder;
|
|
103
|
+
}
|
|
104
|
+
get shellModulePath() {
|
|
105
|
+
return this.commands.modulePath;
|
|
106
|
+
}
|
|
107
|
+
get shellNames() {
|
|
108
|
+
return this.commands.names;
|
|
109
|
+
}
|
|
110
|
+
get shellBuiltins() {
|
|
111
|
+
return this.commands.builtins;
|
|
112
|
+
}
|
|
113
|
+
get isShellPathReset() {
|
|
114
|
+
return this.commands.isPathReset;
|
|
115
|
+
}
|
|
116
|
+
get isShellModulePathReset() {
|
|
117
|
+
return this.commands.isModulePathReset;
|
|
118
|
+
}
|
|
119
|
+
get prompts() {
|
|
120
|
+
return this.commands.prompts;
|
|
121
|
+
}
|
|
122
|
+
get hasExited() {
|
|
123
|
+
return this.exit !== null;
|
|
124
|
+
}
|
|
125
|
+
get receivedLength() {
|
|
126
|
+
return this.receivedOffset;
|
|
127
|
+
}
|
|
128
|
+
write(data, isKey = true) {
|
|
129
|
+
if (isKey)
|
|
130
|
+
this.options.cancel();
|
|
131
|
+
this.wakeup?.beforeInput();
|
|
132
|
+
for (let lines = data.replace(Resources.bracketedPastes, "").split(Resources.lineBreaks).length - 1; lines > 0; lines--)
|
|
133
|
+
this.commands.enterLine();
|
|
134
|
+
this.terminal.write(data);
|
|
135
|
+
}
|
|
136
|
+
typeLine(text) {
|
|
137
|
+
const line = new TypedLine(this.text.endOffset, text);
|
|
138
|
+
this.typedLines.add(line);
|
|
139
|
+
this.write(text + Resources.enterKey);
|
|
140
|
+
return line;
|
|
141
|
+
}
|
|
142
|
+
async waitForStartAsync(isStarted, signal) {
|
|
143
|
+
const startedAt = Date.now();
|
|
144
|
+
await this.changes.waitUntilAsync(isStarted, Resources.integrationWaitMilliseconds, signal);
|
|
145
|
+
if (!this.hasIntegration || isStarted() || this.text.endOffset > 0)
|
|
146
|
+
return false;
|
|
147
|
+
await this.changes.waitUntilAsync(() => isStarted() || this.text.endOffset > 0, Math.max(0, Resources.shellStartMilliseconds - (Date.now() - startedAt)), signal);
|
|
148
|
+
return true;
|
|
149
|
+
}
|
|
150
|
+
async askOptionsAsync(keys) {
|
|
151
|
+
if (!this.hasIntegration || !this.commands.hasOptionsBinding || !this.commands.isWaitingAtPrompt || this.commands.runningCommand !== null
|
|
152
|
+
|| this.exit !== null)
|
|
153
|
+
return ShellOptionsReply.none;
|
|
154
|
+
const reply = this.options.askAsync(Resources.optionsMilliseconds);
|
|
155
|
+
this.wakeup?.beforeInput();
|
|
156
|
+
this.terminal.write(keys);
|
|
157
|
+
const answered = await reply;
|
|
158
|
+
if (answered.isTimedOut)
|
|
159
|
+
this.write(Resources.tabKey);
|
|
160
|
+
return answered;
|
|
161
|
+
}
|
|
162
|
+
isBracketedPasteAsync() {
|
|
163
|
+
return this.mirror.isBracketedPasteAsync();
|
|
164
|
+
}
|
|
165
|
+
readPromptLine() {
|
|
166
|
+
const promptEnd = this.commands.promptEndOffset;
|
|
167
|
+
if (promptEnd === null || this.commands.runningCommand !== null)
|
|
168
|
+
return null;
|
|
169
|
+
return this.text.readSince(promptEnd).text.trim();
|
|
170
|
+
}
|
|
171
|
+
resize(size) {
|
|
172
|
+
if (size.equals(this.size))
|
|
173
|
+
return false;
|
|
174
|
+
this.size = size;
|
|
175
|
+
this.terminal.resize(size);
|
|
176
|
+
this.watchResize();
|
|
177
|
+
this.mirror.resize(size);
|
|
178
|
+
return true;
|
|
179
|
+
}
|
|
180
|
+
async attachAsync(paneId, commands) {
|
|
181
|
+
const snapshot = await this.mirror.snapshotAsync();
|
|
182
|
+
this.flow.attach(paneId, snapshot.offset);
|
|
183
|
+
if (this.exit === null)
|
|
184
|
+
this.redraw();
|
|
185
|
+
return new TerminalAttachment(this.sessionId, snapshot.snapshot, snapshot.pending, snapshot.offset, this.size, this.mirror.title, this.cwd, this.exit, this.windowsPty, this.history.stored, snapshot.input, this.marksInput ? commands : [], this.shellKind);
|
|
186
|
+
}
|
|
187
|
+
anchorRunAsync() {
|
|
188
|
+
const prompt = this.promptLine;
|
|
189
|
+
return prompt === null ? this.mirror.anchorAsync(null) : prompt.then(t => this.mirror.anchorAsync(t));
|
|
190
|
+
}
|
|
191
|
+
readRunLinesAsync(start, end, maximum) {
|
|
192
|
+
return this.mirror.readLinesAsync(start, end, maximum);
|
|
193
|
+
}
|
|
194
|
+
readLinesAsync(start, limit) {
|
|
195
|
+
return this.history.readAsync(start, limit);
|
|
196
|
+
}
|
|
197
|
+
acknowledge(paneId, offset) {
|
|
198
|
+
this.flow.acknowledge(paneId, offset);
|
|
199
|
+
}
|
|
200
|
+
setVisible(paneId, isVisible) {
|
|
201
|
+
this.flow.setVisible(paneId, isVisible);
|
|
202
|
+
}
|
|
203
|
+
detach(paneId) {
|
|
204
|
+
this.flow.detach(paneId);
|
|
205
|
+
}
|
|
206
|
+
async endAsync() {
|
|
207
|
+
this.isClosing = true;
|
|
208
|
+
if (this.commands.hasMarks && !this.commands.isWaitingAtPrompt)
|
|
209
|
+
await this.changes.waitUntilAsync(() => this.commands.isWaitingAtPrompt || this.hasExited, Resources.promptSettleMilliseconds, this.disposal.signal);
|
|
210
|
+
return this.terminal.endAsync();
|
|
211
|
+
}
|
|
212
|
+
restart(process, nonce, size) {
|
|
213
|
+
this.wakeup?.stop();
|
|
214
|
+
this.terminal = this.startTerminal(process);
|
|
215
|
+
if (this.flow.isPaused)
|
|
216
|
+
this.terminal.pause();
|
|
217
|
+
this.scanner = nonce === null ? null : new MarkScanner(nonce);
|
|
218
|
+
this.hasIntegration = nonce !== null;
|
|
219
|
+
this.commands = this.trackCommands();
|
|
220
|
+
this.options.reset();
|
|
221
|
+
this.isPromptNew = false;
|
|
222
|
+
this.replacePromptLine(null);
|
|
223
|
+
this.typedLines.clear();
|
|
224
|
+
this.reportedCwd = null;
|
|
225
|
+
this.exit = null;
|
|
226
|
+
this.isClosing = false;
|
|
227
|
+
this.size = size;
|
|
228
|
+
this.mirror.resize(size);
|
|
229
|
+
this.insert(Resources.restartReset + Resources.lineFeed.repeat(this.size.rows) + Resources.cursorHome, true);
|
|
230
|
+
}
|
|
231
|
+
writeNote(text) {
|
|
232
|
+
this.insert(TerminalSession.toNote(text));
|
|
233
|
+
}
|
|
234
|
+
toSummary(folder, isAgentTerminal, isPanelTerminal, owner, opensTab, name, shellName) {
|
|
235
|
+
return new TerminalSummary(this.sessionId, this.shellId, folder, this.mirror.title, this.exit, isAgentTerminal, isPanelTerminal, owner, opensTab, name, shellName, this.shellKind);
|
|
236
|
+
}
|
|
237
|
+
disposeAsync() {
|
|
238
|
+
this.disposal.abort();
|
|
239
|
+
this.wakeup?.stop();
|
|
240
|
+
this.batcher.dispose();
|
|
241
|
+
this.flow.dispose();
|
|
242
|
+
this.mirror.dispose();
|
|
243
|
+
return this.history.closeAsync();
|
|
244
|
+
}
|
|
245
|
+
static toNote(text) {
|
|
246
|
+
return Resources.noteStyle + text.replace(Resources.controlCharacters, Resources.controlReplacement) + Resources.styleReset + Resources.noteLineBreak;
|
|
247
|
+
}
|
|
248
|
+
receive(data) {
|
|
249
|
+
const marks = this.scanner?.scan(data, this.receivedOffset) ?? [];
|
|
250
|
+
const edges = marks.map(t => this.marksInput ? TerminalSession.toInputEdge(t.kind) : null);
|
|
251
|
+
this.batcher.append(data, marks.flatMap((t, index) => {
|
|
252
|
+
const edge = edges[index] ?? null;
|
|
253
|
+
return edge === null ? [] : [new TerminalInputMark(t.offset - this.receivedOffset, edge)];
|
|
254
|
+
}));
|
|
255
|
+
this.flow.produce(data.length);
|
|
256
|
+
let position = 0;
|
|
257
|
+
let mirrored = 0;
|
|
258
|
+
for (const [index, mark] of marks.entries()) {
|
|
259
|
+
const end = mark.offset - this.receivedOffset;
|
|
260
|
+
this.text.append(data.slice(position, end));
|
|
261
|
+
position = end;
|
|
262
|
+
const edge = edges[index] ?? null;
|
|
263
|
+
if (mark.kind === MarkKind.PromptStart || edge !== null) {
|
|
264
|
+
this.mirror.write(data.slice(mirrored, end));
|
|
265
|
+
mirrored = end;
|
|
266
|
+
}
|
|
267
|
+
if (mark.kind === MarkKind.PromptStart)
|
|
268
|
+
this.replacePromptLine(this.mirror.anchorAsync(null));
|
|
269
|
+
if (edge === TerminalInputEdge.Start)
|
|
270
|
+
this.mirror.startInput();
|
|
271
|
+
else if (edge === TerminalInputEdge.RightPrompt)
|
|
272
|
+
this.mirror.markRightPrompt();
|
|
273
|
+
else if (edge === TerminalInputEdge.End)
|
|
274
|
+
this.endInput(mark.kind === MarkKind.OutputStart, mark.value);
|
|
275
|
+
this.applyMark(new ShellMark(mark.kind, mark.value, this.text.endOffset, mark.values));
|
|
276
|
+
}
|
|
277
|
+
this.mirror.write(data.slice(mirrored));
|
|
278
|
+
this.text.append(data.slice(position));
|
|
279
|
+
this.wakeup?.receive(data);
|
|
280
|
+
this.receivedOffset += data.length;
|
|
281
|
+
this.changes.notify();
|
|
282
|
+
}
|
|
283
|
+
replacePromptLine(line) {
|
|
284
|
+
const previous = this.promptLine;
|
|
285
|
+
this.promptLine = line;
|
|
286
|
+
void (line ?? Promise.resolve()).then(() => previous?.then(t => t.dispose()));
|
|
287
|
+
}
|
|
288
|
+
trackCommands() {
|
|
289
|
+
return new CommandTracker((start, end) => this.text.readRange(start, end));
|
|
290
|
+
}
|
|
291
|
+
insert(data, endsInput = false) {
|
|
292
|
+
const endInput = endsInput && this.marksInput;
|
|
293
|
+
this.mirror.write(data);
|
|
294
|
+
if (endInput)
|
|
295
|
+
this.endInput(false, null);
|
|
296
|
+
this.batcher.append(data, endInput ? [new TerminalInputMark(data.length, TerminalInputEdge.End)] : []);
|
|
297
|
+
this.flow.produce(data.length);
|
|
298
|
+
this.changes.notify();
|
|
299
|
+
}
|
|
300
|
+
applyMark(mark) {
|
|
301
|
+
if (mark.kind === MarkKind.Options)
|
|
302
|
+
this.options.answer(mark.values);
|
|
303
|
+
else if (mark.kind === MarkKind.OutputStart || mark.kind === MarkKind.PromptStart && this.isPromptNew)
|
|
304
|
+
this.options.reset();
|
|
305
|
+
if (mark.kind === MarkKind.CommandEnd || mark.kind === MarkKind.Property)
|
|
306
|
+
this.isPromptNew = true;
|
|
307
|
+
else if (mark.kind === MarkKind.PromptStart)
|
|
308
|
+
this.isPromptNew = false;
|
|
309
|
+
this.commands.apply(mark);
|
|
310
|
+
this.settleTypedLines(mark);
|
|
311
|
+
if (mark.kind === MarkKind.CommandEnd) {
|
|
312
|
+
const exitCode = this.commands.lastCommand?.exitCode ?? null;
|
|
313
|
+
this.listener.onCommandEnded(new TerminalCommandEnded(this.sessionId, exitCode !== null && exitCode >= ProtocolResources.minimumExitCode && exitCode <= ProtocolResources.maximumExitCode ? exitCode : null));
|
|
314
|
+
}
|
|
315
|
+
else if (mark.kind === MarkKind.PromptStart)
|
|
316
|
+
this.listener.onPromptStarted(new TerminalReference(this.sessionId));
|
|
317
|
+
const cwd = this.commands.cwd;
|
|
318
|
+
if (cwd === null || cwd === this.reportedCwd || cwd.trim().length === 0 || cwd.length > ProtocolResources.maximumPathLength)
|
|
319
|
+
return;
|
|
320
|
+
this.reportedCwd = cwd;
|
|
321
|
+
this.listener.onCwdChanged(new TerminalCwd(this.sessionId, cwd));
|
|
322
|
+
}
|
|
323
|
+
settleTypedLines(mark) {
|
|
324
|
+
const command = mark.kind === MarkKind.OutputStart ? this.commands.runningCommand
|
|
325
|
+
: mark.kind === MarkKind.CommandEnd ? this.commands.lastCommand : null;
|
|
326
|
+
for (const line of this.typedLines) {
|
|
327
|
+
if (command !== null)
|
|
328
|
+
line.command = command;
|
|
329
|
+
else if (mark.kind === MarkKind.PromptStart)
|
|
330
|
+
line.prompt(mark.offset);
|
|
331
|
+
if (!line.isOpen)
|
|
332
|
+
this.typedLines.delete(line);
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
startTerminal(process) {
|
|
336
|
+
const terminal = new PseudoTerminal(process, {
|
|
337
|
+
onData: t => {
|
|
338
|
+
if (this.terminal === terminal)
|
|
339
|
+
this.receive(t);
|
|
340
|
+
},
|
|
341
|
+
onExit: t => {
|
|
342
|
+
if (this.terminal === terminal)
|
|
343
|
+
this.end(t);
|
|
344
|
+
}
|
|
345
|
+
});
|
|
346
|
+
return terminal;
|
|
347
|
+
}
|
|
348
|
+
end(exitCode) {
|
|
349
|
+
this.wakeup?.stop();
|
|
350
|
+
this.options.reset();
|
|
351
|
+
this.batcher.flush();
|
|
352
|
+
this.exit = new TerminalExit(exitCode, this.isClosing ? EndedBy.Close : EndedBy.Exit);
|
|
353
|
+
this.listener.onExited(new TerminalExited(this.sessionId, this.exit));
|
|
354
|
+
this.changes.notify();
|
|
355
|
+
}
|
|
356
|
+
redraw() {
|
|
357
|
+
if (this.size.rows < 2)
|
|
358
|
+
return;
|
|
359
|
+
this.terminal.resize(new TerminalSize(this.size.columns, this.size.rows - 1));
|
|
360
|
+
this.terminal.resize(this.size);
|
|
361
|
+
this.watchResize();
|
|
362
|
+
}
|
|
363
|
+
watchResize() {
|
|
364
|
+
if (this.commands.isWaitingAtPrompt)
|
|
365
|
+
this.wakeup?.watch();
|
|
366
|
+
else
|
|
367
|
+
this.wakeup?.stop();
|
|
368
|
+
}
|
|
369
|
+
endInput(startsCommand, command) {
|
|
370
|
+
void this.mirror.endInput().then(t => {
|
|
371
|
+
if (startsCommand && t !== null)
|
|
372
|
+
this.listener.onCommandStarted(TerminalSession.toCommand(command) ?? t);
|
|
373
|
+
}).catch((error) => this.listener.onFailed(this.sessionId, FailureText.read(error)));
|
|
374
|
+
}
|
|
375
|
+
static toCommand(value) {
|
|
376
|
+
if (value?.startsWith(Resources.commandProperty) !== true)
|
|
377
|
+
return null;
|
|
378
|
+
const text = value.slice(Resources.commandProperty.length);
|
|
379
|
+
const end = text.search(Resources.terminalSessionLineBreak);
|
|
380
|
+
const command = (end < 0 ? text : text.slice(0, end)).trim().slice(0, ProtocolResources.maximumSuggestionLength).trim();
|
|
381
|
+
return command.length === 0 ? null : command;
|
|
382
|
+
}
|
|
383
|
+
static toInputEdge(kind) {
|
|
384
|
+
switch (kind) {
|
|
385
|
+
case MarkKind.PromptEnd:
|
|
386
|
+
return TerminalInputEdge.Start;
|
|
387
|
+
case MarkKind.RightPrompt:
|
|
388
|
+
return TerminalInputEdge.RightPrompt;
|
|
389
|
+
case MarkKind.ContinuationEnd:
|
|
390
|
+
return TerminalInputEdge.Continuation;
|
|
391
|
+
case MarkKind.Property:
|
|
392
|
+
case MarkKind.Options:
|
|
393
|
+
return null;
|
|
394
|
+
default:
|
|
395
|
+
return TerminalInputEdge.End;
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright (c) Noldova.
|
|
4
|
+
*
|
|
5
|
+
* This source code is licensed under the license found in the
|
|
6
|
+
* LICENSE file in the root directory of this source tree.
|
|
7
|
+
*/
|
|
8
|
+
import path from "node:path";
|
|
9
|
+
import { ArgumentException } from "@noldova/context.exceptions";
|
|
10
|
+
import { JsonException, JsonReader } from "@noldova/context.json";
|
|
11
|
+
import { CommandFailedException } from "../exceptions/command-failed.exception.js";
|
|
12
|
+
import { VisualStudioUnreadableException } from "../exceptions/visual-studio-unreadable.exception.js";
|
|
13
|
+
import { VisualStudioInstallation } from "../models/visual-studio-installation.js";
|
|
14
|
+
import { Resources } from "../resources.js";
|
|
15
|
+
export class VisualStudioReader {
|
|
16
|
+
runner;
|
|
17
|
+
fileSystem;
|
|
18
|
+
vswhere;
|
|
19
|
+
constructor(runner, fileSystem, programFilesX86) {
|
|
20
|
+
this.runner = runner;
|
|
21
|
+
this.fileSystem = fileSystem;
|
|
22
|
+
this.vswhere = programFilesX86 === undefined || programFilesX86.length === 0 ? null : path.win32.join(programFilesX86, ...Resources.vswherePath);
|
|
23
|
+
}
|
|
24
|
+
async readAsync() {
|
|
25
|
+
if (this.vswhere === null || !await this.fileSystem.isFileAsync(this.vswhere))
|
|
26
|
+
return [];
|
|
27
|
+
let output;
|
|
28
|
+
try {
|
|
29
|
+
output = await this.runner.runAsync(this.vswhere, Resources.vswhereArguments, Resources.shellListMilliseconds);
|
|
30
|
+
}
|
|
31
|
+
catch (error) {
|
|
32
|
+
if (error instanceof CommandFailedException)
|
|
33
|
+
throw new VisualStudioUnreadableException(error);
|
|
34
|
+
throw error;
|
|
35
|
+
}
|
|
36
|
+
return VisualStudioReader.parse(output);
|
|
37
|
+
}
|
|
38
|
+
static parse(output) {
|
|
39
|
+
let value;
|
|
40
|
+
try {
|
|
41
|
+
value = JSON.parse(output);
|
|
42
|
+
}
|
|
43
|
+
catch (error) {
|
|
44
|
+
if (error instanceof SyntaxError)
|
|
45
|
+
throw new VisualStudioUnreadableException(error);
|
|
46
|
+
throw error;
|
|
47
|
+
}
|
|
48
|
+
if (!Array.isArray(value))
|
|
49
|
+
throw new VisualStudioUnreadableException();
|
|
50
|
+
return value.flatMap((t) => VisualStudioReader.parseInstance(t));
|
|
51
|
+
}
|
|
52
|
+
static parseInstance(value) {
|
|
53
|
+
let instanceId;
|
|
54
|
+
let name;
|
|
55
|
+
let installation;
|
|
56
|
+
try {
|
|
57
|
+
const reader = JsonReader.fromValue(value);
|
|
58
|
+
instanceId = reader.readNonBlankString(Resources.vswhereInstanceIdField);
|
|
59
|
+
name = reader.readNonBlankString(Resources.vswhereDisplayNameField);
|
|
60
|
+
installation = reader.readNonBlankString(Resources.vswhereInstallationPathField);
|
|
61
|
+
}
|
|
62
|
+
catch (error) {
|
|
63
|
+
if (error instanceof JsonException)
|
|
64
|
+
throw new VisualStudioUnreadableException(error);
|
|
65
|
+
throw error;
|
|
66
|
+
}
|
|
67
|
+
try {
|
|
68
|
+
return [new VisualStudioInstallation(instanceId, name, installation)];
|
|
69
|
+
}
|
|
70
|
+
catch (error) {
|
|
71
|
+
if (error instanceof ArgumentException)
|
|
72
|
+
return [];
|
|
73
|
+
throw error;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright (c) Noldova.
|
|
4
|
+
*
|
|
5
|
+
* This source code is licensed under the license found in the
|
|
6
|
+
* LICENSE file in the root directory of this source tree.
|
|
7
|
+
*/
|
|
8
|
+
import path from "node:path";
|
|
9
|
+
import { CommandExitedException } from "../exceptions/command-exited.exception.js";
|
|
10
|
+
import { EnvironmentKeyUnreadableException } from "../exceptions/environment-key-unreadable.exception.js";
|
|
11
|
+
import { EnvironmentExportInvalidException } from "../exceptions/environment-export-invalid.exception.js";
|
|
12
|
+
import { SystemRootMissingException } from "../exceptions/system-root-missing.exception.js";
|
|
13
|
+
import { EnvironmentVariable } from "../models/environment-variable.js";
|
|
14
|
+
import { Resources } from "../resources.js";
|
|
15
|
+
import { FailureText } from "./failure-text.js";
|
|
16
|
+
import { RegistryExport } from "./registry-export.js";
|
|
17
|
+
export class WindowsEnvironment {
|
|
18
|
+
environment;
|
|
19
|
+
tools;
|
|
20
|
+
log;
|
|
21
|
+
reg;
|
|
22
|
+
ending = Promise.withResolvers();
|
|
23
|
+
reading = null;
|
|
24
|
+
folder = null;
|
|
25
|
+
readCount = 0;
|
|
26
|
+
ended = false;
|
|
27
|
+
constructor(environment, tools, log) {
|
|
28
|
+
const systemRoot = environment[Resources.systemRootVariable];
|
|
29
|
+
this.environment = environment;
|
|
30
|
+
this.tools = tools;
|
|
31
|
+
this.log = log;
|
|
32
|
+
this.reg = systemRoot === undefined || systemRoot.length === 0 ? null : path.win32.join(systemRoot, ...Resources.regPath);
|
|
33
|
+
}
|
|
34
|
+
start() {
|
|
35
|
+
void this.readAsync();
|
|
36
|
+
}
|
|
37
|
+
readAsync() {
|
|
38
|
+
if (this.ended)
|
|
39
|
+
return Promise.resolve(this.environment);
|
|
40
|
+
if (this.reading === null) {
|
|
41
|
+
const reading = Promise.race([this.readNowAsync(), this.ending.promise.then(() => this.environment)]);
|
|
42
|
+
this.reading = reading;
|
|
43
|
+
void reading.then(() => {
|
|
44
|
+
this.reading = null;
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
return this.reading;
|
|
48
|
+
}
|
|
49
|
+
end() {
|
|
50
|
+
this.ended = true;
|
|
51
|
+
this.ending.resolve();
|
|
52
|
+
const folder = this.folder;
|
|
53
|
+
this.folder = null;
|
|
54
|
+
void folder?.then(t => this.tools.writer.removeFolderAsync(t)).catch((error) => this.logCleanupFailure(error));
|
|
55
|
+
}
|
|
56
|
+
async readNowAsync() {
|
|
57
|
+
try {
|
|
58
|
+
const reg = this.reg;
|
|
59
|
+
if (reg === null)
|
|
60
|
+
throw new SystemRootMissingException();
|
|
61
|
+
const folder = await this.prepareFolderAsync();
|
|
62
|
+
if (folder === null || this.ended)
|
|
63
|
+
return this.environment;
|
|
64
|
+
const read = ++this.readCount;
|
|
65
|
+
try {
|
|
66
|
+
const machine = await this.readScopeAsync(reg, Resources.machineEnvironmentKey, folder, `${Resources.windowsEnvironmentMachineFilePrefix}${read}`);
|
|
67
|
+
if (this.ended)
|
|
68
|
+
return this.environment;
|
|
69
|
+
const user = await this.readScopeAsync(reg, Resources.userEnvironmentKey, folder, `${Resources.windowsEnvironmentUserFilePrefix}${read}`);
|
|
70
|
+
return WindowsEnvironment.build(this.environment, machine, user);
|
|
71
|
+
}
|
|
72
|
+
finally {
|
|
73
|
+
if (this.ended)
|
|
74
|
+
await this.tools.writer.removeFolderAsync(folder).catch((error) => this.logCleanupFailure(error));
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
catch (error) {
|
|
78
|
+
this.log.write(Resources.formatEnvironmentUnread(FailureText.read(error)));
|
|
79
|
+
return this.environment;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
async prepareFolderAsync() {
|
|
83
|
+
const folder = await this.ensureFolderAsync();
|
|
84
|
+
if (folder === null || await this.tools.reader.isFolderAsync(folder))
|
|
85
|
+
return folder;
|
|
86
|
+
this.folder = null;
|
|
87
|
+
return await this.ensureFolderAsync();
|
|
88
|
+
}
|
|
89
|
+
async ensureFolderAsync() {
|
|
90
|
+
if (this.ended)
|
|
91
|
+
return null;
|
|
92
|
+
this.folder ??= this.createFolderAsync();
|
|
93
|
+
try {
|
|
94
|
+
return await this.folder;
|
|
95
|
+
}
|
|
96
|
+
catch (error) {
|
|
97
|
+
this.folder = null;
|
|
98
|
+
throw error;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
async createFolderAsync() {
|
|
102
|
+
const folder = await this.tools.writer.createPrivateFolderAsync(path.join(this.tools.temporaryFolder, Resources.environmentFolderPrefix));
|
|
103
|
+
try {
|
|
104
|
+
await this.tools.access?.restrictAsync(folder);
|
|
105
|
+
}
|
|
106
|
+
catch (error) {
|
|
107
|
+
await this.tools.writer.removeFolderAsync(folder).catch((removalError) => this.logCleanupFailure(removalError));
|
|
108
|
+
throw error;
|
|
109
|
+
}
|
|
110
|
+
return folder;
|
|
111
|
+
}
|
|
112
|
+
async readScopeAsync(reg, key, folder, name) {
|
|
113
|
+
let file = path.win32.join(folder, `${name}${Resources.windowsEnvironmentExportExtension}`);
|
|
114
|
+
const files = [file];
|
|
115
|
+
try {
|
|
116
|
+
try {
|
|
117
|
+
await this.exportAsync(reg, key, file);
|
|
118
|
+
}
|
|
119
|
+
catch (error) {
|
|
120
|
+
if (!(error instanceof CommandExitedException) || error.exitCode === null)
|
|
121
|
+
throw new EnvironmentKeyUnreadableException(key, error);
|
|
122
|
+
const isPresent = await this.isPresentAsync(reg, key).catch(() => {
|
|
123
|
+
throw new EnvironmentKeyUnreadableException(key, error);
|
|
124
|
+
});
|
|
125
|
+
if (!isPresent && key === Resources.userEnvironmentKey)
|
|
126
|
+
return new Map();
|
|
127
|
+
if (!isPresent)
|
|
128
|
+
throw new EnvironmentKeyUnreadableException(key, error);
|
|
129
|
+
this.log.write(Resources.formatEnvironmentExportRetried(key, error.errorText));
|
|
130
|
+
await this.waitAsync(Resources.environmentExportRetryMilliseconds);
|
|
131
|
+
if (this.ended)
|
|
132
|
+
return new Map();
|
|
133
|
+
file = path.win32.join(folder, `${name}${Resources.windowsEnvironmentRetryFileSuffix}${Resources.windowsEnvironmentExportExtension}`);
|
|
134
|
+
files.push(file);
|
|
135
|
+
await this.exportAsync(reg, key, file).catch((retryError) => {
|
|
136
|
+
throw new EnvironmentKeyUnreadableException(key, retryError);
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
const bytes = await this.tools.reader.readBytesOrNullAsync(file);
|
|
140
|
+
if (bytes === null)
|
|
141
|
+
throw new EnvironmentExportInvalidException();
|
|
142
|
+
return RegistryExport.parse(new TextDecoder(Resources.registryExportUtf16Label).decode(bytes));
|
|
143
|
+
}
|
|
144
|
+
finally {
|
|
145
|
+
for (const written of files)
|
|
146
|
+
await this.tools.writer.removeFileAsync(written).catch((error) => this.logCleanupFailure(error));
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
async exportAsync(reg, key, file) {
|
|
150
|
+
await this.tools.runner.runAsync(reg, [Resources.regExportArgument, key, file, Resources.regOverwriteArgument], Resources.environmentReadMilliseconds);
|
|
151
|
+
}
|
|
152
|
+
async isPresentAsync(reg, key) {
|
|
153
|
+
try {
|
|
154
|
+
await this.tools.runner.runAsync(reg, [Resources.regQueryArgument, key], Resources.environmentReadMilliseconds);
|
|
155
|
+
return true;
|
|
156
|
+
}
|
|
157
|
+
catch (error) {
|
|
158
|
+
if (error instanceof CommandExitedException && error.exitCode !== null)
|
|
159
|
+
return false;
|
|
160
|
+
throw error;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
async waitAsync(milliseconds) {
|
|
164
|
+
let timer;
|
|
165
|
+
await Promise.race([new Promise(resolve => {
|
|
166
|
+
timer = setTimeout(resolve, milliseconds);
|
|
167
|
+
}), this.ending.promise]);
|
|
168
|
+
clearTimeout(timer);
|
|
169
|
+
}
|
|
170
|
+
logCleanupFailure(error) {
|
|
171
|
+
this.log.write(Resources.formatEnvironmentCleanupFailed(FailureText.read(error)));
|
|
172
|
+
}
|
|
173
|
+
static build(environment, machine, user) {
|
|
174
|
+
const variables = new Map();
|
|
175
|
+
for (const [name, value] of Object.entries(environment)) {
|
|
176
|
+
if (value !== undefined)
|
|
177
|
+
variables.set(name.toUpperCase(), new EnvironmentVariable(name, value));
|
|
178
|
+
}
|
|
179
|
+
const machinePath = WindowsEnvironment.apply(variables, machine);
|
|
180
|
+
const userPath = WindowsEnvironment.apply(variables, user);
|
|
181
|
+
if (machinePath !== undefined && userPath !== undefined)
|
|
182
|
+
variables.set(Resources.pathVariable, new EnvironmentVariable(machinePath.name, `${machinePath.value}${path.win32.delimiter}${userPath.value}`));
|
|
183
|
+
return Object.fromEntries([...variables.values()].map(t => [t.name, t.value]));
|
|
184
|
+
}
|
|
185
|
+
static apply(variables, scope) {
|
|
186
|
+
let scopePath;
|
|
187
|
+
for (const isExpandedPass of [false, true]) {
|
|
188
|
+
for (const [name, value] of scope) {
|
|
189
|
+
if (value.isExpandable !== isExpandedPass)
|
|
190
|
+
continue;
|
|
191
|
+
const variable = new EnvironmentVariable(name, value.isExpandable ? WindowsEnvironment.expand(value.text, variables) : value.text);
|
|
192
|
+
variables.set(name.toUpperCase(), variable);
|
|
193
|
+
if (name.toUpperCase() === Resources.pathVariable)
|
|
194
|
+
scopePath = variable;
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
return scopePath;
|
|
198
|
+
}
|
|
199
|
+
static expand(value, variables) {
|
|
200
|
+
let text = "";
|
|
201
|
+
let index = 0;
|
|
202
|
+
for (;;) {
|
|
203
|
+
const start = value.indexOf(Resources.windowsEnvironmentVariableDelimiter, index);
|
|
204
|
+
const end = start === -1 ? -1 : value.indexOf(Resources.windowsEnvironmentVariableDelimiter, start + 1);
|
|
205
|
+
if (end === -1)
|
|
206
|
+
return text + value.slice(index);
|
|
207
|
+
const variable = variables.get(value.slice(start + 1, end).toUpperCase());
|
|
208
|
+
if (variable === undefined) {
|
|
209
|
+
text += value.slice(index, end);
|
|
210
|
+
index = end;
|
|
211
|
+
}
|
|
212
|
+
else {
|
|
213
|
+
text += value.slice(index, start) + variable.value;
|
|
214
|
+
index = end + 1;
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
}
|