Files
magpie/internal/gui/assets_packed.go
T
yetone 89f66966d9 release builds: the UI's files are packed into one zip, ~5.7 MB off each binary (@ZhangXiaof48473 on X)
The Windows and Linux downloads (and the CLI builds) are the raw binary, so
what is embedded counts in full. 8.2 MB of the 12 MB under internal/gui/assets
is scripts, styles and svgs. `make assets-pack` (build/assetpack) writes them
to internal/gui/assets.zip (deflate; png/jpg/webm stored), and the release
targets and `make app` build with -tags packedassets, which embeds that zip
instead of the tree (assets_packed.go: a file is unpacked once, on first use,
and served seekable with no modification time, as an embedded one is). Plain
`go build`, `make build`, `make cli` and every test still embed the tree.
The zip is git-ignored and made before every release build.

Sizes (go build -trimpath -ldflags="-s -w", on mote, at e430e2750):
  linux/amd64 nogui   73,203,874 -> 67,502,242  (-5,701,632)
  windows/amd64 app   88,401,408 -> 82,755,584  (-5,645,824)
The macOS zip/dmg compress the binary already, so they gain nothing.

Run: assetpack, then `go test -tags packedassets -run TestPacked ./internal/gui`
(passes: every file byte for byte and no other; / and a range of the webm served;
no Last-Modified). The first try failed it with "packed has 0 files, the tree
301" (zip entries lacked the assets/ prefix), so it does go red on a bad pack.
go vet ./internal/gui on darwin, linux nogui, windows and -tags packedassets:
clean. gofmt clean.

Not run: the full suite and the GUI suite (default builds embed the tree as
before; api.go only lost the embed line), a real release through
magpie-releases (its release.yml calls these make targets), the Linux GTK
build (cgo).
2026-11-11 10:28:05 +08:00

113 lines
2.5 KiB
Go

//go:build packedassets
package gui
import (
"archive/zip"
"bytes"
"errors"
"io"
"io/fs"
"sync"
"time"
_ "embed"
)
// A release build embeds the UI's files as one zip (build/assetpack, run by
// the Makefile before it builds): the scripts and styles are a quarter of
// their size, where the Windows and Linux downloads are the raw binary. A
// file is unpacked the first time it is asked for and kept, as the page's
// short list of files is all there is.
//
//go:embed assets.zip
var assetsZip []byte
var assets = &packedAssets{}
type packedAssets struct {
once sync.Once
zr *zip.Reader
err error
mu sync.Mutex
files map[string][]byte
}
func (p *packedAssets) reader() (*zip.Reader, error) {
p.once.Do(func() { p.zr, p.err = zip.NewReader(bytes.NewReader(assetsZip), int64(len(assetsZip))) })
return p.zr, p.err
}
// load unpacks a file, once; dir is true for a folder.
func (p *packedAssets) load(name string) (b []byte, st fs.FileInfo, dir bool, err error) {
zr, err := p.reader()
if err != nil {
return nil, nil, false, err
}
f, err := zr.Open(name)
if err != nil {
return nil, nil, false, err
}
defer f.Close()
if st, err = f.Stat(); err != nil || st.IsDir() {
return nil, st, err == nil, err
}
p.mu.Lock()
b, ok := p.files[name]
p.mu.Unlock()
if ok {
return b, st, false, nil
}
if b, err = io.ReadAll(f); err != nil {
return nil, nil, false, err
}
p.mu.Lock()
if p.files == nil {
p.files = map[string][]byte{}
}
p.files[name] = b
p.mu.Unlock()
return b, st, false, nil
}
// Open gives a file that seeks (a range request for a video needs it) and,
// as an embedded file does, has no modification time.
func (p *packedAssets) Open(name string) (fs.File, error) {
b, st, dir, err := p.load(name)
if err != nil {
return nil, err
}
if dir {
zr, _ := p.reader()
return zr.Open(name)
}
return &packedFile{Reader: bytes.NewReader(b), info: packedInfo{st, int64(len(b))}}, nil
}
func (p *packedAssets) ReadFile(name string) ([]byte, error) {
b, _, dir, err := p.load(name)
if err != nil {
return nil, err
}
if dir {
return nil, &fs.PathError{Op: "read", Path: name, Err: errors.New("is a directory")}
}
return bytes.Clone(b), nil
}
type packedFile struct {
*bytes.Reader
info packedInfo
}
func (f *packedFile) Stat() (fs.FileInfo, error) { return f.info, nil }
func (f *packedFile) Close() error { return nil }
type packedInfo struct {
fs.FileInfo
size int64
}
func (i packedInfo) Size() int64 { return i.size }
func (i packedInfo) ModTime() time.Time { return time.Time{} }