solutions: load a catalog without a disk (LoadFSWith, LoadBigPictureFS); skip *.conflict.md
LoadFSWith reads the Big Picture from its own fs.FS, so a catalog can be embedded -- the web wizard runs the engine in the browser as WebAssembly. The knowledge index leaves out wiki-sync conflict copies (*.conflict.md). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
c8a219a84b
commit
c1c0b8a116
4 files changed
+60
-6
No files matched your search
@@ -2,7 +2,9 @@ package catalog
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strings"
|
||||
@@ -15,7 +17,8 @@ import (
|
||||
// here they are decoded leniently and only the fields the guide uses are
|
||||
// kept.
|
||||
type BigPicture struct {
|
||||
Dir string `json:"-"`
|
||||
Dir string `json:"-"`
|
||||
fsys fs.FS
|
||||
Title string `json:"title"`
|
||||
WebPath string `json:"webPath"` // "map/"
|
||||
Topics map[string]*BPTopic `json:"topics"`
|
||||
@@ -89,8 +92,14 @@ type BPInterface struct {
|
||||
|
||||
// LoadBigPicture reads dir/manifest.yaml and the files it names.
|
||||
func LoadBigPicture(dir string) (*BigPicture, error) {
|
||||
return LoadBigPictureFS(os.DirFS(dir), dir)
|
||||
}
|
||||
|
||||
// LoadBigPictureFS reads manifest.yaml and the files it names from fsys
|
||||
// (an embedded copy, e.g. in the browser); dir only names it in messages.
|
||||
func LoadBigPictureFS(fsys fs.FS, dir string) (*BigPicture, error) {
|
||||
bp := &BigPicture{
|
||||
Dir: dir, WebPath: "map/",
|
||||
Dir: dir, fsys: fsys, WebPath: "map/",
|
||||
Topics: map[string]*BPTopic{}, Stories: map[string]*BPStory{},
|
||||
Products: map[string]*BPProduct{}, Interfaces: map[string]BPInterface{},
|
||||
}
|
||||
@@ -157,7 +166,7 @@ func (bp *BigPicture) read(name string, v any) error {
|
||||
if name == "" {
|
||||
return fmt.Errorf("big picture: manifest names no file")
|
||||
}
|
||||
b, err := os.ReadFile(filepath.Join(bp.Dir, name))
|
||||
b, err := fs.ReadFile(bp.fsys, path.Clean(filepath.ToSlash(name)))
|
||||
if err != nil {
|
||||
return fmt.Errorf("big picture: %w", err)
|
||||
}
|
||||
@@ -172,7 +181,7 @@ func (bp *BigPicture) read(name string, v any) error {
|
||||
}
|
||||
|
||||
func (bp *BigPicture) readDir(sub string, fn func(string) error) error {
|
||||
ents, err := os.ReadDir(filepath.Join(bp.Dir, sub))
|
||||
ents, err := fs.ReadDir(bp.fsys, path.Clean(filepath.ToSlash(sub)))
|
||||
if err != nil {
|
||||
return fmt.Errorf("big picture: %w", err)
|
||||
}
|
||||
|
||||
@@ -156,6 +156,14 @@ func Load(root string) (*Catalog, error) {
|
||||
// LoadFS reads a catalog from fsys (root is only used for messages and
|
||||
// to find the Big Picture relative to it, when fsys is a directory).
|
||||
func LoadFS(fsys fs.FS, root string) (*Catalog, error) {
|
||||
return LoadFSWith(fsys, root, nil)
|
||||
}
|
||||
|
||||
// LoadFSWith is LoadFS with the Big Picture read from bigPicture instead
|
||||
// of the directory catalog.yaml names -- for a catalog without a disk
|
||||
// (embedded in a program, or in the browser as WebAssembly). nil: as
|
||||
// LoadFS.
|
||||
func LoadFSWith(fsys fs.FS, root string, bigPicture fs.FS) (*Catalog, error) {
|
||||
c := &Catalog{
|
||||
Root: root,
|
||||
Capabilities: map[string]*Capability{},
|
||||
@@ -206,7 +214,15 @@ func LoadFS(fsys fs.FS, root string) (*Catalog, error) {
|
||||
if !sawRoot {
|
||||
c.add("error", "catalog.yaml", 0, "", "no-root", "no Catalog manifest (catalog.yaml) found")
|
||||
}
|
||||
if c.Spec.BigPicture != "" {
|
||||
if c.Spec.BigPicture != "" && bigPicture != nil {
|
||||
bp, err := LoadBigPictureFS(bigPicture, c.Spec.BigPicture)
|
||||
if err != nil {
|
||||
c.add("error", "catalog.yaml", 0, "", "bigpicture", err.Error())
|
||||
} else {
|
||||
c.BigPicture = bp
|
||||
c.Files = append(c.Files, bp.Files...)
|
||||
}
|
||||
} else if c.Spec.BigPicture != "" {
|
||||
bpDir := c.Spec.BigPicture
|
||||
if !filepath.IsAbs(bpDir) {
|
||||
bpDir = filepath.Join(root, bpDir)
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
package catalog
|
||||
|
||||
import (
|
||||
"os"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// A catalog without a disk (embedded, or in the browser) loads the Big
|
||||
// Picture from its own fs.FS and gives the same catalog as from disk.
|
||||
func TestLoadFSWith(t *testing.T) {
|
||||
disk, err := Load("../engine/testdata/cat")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// "/nonexistent" as root: nothing may be read from disk
|
||||
emb, err := LoadFSWith(os.DirFS("../engine/testdata/cat"), "/nonexistent", os.DirFS("../engine/testdata/bp"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if emb.BigPicture == nil || len(emb.BigPicture.Products) == 0 {
|
||||
t.Fatal("no Big Picture")
|
||||
}
|
||||
if !reflect.DeepEqual(SortedIDs(disk.BigPicture.Products), SortedIDs(emb.BigPicture.Products)) || disk.Hash() != emb.Hash() {
|
||||
t.Fatalf("embedded catalog differs: %v / %v", disk.Hash(), emb.Hash())
|
||||
}
|
||||
}
|
||||
@@ -224,7 +224,9 @@ func (ix *Index) manuals() {
|
||||
if d.IsDir() && strings.HasPrefix(d.Name(), ".") {
|
||||
return fs.SkipDir
|
||||
}
|
||||
if !d.IsDir() && strings.HasSuffix(p, ".md") {
|
||||
// *.conflict.md: a wiki-sync conflict copy (meta-repo
|
||||
// AGENTS.md), never documentation of its own
|
||||
if !d.IsDir() && strings.HasSuffix(p, ".md") && !strings.HasSuffix(p, ".conflict.md") {
|
||||
files = append(files, rel)
|
||||
}
|
||||
return nil
|
||||
|
||||
Reference in new issue
Block a user