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
73 lines (66 loc) · 1.82 KB
/
deploy.ts
File metadata and controls
73 lines (66 loc) · 1.82 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
import {
bufferEq,
DeviceScriptManagerCmd,
DeviceScriptManagerReg,
JDBus,
JDService,
OutPipe,
prettySize,
sha256,
SRV_DEVICE_SCRIPT_MANAGER,
toHex,
} from "jacdac-ts"
import { BuildOptions, devsStartWithNetwork } from "./build"
import { CmdOptions, error } from "./command"
import { readCompiled } from "./run"
export interface RunOptions {
tcp?: boolean
}
export async function deployScript(
fn: string,
options: RunOptions & CmdOptions & BuildOptions
) {
const inst = await devsStartWithNetwork(options)
inst.deployHandler = code => {
if (code) error(`deploy error ${code}`)
process.exit(code)
}
const prog = await readCompiled(fn, options)
const r = inst.devsClientDeploy(prog.binary)
if (r) throw new Error("deploy error: " + r)
console.log(`remote-deployed ${fn}`)
}
export async function deployToService(
service: JDService,
bytecode: Uint8Array
) {
console.log(`deploy to ${service.device}`)
const sha = service.register(DeviceScriptManagerReg.ProgramSha256)
await sha.refresh()
if (sha.data?.length == 32) {
const exp = await sha256([bytecode])
if (bufferEq(exp, sha.data)) {
console.log(` sha256 match ${toHex(exp)}, skip`)
return
}
}
await OutPipe.sendBytes(
service,
DeviceScriptManagerCmd.DeployBytecode,
bytecode,
p => {
// console.debug(` prog: ${(p * 100).toFixed(1)}%`)
}
)
console.log(` --> done, ${prettySize(bytecode.length)}`)
}
export async function deployToBus(bus: JDBus, bytecode: Uint8Array) {
let num = 0
for (const service of bus.services({
serviceClass: SRV_DEVICE_SCRIPT_MANAGER,
})) {
await deployToService(service, bytecode)
num++
}
return num
}