A seed that sets master page content, a theme preset, mailing lists, posts, email templates or page SEO must declare a plugin_sdk constraint that refuses hosts before v0.3.11, so an older CMS rejects the artifact instead of applying half a site. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
745 lines
25 KiB
Go
745 lines
25 KiB
Go
// Package bnp builds codeless .bnp plugin artifacts: it validates the
|
|
// declarative plugin files and packs them as a zstd-compressed tar.
|
|
package bnp
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"io/fs"
|
|
"os"
|
|
"path"
|
|
"path/filepath"
|
|
"strings"
|
|
"time"
|
|
|
|
abiv1 "git.dev.alexdunmow.com/block/pluginsdk/abi/v1"
|
|
core "git.dev.alexdunmow.com/block/pluginsdk/plugin"
|
|
"golang.org/x/mod/semver"
|
|
"google.golang.org/protobuf/proto"
|
|
"gopkg.in/yaml.v3"
|
|
)
|
|
|
|
// Codeless artifacts (WO-WZ-020): a .bnp with NO plugin.wasm — pure
|
|
// declaration the host runs. Classification is by repo shape: a plugin repo
|
|
// with no Go source builds codeless; a repo with Go builds wasm as always
|
|
// (a converted repo DELETES its Go — that's the point). Both artifact kinds
|
|
// may carry blocks/, templates/, and seed/; codeless just has nothing else.
|
|
|
|
// Required top-level artifact members (mirrors the CMS reader).
|
|
const (
|
|
FileMod = "plugin.mod"
|
|
FileManifest = "manifest.pb"
|
|
)
|
|
|
|
// Declarative artifact dirs shared by both build paths.
|
|
const (
|
|
DirBlocks = "blocks"
|
|
DirTemplates = "templates"
|
|
DirSeed = "seed"
|
|
)
|
|
|
|
// hostAbiVersion is the ABI major version the packer speaks. It must equal
|
|
// the guest AbiVersion / the reader's supported major (1).
|
|
const hostAbiVersion uint32 = 1
|
|
|
|
// manifestYAMLName is the optional root-level declarative manifest source for
|
|
// codeless plugins (the counterpart of what DESCRIBE captures from Go code).
|
|
const manifestYAMLName = "manifest.yaml"
|
|
|
|
// BuildOptions configures BuildCodeless.
|
|
type BuildOptions struct {
|
|
// Dir is the plugin repo root (holds plugin.mod and the main Go package).
|
|
Dir string
|
|
// Output is the destination .bnp path. Empty → "<name>-<version>.bnp" in
|
|
// the current working directory.
|
|
Output string
|
|
}
|
|
|
|
// BuildResult summarizes a produced artifact for the CLI summary table.
|
|
type BuildResult struct {
|
|
OutputPath string
|
|
Name string
|
|
Version string
|
|
// Codeless marks a declarative artifact (no plugin.wasm, WO-WZ-020).
|
|
Codeless bool
|
|
WasmBytes int64
|
|
ManifestBytes int64
|
|
ArtifactBytes int64 // on-disk .bnp size
|
|
UncompBytes int64 // sum of packed file sizes
|
|
BlockCount int
|
|
TemplateCount int
|
|
AdminPages int
|
|
JobTypes int
|
|
Hooks []string
|
|
DataDir bool
|
|
IncludedDirs []string
|
|
// PreviewIncluded/ScreenshotCount report the packed presentation members
|
|
// (root preview.png + screenshots/); the registry ingests these into the
|
|
// plugin's gallery at publish.
|
|
PreviewIncluded bool
|
|
ScreenshotCount int
|
|
}
|
|
|
|
// manifestYAML mirrors the declarative PluginManifest fields a codeless
|
|
// plugin can set. File-valued keys are paths relative to the repo root.
|
|
type manifestYAML struct {
|
|
ThemePresets string `yaml:"theme_presets"` // JSON file
|
|
BundledFonts string `yaml:"bundled_fonts"` // JSON file
|
|
SettingsSchema string `yaml:"settings_schema"` // JSON file
|
|
MasterPages string `yaml:"master_pages"` // JSON file ([]masterPageJSON)
|
|
RequiredIconPacks []string `yaml:"required_icon_packs"`
|
|
SystemTemplates []struct {
|
|
Key string `yaml:"key"`
|
|
Title string `yaml:"title"`
|
|
Description string `yaml:"description"`
|
|
} `yaml:"system_templates"`
|
|
PageTemplates []struct {
|
|
System string `yaml:"system"`
|
|
Key string `yaml:"key"`
|
|
Title string `yaml:"title"`
|
|
Description string `yaml:"description"`
|
|
Slots []string `yaml:"slots"`
|
|
} `yaml:"page_templates"`
|
|
// TemplateOverrides declare theme-scoped overrides of builtin block
|
|
// rendering. Source convention: templates/overrides/<template>/<block>.ninjatpl,
|
|
// rendered host-side with the block's content map as the template context.
|
|
TemplateOverrides []struct {
|
|
Template string `yaml:"template"` // template/system key the override binds to
|
|
Block string `yaml:"block"` // builtin block key being overridden
|
|
} `yaml:"template_overrides"`
|
|
// EmailWrappers declare branded email wrappers per system key. Source
|
|
// convention: templates/email/<system>.ninjatpl, rendered host-side with
|
|
// body/colors/site/unsubscribe_url/preview_text in the context (emit the
|
|
// pre-rendered body with |safe).
|
|
EmailWrappers []string `yaml:"email_wrappers"`
|
|
CSS *struct {
|
|
NpmPackages map[string]string `yaml:"npm_packages"`
|
|
CSSDirectives []string `yaml:"css_directives"`
|
|
InputCSSAppend string `yaml:"input_css_append"`
|
|
} `yaml:"css"`
|
|
Dependencies []struct {
|
|
Plugin string `yaml:"plugin"`
|
|
MinVersion string `yaml:"min_version"`
|
|
Required bool `yaml:"required"`
|
|
} `yaml:"dependencies"`
|
|
}
|
|
|
|
// masterPageJSON mirrors abiv1.MasterPageDefinition for the declarative file.
|
|
type masterPageJSON struct {
|
|
Key string `json:"key"`
|
|
Title string `json:"title"`
|
|
PageTemplates []string `json:"page_templates"`
|
|
Blocks []struct {
|
|
BlockKey string `json:"block_key"`
|
|
Title string `json:"title"`
|
|
Content map[string]any `json:"content"`
|
|
HTMLContent *string `json:"html_content"`
|
|
Slot string `json:"slot"`
|
|
SortOrder int32 `json:"sort_order"`
|
|
} `json:"blocks"`
|
|
}
|
|
|
|
// blocksYAML is the structural mirror of the CMS blocks.yaml manifest
|
|
// (cms blocks.LoadManifest) — a deliberate light duplication, same as the
|
|
// Verify↔reader pairing: the packer validates shape and file presence; full
|
|
// semantic validation (schema/template/provider checks) runs at install.
|
|
type blocksYAML struct {
|
|
Blocks []struct {
|
|
Key string `yaml:"key"`
|
|
Title string `yaml:"title"`
|
|
Description string `yaml:"description"`
|
|
Category string `yaml:"category"`
|
|
Schema string `yaml:"schema"`
|
|
Template string `yaml:"template"`
|
|
SampleData string `yaml:"sample_data"`
|
|
Providers []string `yaml:"providers"`
|
|
RequiredTags []string `yaml:"required_tags"`
|
|
Aliases []string `yaml:"aliases"`
|
|
} `yaml:"blocks"`
|
|
}
|
|
|
|
// seedJSON is the declarative seed schema (seed/seed.json). Applied by the
|
|
// host at load via the WO-WZ-019 provisioner (idempotent).
|
|
type seedJSON struct {
|
|
Settings *struct {
|
|
Merge map[string]any `json:"merge"`
|
|
Override map[string]any `json:"override"`
|
|
Ensure map[string]any `json:"ensure"`
|
|
} `json:"settings"`
|
|
Media []struct {
|
|
ID string `json:"id"` // UUID, required (deterministic media key)
|
|
File string `json:"file"`
|
|
Alt string `json:"alt"`
|
|
Folder string `json:"folder"`
|
|
} `json:"media"`
|
|
Pages []struct {
|
|
Slug string `json:"slug"`
|
|
ParentSlug string `json:"parent_slug"`
|
|
Title string `json:"title"`
|
|
TemplateKey string `json:"template_key"`
|
|
ReconcileBlocks bool `json:"reconcile_blocks"`
|
|
ReconcileTemplate bool `json:"reconcile_template"`
|
|
Blocks []struct {
|
|
BlockKey string `json:"block_key"`
|
|
Title string `json:"title"`
|
|
Content map[string]any `json:"content"`
|
|
HTMLContent *string `json:"html_content"`
|
|
Slot string `json:"slot"`
|
|
SortOrder int32 `json:"sort_order"`
|
|
} `json:"blocks"`
|
|
SEO *seedSEO `json:"seo"`
|
|
ReplaceExisting bool `json:"replace_existing"`
|
|
} `json:"pages"`
|
|
MenuItems []struct {
|
|
Menu string `json:"menu"`
|
|
Label string `json:"label"`
|
|
URL string `json:"url"`
|
|
PageSlug string `json:"page_slug"`
|
|
SortOrder int32 `json:"sort_order"`
|
|
} `json:"menu_items"`
|
|
DataTables []struct {
|
|
Key string `json:"key"`
|
|
} `json:"data_tables"`
|
|
MasterPages []struct {
|
|
Template string `json:"template"`
|
|
Blocks []struct {
|
|
BlockKey string `json:"block_key"`
|
|
Content map[string]any `json:"content"`
|
|
} `json:"blocks"`
|
|
} `json:"master_pages"`
|
|
ThemePreset *struct {
|
|
Template string `json:"template"`
|
|
Preset string `json:"preset"`
|
|
} `json:"theme_preset"`
|
|
MailingLists []struct {
|
|
Slug string `json:"slug"`
|
|
Name string `json:"name"`
|
|
} `json:"mailing_lists"`
|
|
Posts []struct {
|
|
Key string `json:"key"`
|
|
Slug string `json:"slug"`
|
|
Title string `json:"title"`
|
|
Body []map[string]any `json:"body"`
|
|
Status string `json:"status"`
|
|
PostDate string `json:"post_date"`
|
|
SEO *seedSEO `json:"seo"`
|
|
} `json:"posts"`
|
|
EmailTemplates []struct {
|
|
Name string `json:"name"`
|
|
Subject string `json:"subject"`
|
|
Body string `json:"body"`
|
|
Table string `json:"table"`
|
|
} `json:"email_templates"`
|
|
}
|
|
|
|
type seedSEO struct {
|
|
MetaTitle string `json:"meta_title"`
|
|
MetaDescription string `json:"meta_description"`
|
|
}
|
|
|
|
// SeedContentMinSDK is the plugin SDK release whose hosts apply the seed
|
|
// sections master_pages, theme_preset, mailing_lists, posts, email_templates
|
|
// and the page fields seo and replace_existing. An older host ignores them, so
|
|
// an artifact that uses them must refuse such hosts through
|
|
// compatibility.plugin_sdk.
|
|
const SeedContentMinSDK = "v0.3.11"
|
|
|
|
// BuildCodeless builds the codeless plugin repo in opts.Dir with
|
|
// BuildCodelessFS and writes the artifact to opts.Output.
|
|
func BuildCodeless(opts BuildOptions) (*BuildResult, error) {
|
|
dir := opts.Dir
|
|
if dir == "" {
|
|
dir = "."
|
|
}
|
|
dir, err := filepath.Abs(dir)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("resolve dir: %w", err)
|
|
}
|
|
|
|
artifact, res, err := BuildCodelessFS(os.DirFS(dir))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
outPath := opts.Output
|
|
if outPath == "" {
|
|
outPath = fmt.Sprintf("%s-%s.bnp", res.Name, res.Version)
|
|
}
|
|
if err := os.WriteFile(outPath, artifact, 0o666); err != nil {
|
|
return nil, fmt.Errorf("write %s: %w", outPath, err)
|
|
}
|
|
res.OutputPath = outPath
|
|
return res, nil
|
|
}
|
|
|
|
// BuildCodelessFS synthesizes manifest.pb from plugin.mod + the declarative
|
|
// files in fsys and returns a packed codeless .bnp (no plugin.wasm, no
|
|
// DESCRIBE probe).
|
|
func BuildCodelessFS(fsys fs.FS) ([]byte, *BuildResult, error) {
|
|
modBytes, err := fs.ReadFile(fsys, FileMod)
|
|
if err != nil {
|
|
return nil, nil, fmt.Errorf("read plugin.mod: %w", err)
|
|
}
|
|
mod, err := core.ParseModFull(modBytes)
|
|
if err != nil {
|
|
return nil, nil, fmt.Errorf("parse plugin.mod: %w", err)
|
|
}
|
|
if mod.Plugin.Name == "" || mod.Plugin.Version == "" {
|
|
return nil, nil, fmt.Errorf("plugin.mod must set both name and version")
|
|
}
|
|
if mod.Plugin.DataDir {
|
|
return nil, nil, fmt.Errorf("codeless plugin cannot request data_dir — there is no code to use it")
|
|
}
|
|
if len(mod.Plugin.PublicRoutes) > 0 || mod.Plugin.Sitemap {
|
|
return nil, nil, fmt.Errorf("codeless plugin cannot declare public_routes or sitemap: there is no HTTP handler to serve them")
|
|
}
|
|
|
|
manifest := &abiv1.PluginManifest{
|
|
AbiVersion: hostAbiVersion,
|
|
Name: mod.Plugin.Name,
|
|
Version: mod.Plugin.Version,
|
|
Codeless: true,
|
|
}
|
|
|
|
blockCount, err := ValidateBlocksDir(fsys)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
if err := ValidateSeedDir(fsys); err != nil {
|
|
return nil, nil, err
|
|
}
|
|
if err := ApplyManifestYAML(fsys, manifest); err != nil {
|
|
return nil, nil, err
|
|
}
|
|
|
|
manifestBytes, err := proto.Marshal(manifest)
|
|
if err != nil {
|
|
return nil, nil, fmt.Errorf("marshal manifest.pb: %w", err)
|
|
}
|
|
|
|
entries := []Entry{
|
|
{ArtifactPath: FileMod, Data: modBytes},
|
|
{ArtifactPath: FileManifest, Data: manifestBytes},
|
|
}
|
|
var includedDirs []string
|
|
optional := []struct{ artifact, src string }{
|
|
{DirBlocks, DirBlocks},
|
|
{DirTemplates, DirTemplates},
|
|
{DirSeed, DirSeed},
|
|
{"migrations", "migrations"},
|
|
{"schemas", "schemas"},
|
|
{"assets", "assets"},
|
|
}
|
|
for _, o := range optional {
|
|
dirEntries, has, dErr := CollectDir(fsys, o.src, o.artifact)
|
|
if dErr != nil {
|
|
return nil, nil, dErr
|
|
}
|
|
if has {
|
|
entries = append(entries, dirEntries...)
|
|
includedDirs = append(includedDirs, o.artifact)
|
|
}
|
|
}
|
|
presEntries, previewIncluded, screenshotCount, err := PresentationEntries(fsys)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
entries = append(entries, presEntries...)
|
|
if screenshotCount > 0 {
|
|
includedDirs = append(includedDirs, DirScreenshots)
|
|
}
|
|
|
|
var artifact bytes.Buffer
|
|
uncomp, err := Pack(&artifact, fsys, entries)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
|
|
return artifact.Bytes(), &BuildResult{
|
|
Name: manifest.GetName(),
|
|
Version: manifest.GetVersion(),
|
|
Codeless: true,
|
|
ManifestBytes: int64(len(manifestBytes)),
|
|
ArtifactBytes: int64(artifact.Len()),
|
|
UncompBytes: uncomp,
|
|
BlockCount: blockCount,
|
|
IncludedDirs: includedDirs,
|
|
PreviewIncluded: previewIncluded,
|
|
ScreenshotCount: screenshotCount,
|
|
}, nil
|
|
}
|
|
|
|
// ValidateBlocksDir structurally validates blocks/blocks.yaml when present:
|
|
// every entry has key/title/schema/template, referenced files exist within
|
|
// blocks/, and schemas are valid JSON. Returns the block count (0 when the
|
|
// dir is absent).
|
|
func ValidateBlocksDir(fsys fs.FS) (int, error) {
|
|
root := DirBlocks
|
|
manifestPath := path.Join(root, "blocks.yaml")
|
|
if _, err := fs.Stat(fsys, root); errors.Is(err, fs.ErrNotExist) {
|
|
return 0, nil
|
|
}
|
|
raw, err := fs.ReadFile(fsys, manifestPath)
|
|
if err != nil {
|
|
return 0, fmt.Errorf("blocks/ present but blocks.yaml unreadable: %w", err)
|
|
}
|
|
var bf blocksYAML
|
|
if err := yaml.Unmarshal(raw, &bf); err != nil {
|
|
return 0, fmt.Errorf("parse blocks/blocks.yaml: %w", err)
|
|
}
|
|
seen := map[string]bool{}
|
|
for i, b := range bf.Blocks {
|
|
if b.Key == "" {
|
|
return 0, fmt.Errorf("blocks.yaml: entry %d has no key", i)
|
|
}
|
|
if seen[b.Key] {
|
|
return 0, fmt.Errorf("blocks.yaml: duplicate key %q", b.Key)
|
|
}
|
|
seen[b.Key] = true
|
|
if b.Title == "" || b.Schema == "" || b.Template == "" {
|
|
return 0, fmt.Errorf("blocks.yaml: %q must declare title, schema, and template", b.Key)
|
|
}
|
|
for _, f := range []string{b.Schema, b.Template, b.SampleData} {
|
|
if f == "" {
|
|
continue
|
|
}
|
|
if err := fileWithin(fsys, root, f); err != nil {
|
|
return 0, fmt.Errorf("blocks.yaml: %q: %w", b.Key, err)
|
|
}
|
|
}
|
|
schemaRaw, err := fs.ReadFile(fsys, path.Join(root, b.Schema))
|
|
if err != nil {
|
|
return 0, fmt.Errorf("blocks.yaml: %q schema: %w", b.Key, err)
|
|
}
|
|
if !json.Valid(schemaRaw) {
|
|
return 0, fmt.Errorf("blocks.yaml: %q schema %s is not valid JSON", b.Key, b.Schema)
|
|
}
|
|
}
|
|
return len(bf.Blocks), nil
|
|
}
|
|
|
|
// ValidateSeedDir validates seed/seed.json when the dir is present: it
|
|
// parses, media entries carry an id + an existing file, pages carry slugs, the
|
|
// content sections carry their required fields, and a seed that uses them
|
|
// declares a plugin_sdk constraint that refuses hosts before SeedContentMinSDK.
|
|
func ValidateSeedDir(fsys fs.FS) error {
|
|
root := DirSeed
|
|
if _, err := fs.Stat(fsys, root); errors.Is(err, fs.ErrNotExist) {
|
|
return nil
|
|
}
|
|
raw, err := fs.ReadFile(fsys, path.Join(root, "seed.json"))
|
|
if err != nil {
|
|
return fmt.Errorf("seed/ present but seed.json unreadable: %w", err)
|
|
}
|
|
var sf seedJSON
|
|
if err := json.Unmarshal(raw, &sf); err != nil {
|
|
return fmt.Errorf("parse seed/seed.json: %w", err)
|
|
}
|
|
for i, m := range sf.Media {
|
|
if m.ID == "" || m.File == "" {
|
|
return fmt.Errorf("seed.json: media entry %d needs both id and file", i)
|
|
}
|
|
if err := fileWithin(fsys, root, m.File); err != nil {
|
|
return fmt.Errorf("seed.json: media %q: %w", m.ID, err)
|
|
}
|
|
}
|
|
for i, p := range sf.Pages {
|
|
if p.Slug == "" || p.Title == "" {
|
|
return fmt.Errorf("seed.json: page entry %d needs slug and title", i)
|
|
}
|
|
}
|
|
for i, mi := range sf.MenuItems {
|
|
if mi.Menu == "" || mi.Label == "" {
|
|
return fmt.Errorf("seed.json: menu item %d needs menu and label", i)
|
|
}
|
|
}
|
|
if err := validateSeedContent(&sf); err != nil {
|
|
return err
|
|
}
|
|
if !sf.usesContentSections() {
|
|
return nil
|
|
}
|
|
modBytes, err := fs.ReadFile(fsys, FileMod)
|
|
if err != nil {
|
|
return fmt.Errorf("read plugin.mod: %w", err)
|
|
}
|
|
mod, err := core.ParseModFull(modBytes)
|
|
if err != nil {
|
|
return fmt.Errorf("parse plugin.mod: %w", err)
|
|
}
|
|
var constraint string
|
|
if mod.Compatibility != nil {
|
|
constraint = mod.Compatibility.PluginSDK
|
|
}
|
|
if !constraintExcludesBelow(constraint, SeedContentMinSDK) {
|
|
return fmt.Errorf("seed.json uses sections that hosts before plugin SDK %s ignore; set compatibility.plugin_sdk to \">=%s\" or later (now %q)",
|
|
SeedContentMinSDK, strings.TrimPrefix(SeedContentMinSDK, "v"), constraint)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (sf *seedJSON) usesContentSections() bool {
|
|
if len(sf.MasterPages) > 0 || sf.ThemePreset != nil || len(sf.MailingLists) > 0 || len(sf.Posts) > 0 || len(sf.EmailTemplates) > 0 {
|
|
return true
|
|
}
|
|
for _, p := range sf.Pages {
|
|
if p.SEO != nil || p.ReplaceExisting {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func validateSeedContent(sf *seedJSON) error {
|
|
tables := map[string]bool{}
|
|
for _, dt := range sf.DataTables {
|
|
tables[dt.Key] = true
|
|
}
|
|
for i, mp := range sf.MasterPages {
|
|
if mp.Template == "" || len(mp.Blocks) == 0 {
|
|
return fmt.Errorf("seed.json: master_pages entry %d needs a template and blocks", i)
|
|
}
|
|
for j, b := range mp.Blocks {
|
|
if b.BlockKey == "" || len(b.Content) == 0 {
|
|
return fmt.Errorf("seed.json: master_pages %q block %d needs block_key and content", mp.Template, j)
|
|
}
|
|
}
|
|
}
|
|
if tp := sf.ThemePreset; tp != nil && (tp.Template == "" || tp.Preset == "") {
|
|
return fmt.Errorf("seed.json: theme_preset needs template and preset")
|
|
}
|
|
lists := map[string]bool{}
|
|
for i, ml := range sf.MailingLists {
|
|
if ml.Slug == "" || lists[ml.Slug] {
|
|
return fmt.Errorf("seed.json: mailing list %d needs a unique slug", i)
|
|
}
|
|
lists[ml.Slug] = true
|
|
}
|
|
keys, slugs := map[string]bool{}, map[string]bool{}
|
|
for i, p := range sf.Posts {
|
|
if p.Key == "" || p.Slug == "" || p.Title == "" {
|
|
return fmt.Errorf("seed.json: post %d needs key, slug and title", i)
|
|
}
|
|
if keys[p.Key] || slugs[p.Slug] {
|
|
return fmt.Errorf("seed.json: post %q repeats a key or slug", p.Key)
|
|
}
|
|
keys[p.Key], slugs[p.Slug] = true, true
|
|
if strings.Contains(p.Slug, "/") {
|
|
return fmt.Errorf("seed.json: post %q slug %q must be a single segment", p.Key, p.Slug)
|
|
}
|
|
if p.Status != "" && p.Status != "published" && p.Status != "draft" {
|
|
return fmt.Errorf("seed.json: post %q status %q must be published or draft", p.Key, p.Status)
|
|
}
|
|
if p.PostDate != "" {
|
|
if _, err := time.Parse("2006-01-02", p.PostDate); err != nil {
|
|
return fmt.Errorf("seed.json: post %q post_date %q must be YYYY-MM-DD", p.Key, p.PostDate)
|
|
}
|
|
}
|
|
if len(p.Body) == 0 {
|
|
return fmt.Errorf("seed.json: post %q needs a body", p.Key)
|
|
}
|
|
}
|
|
names := map[string]bool{}
|
|
for i, et := range sf.EmailTemplates {
|
|
if et.Name == "" || et.Subject == "" || et.Body == "" || names[et.Name] {
|
|
return fmt.Errorf("seed.json: email template %d needs a unique name, a subject and a body", i)
|
|
}
|
|
names[et.Name] = true
|
|
if et.Table != "" && !tables[et.Table] {
|
|
return fmt.Errorf("seed.json: email template %q names data table %q that data_tables does not declare", et.Name, et.Table)
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// constraintExcludesBelow reports whether a plugin_sdk constraint such as
|
|
// ">=0.3.11 <0.4.0" admits no host version below minVersion.
|
|
func constraintExcludesBelow(constraint, minVersion string) bool {
|
|
for tok := range strings.FieldsSeq(constraint) {
|
|
op, v := "", tok
|
|
for _, o := range []string{">=", "==", ">", "="} {
|
|
if after, ok := strings.CutPrefix(tok, o); ok {
|
|
op, v = o, after
|
|
break
|
|
}
|
|
}
|
|
if op == "" {
|
|
continue
|
|
}
|
|
if !strings.HasPrefix(v, "v") {
|
|
v = "v" + v
|
|
}
|
|
if !semver.IsValid(v) {
|
|
continue
|
|
}
|
|
if semver.Compare(v, minVersion) >= 0 {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// ApplyManifestYAML folds the optional root manifest.yaml into the manifest.
|
|
func ApplyManifestYAML(fsys fs.FS, m *abiv1.PluginManifest) error {
|
|
raw, err := fs.ReadFile(fsys, manifestYAMLName)
|
|
if errors.Is(err, fs.ErrNotExist) {
|
|
return nil
|
|
}
|
|
if err != nil {
|
|
return fmt.Errorf("read %s: %w", manifestYAMLName, err)
|
|
}
|
|
var my manifestYAML
|
|
if err := yaml.Unmarshal(raw, &my); err != nil {
|
|
return fmt.Errorf("parse %s: %w", manifestYAMLName, err)
|
|
}
|
|
|
|
readJSONFile := func(rel, what string) ([]byte, error) {
|
|
if rel == "" {
|
|
return nil, nil
|
|
}
|
|
if err := fileWithin(fsys, ".", rel); err != nil {
|
|
return nil, fmt.Errorf("%s: %s: %w", manifestYAMLName, what, err)
|
|
}
|
|
b, err := fs.ReadFile(fsys, path.Clean(rel))
|
|
if err != nil {
|
|
return nil, fmt.Errorf("%s: %s: %w", manifestYAMLName, what, err)
|
|
}
|
|
if !json.Valid(b) {
|
|
return nil, fmt.Errorf("%s: %s %s is not valid JSON", manifestYAMLName, what, rel)
|
|
}
|
|
return b, nil
|
|
}
|
|
|
|
// Only overwrite a field when manifest.yaml actually declares it. For a
|
|
// codeless build the manifest starts empty, so an undeclared key leaving the
|
|
// field at its proto zero is identical to the old unconditional assignment.
|
|
// For a mixed-form wasm build the manifest already carries the guest's
|
|
// DESCRIBE output, so these guards are what keep an undeclared key from
|
|
// WIPING a guest-populated field (theme_presets/bundled_fonts/etc.) —
|
|
// declarative keys supplement/override, they never clear (WO-WZ-021).
|
|
if my.ThemePresets != "" {
|
|
if m.ThemePresets, err = readJSONFile(my.ThemePresets, "theme_presets"); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
if my.BundledFonts != "" {
|
|
if m.BundledFonts, err = readJSONFile(my.BundledFonts, "bundled_fonts"); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
if my.SettingsSchema != "" {
|
|
if m.SettingsSchema, err = readJSONFile(my.SettingsSchema, "settings_schema"); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
if len(my.RequiredIconPacks) > 0 {
|
|
m.RequiredIconPacks = my.RequiredIconPacks
|
|
}
|
|
if my.CSS != nil {
|
|
m.CssManifest = &abiv1.CssManifest{
|
|
NpmPackages: my.CSS.NpmPackages,
|
|
CssDirectives: my.CSS.CSSDirectives,
|
|
InputCssAppend: my.CSS.InputCSSAppend,
|
|
}
|
|
}
|
|
for _, d := range my.Dependencies {
|
|
m.Dependencies = append(m.Dependencies, &abiv1.Dependency{
|
|
Plugin: d.Plugin, MinVersion: d.MinVersion, Required: d.Required,
|
|
})
|
|
}
|
|
|
|
for _, st := range my.SystemTemplates {
|
|
if st.Key == "" || st.Title == "" {
|
|
return fmt.Errorf("%s: system_templates entries need key and title", manifestYAMLName)
|
|
}
|
|
m.SystemTemplates = append(m.SystemTemplates, &abiv1.SystemTemplateMeta{
|
|
Key: st.Key, Title: st.Title, Description: st.Description,
|
|
})
|
|
}
|
|
for _, pt := range my.PageTemplates {
|
|
if pt.System == "" || pt.Key == "" || pt.Title == "" {
|
|
return fmt.Errorf("%s: page_templates entries need system, key, and title", manifestYAMLName)
|
|
}
|
|
// Layout source convention: templates/<system>/<key>.ninjatpl,
|
|
// rendered host-side (core/docs/codeless-bnp.md).
|
|
rel := path.Join(DirTemplates, pt.System, pt.Key+".ninjatpl")
|
|
if err := fileWithin(fsys, ".", rel); err != nil {
|
|
return fmt.Errorf("%s: page template %s/%s: %w", manifestYAMLName, pt.System, pt.Key, err)
|
|
}
|
|
m.PageTemplates = append(m.PageTemplates, &abiv1.PageTemplateMeta{
|
|
SystemKey: pt.System, Key: pt.Key, Title: pt.Title,
|
|
Description: pt.Description, Slots: pt.Slots,
|
|
})
|
|
}
|
|
|
|
for _, to := range my.TemplateOverrides {
|
|
if to.Template == "" || to.Block == "" {
|
|
return fmt.Errorf("%s: template_overrides entries need template and block", manifestYAMLName)
|
|
}
|
|
rel := path.Join(DirTemplates, "overrides", to.Template, to.Block+".ninjatpl")
|
|
if err := fileWithin(fsys, ".", rel); err != nil {
|
|
return fmt.Errorf("%s: template override %s/%s: %w", manifestYAMLName, to.Template, to.Block, err)
|
|
}
|
|
m.BlockTemplateOverrides = append(m.BlockTemplateOverrides, &abiv1.BlockTemplateOverride{
|
|
TemplateKey: to.Template, BlockKey: to.Block,
|
|
})
|
|
}
|
|
for _, ek := range my.EmailWrappers {
|
|
if ek == "" {
|
|
return fmt.Errorf("%s: email_wrappers entries must be non-empty system keys", manifestYAMLName)
|
|
}
|
|
rel := path.Join(DirTemplates, "email", ek+".ninjatpl")
|
|
if err := fileWithin(fsys, ".", rel); err != nil {
|
|
return fmt.Errorf("%s: email wrapper %s: %w", manifestYAMLName, ek, err)
|
|
}
|
|
m.EmailWrapperSystemKeys = append(m.EmailWrapperSystemKeys, ek)
|
|
}
|
|
|
|
if my.MasterPages != "" {
|
|
mpRaw, err := readJSONFile(my.MasterPages, "master_pages")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
var mps []masterPageJSON
|
|
if err := json.Unmarshal(mpRaw, &mps); err != nil {
|
|
return fmt.Errorf("%s: master_pages %s: %w", manifestYAMLName, my.MasterPages, err)
|
|
}
|
|
for _, mp := range mps {
|
|
def := &abiv1.MasterPageDefinition{
|
|
Key: mp.Key, Title: mp.Title, PageTemplates: mp.PageTemplates,
|
|
}
|
|
for _, b := range mp.Blocks {
|
|
contentJSON, err := json.Marshal(b.Content)
|
|
if err != nil {
|
|
return fmt.Errorf("master page %q block %q: %w", mp.Key, b.BlockKey, err)
|
|
}
|
|
def.Blocks = append(def.Blocks, &abiv1.MasterPageBlock{
|
|
BlockKey: b.BlockKey, Title: b.Title, ContentJson: contentJSON,
|
|
HtmlContent: b.HTMLContent, Slot: b.Slot, SortOrder: b.SortOrder,
|
|
})
|
|
}
|
|
m.MasterPages = append(m.MasterPages, def)
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// fileWithin ensures rel names an existing regular file inside root (no
|
|
// traversal escapes).
|
|
func fileWithin(fsys fs.FS, root, rel string) error {
|
|
clean := path.Clean(rel)
|
|
if path.IsAbs(clean) || strings.HasPrefix(clean, "..") {
|
|
return fmt.Errorf("path %q escapes the plugin directory", rel)
|
|
}
|
|
fi, err := fs.Stat(fsys, path.Join(root, clean))
|
|
if err != nil {
|
|
return fmt.Errorf("referenced file %q: %w", rel, err)
|
|
}
|
|
if !fi.Mode().IsRegular() {
|
|
return fmt.Errorf("referenced path %q is not a regular file", rel)
|
|
}
|
|
return nil
|
|
}
|