Merge pull request #591 from trheyi/main
[add] sui.sync.assetfile process
This commit is contained in:
commit
8303141c06
4 changed files with 91 additions and 0 deletions
|
|
@ -59,6 +59,8 @@ func init() {
|
||||||
"build.all": BuildAll,
|
"build.all": BuildAll,
|
||||||
"build.page": BuildPage,
|
"build.page": BuildPage,
|
||||||
|
|
||||||
|
"sync.assetfile": SyncAssetFile, // Will be deprecated or change in the future
|
||||||
|
|
||||||
// Will be deprecated or change in the future
|
// Will be deprecated or change in the future
|
||||||
"types.QueryParam": TypesQueryParam,
|
"types.QueryParam": TypesQueryParam,
|
||||||
})
|
})
|
||||||
|
|
@ -896,6 +898,43 @@ func PreviewRender(process *process.Process) interface{} {
|
||||||
return html
|
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
|
// BuildAll handle the render page request
|
||||||
func BuildAll(process *process.Process) interface{} {
|
func BuildAll(process *process.Process) interface{} {
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -747,6 +747,24 @@ func TestBuildPage(t *testing.T) {
|
||||||
assert.Nil(t, res)
|
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) {
|
func load(t *testing.T) {
|
||||||
prepare(t)
|
prepare(t)
|
||||||
err := Load(config.Conf)
|
err := Load(config.Conf)
|
||||||
|
|
|
||||||
|
|
@ -60,6 +60,7 @@ type ITemplate interface {
|
||||||
|
|
||||||
Build(option *BuildOption) error
|
Build(option *BuildOption) error
|
||||||
SyncAssets(option *BuildOption) error
|
SyncAssets(option *BuildOption) error
|
||||||
|
SyncAssetFile(file string, option *BuildOption) error
|
||||||
|
|
||||||
GetRoot() string
|
GetRoot() string
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -52,6 +52,39 @@ func (tmpl *Template) Build(option *core.BuildOption) error {
|
||||||
return err
|
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
|
// SyncAssets sync the assets
|
||||||
func (tmpl *Template) SyncAssets(option *core.BuildOption) error {
|
func (tmpl *Template) SyncAssets(option *core.BuildOption) error {
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue