forked from microsoft/devicescript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy.ts
More file actions
104 lines (95 loc) · 3.1 KB
/
Copy pathdeploy.ts
File metadata and controls
104 lines (95 loc) · 3.1 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
import {
delay,
DeviceScriptManagerReg,
JDService,
versionTryParse,
} from "jacdac-ts"
import * as vscode from "vscode"
import { DeviceScriptExtensionState } from "./state"
import { showErrorMessage } from "./telemetry"
export async function readRuntimeVersion(srv: JDService) {
const runtimeVersion = srv.register(DeviceScriptManagerReg.RuntimeVersion)
let retry = 3
while (retry-- >= 0 && runtimeVersion.data === undefined) {
await runtimeVersion.refresh(true)
await delay(50)
}
const v = runtimeVersion?.unpackedValue
if (!v) return undefined
return `v${v[2]}.${v[1]}.${v[0]}`
}
export async function checkRuntimeVersion(minVersion: string, srv: JDService) {
if (shouldIgnoreRuntimeVersion()) return true
const flashCommand = "Upgrade Firmware..."
const version = await readRuntimeVersion(srv)
console.debug(`deploy: version min ${minVersion}, device ${version}`)
if (version === undefined) {
// might be in a broken state that prevents querying the runtime
return false
}
const vmin = versionTryParse(minVersion)
const vcurr = versionTryParse(version)
if (
vcurr.major < vmin.major ||
(vcurr.major == vmin.major && vcurr.minor < vmin.minor)
) {
showErrorMessage(
"deploy.firmware.outdated",
`Deploy cancelled. Your device firmware (${version}) is outdated (min ${minVersion}).`,
flashCommand
).then(cmd => {
if (cmd === flashCommand)
vscode.commands.executeCommand(
"extension.devicescript.device.flash",
srv.device
)
})
return false
} else if (vcurr.major > vmin.major) {
showErrorMessage(
"deploy.firmware.ahead",
`Deploy cancelled. Your device firmware (${version}) is ahead of the device script tools (${minVersion}). Update your dependencies.`
)
return false
}
return true
}
function shouldIgnoreRuntimeVersion() {
const config = vscode.workspace.getConfiguration("devicescript.deploy")
return !!config.get("ignoreRuntimeVersion")
}
/**
* Check if runtime versions are compatible.
* @param runtimeVersion
* @param service
* @returns
*/
export async function checkDeviceScriptManagerRuntimeVersion(
runtimeVersion: string,
service: JDService
) {
if (!runtimeVersion) {
showErrorMessage(
"deploy.toolsnotstarted",
"Deploy cancelled. Developer tools not started."
)
return false
}
if (!service) {
showErrorMessage(
"deploy.devicenotfound",
"Deploy cancelled. No DeviceScript device found."
)
return false
}
return await checkRuntimeVersion(runtimeVersion, service)
}
export async function prepareForDeploy(
extensionState: DeviceScriptExtensionState,
service: JDService
) {
// disable autostart (which is really auto-restart when the program stops)
await service
.register(DeviceScriptManagerReg.Autostart)
.sendSetAsync(new Uint8Array([0]))
}