Phase 6 part 5, step 3: theme updates by pull request
The theme carries a MANIFEST of its files' SHA-256s. A site's Theme card shows its copy's version against Supermarket's and any files changed by hand; Update the theme opens a pull request that writes new and changed files, removes retired ones, rebuilds, and lists what it replaces. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,181 @@
|
||||
package sites
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"supermarket/internal/api/gitea"
|
||||
"supermarket/internal/hugorepo"
|
||||
"supermarket/internal/seo"
|
||||
"supermarket/internal/theme"
|
||||
)
|
||||
|
||||
// ThemeState is a site's copy of Supermarket's theme.
|
||||
type ThemeState struct {
|
||||
Has bool
|
||||
Version int // the copy's; 0 when its VERSION can't be read
|
||||
Latest int // Supermarket's
|
||||
Edited []string // files changed by hand since the copy was made
|
||||
}
|
||||
|
||||
// Outdated says whether Supermarket has a newer theme than the copy.
|
||||
func (t ThemeState) Outdated() bool { return t.Has && t.Version < t.Latest }
|
||||
|
||||
// themeFiles reads a site's copy of the theme: path (inside the theme) →
|
||||
// content.
|
||||
func themeFiles(siteDir string) (map[string][]byte, error) {
|
||||
root := filepath.Join(siteDir, theme.Dir)
|
||||
out := map[string][]byte{}
|
||||
err := filepath.WalkDir(root, func(p string, d fs.DirEntry, err error) error {
|
||||
if err != nil || d.IsDir() {
|
||||
return err
|
||||
}
|
||||
raw, err := os.ReadFile(p)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
rel, _ := filepath.Rel(root, p)
|
||||
out[filepath.ToSlash(rel)] = raw
|
||||
return nil
|
||||
})
|
||||
return out, err
|
||||
}
|
||||
|
||||
// ReadTheme reads a site's copy of the theme from its working copy.
|
||||
func ReadTheme(siteDir string) ThemeState {
|
||||
st := ThemeState{Latest: theme.Version}
|
||||
files, err := themeFiles(siteDir)
|
||||
if err != nil || len(files) == 0 {
|
||||
return st
|
||||
}
|
||||
st.Has = true
|
||||
st.Version, _ = strconv.Atoi(strings.TrimSpace(string(files["VERSION"])))
|
||||
st.Edited = edited(files)
|
||||
return st
|
||||
}
|
||||
|
||||
// edited lists the files in a copy that don't match its manifest: changed
|
||||
// or added by hand (a copy without a manifest can't be checked).
|
||||
func edited(files map[string][]byte) []string {
|
||||
man, ok := files["MANIFEST"]
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
sums := theme.ParseManifest(string(man))
|
||||
var out []string
|
||||
for p, raw := range files {
|
||||
if p == "VERSION" || p == "MANIFEST" {
|
||||
continue
|
||||
}
|
||||
if sums[p] != theme.Sum(raw) {
|
||||
out = append(out, p)
|
||||
}
|
||||
}
|
||||
sort.Strings(out)
|
||||
return out
|
||||
}
|
||||
|
||||
// ThemeDiff is what an update changes in the copy.
|
||||
type ThemeDiff struct {
|
||||
From, To int
|
||||
Added, Changed, Removed []string
|
||||
Overwritten []string // edited by hand, and replaced
|
||||
}
|
||||
|
||||
// UpdateTheme opens a pull request that replaces the site's copy of the
|
||||
// theme with Supermarket's: files added, changed and removed, checked with
|
||||
// a build. Files changed by hand are listed, since the update overwrites
|
||||
// them (the site's own layouts/ is the place for changes: it wins over the
|
||||
// theme).
|
||||
func UpdateTheme(ctx context.Context, ap *seo.Applier) (*gitea.Pull, *ThemeDiff, error) {
|
||||
wc, full, published, err := ap.WorkingCopy(ctx)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
defer wc.Release()
|
||||
old, err := themeFiles(wc.Dir)
|
||||
if err != nil || len(old) == 0 {
|
||||
return nil, nil, errors.New("this site doesn't use Supermarket's theme (there's no " + theme.Dir + ")")
|
||||
}
|
||||
d := &ThemeDiff{To: theme.Version, Overwritten: edited(old)}
|
||||
d.From, _ = strconv.Atoi(strings.TrimSpace(string(old["VERSION"])))
|
||||
if d.From >= d.To {
|
||||
return nil, nil, fmt.Errorf("the site already has the theme's version %d", d.From)
|
||||
}
|
||||
branch := fmt.Sprintf("supermarket/theme-%d", theme.Version)
|
||||
if err := wc.NewBranch(ctx, branch); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
latest := theme.Files()
|
||||
for p, body := range latest {
|
||||
prev, had := old[p]
|
||||
switch {
|
||||
case !had:
|
||||
d.Added = append(d.Added, p)
|
||||
case string(prev) != body:
|
||||
if p != "VERSION" && p != "MANIFEST" {
|
||||
d.Changed = append(d.Changed, p)
|
||||
}
|
||||
default:
|
||||
continue
|
||||
}
|
||||
if err := wc.WriteFile(theme.Dir+"/"+p, body); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
}
|
||||
for p := range old {
|
||||
if _, keep := latest[p]; !keep {
|
||||
d.Removed = append(d.Removed, p)
|
||||
if err := wc.RemoveFile(theme.Dir + "/" + p); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
}
|
||||
}
|
||||
sort.Strings(d.Added)
|
||||
sort.Strings(d.Changed)
|
||||
sort.Strings(d.Removed)
|
||||
note := "Not checked with a build: there's no Hugo in Supermarket's image."
|
||||
if hugorepo.HugoVersion(ctx) != "" {
|
||||
out, err := wc.Build(ctx)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("with the new theme, %w", err)
|
||||
}
|
||||
os.RemoveAll(out)
|
||||
note = "Checked with a build: the site builds with the new theme."
|
||||
}
|
||||
subject := fmt.Sprintf("Sites: update the theme to version %d", theme.Version)
|
||||
if err := wc.CommitAndPush(ctx, branch, subject); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
var b strings.Builder
|
||||
fmt.Fprintf(&b, "Updates the site's copy of Supermarket's theme (`%s`) from version %d to %d.\n\n", theme.Dir, d.From, d.To)
|
||||
list := func(title string, ps []string) {
|
||||
if len(ps) == 0 {
|
||||
return
|
||||
}
|
||||
fmt.Fprintf(&b, "**%s:**\n", title)
|
||||
for _, p := range ps {
|
||||
fmt.Fprintf(&b, "- `%s`\n", p)
|
||||
}
|
||||
b.WriteString("\n")
|
||||
}
|
||||
list("Changed", d.Changed)
|
||||
list("Added", d.Added)
|
||||
list("Removed", d.Removed)
|
||||
if len(d.Overwritten) > 0 {
|
||||
list("Changed by hand in the site's copy, and replaced by this update (move such changes to the site's own layouts/ or static/, which win over the theme)", d.Overwritten)
|
||||
}
|
||||
b.WriteString(note + "\n\nPreview it in Supermarket (Sites → Previews) before merging: merging publishes it.")
|
||||
pull, err := ap.Gitea.OpenPull(ctx, full, branch, published, subject, b.String())
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
return pull, d, nil
|
||||
}
|
||||
Reference in New Issue
Block a user