Skip to content

Commit

Permalink
refactor: Update SUI page build process to execute before and after b…
Browse files Browse the repository at this point in the history
…uild scripts
  • Loading branch information
trheyi committed Jun 29, 2024
1 parent c01b4cc commit 6fa9eb5
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 10 deletions.
3 changes: 2 additions & 1 deletion sui/core/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion sui/core/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 25 additions & 2 deletions sui/storages/local/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Expand Down Expand Up @@ -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
}

Expand Down
51 changes: 45 additions & 6 deletions sui/storages/local/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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) execCommand(script *core.TemplateScript) core.TemplateScirptResult {
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
}

// Set the current working directory to the template root
func (tmpl *Template) execCommand(script *core.TemplateScript) core.TemplateScirptResult {
result := core.TemplateScirptResult{Script: script, Message: "", Error: nil}
root := filepath.Join(tmpl.local.fs.Root(), tmpl.Root)

// Parse the command
Expand Down

0 comments on commit 6fa9eb5

Please sign in to comment.