Skip to content

Commit

Permalink
patch: send dbg to dev tools
Browse files Browse the repository at this point in the history
  • Loading branch information
pelikhan committed Dec 6, 2022
1 parent 3275e2c commit 62f1a6a
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 37 deletions.
30 changes: 4 additions & 26 deletions cli/src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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}...`)
Expand All @@ -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) {
Expand Down
18 changes: 10 additions & 8 deletions cli/src/devtools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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"
Expand All @@ -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))
}
Expand Down Expand Up @@ -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)
}
}
5 changes: 2 additions & 3 deletions compiler/src/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: "",
Expand All @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions compiler/src/devicescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export * from "./specgen"
export * from "./embedspecs"
export * from "./tsiface"
export * from "./info"
export { prettySize } from "./jdutil"

import { compile } from "./compiler"

Expand Down
9 changes: 9 additions & 0 deletions compiler/src/jdutil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit 62f1a6a

Please sign in to comment.