This repository was archived by the owner on Dec 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathCargoTaskManager.ts
310 lines (287 loc) · 11.4 KB
/
CargoTaskManager.ts
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
import { join } from 'path';
import { ExtensionContext, window } from 'vscode';
import { CargoInvocationManager } from '../../CargoInvocationManager';
import { ShellProviderManager } from '../../ShellProviderManager';
import { Configuration } from '../configuration/Configuration';
import { CurrentWorkingDirectoryManager }
from '../configuration/current_working_directory_manager';
import { ChildLogger } from '../logging/child_logger';
import { CommandInvocationReason } from './CommandInvocationReason';
import { CrateType } from './CrateType';
import { CommandStartHandleResult, Helper } from './helper';
import { OutputChannelTaskManager } from './output_channel_task_manager';
import { TerminalTaskManager } from './terminal_task_manager';
import { UserDefinedArgs } from './UserDefinedArgs';
export class CargoTaskManager {
private _configuration: Configuration;
private _cargoInvocationManager: CargoInvocationManager;
private _currentWorkingDirectoryManager: CurrentWorkingDirectoryManager;
private _logger: ChildLogger;
private _outputChannelTaskManager: OutputChannelTaskManager;
private _terminalTaskManager: TerminalTaskManager;
public constructor(
context: ExtensionContext,
configuration: Configuration,
cargoInvocationManager: CargoInvocationManager,
currentWorkingDirectoryManager: CurrentWorkingDirectoryManager,
shellProviderManager: ShellProviderManager,
logger: ChildLogger,
stopCommandName: string
) {
this._configuration = configuration;
this._cargoInvocationManager = cargoInvocationManager;
this._currentWorkingDirectoryManager = currentWorkingDirectoryManager;
this._logger = logger;
this._outputChannelTaskManager = new OutputChannelTaskManager(
configuration,
logger.createChildLogger('OutputChannelTaskManager: '),
stopCommandName
);
this._terminalTaskManager = new TerminalTaskManager(
context,
configuration,
shellProviderManager
);
}
public async invokeCargoInit(crateType: CrateType, name: string, workingDirectory: string): Promise<void> {
const args = ['--name', name];
switch (crateType) {
case CrateType.Application:
args.push('--bin');
break;
case CrateType.Library:
args.push('--lib');
break;
default:
throw new Error(`Unhandled crate type=${crateType}`);
}
await this.processRequestToStartTask(
'init',
args,
workingDirectory,
true,
CommandInvocationReason.CommandExecution,
false,
false,
false
);
}
public invokeCargoBuildWithArgs(args: string[], reason: CommandInvocationReason): void {
this.runCargo('build', args, true, reason);
}
public invokeCargoBuildUsingBuildArgs(reason: CommandInvocationReason): void {
this.invokeCargoBuildWithArgs(UserDefinedArgs.getBuildArgs(), reason);
}
public invokeCargoCheckWithArgs(args: string[], reason: CommandInvocationReason): void {
this.runCargo('check', args, true, reason);
}
public invokeCargoCheckUsingCheckArgs(reason: CommandInvocationReason): void {
this.invokeCargoCheckWithArgs(UserDefinedArgs.getCheckArgs(), reason);
}
public invokeCargoClippyWithArgs(args: string[], reason: CommandInvocationReason): void {
this.runCargo('clippy', args, true, reason);
}
public invokeCargoClippyUsingClippyArgs(reason: CommandInvocationReason): void {
this.invokeCargoClippyWithArgs(UserDefinedArgs.getClippyArgs(), reason);
}
public invokeCargoDocWithArgs(args: string[], reason: CommandInvocationReason): void {
this.runCargo('doc', args, true, reason);
}
public invokeCargoDocUsingDocArgs(reason: CommandInvocationReason): void {
this.invokeCargoDocWithArgs(UserDefinedArgs.getDocArgs(), reason);
}
public async invokeCargoNew(projectName: string, isBin: boolean, workingDirectory: string): Promise<void> {
const args = [projectName, isBin ? '--bin' : '--lib'];
await this.processRequestToStartTask(
'new',
args,
workingDirectory,
true,
CommandInvocationReason.CommandExecution,
false,
false,
false
);
}
public invokeCargoRunWithArgs(args: string[], reason: CommandInvocationReason): void {
this.runCargo('run', args, true, reason);
}
public invokeCargoRunUsingRunArgs(reason: CommandInvocationReason): void {
this.invokeCargoRunWithArgs(UserDefinedArgs.getRunArgs(), reason);
}
public invokeCargoTestWithArgs(args: string[], reason: CommandInvocationReason): void {
this.runCargo('test', args, true, reason);
}
public invokeCargoTestUsingTestArgs(reason: CommandInvocationReason): void {
this.invokeCargoTestWithArgs(UserDefinedArgs.getTestArgs(), reason);
}
public invokeCargo(command: string, args: string[]): void {
this.runCargo(command, args, true, CommandInvocationReason.CommandExecution);
}
public stopTask(): void {
if (this._outputChannelTaskManager.hasRunningTask()) {
this._outputChannelTaskManager.stopRunningTask();
}
}
private async processRequestToStartTask(
command: string,
args: string[],
workingDirectory: string,
isStoppingRunningTaskAllowed: boolean,
reason: CommandInvocationReason,
shouldStartTaskInTerminal: boolean,
shouldUseUserWorkingDirectory: boolean,
shouldParseOutput: boolean
): Promise<void> {
const canStartTask = await this.processPossiblyRunningTask(
isStoppingRunningTaskAllowed,
shouldStartTaskInTerminal
);
if (!canStartTask) {
return;
}
if (shouldUseUserWorkingDirectory) {
({ args, workingDirectory } = this.processPossibleUserRequestToChangeWorkingDirectory(
args,
workingDirectory
));
}
const { executable, args: preCommandArgs } = this._cargoInvocationManager.getExecutableAndArgs();
this.startTask(
executable,
preCommandArgs,
command,
args,
workingDirectory,
reason,
shouldStartTaskInTerminal,
shouldParseOutput
);
}
private async runCargo(
command: string,
args: string[],
force: boolean,
reason: CommandInvocationReason
): Promise<void> {
let workingDirectory: string;
try {
workingDirectory = await this._currentWorkingDirectoryManager.cwd();
} catch (error) {
window.showErrorMessage(error.message);
return;
}
const shouldExecuteCargoCommandInTerminal = this._configuration.shouldExecuteCargoCommandInTerminal();
this.processRequestToStartTask(
command,
args,
workingDirectory,
force,
reason,
shouldExecuteCargoCommandInTerminal,
true,
true
);
}
/**
* Checks whether some task is running and it is, then checks whether it can be stopped
* @param isStoppingRunningTaskAllowed The flag indicating whether the currently running task
* can be stopped
* @param isPossiblyRunningTaskRunInTerminal The flag indicating whether the currently
* running task is run in the terminal
* @return The flag inidicating whether there is no running task (there was no running task or
* the running task has been stopped)
*/
private async processPossiblyRunningTask(
isStoppingRunningTaskAllowed: boolean,
isPossiblyRunningTaskRunInTerminal: boolean
): Promise<boolean> {
let hasRunningTask = false;
if (isPossiblyRunningTaskRunInTerminal) {
hasRunningTask = this._terminalTaskManager.hasRunningTask();
} else {
hasRunningTask = this._outputChannelTaskManager.hasRunningTask();
}
if (!hasRunningTask) {
return true;
}
if (!isStoppingRunningTaskAllowed) {
return false;
}
let shouldStopRunningTask = false;
const helper = new Helper(this._configuration);
const result = await helper.handleCommandStartWhenThereIsRunningCommand();
switch (result) {
case CommandStartHandleResult.IgnoreNewCommand:
break;
case CommandStartHandleResult.StopRunningCommand:
shouldStopRunningTask = true;
}
if (shouldStopRunningTask) {
if (isPossiblyRunningTaskRunInTerminal) {
this._terminalTaskManager.stopRunningTask();
} else {
this._outputChannelTaskManager.stopRunningTask();
}
return true;
} else {
return false;
}
}
private async startTask(
executable: string,
preCommandArgs: string[],
command: string,
args: string[],
cwd: string,
reason: CommandInvocationReason,
shouldExecuteCargoCommandInTerminal: boolean,
shouldParseOutput: boolean
): Promise<void> {
if (shouldExecuteCargoCommandInTerminal) {
await this._terminalTaskManager.startTask(
executable,
preCommandArgs,
command,
args,
cwd
);
} else {
// The output channel should be shown only if the user wants that.
// The only exception is checking invoked on saving the active document - in that case the output channel shouldn't be shown.
const shouldShowOutputChannel: boolean =
this._configuration.shouldShowRunningCargoTaskOutputChannel() &&
!(command === 'check' && reason === CommandInvocationReason.ActionOnSave);
await this._outputChannelTaskManager.startTask(
executable,
preCommandArgs,
command,
args,
cwd,
shouldParseOutput,
shouldShowOutputChannel
);
}
}
/**
* The user can specify some directory which Cargo commands should be run in. In this case,
* Cargo should be known whether the correct manifest is located. The function checks whether
* the user specify some directory and if it is, then adds the manifest path to the arguments
* and replaces the working directory
* @param args The arguments to change
* @param workingDirectory The current working directory
* @return The new arguments and new working directory
*/
private processPossibleUserRequestToChangeWorkingDirectory(
args: string[],
workingDirectory: string
): { args: string[], workingDirectory: string } {
const userWorkingDirectory = this._configuration.getCargoCwd();
if (userWorkingDirectory !== undefined && userWorkingDirectory !== workingDirectory) {
const manifestPath = join(workingDirectory, 'Cargo.toml');
args = ['--manifest-path', manifestPath].concat(args);
workingDirectory = userWorkingDirectory;
}
return { args, workingDirectory };
}
}