[add] sui.sync.assetfile process

This commit is contained in:
Max 2024-03-13 19:39:53 +08:00
parent ab758e0c7d
commit 76b78e2102
4 changed files with 91 additions and 0 deletions

View file

@ -59,6 +59,8 @@ func init() {
"build.all": BuildAll,
"build.page": BuildPage,
"sync.assetfile": SyncAssetFile, // Will be deprecated or change in the future
// Will be deprecated or change in the future
"types.QueryParam": TypesQueryParam,
})
@ -896,6 +898,43 @@ func PreviewRender(process *process.Process) interface{} {
return html
}
// SyncAssetFile handle the render page request
func SyncAssetFile(process *process.Process) interface{} {
process.ValidateArgNums(4)
sui := get(process)
templateID := process.ArgsString(1)
filename := process.ArgsString(2)
option := process.ArgsMap(3, map[string]interface{}{})
ssr := true
if v, ok := option["ssr"].(bool); ok {
ssr = v
}
assetRoot := ""
if v, ok := option["asset_root"].(string); ok {
assetRoot = v
}
data := map[string]interface{}{}
if v, ok := option["data"].(map[string]interface{}); ok {
data = v
}
tmpl, err := sui.GetTemplate(templateID)
if err != nil {
exception.New(err.Error(), 500).Throw()
}
err = tmpl.SyncAssetFile(filename, &core.BuildOption{SSR: ssr, AssetRoot: assetRoot, Data: data})
if err != nil {
exception.New(err.Error(), 500).Throw()
}
return nil
}
// BuildAll handle the render page request
func BuildAll(process *process.Process) interface{} {

View file

@ -747,6 +747,24 @@ func TestBuildPage(t *testing.T) {
assert.Nil(t, res)
}
func TestSyncAssetFile(t *testing.T) {
load(t)
defer clean()
// test demo
p, err := process.Of("sui.sync.assetfile", "demo", "tech-blue", "/images/about/ab01.jpg", map[string]interface{}{"ssr": true})
if err != nil {
t.Fatal(err)
}
res, err := p.Exec()
if err != nil {
t.Fatal(err)
}
assert.Nil(t, res)
}
func load(t *testing.T) {
prepare(t)
err := Load(config.Conf)

View file

@ -60,6 +60,7 @@ type ITemplate interface {
Build(option *BuildOption) error
SyncAssets(option *BuildOption) error
SyncAssetFile(file string, option *BuildOption) error
GetRoot() string
}

View file

@ -52,6 +52,39 @@ func (tmpl *Template) Build(option *core.BuildOption) error {
return err
}
// SyncAssetFile sync the assets
func (tmpl *Template) SyncAssetFile(file string, option *core.BuildOption) error {
// get source abs path
sourceRoot := filepath.Join(tmpl.local.fs.Root(), tmpl.Root, "__assets")
if exist, _ := os.Stat(sourceRoot); exist == nil {
return nil
}
//get target abs path
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)
root = tmpl.local.DSL.Public.Root
}
targetRoot := filepath.Join(application.App.Root(), "public", root, "assets")
if exist, _ := os.Stat(targetRoot); exist == nil {
os.MkdirAll(targetRoot, os.ModePerm)
}
os.RemoveAll(targetRoot)
sourceFile := filepath.Join(sourceRoot, file)
targetFile := filepath.Join(targetRoot, file)
// create the target directory
if exist, _ := os.Stat(targetFile); exist == nil {
os.MkdirAll(filepath.Dir(targetFile), os.ModePerm)
}
return copy(sourceFile, targetFile)
}
// SyncAssets sync the assets
func (tmpl *Template) SyncAssets(option *core.BuildOption) error {