-
Notifications
You must be signed in to change notification settings - Fork 29.7k
/
commands.contribution.ts
156 lines (135 loc) · 4.66 KB
/
commands.contribution.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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as nls from 'vs/nls';
import { Action2, registerAction2 } from 'vs/platform/actions/common/actions';
import { ICommandService } from 'vs/platform/commands/common/commands';
import { ServicesAccessor } from 'vs/platform/instantiation/common/instantiation';
import { ILogService } from 'vs/platform/log/common/log';
import { INotificationService } from 'vs/platform/notification/common/notification';
type RunnableCommand = string | { command: string; args: any[] };
type CommandArgs = {
commands: RunnableCommand[];
};
/** Runs several commands passed to it as an argument */
class RunCommands extends Action2 {
constructor() {
super({
id: 'runCommands',
title: { value: nls.localize('runCommands', "Run Commands"), original: 'Run Commands' },
f1: false,
description: {
description: nls.localize('runCommands.description', "Run several commands"),
args: [
{
name: 'args',
schema: {
type: 'object',
required: ['commands'],
properties: {
commands: {
type: 'array',
description: nls.localize('runCommands.commands', "Commands to run"),
items: {
anyOf: [
{
$ref: 'vscode://schemas/keybindings#/definitions/commandNames'
},
{
type: 'string',
},
{
type: 'object',
required: ['command'],
properties: {
command: {
'anyOf': [
{
$ref: 'vscode://schemas/keybindings#/definitions/commandNames'
},
{
type: 'string'
},
]
}
},
$ref: 'vscode://schemas/keybindings#/definitions/commandsSchemas'
}
]
}
}
}
}
}
]
}
});
}
// dev decisions:
// - this command takes a single argument-object because
// - keybinding definitions don't allow running commands with several arguments
// - and we want to be able to take on different other arguments in future, e.g., `runMode : 'serial' | 'concurrent'`
async run(accessor: ServicesAccessor, args: unknown) {
const notificationService = accessor.get(INotificationService);
if (!this._isCommandArgs(args)) {
notificationService.error(nls.localize('runCommands.invalidArgs', "'runCommands' has received an argument with incorrect type. Please, review the argument passed to the command."));
return;
}
if (args.commands.length === 0) {
notificationService.warn(nls.localize('runCommands.noCommandsToRun', "'runCommands' has not received commands to run. Did you forget to pass commands in the 'runCommands' argument?"));
return;
}
const commandService = accessor.get(ICommandService);
const logService = accessor.get(ILogService);
let i = 0;
try {
for (; i < args.commands.length; ++i) {
const cmd = args.commands[i];
logService.debug(`runCommands: executing ${i}-th command: ${JSON.stringify(cmd)}`);
const r = await this._runCommand(commandService, cmd);
logService.debug(`runCommands: executed ${i}-th command with return value: ${JSON.stringify(r)}`);
}
} catch (err) {
logService.debug(`runCommands: executing ${i}-th command resulted in an error: ${err instanceof Error ? err.message : JSON.stringify(err)}`);
notificationService.error(err);
}
}
private _isCommandArgs(args: unknown): args is CommandArgs {
if (!args || typeof args !== 'object') {
return false;
}
if (!('commands' in args) || !Array.isArray(args.commands)) {
return false;
}
for (const cmd of args.commands) {
if (typeof cmd === 'string') {
continue;
}
if (typeof cmd === 'object' && typeof cmd.command === 'string') {
continue;
}
return false;
}
return true;
}
private _runCommand(commandService: ICommandService, cmd: RunnableCommand) {
let commandID: string, commandArgs;
if (typeof cmd === 'string') {
commandID = cmd;
} else {
commandID = cmd.command;
commandArgs = cmd.args;
}
if (commandArgs === undefined) {
return commandService.executeCommand(commandID);
} else {
if (Array.isArray(commandArgs)) {
return commandService.executeCommand(commandID, ...commandArgs);
} else {
return commandService.executeCommand(commandID, commandArgs);
}
}
}
}
registerAction2(RunCommands);