Documentation
¶
Overview ¶
Package manifest walks an assets directory and generates a typed Go source file referencing every asset, so missing or removed assets fail the build at compile time.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Generate ¶
Generate scans dir and returns a gofmt-formatted Go source file declaring typed references for every asset in dir.
Types ¶
type Entry ¶
type Entry struct {
// Path is the asset's slash-separated path relative to the scanned directory.
Path string
// URI is the asset's URI with the configured prefix, such as "asset:hero.png".
URI string
// Ident is the exported Go identifier generated for this asset, such as "ImageHero".
Ident string
// Kind is the asset's classified type.
Kind Kind
}
Entry is one asset file discovered in the assets directory.
func Scan ¶
Scan walks dir deterministically, skipping dot-prefixed entries, Go source files and import settings sidecars (files named *.import.yaml, as package importsettings names them), and returns the discovered asset entries sorted by Path with unique Go identifiers. When opts.Embed is true, Scan also rejects any asset path (or opts.EmbedDir) that Go's //go:embed cannot embed.
Example ¶
Scan lists a game's assets with the typed Go names the generated manifest gives them, so code refers to assets by checked identifiers instead of strings; Verify reports references to files that are missing.
package main
import (
"fmt"
"os"
"path/filepath"
"github.com/nsgo-dev/game/pkg/tooling/manifest"
)
func main() {
dir, err := os.MkdirTemp("", "assets")
if err != nil {
fmt.Println(err)
return
}
defer os.RemoveAll(dir)
for _, name := range []string{"hero.png", "sounds/jump.wav", "levels/level-1.tmj"} {
path := filepath.Join(dir, filepath.FromSlash(name))
os.MkdirAll(filepath.Dir(path), 0o755)
os.WriteFile(path, []byte("x"), 0o644)
}
entries, err := manifest.Scan(dir, manifest.DefaultOptions())
if err != nil {
fmt.Println(err)
return
}
for _, e := range entries {
fmt.Println(e.Ident, e.URI, e.Kind)
}
fmt.Println(manifest.Verify(dir, []string{"asset:hero.png"}))
fmt.Println(manifest.Verify(dir, []string{"asset:villain.png"}) != nil)
}
Output: ImageHero asset:hero.png image MapLevelsLevel1 asset:levels/level-1.tmj map SoundSoundsJump asset:sounds/jump.wav audio <nil> true
type Kind ¶
type Kind int
Kind classifies a discovered asset file by its extension.
const ( // KindOther is any asset that is not an image, audio or map file. KindOther Kind = iota // KindImage is an image file exposed as a core.ImageSource. KindImage // KindAudio is an audio file exposed as an audio.Source. KindAudio // KindMap is a tilemap file (.tmj, .tmx, .tsj, .tsx). KindMap )
type Options ¶
type Options struct {
// Package is the generated Go file's package name ("assets" when empty).
Package string
// Prefix is prepended to each asset's relative path to form its URI ("asset:" when empty).
Prefix string
// Embed emits //go:embed directives so asset bytes are compiled into the binary instead of referencing URIs.
Embed bool
// EmbedDir is an optional slash-separated directory prefix prepended to each //go:embed pattern, for when the generated file lives in a parent of the assets directory.
EmbedDir string
// Header is the comment line at the top of the generated file (a standard generated-file notice when empty).
Header string
}
Options configures asset scanning and Go source generation.
func DefaultOptions ¶
func DefaultOptions() Options
DefaultOptions returns Options with the generator's defaults: package "assets", URI prefix "asset:" and URI mode.