Implement resource auto-updates.

This commit is contained in:
Mahlon E. Smith 2025-08-22 11:47:04 -07:00
parent e01dc377a7
commit 3d8dc5cdf2
Signed by: mahlon
SSH key fingerprint: SHA256:dP84sRGKZRpOOiPD/+GuOq+SHSxEw9qi5BWLQobaHm0
5 changed files with 42 additions and 16 deletions

View file

@ -9,13 +9,16 @@ import
std/files,
std/paths,
std/strformat,
std/strutils,
std/tempfiles
import
constants
type
EmbeddedFS* = object
EmbeddedFSError* = object of CatchableError
let embeddedFs* = EmbeddedFS()
when not defined( emscripten ):
import zippy/tarballs
@ -33,6 +36,7 @@ else:
let confFile* = confDir / Path("config.ini")
proc path*( data: EmbeddedFS, path: string ): Path =
## Returns the path of an embedded file.
return dataDir / Path( path )
@ -48,23 +52,38 @@ proc `[]`*( data: EmbeddedFS, path: string ): string =
return f.readAll
# TODO: version check, extract over old stuff
# no-op if versions match
#
proc update*( e: EmbeddedFS ) =
when not defined( emscripten ):
createDir( getDataDir() )
createDir( $confDir )
proc newEmbeddedFS(): EmbeddedFS =
## Initialize the embedded filesystem.
##
## If the current runtime version is different than the binary version,
## re-extract the embedded filesystem to disk.
# check existing path / version
result = EmbeddedFS()
when defined( emscripten ): return
let ( tarball, path ) = createTempFile( "{appname}_resources_".fmt, ".tgz" )
tarball.write( RESOURCES )
tarball.close
try:
var fsVersion = readFile( $result.path(".version") )
fsVersion.removeSuffix
if fsVersion == VERSION: return
except:
discard
extractAll( path, $dataDir )
removeFile( path )
echo "Extracting embedded resources for v{VERSION}".fmt
let source = dataDir / Path( "config.ini" )
if not fileExists( confFile ): moveFile( source, confFile )
createDir( getDataDir() )
createDir( $confDir )
let ( tarball, path ) = createTempFile( "{appname}_resources_".fmt, ".tgz" )
tarball.write( RESOURCES )
tarball.close
removeDir( $dataDir )
extractAll( path, $dataDir )
removeFile( path )
let source = dataDir / Path( "config.ini" )
if not fileExists( confFile ): copyFile( $source, $confFile )
let embeddedFs* = newEmbeddedFS()