Skip to content

Commit

Permalink
refactor: Improve SUI page build performance by adding script executi…
Browse files Browse the repository at this point in the history
…on time measurement
  • Loading branch information
trheyi committed Jun 29, 2024
1 parent 481a958 commit c01b4cc
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 14 deletions.
10 changes: 7 additions & 3 deletions cmd/sui/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"
"path/filepath"
"strings"
"time"

"github.com/fatih/color"
"github.com/google/uuid"
Expand Down Expand Up @@ -78,12 +79,15 @@ var BuildCmd = &cobra.Command{
fmt.Println(color.WhiteString(" Session: %s", strings.TrimLeft(data, "::")))
fmt.Println(color.WhiteString("-----------------------"))

err = tmpl.Build(&core.BuildOption{SSR: true, AssetRoot: assetRoot})
// Timecost
start := time.Now()
err = tmpl.Build(&core.BuildOption{SSR: true, AssetRoot: assetRoot, ExecScripts: true})
if err != nil {
fmt.Fprintln(os.Stderr, color.RedString(err.Error()))
return
}

fmt.Print(color.GreenString("Build Success\n"))
end := time.Now()
timecost := end.Sub(start).Truncate(time.Millisecond)
fmt.Println(color.GreenString("Build success in %s", timecost))
},
}
7 changes: 6 additions & 1 deletion cmd/sui/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"strings"
"sync"
"syscall"
"time"

"github.com/fatih/color"
"github.com/fsnotify/fsnotify"
Expand Down Expand Up @@ -97,12 +98,16 @@ var WatchCmd = &cobra.Command{
return
}

// Timecost
start := time.Now()
err = tmpl.Build(&core.BuildOption{SSR: true, AssetRoot: assetRoot})
if err != nil {
fmt.Fprint(os.Stderr, color.RedString(fmt.Sprintf("Failed: %s\n", err.Error())))
return
}
fmt.Print(color.GreenString("Success\n"))
end := time.Now()
timecost := end.Sub(start).Truncate(time.Millisecond)
fmt.Printf(color.GreenString("Success (%s)\n"), timecost.String())
}
}, watchDone)

Expand Down
3 changes: 2 additions & 1 deletion sui/core/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,9 @@ type ITemplate interface {
Build(option *BuildOption) error
SyncAssets(option *BuildOption) error
SyncAssetFile(file string, option *BuildOption) error

GetRoot() string

ExecBuildScripts() []TemplateScirptResult
}

// IPage is the interface for the page
Expand Down
39 changes: 30 additions & 9 deletions sui/core/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,15 +141,35 @@ type LayoutItem struct {

// Template is the struct for the template
type Template struct {
Version int `json:"version"` // Yao Builder version
ID string `json:"id"`
Name string `json:"name"`
Descrption string `json:"description"`
Screenshots []string `json:"screenshots"`
Themes []SelectOption `json:"themes"`
Locales []SelectOption `json:"locales"`
Document []byte `json:"-"`
GlobalData []byte `json:"-"`
Version int `json:"version"` // Yao Builder version
ID string `json:"id"`
Name string `json:"name"`
Descrption string `json:"description"`
Screenshots []string `json:"screenshots"`
Themes []SelectOption `json:"themes"`
Locales []SelectOption `json:"locales"`
Document []byte `json:"-"`
GlobalData []byte `json:"-"`
Scripts *TemplateScirpts `json:"scripts,omitempty"`
}

// TemplateScirpts is the struct for the template scripts
type TemplateScirpts struct {
Build []*TemplateScript `json:"build,omitempty"` // Run before build
}

// TemplateScript is the struct for the template script
type TemplateScript struct {
Type string `json:"type"`
Content string `json:"content"`
}

// TemplateScirptResult is the struct for the template script result
type TemplateScirptResult struct {
Message string `json:"message,omitempty"`
Error error `json:"error,omitempty"`
Pid int `json:"pid,omitempty"`
Script *TemplateScript `json:"script,omitempty"`
}

// Theme is the struct for the theme
Expand Down Expand Up @@ -210,6 +230,7 @@ type BuildOption struct {
ComponentName string `json:"component_name,omitempty"`
ScriptMinify bool `json:"scriptminify,omitempty"`
StyleMinify bool `json:"styleminify,omitempty"`
ExecScripts bool `json:"exec_scripts,omitempty"`
}

// Request is the struct for the request
Expand Down
15 changes: 15 additions & 0 deletions sui/storages/local/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"os"
"path/filepath"
"strings"

"github.com/hashicorp/go-multierror"
"github.com/yaoapp/gou/application"
Expand All @@ -16,6 +17,20 @@ import (
func (tmpl *Template) Build(option *core.BuildOption) error {
var err error

// Execute the build hook
if option.ExecScripts {
res := tmpl.ExecBuildScripts()
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"))
}
}

root, err := tmpl.local.DSL.PublicRoot(option.Data)
if err != nil {
log.Error("SyncAssets: Get the public root error: %s. use %s", err.Error(), tmpl.local.DSL.Public.Root)
Expand Down
37 changes: 37 additions & 0 deletions sui/storages/local/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io"
"net/url"
"os"
"os/exec"
"path/filepath"
"strings"
"time"
Expand All @@ -24,6 +25,42 @@ 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 {
return nil
}
results := []core.TemplateScirptResult{}
for _, script := range tmpl.Scripts.Build {
switch script.Type {
case "command":
results = append(results, tmpl.execCommand(script))
}
}
return results
}

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
cmd := strings.Split(script.Content, " ")
if len(cmd) == 0 {
result.Error = fmt.Errorf("Command is empty")
return result
}

execCmd := exec.Command(cmd[0], cmd[1:]...)
execCmd.Dir = root
output, err := execCmd.CombinedOutput()
result.Error = err
result.Message = string(output)
return result
}

// Locales get the global locales
func (tmpl *Template) Locales() []core.SelectOption {
if tmpl.locales != nil {
Expand Down

0 comments on commit c01b4cc

Please sign in to comment.