Skip to content

Commit

Permalink
Update: build log auto scroll
Browse files Browse the repository at this point in the history
  • Loading branch information
darakuneko committed Dec 27, 2022
1 parent 3c88c2b commit 72c8c10
Show file tree
Hide file tree
Showing 8 changed files with 193 additions and 130 deletions.
15 changes: 10 additions & 5 deletions command.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ const responseStreamLog = async (res, mainWindow, channel) => {
buildCompleted = false
const stream = res.data
stream.on('data', data => mainWindow.webContents.send(channel, data.toString()))
stream.on('end', () => buildCompleted = true)
stream.on('end', () => {
buildCompleted = true
mainWindow.webContents.send(channel, "finish!!")
})
}

const command = {
Expand All @@ -53,10 +56,10 @@ const command = {
},
existSever: async () => {
if(isDockerUp) {
const res = await axios(url("")).catch(e => {})
return res.status === 200
const res = await axios(url("")).catch(e => e)
return res.status ? res.status : 404
}
return isDockerUp
return 403
},
tags: async () => {
const res = await axios(url('/tags/qmk'))
Expand All @@ -82,7 +85,9 @@ const command = {
responseType: 'stream',
data: data
}).catch(e => {})
return res.status === 200 ? responseStreamLog(res, mainWindow, "streamBuildLog") :
const channel = "streamBuildLog"
mainWindow.webContents.send(channel, "@@@@@init@@@@")
return res.status === 200 ? responseStreamLog(res, mainWindow, channel) :
mainWindow.webContents.send("streamLog", `Cannot POST ${u}`)
},
buildCompleted: () => buildCompleted,
Expand Down
55 changes: 32 additions & 23 deletions gpk_fwmaker/server/src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ const app = express()

const server = app.listen(3000, async() =>console.log("Node.js is listening to PORT:" + server.address().port))

const streamResponce = async (res, fn) => {
res.writeHead(200, { "Content-Type": "text/event-stream"})
await fn()
}

app.use(bodyParser.urlencoded({ extended: true }))
app.use(bodyParser.json())

Expand All @@ -20,33 +25,37 @@ app.get('/update/repository/qmk', async (req, res) => await cmd.updateRepository
app.get('/update/repository/vial', async (req, res) => await cmd.updateRepositoryVial(res))

app.post('/build/qmk', async (req, res) => {
try {
const kb = req.body.kb
const kbDir = kb.replace(/\/.*/g, "")
const km = req.body.km.replace(/:.*|flash/g, "")
const tag = req.body.tag
const currentTag = await cmd.currentTag()
if(tag !== currentTag) await cmd.checkoutQmk(tag)
await cmd.cpConfigsToQmk(kbDir)
await cmd.buildQmkFirmware(res, kb, km)
} catch (e) { streamError(res, e) }
const fn = async () => {
try {
const kb = req.body.kb
const kbDir = kb.replace(/\/.*/g, "")
const km = req.body.km.replace(/:.*|flash/g, "")
const tag = req.body.tag
const currentTag = await cmd.currentTag()
if (tag !== currentTag) await cmd.checkoutQmk(res, tag)
await cmd.cpConfigsToQmk(kbDir)
await cmd.buildQmkFirmware(res, kb, km)
} catch (e) {
streamError(res, e)
}
}
await streamResponce(res, fn)
})

app.post('/build/vial', async (req, res) => {
try {
const kb = req.body.kb
const kbDir = kb.replace(/\/.*/g, "")
const km = req.body.km.replace(/:.*|flash/g, "")
const fn = async () => {
try {
const kb = req.body.kb
const kbDir = kb.replace(/\/.*/g, "")
const km = req.body.km.replace(/:.*|flash/g, "")
const commit = "commit" in req.body && req.body.commit.length > 0 ? req.body.commit : "vial"
await cmd.checkoutVial(res, commit)
await cmd.cpConfigsToVial(kbDir)
await cmd.buildVialFirmware(res, kb, km)
} catch (e) { streamError(res, e) }
}

if("commit" in req.body) {
const commit = req.body.commit
if(commit.length > 0) await cmd.checkoutVial(commit)
} else {
await cmd.checkoutVial()
}
await cmd.cpConfigsToVial(kbDir)
await cmd.buildVialFirmware(res,kb, km)
} catch (e) { streamError(res, e) }
await streamResponce(res, fn)
})

app.post('/generate/qmk/file', async (req, res) => {
Expand Down
108 changes: 57 additions & 51 deletions gpk_fwmaker/server/src/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,27 @@ const util = require('util')
const childProcess = require('child_process')
const exec = util.promisify(childProcess.exec)

const qmkDir = `/root/qmk_firmware`
const vialDir = `/root/vial-qmk`
const qmkCmd = async(cmd) => await exec(`cd ${qmkDir} && ${cmd}`)
const vialCmd = async(cmd) => await exec(`cd ${vialDir} && ${cmd}`)
const dirQMK = `/root/qmk_firmware`
const dirVial = `/root/vial-qmk`

const findFirmwareLine = (dir) => `find ${dir}/* -maxdepth 0 -regex ".*\\.\\(bin\\|hex\\|uf2\\)"`
const execFW = async (dir, line) => await exec(`cd ${dir} && ${line}`)
const execQMK = async (line) => await execFW(dirQMK, line)
const execVial = async (line) => await execFW(dirVial, line)

const spawn = (line) => childProcess.spawn(line, { shell: true })

const streamWriteLine = async (dir, line, res) => {
await exec(`cd ${dir} && ${line}`)
res.write(`${line}\n`)
}
const streamWriteLineQMK = async (line, res) => await streamWriteLine(dirQMK, line, res)
const streamWriteLineVial = async (line, res) => await streamWriteLine(dirVial, line, res)

const findFirmwareL = (dir) => `find ${dir}/* -maxdepth 0 -regex ".*\\.\\(bin\\|hex\\|uf2\\)"`
const cpFirmwareL = (dir) => `${findFirmwareL(dir)} -type f -exec cp {} /root/keyboards \\;`

const rmL = (path) => `rm -rf ${path}`
const rmKeyboardsL = (path) => rmL(`${path}/keyboards/*`)

const tagZeroFill2Int = (str) => {
const s = str
Expand All @@ -18,27 +33,20 @@ const tagZeroFill2Int = (str) => {
return parseInt(s)
}

const rmQmk = async () => await exec(`rm -rf ${qmkDir}`)
const rmVial = async () => await exec(`rm -rf ${vialDir}`)
const rmQmkKeyboards = async () => await exec(`rm -rf ${qmkDir}/keyboards/*`)
const rmVialKeyboards = async () => await exec(`rm -rf ${vialDir}/keyboards/*`)

const streamLog = (fn, res) => {
res.writeHead(200, { "Content-Type": "text/event-stream"})
const response = fn()
const streamLog = (line, res) => {
const response = spawn(line)
response.stdout.on('data', (data) => res.write(data.toString()))
response.stderr.on('data', (data) => res.write(data.toString()))
response.on('close', () => res.end(''))
}

const streamError = (res, e) => {
res.writeHead(200, { "Content-Type": "text/event-stream"})
res.end(e.toString())
}

const cmd = {
tags: async () => {
const result = await qmkCmd(`git ls-remote --tags`)
const result = await execQMK(`git ls-remote --tags`)

return result
.stdout
Expand All @@ -47,68 +55,66 @@ const cmd = {
.sort((a, b) => tagZeroFill2Int(b) - tagZeroFill2Int(a))
},
currentTag: async () => {
const result = await qmkCmd(`git branch`)
const result = await execQMK(`git branch`)
return result.stdout.split('\n').filter(v => v.match(/^\* /))[0].replace(/^\* /, '')
},
checkoutQmk: async (tag) => {
await rmQmkKeyboards()
await qmkCmd(`git reset --hard HEAD^`)
try { await qmkCmd(`git checkout -b ${tag} refs/tags/${tag}`) } catch (e){
await qmkCmd(`git branch -D ${tag}`)
await qmkCmd(`git checkout -b ${tag} refs/tags/${tag}`)
checkoutQmk: async (res, tag) => {
await exec(rmKeyboardsL(dirQMK))
await streamWriteLineQMK("git reset --hard HEAD^", res)
try { await streamWriteLineQMK(`git checkout -b ${tag} refs/tags/${tag}`, res) } catch (e){
await streamWriteLineQMK(`git branch -D ${tag}`, res)
await streamWriteLineQMK(`git checkout -b ${tag} refs/tags/${tag}`, res)
}
await qmkCmd(`make git-submodule`)
await rmQmkKeyboards()
await streamWriteLineQMK(`make git-submodule\n`, res)
await exec(rmKeyboardsL(dirQMK))
},
checkoutVial: async (commit) => {
await rmVialKeyboards()
await vialCmd(`git reset --hard HEAD^`)
await vialCmd(`git checkout ${commit}`)
await vialCmd(`make git-submodule`)
await rmVialKeyboards()
checkoutVial: async (res, commit) => {
await exec(rmKeyboardsL(dirVial))
await streamWriteLineVial(`git reset --hard HEAD^`, res)
await streamWriteLineVial(`git checkout ${commit}`, res)
await streamWriteLineVial(`make git-submodule\n`, res)
await exec(rmKeyboardsL(dirVial))
},
buildQmkFirmware: async (res, kb, km) => {
await exec(`${findFirmwareLine(qmkDir)} -delete`)
const fn = () => childProcess.spawn(`qmk compile -kb ${kb} -km ${km} && ${findFirmwareLine(qmkDir)} -type f -exec cp {} /root/keyboards \\;`, { shell: true })
streamLog(fn, res)
await exec(`${findFirmwareL(dirQMK)} -delete`)
const line = `qmk compile -kb ${kb} -km ${km} && ${cpFirmwareL(dirQMK)}`
streamLog(line, res)
},
buildVialFirmware: async (res, kb, km) => {
await exec(`${findFirmwareLine(vialDir)} -delete`)
const fn = () => childProcess.spawn(`cd ${vialDir} && make ${kb}:${km} && ${findFirmwareLine(vialDir)} -type f -exec cp {} /root/keyboards \\;`, { shell: true })
streamLog(fn, res)
await exec(`${findFirmwareL(dirVial)} -delete`)
const line = `cd ${dirVial} && make ${kb}:${km} && ${cpFirmwareL(dirVial)}`
streamLog(line, res)
},
updateRepositoryQmk: async (res) => {
await rmQmk()
const fn = () => childProcess.spawn("cd /root && qmk setup -y", { shell: true })
streamLog(fn, res)
await exec(rmL(dirQMK))
const line = "cd /root && qmk setup -y"
streamLog(line, res)
},
updateRepositoryVial: async (res) => {
await rmVial()
const fn = () => childProcess.spawn(
`cd /root && git clone https://github.com/vial-kb/vial-qmk.git && cd ${vialDir} && make git-submodule`,
{ shell: true })
streamLog(fn, res)
await exec(rmL(dirVial))
const line = `cd /root && git clone https://github.com/vial-kb/vial-qmk.git && cd ${dirVial} && make git-submodule`
streamLog(line, res)
},
generateQmkFile: async (kb, mcu, layout, user) => {
await rmQmkKeyboards()
await exec(rmKeyboardsL(dirQMK))
const result = await exec(`qmk new-keyboard -kb ${kb} -t ${mcu} -l ${layout} -u ${user}`)
return result
},
generateVialId: async () => {
const result = await vialCmd("python3 util/vial_generate_keyboard_uid.py")
const result = await execVial("python3 util/vial_generate_keyboard_uid.py")
return result.stdout
},
cpConfigsToQmk: async (kbDir) => {
await rmQmkKeyboards()
await exec(`cp -rf /root/keyboards/${kbDir} ${qmkDir}/keyboards`)
await exec(rmKeyboardsL(dirQMK))
await exec(`cp -rf /root/keyboards/${kbDir} ${dirQMK}/keyboards`)
},
cpConfigsToVial: async (kbDir) => {
await rmVialKeyboards()
await exec(`cp -rf /root/keyboards/${kbDir} ${vialDir}/keyboards`)
await exec(rmKeyboardsL(dirVial))
await exec(`cp -rf /root/keyboards/${kbDir} ${dirVial}/keyboards`)
},
mvQmkConfigsToVolume: async (kbDir) => {
await exec(`rm -rf /root/keyboards/${kbDir} `)
await exec(`mv -f ${qmkDir}/keyboards/${kbDir} /root/keyboards`)
await exec(`mv -f ${dirQMK}/keyboards/${kbDir} /root/keyboards`)
}
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"dist:linux": "npm run build && electron-builder -l"
},
"name": "gpk_fwbuilder",
"version": "0.3.1",
"version": "0.3.2",
"description": "GPK FWBuilder",
"homepage": "https://github.com/darakuneko",
"author": {
Expand Down
9 changes: 6 additions & 3 deletions src/content.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, {useEffect, useState} from 'react'
import React, {useCallback, useEffect, useRef, useState} from 'react'
import Box from "@mui/material/Box"
import Tabs from "@mui/material/Tabs"
import Tab from "@mui/material/Tab"
Expand All @@ -23,7 +23,7 @@ const Content = () => {
let id
const checkFn = async () => {
const exist = await api.existSever()
if(exist){
if(exist === 200){
const reStoreState = await api.getState()
state.version = await api.appVersion()
if(reStoreState && reStoreState.version === state.version) {
Expand All @@ -36,6 +36,9 @@ const Content = () => {
setState(state)
clearInterval(id)
setInitServer(false)
} else if(exist === 404) {
state.logs = 'connecting....\nIf the connection does not work for a long time, please start up again or delete the docker image once.'
setState(state)
}
}
id = setInterval(await checkFn, 1000)
Expand Down Expand Up @@ -65,7 +68,7 @@ const Content = () => {
useEffect(() => {
api.on("streamBuildLog", async (log) => {
const state = await getState()
state.logs = state.logs + log
log.match(/@@@@@init@@@@/m) ? state.logs = '' : state.logs = state.logs + log
setState(state)
})
return () => {}
Expand Down
19 changes: 12 additions & 7 deletions src/renderer/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ const Generate = () => {

const [disabledBuildButton, setDisabledBuildButton] = useState(true)
const [disabledBuildText, setDisabledBuildText] = useState(false)
const [init, setInit] = useState(true)
const [disabledVialID, setDisabledVialID] = useState(false)

const [qmkFile, setQmkFile] = useState(true)

const validBuildButton = () => {
const qmkFile = state.generate.qmkFile
Expand All @@ -42,9 +44,9 @@ const Generate = () => {
setDisabledBuildButton(!validDisableButton)
}

const initDisabledBuildButton = () => {
const qmkFileDisabledBuildButton = () => {
validBuildButton()
setInit(false)
setQmkFile(false)
return disabledBuildButton
}

Expand All @@ -62,9 +64,10 @@ const Generate = () => {
}

const generateMsg = "Generating...."
const handleQmkFileSubmit = (key) => async () => {
const handleQmkFileSubmit = () => async () => {
setDisabledBuildButton(true)
setDisabledBuildText(true)
setDisabledVialID(true)
state.logs = generateMsg
state.tabDisabled = true
setState(state)
Expand All @@ -73,12 +76,13 @@ const Generate = () => {

setDisabledBuildButton(false)
setDisabledBuildText(false)
setDisabledVialID(false)
state.logs = logs
state.tabDisabled = false
setState(state)
}

const handleVailIdSubmit = (key) => async () => {
const handleVailIdSubmit = () => async () => {
state.logs = generateMsg
state.tabDisabled = true
setState(state)
Expand Down Expand Up @@ -149,8 +153,8 @@ const Generate = () => {
alignItems: 'center'
}} >
<Button variant="contained"
onClick={handleQmkFileSubmit("qmkFile")}
disabled={init ? initDisabledBuildButton() : disabledBuildButton }
onClick={handleQmkFileSubmit()}
disabled={qmkFile ? qmkFileDisabledBuildButton() : disabledBuildButton }
>Generate</Button>
</Box>
</Box>
Expand All @@ -169,6 +173,7 @@ const Generate = () => {
}} >
<Button variant="contained"
onClick={handleVailIdSubmit("VialId")}
disabled={ disabledVialID }
>Generate</Button>
</Box>
</Box>
Expand Down
Loading

0 comments on commit 72c8c10

Please sign in to comment.