package assets import ( "fmt" "path/filepath" "sort" "strings" "unicode" ) // Generated-file headers. Both outputs are regenerated wholesale from the // lockfile after every sync. const ( luauHeader = "-- generated by rotor asset sync; do not edit\n" typesHeader = "// generated by rotor asset sync; do not edit\n" ) // WriteOutputs regenerates the Luau module and/or the .d.ts declaration from // the lockfile, writing each atomically. Empty paths are skipped. Paths are // resolved relative to projectDir. func WriteOutputs(projectDir, luauPath, typesPath string, lock *Lockfile) error { if luauPath == "" && typesPath == "" { return nil } root, err := buildTree(lock) if err != nil { return err } if luauPath != "" { out := filepath.Join(projectDir, filepath.FromSlash(luauPath)) if err := writeFileAtomic(out, []byte(renderLuau(root))); err != nil { return fmt.Errorf("assets: writing %s: %w", luauPath, err) } } if typesPath != "" { out := filepath.Join(projectDir, filepath.FromSlash(typesPath)) if err := writeFileAtomic(out, []byte(renderTypes(root))); err != nil { return fmt.Errorf("assets: writing %s: %w", typesPath, err) } } return nil } // GenerateLuau renders the Luau accessor module for the lockfile: // `return { logo = "rbxassetid://123", sounds = { hit = "rbxassetid://456" } }`. func GenerateLuau(lock *Lockfile) (string, error) { root, err := buildTree(lock) if err != nil { return "", err } return renderLuau(root), nil } // GenerateTypes renders the matching .d.ts declaration: // `declare const assets: { readonly logo: string; ... }; export = assets;`. func GenerateTypes(lock *Lockfile) (string, error) { root, err := buildTree(lock) if err != nil { return "", err } return renderTypes(root), nil } // --- tree construction --- // genNode is one table (directory) or string leaf (asset) in the generated // module. source remembers the lockfile path that created the node, for // collision diagnostics. type genNode struct { children map[string]*genNode isLeaf bool assetID int64 source string } func newGenNode(source string) *genNode { return &genNode{children: map[string]*genNode{}, source: source} } // buildTree nests lockfile entries by their project-relative directory // structure. When every asset shares the same first path segment (typically // "assets/"), that segment is stripped so accessors read `assets.logo`, not // `assets.assets.logo`. Directory names and file stems are sanitized into // Luau identifiers; collisions are an error rather than a silent overwrite. func buildTree(lock *Lockfile) (*genNode, error) { paths := make([]string, 0, len(lock.Assets)) for p := range lock.Assets { paths = append(paths, p) } sort.Strings(paths) strip := sharedFirstSegment(paths) root := newGenNode("") for _, p := range paths { segs := strings.Split(p, "/") if strip != "" { segs = segs[1:] } cur := root for _, dir := range segs[:len(segs)-1] { id, err := sanitizeIdent(dir) if err != nil { return nil, fmt.Errorf("assets: codegen: folder %q in %s: %w", dir, p, err) } child := cur.children[id] if child == nil { child = newGenNode(p) cur.children[id] = child } else if child.isLeaf { return nil, fmt.Errorf("assets: codegen: identifier %q is both a file (%s) and a folder (%s); rename one", id, child.source, p) } cur = child } fileName := segs[len(segs)-1] id, err := sanitizeIdent(stem(fileName)) if err != nil { return nil, fmt.Errorf("assets: codegen: file %q (%s): %w", fileName, p, err) } if existing := cur.children[id]; existing != nil { if existing.isLeaf { return nil, fmt.Errorf("assets: codegen: identifier %q collides: %s and %s both generate it; rename one", id, existing.source, p) } return nil, fmt.Errorf("assets: codegen: identifier %q is both a folder (%s) and a file (%s); rename one", id, existing.source, p) } cur.children[id] = &genNode{isLeaf: true, assetID: lock.Assets[p].AssetID, source: p} } return root, nil } // sharedFirstSegment returns the first path segment if every path has at // least two segments and they all start with the same one, else "". func sharedFirstSegment(paths []string) string { if len(paths) == 0 { return "" } first := "" for i, p := range paths { idx := strings.IndexByte(p, '/') if idx < 0 { return "" } if i == 0 { first = p[:idx] } else if p[:idx] != first { return "" } } return first } // sortedChildKeys returns the node's child identifiers in sorted order. func sortedChildKeys(n *genNode) []string { keys := make([]string, 0, len(n.children)) for k := range n.children { keys = append(keys, k) } sort.Strings(keys) return keys } // --- rendering --- func renderLuau(root *genNode) string { var b strings.Builder b.WriteString(luauHeader) b.WriteString("return ") writeLuauNode(&b, root, 0) b.WriteString("\n") return b.String() } func writeLuauNode(b *strings.Builder, n *genNode, depth int) { if n.isLeaf { fmt.Fprintf(b, "%q", fmt.Sprintf("rbxassetid://%d", n.assetID)) return } if len(n.children) == 0 { b.WriteString("{}") return } b.WriteString("{\n") for _, k := range sortedChildKeys(n) { b.WriteString(strings.Repeat("\t", depth+1)) b.WriteString(k) b.WriteString(" = ") writeLuauNode(b, n.children[k], depth+1) b.WriteString(",\n") } b.WriteString(strings.Repeat("\t", depth)) b.WriteString("}") } func renderTypes(root *genNode) string { var b strings.Builder b.WriteString(typesHeader) b.WriteString("declare const assets: ") writeTypeNode(&b, root, 0) b.WriteString(";\nexport = assets;\n") return b.String() } func writeTypeNode(b *strings.Builder, n *genNode, depth int) { if n.isLeaf { b.WriteString("string") return } if len(n.children) == 0 { b.WriteString("{}") return } b.WriteString("{\n") for _, k := range sortedChildKeys(n) { b.WriteString(strings.Repeat("\t", depth+1)) b.WriteString("readonly ") b.WriteString(k) b.WriteString(": ") writeTypeNode(b, n.children[k], depth+1) b.WriteString(";\n") } b.WriteString(strings.Repeat("\t", depth)) b.WriteString("}") } // --- identifiers --- // luauKeywords are Luau's reserved words; a generated identifier matching one // is prefixed with "_". (`continue` is contextual in Luau, not reserved.) var luauKeywords = map[string]bool{ "and": true, "break": true, "do": true, "else": true, "elseif": true, "end": true, "false": true, "for": true, "function": true, "if": true, "in": true, "local": true, "nil": true, "not": true, "or": true, "repeat": true, "return": true, "then": true, "true": true, "until": true, "while": true, } // sanitizeIdent converts a file stem or directory name into a Luau- and // TypeScript-safe identifier: runs of non-alphanumeric characters (kebab, // snake, spaces, punctuation, non-ASCII) act as word separators and the // following word is capitalized (camelCase); a leading digit or a Luau // keyword gets a "_" prefix. Names with no usable characters are an error. // Luau identifiers are ASCII-only, so only [A-Za-z0-9] survive. func sanitizeIdent(name string) (string, error) { var b strings.Builder newWord := false for _, r := range name { if !isASCIIAlnum(r) { if b.Len() > 0 { newWord = true } continue } if newWord { r = unicode.ToUpper(r) newWord = false } b.WriteRune(r) } id := b.String() if id == "" { return "", fmt.Errorf("name %q produces an empty identifier", name) } if r := rune(id[0]); r >= '0' && r <= '9' { id = "_" + id } if luauKeywords[id] { id = "_" + id } return id, nil } func isASCIIAlnum(r rune) bool { return (r >= 'a' && r <= 'z') || (r >= 'A' && r <= 'Z') || (r >= '0' && r <= '9') }