refactor: Update SUI page build process to execute before and after build scripts

This commit is contained in:
Max 2024-06-29 21:38:40 +08:00
parent c01b4ccb97
commit 6fa9eb51ab
4 changed files with 74 additions and 10 deletions

View file

@ -63,7 +63,8 @@ type ITemplate interface {
SyncAssetFile(file string, option *BuildOption) error
GetRoot() string
ExecBuildScripts() []TemplateScirptResult
ExecBeforeBuildScripts() []TemplateScirptResult
ExecAfterBuildScripts() []TemplateScirptResult
}
// IPage is the interface for the page

View file

@ -155,7 +155,8 @@ type Template struct {
// TemplateScirpts is the struct for the template scripts
type TemplateScirpts struct {
Build []*TemplateScript `json:"build,omitempty"` // Run before build
BeforeBuild []*TemplateScript `json:"before:build,omitempty"` // Run before build
AfterBuild []*TemplateScript `json:"after:build,omitempty"` // Run after build
}
// TemplateScript is the struct for the template script

View file

@ -17,9 +17,9 @@ import (
func (tmpl *Template) Build(option *core.BuildOption) error {
var err error
// Execute the build hook
// Execute the build before hook
if option.ExecScripts {
res := tmpl.ExecBuildScripts()
res := tmpl.ExecBeforeBuildScripts()
scriptsErrorMessages := []string{}
for _, r := range res {
if r.Error != nil {
@ -29,6 +29,11 @@ func (tmpl *Template) Build(option *core.BuildOption) error {
if len(scriptsErrorMessages) > 0 {
return fmt.Errorf("Build scripts error: %s", strings.Join(scriptsErrorMessages, ";\n"))
}
err = tmpl.Reload()
if err != nil {
return err
}
}
root, err := tmpl.local.DSL.PublicRoot(option.Data)
@ -65,6 +70,24 @@ func (tmpl *Template) Build(option *core.BuildOption) error {
}
}
if err != nil {
return err
}
// Execute the build after hook
if option.ExecScripts {
res := tmpl.ExecAfterBuildScripts()
scriptsErrorMessages := []string{}
for _, r := range res {
if r.Error != nil {
scriptsErrorMessages = append(scriptsErrorMessages, fmt.Sprintf("%s: %s", r.Script.Content, r.Error.Error()))
}
}
if len(scriptsErrorMessages) > 0 {
return fmt.Errorf("Build scripts error: %s", strings.Join(scriptsErrorMessages, ";\n"))
}
}
return err
}

View file

@ -11,6 +11,7 @@ import (
"time"
"github.com/google/uuid"
"github.com/yaoapp/gou/process"
"github.com/yaoapp/yao/sui/core"
"golang.org/x/text/language"
)
@ -25,25 +26,63 @@ func (tmpl *Template) GetRoot() string {
return tmpl.Root
}
// ExecBuildScripts execute the build scripts
func (tmpl *Template) ExecBuildScripts() []core.TemplateScirptResult {
if tmpl.Scripts == nil || len(tmpl.Scripts.Build) == 0 {
// Reload the template
func (tmpl *Template) Reload() error {
newTmpl, err := tmpl.local.getTemplateFrom(tmpl.Root)
if err != nil {
return err
}
*tmpl = *newTmpl
return nil
}
// ExecBeforeBuildScripts execute the before build scripts
func (tmpl *Template) ExecBeforeBuildScripts() []core.TemplateScirptResult {
if tmpl.Scripts == nil || len(tmpl.Scripts.BeforeBuild) == 0 {
return nil
}
return tmpl.ExecScripts(tmpl.Scripts.BeforeBuild)
}
// ExecAfterBuildScripts execute the after build scripts
func (tmpl *Template) ExecAfterBuildScripts() []core.TemplateScirptResult {
if tmpl.Scripts == nil || len(tmpl.Scripts.AfterBuild) == 0 {
return nil
}
return tmpl.ExecScripts(tmpl.Scripts.AfterBuild)
}
// ExecScripts execute the scripts
func (tmpl *Template) ExecScripts(scripts []*core.TemplateScript) []core.TemplateScirptResult {
results := []core.TemplateScirptResult{}
for _, script := range tmpl.Scripts.Build {
for _, script := range scripts {
switch script.Type {
case "command":
results = append(results, tmpl.execCommand(script))
case "process":
results = append(results, tmpl.execProcess(script))
}
}
return results
}
func (tmpl *Template) execProcess(script *core.TemplateScript) core.TemplateScirptResult {
result := core.TemplateScirptResult{Script: script, Message: "", Error: nil}
name := script.Content
p, err := process.Of(name, tmpl.Root)
if err != nil {
result.Error = err
return result
}
output, err := p.Exec()
result.Error = err
result.Message = fmt.Sprintf("%v", output)
return result
}
func (tmpl *Template) execCommand(script *core.TemplateScript) core.TemplateScirptResult {
result := core.TemplateScirptResult{Script: script, Message: "", Error: nil}
// Set the current working directory to the template root
root := filepath.Join(tmpl.local.fs.Root(), tmpl.Root)
// Parse the command