[add] sui build command
This commit is contained in:
parent
2ba388ab16
commit
38af2dddfc
4 changed files with 102 additions and 9 deletions
11
cmd/root.go
11
cmd/root.go
|
|
@ -66,7 +66,7 @@ var langs = map[string]string{
|
|||
"SUI Template Engine": "SUI 模板引擎命令",
|
||||
}
|
||||
|
||||
// L 多语言切换
|
||||
// L Language switch
|
||||
func L(words string) string {
|
||||
if lang == "" {
|
||||
return words
|
||||
|
|
@ -126,11 +126,14 @@ var suiCmd = &cobra.Command{
|
|||
},
|
||||
}
|
||||
|
||||
// 加载命令
|
||||
// Command initialize
|
||||
func init() {
|
||||
|
||||
studioCmd.AddCommand(studio.RunCmd)
|
||||
|
||||
// Sui
|
||||
suiCmd.AddCommand(sui.WatchCmd)
|
||||
suiCmd.AddCommand(sui.BuildCmd)
|
||||
|
||||
rootCmd.AddCommand(
|
||||
versionCmd,
|
||||
|
|
@ -154,7 +157,7 @@ func init() {
|
|||
rootCmd.PersistentFlags().StringVarP(&licenseKey, "key", "k", "", L("Application license key"))
|
||||
}
|
||||
|
||||
// Execute 运行Root
|
||||
// Execute Command
|
||||
func Execute() {
|
||||
if err := rootCmd.Execute(); err != nil {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
|
|
@ -162,7 +165,7 @@ func Execute() {
|
|||
}
|
||||
}
|
||||
|
||||
// Boot 设定配置
|
||||
// Boot Setting
|
||||
func Boot() {
|
||||
|
||||
root := config.Conf.Root
|
||||
|
|
|
|||
89
cmd/sui/build.go
Normal file
89
cmd/sui/build.go
Normal file
|
|
@ -0,0 +1,89 @@
|
|||
package sui
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/fatih/color"
|
||||
"github.com/google/uuid"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/yaoapp/gou/session"
|
||||
"github.com/yaoapp/yao/config"
|
||||
"github.com/yaoapp/yao/engine"
|
||||
"github.com/yaoapp/yao/sui/core"
|
||||
)
|
||||
|
||||
// BuildCmd command
|
||||
var BuildCmd = &cobra.Command{
|
||||
Use: "build",
|
||||
Short: L("Build the template"),
|
||||
Long: L("Build the template"),
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
if len(args) < 2 {
|
||||
fmt.Fprintln(os.Stderr, color.RedString(L("yao cui watch <sui> <template> [data]")))
|
||||
return
|
||||
}
|
||||
|
||||
Boot()
|
||||
|
||||
cfg := config.Conf
|
||||
err := engine.Load(cfg)
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, color.RedString(err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
id := args[0]
|
||||
template := args[1]
|
||||
|
||||
var sessionData map[string]interface{}
|
||||
err = jsoniter.UnmarshalFromString(strings.TrimPrefix(data, "::"), &sessionData)
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, color.RedString(err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
sid := uuid.New().String()
|
||||
if sessionData != nil && len(sessionData) > 0 {
|
||||
session.Global().ID(sid).SetMany(sessionData)
|
||||
}
|
||||
|
||||
sui, has := core.SUIs[id]
|
||||
if !has {
|
||||
fmt.Fprintf(os.Stderr, color.RedString(("the sui " + id + " does not exist")))
|
||||
return
|
||||
}
|
||||
sui.WithSid(sid)
|
||||
|
||||
tmpl, err := sui.GetTemplate(template)
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, color.RedString(err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
// -
|
||||
publicRoot, err := sui.PublicRootWithSid(sid)
|
||||
assetRoot := filepath.Join(publicRoot, "assets")
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, color.RedString(err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Println(color.WhiteString("-----------------------"))
|
||||
fmt.Println(color.WhiteString("Public Root: /public%s", publicRoot))
|
||||
fmt.Println(color.WhiteString(" Template: %s", tmpl.GetRoot()))
|
||||
fmt.Println(color.WhiteString(" Session: %s", strings.TrimLeft(data, "::")))
|
||||
fmt.Println(color.WhiteString("-----------------------"))
|
||||
|
||||
err = tmpl.Build(&core.BuildOption{SSR: true, AssetRoot: assetRoot})
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, color.RedString(err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Print(color.GreenString("Build Success\n"))
|
||||
},
|
||||
}
|
||||
5
cmd/sui/sui.go
Normal file
5
cmd/sui/sui.go
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
package sui
|
||||
|
||||
func init() {
|
||||
WatchCmd.PersistentFlags().StringVarP(&data, "data", "d", "::{}", L("Session Data"))
|
||||
}
|
||||
|
|
@ -33,7 +33,7 @@ var WatchCmd = &cobra.Command{
|
|||
Long: L("Auto-build when the template file changes"),
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
if len(args) < 2 {
|
||||
fmt.Fprintln(os.Stderr, color.RedString(L("yao cui watch <sui> <template>")))
|
||||
fmt.Fprintln(os.Stderr, color.RedString(L("yao cui watch <sui> <template> [data]")))
|
||||
return
|
||||
}
|
||||
|
||||
|
|
@ -121,10 +121,6 @@ var WatchCmd = &cobra.Command{
|
|||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
WatchCmd.PersistentFlags().StringVarP(&data, "data", "d", "::{}", L("Session Data"))
|
||||
}
|
||||
|
||||
func watch(root string, handler func(event string, name string), interrupt chan uint8) error {
|
||||
watcher, err := fsnotify.NewWatcher()
|
||||
if err != nil {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue