ludic/packages/ludic.update/steps.ludic

92 lines
3.5 KiB
Text

# steps.ludic - each step started: the feed asked, a package streamed into the packages folder, its
# SHA-256 by a child (Get-FileHash, shasum) so the frame keeps drawing, a patch, the apply
# the Update button: false (and nothing started) when Velopack did not install this copy
export function update_start(update_st: mut UpdateState) -> bool {
if not update_installed(update_st) {
update_st.update__err = UPDATE_ERR_NOT_INSTALLED
return false
}
update__release(update_st)
Fs.mkdir(update_packages(update_st))
update_st.update__err = UPDATE_ERR_NONE
update_st.update__h = UpdateNet.get(update__conf(update_st).feed + "/" + update__feed_file())
if update_st.update__h < 0 {
update__fail(update_st, UPDATE_ERR_UNREACHABLE)
return true
}
update_st.update__state = UPDATE_FEED
return true
}
function update__release(update_st: mut UpdateState) -> void {
if update_st.update__h >= 0 { UpdateNet.free(update_st.update__h) }
update_st.update__h = -1
if update_st.update__proc >= 0 { UpdateProc.free(update_st.update__proc) }
update_st.update__proc = -1
}
function update__fail(update_st: mut UpdateState, err: int) -> void {
update__release(update_st)
update_st.update__err = err
update_st.update__state = UPDATE_FAILED
update__fact(update_st, UPDATE_STOPPED)
}
# fetch file i, unless a copy of the right size is there already - then straight to its check
function update__fetch(update_st: mut UpdateState, i: int) -> void {
update_st.update__i = i
let p = update__path(update_st, i)
if Fs.exists(p) and Fs.size(p) == update_st.update__size[i] {
update__check(update_st, i)
return
}
if Fs.exists(p) { Fs.remove(p) }
update_st.update__h = UpdateNet.download(update__conf(update_st).feed + "/" + update_st.update__files[i], p)
if update_st.update__h < 0 {
update__fail(update_st, UPDATE_ERR_UNREACHABLE)
return
}
update_st.update__rate_t = -1
update_st.update__state = UPDATE_FETCH
}
function update__hash_file(update_st: UpdateState) -> string {
if len(update__conf(update_st).scratch) > 0 { return update__conf(update_st).scratch }
return Os.temp_dir() + "/ludic-update-hash.txt"
}
# no cmd.exe: it parses its own command line and would not survive Process.spawn's quoting
function update__check(update_st: mut UpdateState, i: int) -> void {
let out = update__hash_file(update_st)
if Fs.exists(out) { Fs.remove(out) }
let args = new []string
if UpdateWorld.platform() == "windows" {
push(args, "-NoProfile")
push(args, "-NonInteractive")
push(args, "-Command")
push(args, "(Get-FileHash -Algorithm SHA256 -LiteralPath " + update__psq(update__path(update_st, i)) + ").Hash | Out-File -Encoding ascii -LiteralPath " + update__psq(out))
update_st.update__proc = UpdateProc.spawn(update__powershell(), args)
} else {
push(args, "-c")
push(args, "shasum -a 256 '" + update__path(update_st, i) + "' | cut -d' ' -f1 > '" + out + "'")
update_st.update__proc = UpdateProc.spawn("/bin/sh", args)
}
if update_st.update__proc < 0 {
update__fail(update_st, UPDATE_ERR_CHECK)
return
}
update_st.update__state = UPDATE_CHECK
update_st.update__step_t0 = UpdateWorld.now_ms()
}
# the hash the child wrote: the first line with anything on it
function update__hash_read(update_st: UpdateState) -> string {
let s = Fs.read_text(update__hash_file(update_st))
if s == null { return "" }
let lines = Text.split(s, "\n")
for i in 0 .. len(lines) {
let t = Text.trim(lines[i])
if len(t) > 0 { return Text.upper(t) }
}
return ""
}