forked from microsoft/devicescript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathactivateDeviceScript.ts
More file actions
235 lines (226 loc) · 8.88 KB
/
activateDeviceScript.ts
File metadata and controls
235 lines (226 loc) · 8.88 KB
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
import * as vscode from "vscode"
import { Utils } from "vscode-uri"
import { activateDebugger } from "./debugger"
import { activateGateway } from "./gateway/activateGateway"
import { startJacdacBus, stopJacdacBus } from "./jacdac"
import { JDomDeviceTreeItem, activateTreeViews } from "./JDomTreeDataProvider"
import { activateMainStatusBar } from "./mainstatusbar"
import {
activateJacdacOutputChannel,
activateDeviceScriptOutputChannel,
activateDeviceScriptI2COutputChannel,
activateDeviceScriptDataChannel,
} from "./output"
import { DeviceScriptExtensionState } from "./state"
import { activateTelemetry } from "./telemetry"
import { JDDevice } from "jacdac-ts"
export function activateDeviceScript(context: vscode.ExtensionContext) {
const { subscriptions } = context
activateTelemetry(context)
// setup bus
const bus = startJacdacBus()
context.subscriptions.push({
dispose: stopJacdacBus,
})
const extensionState = new DeviceScriptExtensionState(context, bus)
const debugFile = async (file: vscode.Uri, noDebug: boolean) => {
if (!file) return
const folder = vscode.workspace.getWorkspaceFolder(file)
if (!folder) return
await vscode.debug.startDebugging(folder, {
type: "devicescript",
request: "launch",
name: "DeviceScript: Run File",
stopOnEntry: false,
noDebug,
program: file.fsPath,
} as vscode.DebugConfiguration)
if (noDebug)
vscode.commands.executeCommand("extension.devicescript.jdom.focus")
}
// build
subscriptions.push(
vscode.commands.registerCommand(
"extension.devicescript.project.new",
async () => {
let askOpen = false
let folder: vscode.Uri
if (!vscode.workspace.workspaceFolders?.length) {
// need to open a new workspace
const folders = await vscode.window.showOpenDialog({
title: "Select a folder to create project",
canSelectMany: false,
canSelectFiles: false,
canSelectFolders: true,
})
folder = folders?.[0]
if (!folder) return
// pick name
const name = await vscode.window.showInputBox({
title: "Pick a name for your project (optional)",
})
if (name === undefined) return
else if (name) folder = Utils.joinPath(folder, name)
askOpen = true
} else {
const workspaceFolder =
vscode.workspace.workspaceFolders?.length === 1
? vscode.workspace.workspaceFolders[0]
: await vscode.window.showWorkspaceFolderPick()
if (workspaceFolder === undefined) return
folder = workspaceFolder.uri
}
// is this project empty?
let projectName: string
const projetFiles = await vscode.workspace.findFiles(
new vscode.RelativePattern(folder, "**/*"),
"**/node_modules/*",
1
)
if (projetFiles.length) {
projectName = await vscode.window.showInputBox({
title: "Pick project subfolder",
prompt: "It will be used as a root for the new DeviceScript project",
})
if (!projectName) return
}
const cwd = projectName
? Utils.joinPath(folder, projectName)
: folder
await vscode.workspace.fs.createDirectory(cwd)
const terminal = vscode.window.createTerminal({
isTransient: true,
cwd,
})
terminal.sendText(
"npx --yes @devicescript/cli@latest init --quiet"
)
terminal.show()
if (askOpen) {
const res = await vscode.window.showInformationMessage(
"Would you like to open the project?",
"Yes"
)
if (res === "Yes") {
await vscode.commands.executeCommand(
"vscode.openFolder",
folder,
true // don't destroy terminal while reopening
)
}
}
}
),
vscode.commands.registerCommand(
"extension.devicescript.configure",
async () => await extensionState.configure()
),
vscode.commands.registerCommand(
"extension.devicescript.device.identify",
async (item: JDomDeviceTreeItem) => {
const device =
item?.device ||
(
await extensionState.pickDeviceScriptManager({
skipUpdate: true,
})
)?.device
await device?.identify()
}
),
vscode.commands.registerCommand(
"extension.devicescript.device.reset",
async (item: JDomDeviceTreeItem) => {
const device = item?.device
await device?.reset()
}
),
vscode.commands.registerCommand(
"extension.devicescript.device.flash",
(device?: JDomDeviceTreeItem | JDDevice) =>
device instanceof JDomDeviceTreeItem
? device.flash()
: extensionState.flashFirmware(device)
),
vscode.commands.registerCommand(
"extension.devicescript.connect",
async () => extensionState.connect()
),
vscode.commands.registerCommand(
"extension.devicescript.sims.add",
async () => extensionState.addSim()
),
vscode.commands.registerCommand(
"extension.devicescript.services.add",
async () => extensionState.addService()
),
vscode.commands.registerCommand(
"extension.devicescript.boards.add",
async () => extensionState.addBoard()
),
vscode.commands.registerCommand(
"extension.devicescript.variables.simulator",
() => extensionState.simulatorScriptManagerId
),
vscode.commands.registerCommand(
"extension.devicescript.simulator.stop",
async () => {
await extensionState.stopSimulator()
}
),
vscode.commands.registerCommand(
"extension.devicescript.simulator.clear",
async () => {
await extensionState.clearSimulatorFlash()
}
),
vscode.commands.registerCommand(
"extension.devicescript.simulator.start",
async () => {
await extensionState.startSimulator()
}
),
vscode.commands.registerCommand(
"extension.devicescript.pickDeviceScriptManager",
() => extensionState.pickDeviceScriptManager()
),
vscode.commands.registerCommand(
"extension.devicescript.editor.run",
async (file: vscode.Uri) => debugFile(file, true)
),
vscode.commands.registerCommand(
"extension.devicescript.editor.debug",
async (file: vscode.Uri) => debugFile(file, false)
),
vscode.commands.registerCommand(
"extension.devicescript.editor.build",
async (file: vscode.Uri) => {
if (!file) return
await extensionState.build(file)
}
),
vscode.commands.registerCommand(
"extension.devicescript.editor.configure",
async (file: vscode.Uri) => {
const editor = await vscode.window.showTextDocument(file)
if (!editor) return
await extensionState.configureHardware(editor)
}
)
)
activateDebugger(extensionState)
activateGateway(context, extensionState)
activateTreeViews(extensionState)
activateMainStatusBar(extensionState)
activateJacdacOutputChannel(extensionState)
activateDeviceScriptI2COutputChannel(extensionState)
activateDeviceScriptOutputChannel(extensionState)
activateDeviceScriptDataChannel(extensionState)
// launch devtools in background
const devToolsConfig = vscode.workspace.getConfiguration(
"devicescript.devtools"
)
if (devToolsConfig.get("autoStart")) {
extensionState.devtools.start({ build: true })
}
}