From 62f1a6a215ec4429535817953c811d8f570af4d9 Mon Sep 17 00:00:00 2001 From: pelikhan Date: Tue, 6 Dec 2022 09:21:26 -0800 Subject: [PATCH] patch: send dbg to dev tools --- cli/src/build.ts | 30 ++++-------------------------- cli/src/devtools.ts | 18 ++++++++++-------- compiler/src/compiler.ts | 5 ++--- compiler/src/devicescript.ts | 1 + compiler/src/jdutil.ts | 9 +++++++++ 5 files changed, 26 insertions(+), 37 deletions(-) diff --git a/cli/src/build.ts b/cli/src/build.ts index df3d185898..144f7d7fc1 100644 --- a/cli/src/build.ts +++ b/cli/src/build.ts @@ -14,6 +14,8 @@ import { DEVS_BYTECODE_FILE, formatDiagnostics, LogInfo, + DEVS_DBG_FILE, + prettySize, } from "devicescript-compiler" import { BINDIR, CmdOptions, debug, error, log } from "./command" import { devtools } from "./devtools" @@ -113,6 +115,7 @@ export async function build(file: string, options: BuildOptions & CmdOptions) { async function buildWatch(file: string, options: BuildOptions) { const bytecodeFile = join(options.outDir, DEVS_BYTECODE_FILE) + const debugFile = join(options.outDir, DEVS_DBG_FILE) // start watch source file log(`watching ${file}...`) @@ -127,32 +130,7 @@ async function buildWatch(file: string, options: BuildOptions) { watch(file, work) // start watching bytecode file - await devtools({ ...options, bytecodeFile }) -} - -function roundWithPrecision( - x: number, - digits: number, - round = Math.round -): number { - digits = digits | 0 - // invalid digits input - if (digits <= 0) return round(x) - if (x == 0) return 0 - let r = 0 - while (r == 0 && digits < 21) { - const d = Math.pow(10, digits++) - r = round(x * d + Number.EPSILON) / d - } - return r -} -function prettySize(b: number) { - b = b | 0 - if (b === 0) return "0kb" - else if (b < 100) return b + "b" - else if (b < 1000) return roundWithPrecision(b / 1e3, 2) + "kb" - else if (b < 1000000) return roundWithPrecision(b / 1e3, 1) + "kb" - else return roundWithPrecision(b / 1e6, 1) + "mb" + await devtools({ ...options, bytecodeFile, debugFile }) } async function buildOnce(file: string, options: BuildOptions & CmdOptions) { diff --git a/cli/src/devtools.ts b/cli/src/devtools.ts index d3b5cb60b3..a4a266a404 100644 --- a/cli/src/devtools.ts +++ b/cli/src/devtools.ts @@ -5,8 +5,9 @@ import http from "http" import https from "https" import url from "url" import net from "net" -import fs from "fs" import { CmdOptions, debug, error, log } from "./command" +import { readFileSync, readJSONSync, watch } from "fs-extra" +import { prettySize } from "devicescript-compiler" const dasboardPath = "tools/devicescript-devtools" @@ -46,10 +47,11 @@ export interface DevToolsOptions { localhost?: boolean bytecodeFile?: string + debugFile?: string } export async function devtools(options: DevToolsOptions & CmdOptions) { - const { internet, localhost, bytecodeFile } = options + const { internet, localhost, bytecodeFile, debugFile } = options const port = 8081 const tcpPort = 8082 const listenHost = internet ? undefined : "127.0.0.1" @@ -67,16 +69,16 @@ export async function devtools(options: DevToolsOptions & CmdOptions) { // upload DeviceScript file is needed const sendDeviceScript = bytecodeFile ? () => { - const bytecode = fs.readFileSync(bytecodeFile) + const bytecode = readFileSync(bytecodeFile) + const dbg = debugFile ? readJSONSync(debugFile) : undefined debug( - `refresh bytecode ${Math.round( - (bytecode.length || 0) / 1000 - )}kb...` + `refresh bytecode ${prettySize(bytecode.length)}...` ) const msg = JSON.stringify({ type: "bytecode", channel: "devicescript", bytecode: bytecode.toString("hex"), + dbg, }) clients.forEach(c => c.send(msg)) } @@ -146,7 +148,7 @@ export async function devtools(options: DevToolsOptions & CmdOptions) { tcpServer.listen(tcpPort, listenHost) if (bytecodeFile) { - debug(`watch ${bytecodeFile}`) - fs.watch(bytecodeFile, sendDeviceScript) + debug(`watching ${bytecodeFile}...`) + watch(bytecodeFile, sendDeviceScript) } } diff --git a/compiler/src/compiler.ts b/compiler/src/compiler.ts index fc73747163..6c90232741 100644 --- a/compiler/src/compiler.ts +++ b/compiler/src/compiler.ts @@ -2871,7 +2871,6 @@ class Program implements TopOpWriter { this.host.write(DEVS_ASSEMBLY_FILE, this.getAssembly()) const { binary, dbg } = this.serialize() - this.host.write(DEVS_BYTECODE_FILE, binary) const progJson = { text: this._source, blocks: "", @@ -2880,10 +2879,10 @@ class Program implements TopOpWriter { this.host.write(DEVS_BODY_FILE, JSON.stringify(progJson, null, 4)) this.host.write(DEVS_DBG_FILE, JSON.stringify(dbg, null, 4)) this.host.write(DEVS_SIZES_FILE, computeSizes(dbg)) - - // write assembly again if (this.numErrors == 0) this.host.write(DEVS_ASSEMBLY_FILE, this.getAssembly()) + // this file is tracked by --watch and should be written last + this.host.write(DEVS_BYTECODE_FILE, binary) if (this.numErrors == 0) { try { diff --git a/compiler/src/devicescript.ts b/compiler/src/devicescript.ts index 389d07907f..abf0cc8af3 100644 --- a/compiler/src/devicescript.ts +++ b/compiler/src/devicescript.ts @@ -6,6 +6,7 @@ export * from "./specgen" export * from "./embedspecs" export * from "./tsiface" export * from "./info" +export { prettySize } from "./jdutil" import { compile } from "./compiler" diff --git a/compiler/src/jdutil.ts b/compiler/src/jdutil.ts index 21825c98b2..cc40c4f9cd 100644 --- a/compiler/src/jdutil.ts +++ b/compiler/src/jdutil.ts @@ -564,6 +564,15 @@ export function renderWithPrecision( return rs } +export function prettySize(b: number) { + b = b | 0 + if (b === 0) return "0kb" + else if (b < 100) return b + "b" + else if (b < 1000) return roundWithPrecision(b / 1e3, 2) + "kb" + else if (b < 1000000) return roundWithPrecision(b / 1e3, 1) + "kb" + else return roundWithPrecision(b / 1e6, 1) + "mb" +} + export function randomRange(min: number, max: number) { return Math.round(Math.random() * (max - min) + min) }