|
1 |
| -var vscode = require('vscode'); |
2 |
| -var { execSync } = require('child_process'); |
| 1 | +const vscode = require('vscode'); |
| 2 | +const { execSync } = require('child_process'); |
3 | 3 | const { prepareSerialPort } = require('./lib/serialport')
|
4 |
| -var PanelView, Pymakr, Pyboard, SettingsWrapper, pb, v, sw, pymakr |
5 |
| - |
| 4 | +const destroyHandles = [] |
| 5 | + |
| 6 | +/** |
| 7 | + * same as vscode.commands.registerCommand but automatically pushes disposables to context subscriptions |
| 8 | + * @param {vscode.ExtensionContext} context |
| 9 | + * @param {Object.<string, (...args: any[]) => any>} commands |
| 10 | + */ |
| 11 | +const batchRegisterCommands = (context, commands) => { |
| 12 | + Object.entries(commands).forEach(([command, callback]) => { |
| 13 | + const disposable = vscode.commands.registerCommand(command, callback) |
| 14 | + context.subscriptions.push(disposable) |
| 15 | + }) |
| 16 | +} |
6 | 17 |
|
| 18 | +/** |
| 19 | + * this method activates the extension |
| 20 | + * @param {vscode.ExtensionContext} context |
| 21 | + */ |
7 | 22 | async function activate(context) {
|
8 | 23 | await prepareSerialPort()
|
9 |
| - SettingsWrapper = require('./lib/main/settings-wrapper'); |
10 | 24 |
|
11 |
| - sw = new SettingsWrapper(function () { |
| 25 | + /** |
| 26 | + * we have to import SettingsWrapper after prepareSerialPort |
| 27 | + * since SettingsWrapper imports serialport |
| 28 | + */ |
| 29 | + const SettingsWrapper = require('./lib/main/settings-wrapper'); |
| 30 | + |
| 31 | + const settingsWrapper = new SettingsWrapper(function () { |
12 | 32 | const nodejs_installed = execSync('node -v', { encoding: 'utf8' }).substr(0, 1) === "v"
|
13 | 33 |
|
14 | 34 | if (!nodejs_installed) {
|
15 |
| - vscode.window.showErrorMessage("NodeJS not detected on this machine, which is required for Pymakr to work. See the Pymakr readme for dependancies.") |
| 35 | + vscode.window.showErrorMessage("NodeJS not detected on this machine, which is required for Pymakr to work. See the Pymakr readme for dependencies.") |
16 | 36 | } else {
|
| 37 | + const PanelView = require('./lib/main/panel-view'); |
| 38 | + const Pymakr = require('./lib/pymakr'); |
| 39 | + const Pyboard = require('./lib/board/pyboard'); |
17 | 40 |
|
18 | 41 |
|
19 |
| - PanelView = require('./lib/main/panel-view'); |
20 |
| - Pymakr = require('./lib/pymakr'); |
21 |
| - Pyboard = require('./lib/board/pyboard'); |
22 |
| - |
23 |
| - |
24 |
| - pb = new Pyboard(sw) |
25 |
| - v = new PanelView(pb, sw) |
26 |
| - pymakr = new Pymakr({}, pb, v, sw) |
27 |
| - |
| 42 | + const pyboard = new Pyboard(settingsWrapper) |
| 43 | + const panelView = new PanelView(pyboard, settingsWrapper) |
| 44 | + const pymakr = new Pymakr({}, pyboard, panelView, settingsWrapper) |
| 45 | + const { terminal } = panelView |
28 | 46 |
|
29 |
| - var terminal = v.terminal |
30 |
| - |
31 |
| - var disposable = vscode.commands.registerCommand('pymakr.help', function () { |
32 |
| - terminal.show() |
33 |
| - pymakr.writeHelpText() |
34 |
| - }) |
35 |
| - context.subscriptions.push(disposable); |
36 |
| - |
37 |
| - var disposable = vscode.commands.registerCommand('pymakr.listCommands', function () { |
38 |
| - v.showQuickPick() |
39 |
| - }) |
40 |
| - context.subscriptions.push(disposable); |
41 |
| - |
42 |
| - var disposable = vscode.commands.registerCommand('pymakr.connect', function () { |
43 |
| - terminal.show() |
44 |
| - pymakr.connect() |
45 |
| - }) |
46 |
| - |
47 |
| - var disposable = vscode.commands.registerCommand('pymakr.run', function () { |
48 |
| - terminal.show() |
49 |
| - pymakr.run() |
50 |
| - }) |
51 |
| - context.subscriptions.push(disposable); |
52 |
| - |
53 |
| - var disposable = vscode.commands.registerCommand('pymakr.runselection', function () { |
54 |
| - terminal.show() |
55 |
| - pymakr.runselection() |
56 |
| - }) |
57 |
| - context.subscriptions.push(disposable); |
58 |
| - |
59 |
| - var disposable = vscode.commands.registerCommand('pymakr.upload', function () { |
60 |
| - terminal.show() |
61 |
| - pymakr.upload() |
62 |
| - }) |
63 |
| - context.subscriptions.push(disposable); |
64 |
| - |
65 |
| - var disposable = vscode.commands.registerCommand('pymakr.uploadFile', function () { |
66 |
| - terminal.show() |
67 |
| - pymakr.uploadFile() |
68 |
| - }) |
69 |
| - context.subscriptions.push(disposable); |
70 |
| - |
71 |
| - var disposable = vscode.commands.registerCommand('pymakr.download', function () { |
72 |
| - terminal.show() |
73 |
| - pymakr.download() |
74 |
| - }) |
75 |
| - context.subscriptions.push(disposable); |
76 |
| - |
77 |
| - var disposable = vscode.commands.registerCommand('pymakr.globalSettings', function () { |
78 |
| - pymakr.openGlobalSettings() |
79 |
| - }) |
80 |
| - context.subscriptions.push(disposable); |
| 47 | + destroyHandles.push(() => panelView.destroy()) |
81 | 48 |
|
82 |
| - var disposable = vscode.commands.registerCommand('pymakr.projectSettings', function () { |
83 |
| - pymakr.openProjectSettings() |
84 |
| - }) |
85 |
| - context.subscriptions.push(disposable); |
86 |
| - |
87 |
| - var disposable = vscode.commands.registerCommand('pymakr.disconnect', function () { |
88 |
| - pymakr.disconnect() |
89 |
| - }); |
90 |
| - context.subscriptions.push(disposable); |
91 |
| - |
92 |
| - // // not used. open/close terminal command is already available. |
93 |
| - // // not used. open/close terminal command is already available. |
94 |
| - // // not used. open/close terminal command is already available. |
95 |
| - // // Terminal opens automatically when doing a connect, run or sync action. |
96 |
| - // var disposable = vscode.commands.registerCommand('pymakr.toggleREPL', function () { |
97 |
| - // pymakr.toggleVisibility() |
98 |
| - // }); |
99 |
| - // context.subscriptions.push(disposable); |
100 |
| - |
101 |
| - var disposable = vscode.commands.registerCommand('pymakr.toggleConnect', function () { |
102 |
| - if (!pymakr.pyboard.connected) { |
| 49 | + batchRegisterCommands(context, { |
| 50 | + 'pymakr.help': () => { |
| 51 | + terminal.show() |
| 52 | + pymakr.writeHelpText() |
| 53 | + }, |
| 54 | + 'pymakr.listCommands': () => { |
| 55 | + panelView.showQuickPick() |
| 56 | + }, |
| 57 | + 'pymakr.connect': () => { |
| 58 | + terminal.show() |
| 59 | + pymakr.connect() |
| 60 | + }, |
| 61 | + 'pymakr.run': () => { |
| 62 | + terminal.show() |
| 63 | + pymakr.run() |
| 64 | + }, |
| 65 | + 'pymakr.runselection': () => { |
| 66 | + terminal.show() |
| 67 | + pymakr.runselection() |
| 68 | + }, |
| 69 | + 'pymakr.upload': () => { |
| 70 | + terminal.show() |
| 71 | + pymakr.upload() |
| 72 | + }, |
| 73 | + 'pymakr.uploadFile': () => { |
| 74 | + terminal.show() |
| 75 | + pymakr.uploadFile() |
| 76 | + }, |
| 77 | + 'pymakr.download': () => { |
| 78 | + terminal.show() |
| 79 | + pymakr.download() |
| 80 | + }, |
| 81 | + 'pymakr.globalSettings': () => { |
| 82 | + pymakr.openGlobalSettings() |
| 83 | + }, |
| 84 | + 'pymakr.projectSettings': () => { |
| 85 | + pymakr.openProjectSettings() |
| 86 | + }, |
| 87 | + 'pymakr.disconnect': () => { |
| 88 | + pymakr.disconnect() |
| 89 | + }, |
| 90 | + 'pymakr.toggleConnect': () => { |
| 91 | + if (!pymakr.pyboard.connected) |
| 92 | + terminal.show() |
| 93 | + pymakr.toggleConnect() |
| 94 | + }, |
| 95 | + 'pymakr.extra.getVersion': () => { |
| 96 | + terminal.show() |
| 97 | + pymakr.getVersion() |
| 98 | + }, |
| 99 | + 'pymakr.extra.getWifiMac': () => { |
103 | 100 | terminal.show()
|
| 101 | + pymakr.getWifiMac() |
| 102 | + }, |
| 103 | + 'pymakr.extra.getSerial': () => { |
| 104 | + terminal.show() |
| 105 | + pymakr.getSerial() |
104 | 106 | }
|
105 |
| - pymakr.toggleConnect() |
106 |
| - }); |
107 |
| - context.subscriptions.push(disposable); |
108 |
| - |
109 |
| - |
110 |
| - var disposable = vscode.commands.registerCommand('pymakr.extra.getVersion', function () { |
111 |
| - terminal.show() |
112 |
| - pymakr.getVersion() |
113 |
| - }); |
114 |
| - context.subscriptions.push(disposable); |
115 |
| - |
116 |
| - var disposable = vscode.commands.registerCommand('pymakr.extra.getWifiMac', function () { |
117 |
| - terminal.show() |
118 |
| - pymakr.getWifiMac() |
119 |
| - }); |
120 |
| - context.subscriptions.push(disposable); |
121 |
| - |
122 |
| - var disposable = vscode.commands.registerCommand('pymakr.extra.getSerial', function () { |
123 |
| - terminal.show() |
124 |
| - pymakr.getSerial() |
125 |
| - }); |
126 |
| - context.subscriptions.push(disposable); |
| 107 | + }) |
127 | 108 |
|
128 | 109 | }
|
129 | 110 |
|
130 | 111 | })
|
131 |
| - |
132 |
| -} |
133 |
| - |
134 |
| - |
135 |
| - |
136 |
| -function deactivate() { |
137 |
| - v.destroy() |
138 | 112 | }
|
139 | 113 |
|
140 |
| - |
141 | 114 | module.exports = {
|
142 | 115 | activate,
|
143 |
| - deactivate |
| 116 | + deactivate: () => destroyHandles.map(destroy => destroy()) |
144 | 117 | }
|
145 | 118 |
|
0 commit comments