manifest

package
v0.2.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 28, 2026 License: Apache-2.0 Imports: 15 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

func Generate(dir string, opts Options) ([]byte, error)

Generate scans dir and returns a gofmt-formatted Go source file declaring typed references for every asset in dir.

func Verify

func Verify(dir string, refs []string) error

Verify checks that every asset reference in refs exists as a regular file under dir. Each ref may be a relative path ("hero.png") or an "asset:" URI ("asset:hero.png").

func WriteFile

func WriteFile(dir, outPath string, opts Options) error

WriteFile generates the manifest for dir and writes it to outPath, creating parent directories if needed.

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

func Scan(dir string, opts Options) ([]Entry, error)

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
)

func (Kind) String

func (k Kind) String() string

String returns the kind's name.

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL