Merge pull request #638 from trheyi/main
refactor: Update SUI page build process to execute before and after b…
This commit is contained in:
commit
f93e964557
4 changed files with 74 additions and 10 deletions
|
|
@ -63,7 +63,8 @@ type ITemplate interface {
|
||||||
SyncAssetFile(file string, option *BuildOption) error
|
SyncAssetFile(file string, option *BuildOption) error
|
||||||
GetRoot() string
|
GetRoot() string
|
||||||
|
|
||||||
ExecBuildScripts() []TemplateScirptResult
|
ExecBeforeBuildScripts() []TemplateScirptResult
|
||||||
|
ExecAfterBuildScripts() []TemplateScirptResult
|
||||||
}
|
}
|
||||||
|
|
||||||
// IPage is the interface for the page
|
// IPage is the interface for the page
|
||||||
|
|
|
||||||
|
|
@ -155,7 +155,8 @@ type Template struct {
|
||||||
|
|
||||||
// TemplateScirpts is the struct for the template scripts
|
// TemplateScirpts is the struct for the template scripts
|
||||||
type TemplateScirpts struct {
|
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
|
// TemplateScript is the struct for the template script
|
||||||
|
|
|
||||||
|
|
@ -17,9 +17,9 @@ import (
|
||||||
func (tmpl *Template) Build(option *core.BuildOption) error {
|
func (tmpl *Template) Build(option *core.BuildOption) error {
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
// Execute the build hook
|
// Execute the build before hook
|
||||||
if option.ExecScripts {
|
if option.ExecScripts {
|
||||||
res := tmpl.ExecBuildScripts()
|
res := tmpl.ExecBeforeBuildScripts()
|
||||||
scriptsErrorMessages := []string{}
|
scriptsErrorMessages := []string{}
|
||||||
for _, r := range res {
|
for _, r := range res {
|
||||||
if r.Error != nil {
|
if r.Error != nil {
|
||||||
|
|
@ -29,6 +29,11 @@ func (tmpl *Template) Build(option *core.BuildOption) error {
|
||||||
if len(scriptsErrorMessages) > 0 {
|
if len(scriptsErrorMessages) > 0 {
|
||||||
return fmt.Errorf("Build scripts error: %s", strings.Join(scriptsErrorMessages, ";\n"))
|
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)
|
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
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -11,6 +11,7 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
|
"github.com/yaoapp/gou/process"
|
||||||
"github.com/yaoapp/yao/sui/core"
|
"github.com/yaoapp/yao/sui/core"
|
||||||
"golang.org/x/text/language"
|
"golang.org/x/text/language"
|
||||||
)
|
)
|
||||||
|
|
@ -25,25 +26,63 @@ func (tmpl *Template) GetRoot() string {
|
||||||
return tmpl.Root
|
return tmpl.Root
|
||||||
}
|
}
|
||||||
|
|
||||||
// ExecBuildScripts execute the build scripts
|
// Reload the template
|
||||||
func (tmpl *Template) ExecBuildScripts() []core.TemplateScirptResult {
|
func (tmpl *Template) Reload() error {
|
||||||
if tmpl.Scripts == nil || len(tmpl.Scripts.Build) == 0 {
|
newTmpl, err := tmpl.local.getTemplateFrom(tmpl.Root)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
*tmpl = *newTmpl
|
||||||
return nil
|
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{}
|
results := []core.TemplateScirptResult{}
|
||||||
for _, script := range tmpl.Scripts.Build {
|
for _, script := range scripts {
|
||||||
switch script.Type {
|
switch script.Type {
|
||||||
case "command":
|
case "command":
|
||||||
results = append(results, tmpl.execCommand(script))
|
results = append(results, tmpl.execCommand(script))
|
||||||
|
case "process":
|
||||||
|
results = append(results, tmpl.execProcess(script))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return results
|
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 {
|
func (tmpl *Template) execCommand(script *core.TemplateScript) core.TemplateScirptResult {
|
||||||
result := core.TemplateScirptResult{Script: script, Message: "", Error: nil}
|
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)
|
root := filepath.Join(tmpl.local.fs.Root(), tmpl.Root)
|
||||||
|
|
||||||
// Parse the command
|
// Parse the command
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue