Files
magpie/build/assetpack/main.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

92 lines
2.1 KiB
Go

// assetpack writes the GUI's assets as one zip, which a release build
// embeds in place of the tree (entries are under assets/, as go:embed names them) (-tags packedassets, internal/gui/assets_packed.go):
// the scripts, styles and svgs shrink to a quarter, where a Windows or Linux
// download is the raw binary. Usage: go run ./build/assetpack <assets dir> <out.zip>
package main
import (
"archive/zip"
"fmt"
"io/fs"
"os"
"path"
"path/filepath"
"sort"
"strings"
)
// already compressed: deflating them only costs time
var stored = map[string]bool{".png": true, ".jpg": true, ".jpeg": true, ".webm": true, ".mp4": true, ".woff": true, ".woff2": true, ".ico": true}
func main() {
if len(os.Args) != 3 {
fmt.Fprintln(os.Stderr, "usage: assetpack <assets dir> <out.zip>")
os.Exit(2)
}
if err := pack(os.Args[1], os.Args[2]); err != nil {
fmt.Fprintln(os.Stderr, "assetpack:", err)
os.Exit(1)
}
}
func pack(dir, out string) error {
var names []string
err := filepath.WalkDir(dir, func(p string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
// go:embed of a folder leaves out names that begin with . or _
if p != dir && (strings.HasPrefix(d.Name(), ".") || strings.HasPrefix(d.Name(), "_")) {
if d.IsDir() {
return filepath.SkipDir
}
return nil
}
if d.Type().IsRegular() {
rel, err := filepath.Rel(dir, p)
if err != nil {
return err
}
names = append(names, filepath.ToSlash(rel))
}
return nil
})
if err != nil {
return err
}
sort.Strings(names)
tmp := out + ".tmp"
f, err := os.Create(tmp)
if err != nil {
return err
}
zw := zip.NewWriter(f)
for _, n := range names {
b, err := os.ReadFile(filepath.Join(dir, filepath.FromSlash(n)))
if err != nil {
f.Close()
return err
}
h := &zip.FileHeader{Name: "assets/" + n, Method: zip.Deflate}
if stored[strings.ToLower(path.Ext(n))] {
h.Method = zip.Store
}
w, err := zw.CreateHeader(h)
if err == nil {
_, err = w.Write(b)
}
if err != nil {
f.Close()
return err
}
}
if err := zw.Close(); err != nil {
f.Close()
return err
}
if err := f.Close(); err != nil {
return err
}
return os.Rename(tmp, out)
}