-
-
Notifications
You must be signed in to change notification settings - Fork 201
/
main.js
324 lines (290 loc) · 9.72 KB
/
main.js
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/**
* Copyright (c) 2017-present PlatformIO <contact@platformio.org>
* All rights reserved.
*
* This source code is licensed under the license found in the LICENSE file in
* the root directory of this source tree.
*/
import * as misc from './misc';
import * as pioNodeHelpers from 'platformio-node-helpers';
import * as piodebug from 'platformio-vscode-debug';
import * as utils from './utils';
import InstallationManager from './installer/manager';
import PIOHome from './home';
import PIOReleaseNotes from './release-notes';
import PIOTerminal from './terminal';
import PIOToolbar from './toolbar';
import ProjectManager from './project/manager';
import QuickAccessTreeProvider from './views/quick-access-tree';
import fs from 'fs-plus';
import { getPIOProjectDirs } from './project/helpers';
import vscode from 'vscode';
class PlatformIOVSCodeExtension {
constructor() {
this.context = undefined;
this.pioTerm = undefined;
this.pioHome = undefined;
this.projectManager = undefined;
this.subscriptions = [];
this._enterpriseSettings = undefined;
}
async activate(context) {
this.context = context;
this.pioHome = new PIOHome();
this.pioTerm = new PIOTerminal();
this.subscriptions.push(this.pioHome, this.pioTerm, new PIOReleaseNotes());
const hasPIOProject = getPIOProjectDirs().length > 0;
// dump global state
console.info(
'PlatformIO IDE Global State',
context.globalState.keys().reduce((state, key) => {
state[key] = context.globalState.get(key);
return state;
}, {}),
);
// temporary workaround for https://github.com/Microsoft/vscode/issues/58348
if (
!vscode.workspace
.getConfiguration('extensions')
.has('showRecommendationsOnlyOnDemand')
) {
vscode.workspace
.getConfiguration('extensions')
.update('showRecommendationsOnlyOnDemand', true);
}
this.patchOSEnviron();
await this.startInstaller(!hasPIOProject);
this.subscriptions.push(this.handleUseDevelopmentPIOCoreConfiguration());
vscode.commands.executeCommand('setContext', 'pioCoreReady', true);
if (typeof this.getEnterpriseSetting('onPIOCoreReady') === 'function') {
await this.getEnterpriseSetting('onPIOCoreReady')();
}
this.subscriptions.push(
vscode.window.registerTreeDataProvider(
'platformio-ide.quickAccess',
new QuickAccessTreeProvider(),
),
);
this.registerGlobalCommands();
if (!hasPIOProject) {
this.subscriptions.push(
new PIOToolbar({ filterCommands: ['platformio-ide.showHome'] }),
);
return;
}
vscode.commands.executeCommand('setContext', 'pioProjectReady', true);
this.subscriptions.push(
new PIOToolbar({
ignoreCommands: this.getEnterpriseSetting('ignoreToolbarCommands'),
}),
);
this.initDebug();
this.projectManager = new ProjectManager();
this.subscriptions.push(this.projectManager);
this.startPIOHome();
misc.maybeRateExtension();
misc.warnAboutConflictedExtensions();
this.subscriptions.push(
vscode.window.onDidChangeActiveTextEditor((editor) =>
misc.warnAboutInoFile(editor),
),
);
}
getConfiguration(id) {
return vscode.workspace.getConfiguration('platformio-ide').get(id);
}
loadEnterpriseSettings() {
const ext = vscode.extensions.all.find(
(item) =>
item.id.startsWith('platformio.') &&
item.id !== 'platformio.platformio-ide' &&
item.isActive,
);
return ext && ext.exports ? ext.exports.settings : undefined;
}
getEnterpriseSetting(id, defaultValue = undefined) {
if (!this._enterpriseSettings) {
this._enterpriseSettings = this.loadEnterpriseSettings();
}
if (this._enterpriseSettings && id in this._enterpriseSettings) {
return this._enterpriseSettings[id];
}
return defaultValue;
}
patchOSEnviron() {
const extraVars = {
PLATFORMIO_IDE: utils.getIDEVersion(),
};
// handle HTTP proxy settings
const http_proxy = vscode.workspace.getConfiguration('http').get('proxy');
if (http_proxy && !process.env.HTTP_PROXY && !process.env.http_proxy) {
extraVars['HTTP_PROXY'] = http_proxy;
}
if (http_proxy && !process.env.HTTPS_PROXY && !process.env.https_proxy) {
extraVars['HTTPS_PROXY'] = http_proxy;
}
if (!vscode.workspace.getConfiguration('http').get('proxyStrictSSL')) {
extraVars['PLATFORMIO_SETTING_ENABLE_PROXY_STRICT_SSL'] = 'false';
}
if (this.getConfiguration('customPyPiIndexUrl')) {
extraVars['PIP_INDEX_URL'] = this.getConfiguration('customPyPiIndexUrl');
}
pioNodeHelpers.proc.patchOSEnviron({
caller: 'vscode',
extraPath: this.getConfiguration('customPATH'),
extraVars,
});
}
async startInstaller(disableAutoUpdates) {
const im = new InstallationManager(disableAutoUpdates);
if (im.locked()) {
vscode.window.showInformationMessage(
'PlatformIO IDE installation has been suspended, because PlatformIO ' +
'IDE Installer is already started in another window.',
);
return;
}
const doInstall = await vscode.window.withProgress(
{
location: vscode.ProgressLocation.Window,
title: 'PlatformIO',
},
async (progress) => {
progress.report({
message: 'Initializing PlatformIO Core...',
});
try {
return !(await im.check());
} catch (err) {}
return true;
},
);
if (!doInstall) {
return;
}
return await vscode.window.withProgress(
{
location: vscode.ProgressLocation.Notification,
title: 'PlatformIO Installer',
},
async (progress) => {
progress.report({
message: 'Installing PlatformIO IDE...',
});
const outputChannel = vscode.window.createOutputChannel(
'PlatformIO Installation',
);
outputChannel.show();
outputChannel.appendLine('Installing PlatformIO IDE...');
outputChannel.appendLine(
'It may take a few minutes depending on your connection speed',
);
outputChannel.appendLine(
'Please do not close this window and do not ' +
'open other folders until this process is completed.',
);
outputChannel.appendLine(
'\nDebugging information is available via VSCode > Help > Toggle Developer Tools > Console.',
);
try {
im.lock();
await im.install(progress);
outputChannel.appendLine('PlatformIO IDE installed successfully.\n');
outputChannel.appendLine('Please restart VSCode.');
const action = 'Reload Now';
const selected = await vscode.window.showInformationMessage(
'PlatformIO IDE has been successfully installed! Please reload window',
action,
);
if (selected === action) {
vscode.commands.executeCommand('workbench.action.reloadWindow');
}
} catch (err) {
outputChannel.appendLine('Failed to install PlatformIO IDE.');
utils.notifyError('Installation Manager', err);
} finally {
im.unlock();
}
im.destroy();
return true;
},
);
}
async startPIOHome() {
if (
this.getConfiguration('disablePIOHomeStartup') ||
!pioNodeHelpers.home.showAtStartup('vscode')
) {
return;
}
vscode.commands.executeCommand('platformio-ide.showHome');
}
registerGlobalCommands() {
this.subscriptions.push(
vscode.commands.registerCommand('platformio-ide.showHome', (startUrl) =>
this.pioHome.toggle(startUrl),
),
vscode.commands.registerCommand('platformio-ide.newTerminal', () =>
this.pioTerm.new().show(),
),
vscode.commands.registerCommand('platformio-ide.openPIOCoreCLI', () =>
this.pioTerm.sendText('pio --help'),
),
vscode.commands.registerCommand('platformio-ide.runPIOCoreCommand', (cmd) =>
this.pioTerm.sendText(cmd),
),
vscode.commands.registerCommand('platformio-ide.startDebugging', () => {
vscode.commands.executeCommand('workbench.view.debug');
vscode.commands.executeCommand('workbench.debug.action.toggleRepl');
vscode.commands.executeCommand('workbench.action.debug.start');
}),
vscode.commands.registerCommand('platformio-ide.upgradeCore', () =>
this.pioTerm.sendText('pio upgrade'),
),
);
}
initDebug() {
piodebug.activate(this.context);
}
handleUseDevelopmentPIOCoreConfiguration() {
return vscode.workspace.onDidChangeConfiguration(async (e) => {
if (
!e.affectsConfiguration('platformio-ide.useDevelopmentPIOCore') ||
!this.getConfiguration('useBuiltinPIOCore')
) {
return;
}
const envDir = pioNodeHelpers.core.getEnvDir();
if (!envDir || !fs.isDirectorySync(envDir)) {
return;
}
await PIOHome.shutdownAllServers();
await pioNodeHelpers.misc.sleep(2000);
try {
fs.removeSync(envDir);
} catch (err) {
console.warn(err);
}
vscode.window.showInformationMessage(
'Please restart VSCode to apply the changes.',
);
});
}
disposeLocalSubscriptions() {
vscode.commands.executeCommand('setContext', 'pioCoreReady', false);
vscode.commands.executeCommand('setContext', 'pioProjectReady', false);
utils.disposeSubscriptions(this.subscriptions);
}
deactivate() {
this.disposeLocalSubscriptions();
}
}
export const extension = new PlatformIOVSCodeExtension();
export function activate(context) {
extension.activate(context);
return extension;
}
export function deactivate() {
extension.deactivate();
piodebug.deactivate();
}