Fix the bug in the SUI language pack path

This commit is contained in:
Max 2024-07-15 16:57:28 +08:00
parent 097bf45583
commit b85e5eecd8
7 changed files with 64 additions and 11 deletions

View file

@ -20,6 +20,38 @@ func TestCompile(t *testing.T) {
assert.Len(t, warnings, 0)
}
func TestTrans(t *testing.T) {
prepare(t)
defer clean()
tmpl := testTmpl(t)
option := &core.BuildOption{SSR: true, AssetRoot: "/unit-test/assets"}
warnings, err := tmpl.Trans(option)
if err != nil {
t.Fatal(err)
}
assert.Len(t, warnings, 0)
warnings, err = tmpl.Build(option)
if err != nil {
t.Fatal(err)
}
assert.Len(t, warnings, 0)
}
func testTmpl(t *testing.T) core.ITemplate {
sui := core.SUIs["test"]
if sui == nil {
t.Fatal("SUI test not found")
}
tmpl, err := sui.GetTemplate("advanced")
if err != nil {
t.Fatal(err)
}
return tmpl
}
func testPage(t *testing.T) *core.Page {
sui := core.SUIs["test"]

View file

@ -159,6 +159,7 @@ func (r *Request) Render() (string, int, error) {
Debug: r.Request.DebugMode(),
DisableCache: r.Request.DisableCache(),
Route: r.Request.URL.Path,
Root: c.Root,
Request: true,
}
@ -196,7 +197,8 @@ func (r *Request) MakeCache() (*core.Cache, int, error) {
configText := ""
cacheStore := ""
cacheTime := 0
dateCacheTime := 0
dataCacheTime := 0
root := ""
configSel := doc.Find("script[name=config]")
if configSel != nil && configSel.Length() > 0 {
@ -222,7 +224,8 @@ func (r *Request) MakeCache() (*core.Cache, int, error) {
// Cache store
cacheStore = conf.CacheStore
cacheTime = conf.Cache
dateCacheTime = conf.DataCache
dataCacheTime = conf.DataCache
root = conf.Root
}
dataText := ""
@ -253,8 +256,9 @@ func (r *Request) MakeCache() (*core.Cache, int, error) {
GuardRedirect: guardRedirect,
Config: configText,
CacheStore: cacheStore,
Root: root,
CacheTime: time.Duration(cacheTime) * time.Second,
DataCacheTime: time.Duration(dateCacheTime) * time.Second,
DataCacheTime: time.Duration(dataCacheTime) * time.Second,
}
go core.SetCache(r.File, cache)

View file

@ -16,6 +16,7 @@ type Cache struct {
Guard string
GuardRedirect string
HTML string
Root string
CacheStore string
CacheTime time.Duration
DataCacheTime time.Duration

View file

@ -54,6 +54,7 @@ func (page *Page) GetConfig() *PageConfig {
page.Config.Mock = &PageMock{Method: "GET"}
}
page.Config.Root = page.Root
return page.Config
}
@ -69,6 +70,7 @@ func (page *Page) ExportConfig() string {
"cache_store": page.CacheStore,
"cache": page.Config.Cache,
"data_cache": page.Config.DataCache,
"root": page.Root,
})
if err != nil {

View file

@ -52,6 +52,7 @@ type ParserOption struct {
Route string `json:"route,omitempty"`
Theme any `json:"theme,omitempty"`
Locale any `json:"locale,omitempty"`
Root string `json:"root,omitempty"`
}
var keepWords = map[string]bool{
@ -128,9 +129,9 @@ func (parser *TemplateParser) Locale() *Locale {
return nil
}
route := parser.option.Route
root := parser.option.Root
route := strings.TrimPrefix(parser.option.Route, root)
disableCache := parser.option.Preview || parser.option.Debug || parser.option.Editor || parser.option.DisableCache
locales, ok = Locales[name]
if !ok {
locales = map[string]*Locale{}
@ -141,7 +142,7 @@ func (parser *TemplateParser) Locale() *Locale {
return locale
}
path := filepath.Join("public", ".locales", name, route+".yml")
path := filepath.Join("public", parser.option.Root, ".locales", name, route+".yml")
if exists, err := application.App.Exists(path); !exists {
if err != nil {
log.Error("[parser] %s Locale %s", route, err.Error())

View file

@ -36,6 +36,7 @@ type Page struct {
SuiID string `json:"-"`
Config *PageConfig `json:"-"`
Path string `json:"-"`
Root string `json:"-"`
Codes SourceCodes `json:"-"`
Document []byte `json:"-"`
GlobalData []byte `json:"-"`
@ -259,6 +260,7 @@ type BuildOption struct {
SSR bool `json:"ssr"`
CDN bool `json:"cdn"`
UpdateAll bool `json:"update_all"`
PublicRoot string `json:"public_root,omitempty"`
AssetRoot string `json:"asset_root,omitempty"`
IgnoreAssetRoot bool `json:"ignore_asset_root,omitempty"`
IgnoreDocument bool `json:"ignore_document,omitempty"`
@ -373,6 +375,7 @@ type PageSetting struct {
Guard string `json:"guard,omitempty"`
CacheStore string `json:"cache_store,omitempty"`
Cache int `json:"cache,omitempty"`
Root string `json:"root,omitempty"`
DataCache int `json:"data_cache,omitempty"`
Description string `json:"description,omitempty"`
SEO *PageSEO `json:"seo,omitempty"`

View file

@ -68,12 +68,16 @@ func (tmpl *Template) Build(option *core.BuildOption) ([]string, error) {
// loaed pages
tmpl.loaded = map[string]core.IPage{}
publicRoot, err := tmpl.local.DSL.PublicRoot(option.Data)
if err != nil {
log.Error("Get the public root error: %s. use %s", err.Error(), publicRoot)
}
option.PublicRoot = publicRoot
for _, page := range pages {
err := page.Load()
if err != nil {
return warnings, err
}
messages, err := page.Build(ctx, option)
if err != nil {
return warnings, err
@ -217,14 +221,20 @@ func (tmpl *Template) SyncAssets(option *core.BuildOption) error {
func (page *Page) Build(globalCtx *core.GlobalBuildContext, option *core.BuildOption) ([]string, error) {
ctx := core.NewBuildContext(globalCtx)
if option.AssetRoot == "" {
root, err := page.tmpl.local.DSL.PublicRoot(option.Data)
var err error = nil
root := option.PublicRoot
if root == "" {
root, err = page.tmpl.local.DSL.PublicRoot(option.Data)
if err != nil {
log.Error("SyncAssets: Get the public root error: %s. use %s", err.Error(), page.tmpl.local.DSL.Public.Root)
root = page.tmpl.local.DSL.Public.Root
log.Error("Get the public root error: %s. use %s", err.Error(), page.tmpl.local.DSL.Public.Root)
}
}
if option.AssetRoot == "" {
option.AssetRoot = filepath.Join(root, "assets")
}
page.Root = root
html, warnings, err := page.Page.Compile(ctx, option)
if err != nil {