chore: migrate mail themes to shared framework and locked upstream inputs
This commit is contained in:
1 parent
5c0589f6f6
commit
fec3ace600
225 files changed
+4718
-15807
No files matched your search
@@ -0,0 +1,32 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"gitea-mail-templates/tools/builder"
|
||||
"gitea-mail-templates/tools/upstream"
|
||||
"github.com/urfave/cli/v2"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
func BuildCommand() *cli.Command {
|
||||
return &cli.Command{Name: "build", Usage: "Generate installable themes from locked official inputs and shared controls", Flags: []cli.Flag{&cli.StringFlag{Name: "folder", Value: "../themes"}}, Action: func(c *cli.Context) error {
|
||||
s, err := upstream.Prepare(c.Context, filepath.Dir(c.String("folder")))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
filter, err := builder.Filter(c.Args().Slice())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
packages, err := builder.BuildAll(c.String("folder"), s, filter)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
output := filepath.Join(filepath.Dir(c.String("folder")), "build", "themes")
|
||||
if err := builder.Write(output, s, packages); err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Printf("[PASS] Built %d themes for %s at %s\n", len(packages), s.Lock.Tag, output)
|
||||
return nil
|
||||
}}
|
||||
}
|
||||
+23
-67
@@ -1,86 +1,42 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"gitea-mail-templates/tools/builder"
|
||||
"github.com/urfave/cli/v2"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/urfave/cli/v2"
|
||||
|
||||
"gitea-mail-templates/tools/config"
|
||||
)
|
||||
|
||||
// CreateCommand returns the "create" subcommand.
|
||||
func CreateCommand() *cli.Command {
|
||||
return &cli.Command{
|
||||
Name: "create",
|
||||
Usage: "Create one or more new theme style directories",
|
||||
UsageText: "go run . create [--folder <themes-dir>] [--config <config-file>] <style-name>...",
|
||||
Flags: []cli.Flag{
|
||||
&cli.StringFlag{
|
||||
Name: "folder",
|
||||
Value: "../themes",
|
||||
Usage: "Path to the themes directory",
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "config",
|
||||
Value: "./data/templates_config.json",
|
||||
Usage: "Path to templates_config.json",
|
||||
},
|
||||
},
|
||||
Action: runCreate,
|
||||
}
|
||||
return &cli.Command{Name: "create", Usage: "Create CSS and metadata using the shared control framework", Flags: []cli.Flag{&cli.StringFlag{Name: "folder", Value: "../themes"}}, Action: runCreate}
|
||||
}
|
||||
|
||||
func runCreate(c *cli.Context) error {
|
||||
names := c.Args().Slice()
|
||||
if len(names) == 0 {
|
||||
return fmt.Errorf("at least one style name is required")
|
||||
return fmt.Errorf("at least one theme name is required")
|
||||
}
|
||||
|
||||
folder := c.String("folder")
|
||||
cfg, err := config.Load(c.String("config"))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, styleName := range names {
|
||||
styleDir := filepath.Join(folder, styleName)
|
||||
|
||||
if info, err := os.Stat(styleDir); err == nil {
|
||||
if info.IsDir() {
|
||||
fmt.Printf("\033[32m[I]\033[0m [CLI] '%s' already exists, skipped\n", styleName)
|
||||
continue
|
||||
}
|
||||
return fmt.Errorf("'%s' exists but is not a directory", styleName)
|
||||
for _, name := range names {
|
||||
if !builder.ValidName(name) {
|
||||
return fmt.Errorf("invalid theme name %q", name)
|
||||
}
|
||||
|
||||
created := 0
|
||||
for _, tplCfg := range cfg.Templates {
|
||||
tmplPath := filepath.Join(styleDir, tplCfg.PathStr())
|
||||
dir := filepath.Dir(tmplPath)
|
||||
|
||||
if err := os.MkdirAll(dir, 0755); err != nil {
|
||||
return fmt.Errorf("cannot create directory %s: %w", dir, err)
|
||||
}
|
||||
|
||||
placeholder := fmt.Sprintf("<!DOCTYPE html>\n<html>\n<head>\n"+
|
||||
" <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\n"+
|
||||
" <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\" />\n"+
|
||||
" <title>{{.locale.Tr \"mail.%s.title\"}}</title>\n"+
|
||||
" <style type=\"text/css\">\n /* TODO: Add your custom styles here */\n"+
|
||||
" </style>\n</head>\n<body style=\"margin:0;padding:0;\">\n"+
|
||||
" <!-- TODO: Add your custom template design for: %s -->\n"+
|
||||
"</body>\n</html>\n", tplCfg.Name, tplCfg.Desc)
|
||||
|
||||
if err := os.WriteFile(tmplPath, []byte(placeholder), 0644); err != nil {
|
||||
return fmt.Errorf("cannot write %s: %w", tmplPath, err)
|
||||
}
|
||||
created++
|
||||
dir := filepath.Join(c.String("folder"), name)
|
||||
if _, err := os.Lstat(dir); err == nil {
|
||||
return fmt.Errorf("theme %s already exists", name)
|
||||
}
|
||||
|
||||
fmt.Printf("\033[32m[I]\033[0m [CLI] Created style '%s' with %d template files\n", styleName, created)
|
||||
if err := os.MkdirAll(dir, 0755); err != nil {
|
||||
return err
|
||||
}
|
||||
meta, _ := json.MarshalIndent(builder.Theme{Name: name, Description: "New theme", Mode: "framed", Layout: "standard"}, "", " ")
|
||||
if err := os.WriteFile(filepath.Join(dir, "theme.json"), append(meta, '\n'), 0644); err != nil {
|
||||
return err
|
||||
}
|
||||
css := "/* Presentation only; shared controls own mail data and translations. */\nbody { margin:0; padding:24px 16px; background:#f4f6f9; }\nbody, td { font-family:Arial, sans-serif; }\n.email-wrap { max-width:600px; margin:auto; background:#fff; }\n.email-header, .email-card, .email-footer { padding:24px; }\n.email-logo { vertical-align:middle; margin-right:8px; }\n.email-btn,.card-body .email-btn,.card-body .email-btn:visited { display:inline-block; padding:12px 24px; color:#fff!important; background:#2563eb; text-decoration:none; border-radius:6px; }\n.email-link-url { overflow-wrap:anywhere; }\na, a:visited { color:#2563eb; }\n"
|
||||
if err := os.WriteFile(filepath.Join(dir, "theme.css"), []byte(css), 0644); err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Printf("[PASS] Created framework theme %s\n", name)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"gitea-mail-templates/tools/builder"
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
@@ -34,12 +35,21 @@ func runDelete(c *cli.Context) error {
|
||||
folder := c.String("folder")
|
||||
|
||||
for _, styleName := range names {
|
||||
if !builder.ValidName(styleName) {
|
||||
return fmt.Errorf("invalid theme name %q", styleName)
|
||||
}
|
||||
styleDir := filepath.Join(folder, styleName)
|
||||
if info, err := os.Lstat(styleDir); err == nil && info.Mode()&os.ModeSymlink != 0 {
|
||||
return fmt.Errorf("refusing to delete a theme symlink")
|
||||
}
|
||||
|
||||
if _, err := os.Stat(styleDir); os.IsNotExist(err) {
|
||||
fmt.Printf("\033[33m[W]\033[0m [CLI] '%s' does not exist, skipped\n", styleName)
|
||||
continue
|
||||
}
|
||||
if _, err := builder.LoadTheme(styleDir); err != nil {
|
||||
return fmt.Errorf("refusing to delete a non-theme directory: %w", err)
|
||||
}
|
||||
|
||||
if err := os.RemoveAll(styleDir); err != nil {
|
||||
return fmt.Errorf("cannot delete %s: %w", styleDir, err)
|
||||
|
||||
+14
-46
@@ -2,56 +2,24 @@ package cli
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
|
||||
"gitea-mail-templates/tools/builder"
|
||||
"github.com/urfave/cli/v2"
|
||||
|
||||
"gitea-mail-templates/tools/preview"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
// ListCommand returns the "list" subcommand.
|
||||
func ListCommand() *cli.Command {
|
||||
return &cli.Command{
|
||||
Name: "list",
|
||||
Usage: "List available theme styles in the target folder",
|
||||
UsageText: "go run . list [--folder <themes-dir>]",
|
||||
Flags: []cli.Flag{
|
||||
&cli.StringFlag{
|
||||
Name: "folder",
|
||||
Value: "../themes",
|
||||
Usage: "Path to the themes directory",
|
||||
},
|
||||
},
|
||||
Action: runList,
|
||||
}
|
||||
}
|
||||
|
||||
func runList(c *cli.Context) error {
|
||||
folder := c.String("folder")
|
||||
|
||||
themes, err := preview.DiscoverThemes(folder)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
absDir, _ := filepath.Abs(folder)
|
||||
fmt.Printf("\033[32m[I]\033[0m [Preview] Available styles in '%s' (%d):\n", filepath.Base(absDir), len(themes))
|
||||
|
||||
sort.Strings(themes)
|
||||
for _, t := range themes {
|
||||
count := 0
|
||||
filepath.Walk(filepath.Join(folder, t), func(path string, info os.FileInfo, err error) error {
|
||||
return &cli.Command{Name: "list", Usage: "List available theme manifests", Flags: []cli.Flag{&cli.StringFlag{Name: "folder", Value: "../themes"}}, Action: func(c *cli.Context) error {
|
||||
names, err := builder.Discover(c.String("folder"))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, name := range names {
|
||||
t, err := builder.LoadTheme(filepath.Join(c.String("folder"), name))
|
||||
if err != nil {
|
||||
return nil
|
||||
return err
|
||||
}
|
||||
if filepath.Ext(path) == ".tmpl" {
|
||||
count++
|
||||
}
|
||||
return nil
|
||||
})
|
||||
fmt.Printf("\033[32m[I]\033[0m [Preview] %-16s (%d .tmpl files)\n", t, count)
|
||||
}
|
||||
return nil
|
||||
fmt.Printf("%-16s %-8s %s\n", name, t.Mode, t.Description)
|
||||
}
|
||||
return nil
|
||||
}}
|
||||
}
|
||||
@@ -66,9 +66,9 @@ func runPreview(c *cli.Context) error {
|
||||
}
|
||||
}
|
||||
|
||||
result := preview.RenderAll(folder, cfg, themeFilter)
|
||||
if result == nil {
|
||||
return fmt.Errorf("no results produced")
|
||||
result, err := preview.RenderAll(folder, cfg, themeFilter)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := preview.WriteRenderedJS(result, outputPath); err != nil {
|
||||
@@ -78,7 +78,7 @@ func runPreview(c *cli.Context) error {
|
||||
preview.PrintDetailedSummary(result, folder, cfg)
|
||||
|
||||
fmt.Printf("\033[32m[I]\033[0m [Preview] Wrote %d themes x %d templates to %s\n",
|
||||
len(result.Summaries), len(cfg.Templates), outputPath)
|
||||
len(result.Themes), len(cfg.Templates), outputPath)
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"gitea-mail-templates/tools/upstream"
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
func UpstreamCommand() *cli.Command {
|
||||
return &cli.Command{Name: "upstream", Usage: "Prepare, synchronize or verify locked official build inputs", Subcommands: []*cli.Command{
|
||||
{Name: "prepare", Usage: "Download missing immutable cache files from gitea.lock.json", Flags: []cli.Flag{&cli.StringFlag{Name: "root", Value: ".."}}, Action: func(c *cli.Context) error { _, err := upstream.Prepare(c.Context, c.String("root")); return err }},
|
||||
{Name: "sync", Usage: "Review a new Gitea tag and regenerate the lock and cache", Flags: []cli.Flag{&cli.StringFlag{Name: "tag", Required: true}, &cli.StringFlag{Name: "root", Value: ".."}}, Action: func(c *cli.Context) error { return upstream.SyncRoot(c.Context, c.String("tag"), c.String("root")) }},
|
||||
{Name: "verify", Usage: "Verify cached inputs against the repository lock offline", Flags: []cli.Flag{&cli.StringFlag{Name: "root", Value: ".."}}, Action: func(c *cli.Context) error {
|
||||
s, err := upstream.LoadPinned(c.String("root"))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
missing, err := upstream.MissingKeys(s)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Printf("[PASS] %s: %d mail files, %d languages\n", s.Lock.Tag, len(s.Templates), len(s.Locales))
|
||||
for _, lang := range s.Languages() {
|
||||
if keys := missing[lang]; len(keys) > 0 {
|
||||
fmt.Printf("[FALLBACK] %s -> en-US: %v\n", lang, keys)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}},
|
||||
}}
|
||||
}
|
||||
Reference in new issue
Block a user