improve package gview for default folder searching

This commit is contained in:
John Guo
2021-12-17 21:38:10 +08:00
parent 656d1bbbd6
commit b9907ae3ba
5 changed files with 78 additions and 63 deletions

View File

@ -42,13 +42,13 @@ var (
customConfigContentMap = gmap.NewStrStrMap(true) // Customized configuration content.
// Prefix array for trying searching in resource manager.
resourceTryFiles = []string{
resourceTryFolders = []string{
"", "/", "config/", "config", "/config", "/config/",
"manifest/config/", "manifest/config", "/manifest/config", "/manifest/config/",
}
// Prefix array for trying searching in local system.
localSystemTryFiles = []string{"", "config/", "manifest/config"}
localSystemTryFolders = []string{"", "config/", "manifest/config"}
)
// NewAdapterFile returns a new configuration management object.

View File

@ -143,6 +143,7 @@ func (c *AdapterFile) AddPath(path string) (err error) {
// It returns an empty `path` string and an error if the given `file` does not exist.
func (c *AdapterFile) GetFilePath(fileName ...string) (path string, err error) {
var (
tempPath string
usedFileName = c.defaultName
)
if len(fileName) > 0 {
@ -150,16 +151,18 @@ func (c *AdapterFile) GetFilePath(fileName ...string) (path string, err error) {
}
// Searching resource manager.
if !gres.IsEmpty() {
for _, v := range resourceTryFiles {
if file := gres.Get(v + usedFileName); file != nil {
for _, tryFolder := range resourceTryFolders {
tempPath = tryFolder + usedFileName
if file := gres.Get(tempPath); file != nil {
path = file.Name()
return
}
}
c.searchPaths.RLockFunc(func(array []string) {
for _, prefix := range array {
for _, v := range resourceTryFiles {
if file := gres.Get(prefix + v + usedFileName); file != nil {
for _, searchPath := range array {
for _, tryFolder := range resourceTryFolders {
tempPath = searchPath + tryFolder + usedFileName
if file := gres.Get(tempPath); file != nil {
path = file.Name()
return
}
@ -167,17 +170,19 @@ func (c *AdapterFile) GetFilePath(fileName ...string) (path string, err error) {
}
})
}
c.autoCheckAndAddMainPkgPathToSearchPaths()
// Searching local file system.
c.searchPaths.RLockFunc(func(array []string) {
for _, prefix := range array {
prefix = gstr.TrimRight(prefix, `\/`)
for _, tryFile := range localSystemTryFiles {
for _, searchPath := range array {
searchPath = gstr.TrimRight(searchPath, `\/`)
for _, tryFolder := range localSystemTryFolders {
relativePath := gstr.TrimRight(
gfile.Join(tryFile, usedFileName),
gfile.Join(tryFolder, usedFileName),
`\/`,
)
if path, _ = gspath.Search(prefix, relativePath); path != "" {
if path, _ = gspath.Search(searchPath, relativePath); path != "" {
return
}
}
@ -195,11 +200,13 @@ func (c *AdapterFile) GetFilePath(fileName ...string) (path string, err error) {
))
c.searchPaths.RLockFunc(func(array []string) {
index := 1
for _, prefix := range array {
prefix = gstr.TrimRight(prefix, `\/`)
for _, tryFile := range localSystemTryFiles {
prefixPath := gfile.Join(prefix, tryFile)
buffer.WriteString(fmt.Sprintf("\n%d. %s", index, prefixPath))
for _, searchPath := range array {
searchPath = gstr.TrimRight(searchPath, `\/`)
for _, tryFolder := range localSystemTryFolders {
buffer.WriteString(fmt.Sprintf(
"\n%d. %s",
index, gfile.Join(searchPath, tryFolder),
))
index++
}
}

View File

@ -24,7 +24,7 @@ import (
// View object for template engine.
type View struct {
paths *garray.StrArray // Searching array for path, NOT concurrent-safe for performance purpose.
searchPaths *garray.StrArray // Searching array for path, NOT concurrent-safe for performance purpose.
data map[string]interface{} // Global template variables.
funcMap map[string]interface{} // Global template function map.
fileCacheMap *gmap.StrAnyMap // File cache map.
@ -67,7 +67,7 @@ func New(path ...string) *View {
ctx = context.TODO()
)
view := &View{
paths: garray.NewStrArray(),
searchPaths: garray.NewStrArray(),
data: make(map[string]interface{}),
funcMap: make(map[string]interface{}),
fileCacheMap: gmap.NewStrAnyMap(true),

View File

@ -111,7 +111,7 @@ func (view *View) SetPath(path string) error {
realPath = gfile.RealPath(path)
if realPath == "" {
// Relative path.
view.paths.RLockFunc(func(array []string) {
view.searchPaths.RLockFunc(func(array []string) {
for _, v := range array {
if path, _ := gspath.Search(v, path); path != "" {
realPath = path
@ -141,11 +141,11 @@ func (view *View) SetPath(path string) error {
return err
}
// Repeated path adding check.
if view.paths.Search(realPath) != -1 {
if view.searchPaths.Search(realPath) != -1 {
return nil
}
view.paths.Clear()
view.paths.Append(realPath)
view.searchPaths.Clear()
view.searchPaths.Append(realPath)
view.fileCacheMap.Clear()
// glog.Debug("[gview] SetPath:", realPath)
return nil
@ -166,7 +166,7 @@ func (view *View) AddPath(path string) error {
realPath = gfile.RealPath(path)
if realPath == "" {
// Relative path.
view.paths.RLockFunc(func(array []string) {
view.searchPaths.RLockFunc(func(array []string) {
for _, v := range array {
if path, _ := gspath.Search(v, path); path != "" {
realPath = path
@ -196,10 +196,10 @@ func (view *View) AddPath(path string) error {
return err
}
// Repeated path adding check.
if view.paths.Search(realPath) != -1 {
if view.searchPaths.Search(realPath) != -1 {
return nil
}
view.paths.Append(realPath)
view.searchPaths.Append(realPath)
view.fileCacheMap.Clear()
return nil
}

View File

@ -12,7 +12,6 @@ import (
"fmt"
htmltpl "html/template"
"strconv"
"strings"
texttpl "text/template"
"github.com/gogf/gf/v2/container/gmap"
@ -48,7 +47,13 @@ var (
templates = gmap.NewStrAnyMap(true)
// Try-folders for resource template file searching.
resourceTryFolders = []string{"template/", "template", "/template", "/template/"}
resourceTryFolders = []string{
"template/", "template", "/template", "/template/",
"resource/template/", "resource/template", "/resource/template", "/resource/template/",
}
// Prefix array for trying searching in local system.
localSystemTryFolders = []string{"", "template/", "resource/template"}
)
// Parse parses given template file `file` with given template variables `params`
@ -306,29 +311,30 @@ func (view *View) formatTemplateObjectCreatingError(filePath, tplName string, er
// Note that, the returned `folder` is the template folder path, but not the folder of
// the returned template file `path`.
func (view *View) searchFile(ctx context.Context, file string) (path string, folder string, resource *gres.File, err error) {
var (
tempPath string
)
// Firstly checking the resource manager.
if !gres.IsEmpty() {
// Try folders.
for _, folderPath := range resourceTryFolders {
if resource = gres.Get(folderPath + file); resource != nil {
for _, tryFolder := range resourceTryFolders {
tempPath = tryFolder + file
if resource = gres.Get(tempPath); resource != nil {
path = resource.Name()
folder = folderPath
folder = tryFolder
return
}
}
// Search folders.
view.paths.RLockFunc(func(array []string) {
for _, v := range array {
v = strings.TrimRight(v, "/"+gfile.Separator)
if resource = gres.Get(v + "/" + file); resource != nil {
path = resource.Name()
folder = v
break
}
if resource = gres.Get(v + "/template/" + file); resource != nil {
path = resource.Name()
folder = v + "/template"
break
view.searchPaths.RLockFunc(func(array []string) {
for _, searchPath := range array {
for _, tryFolder := range resourceTryFolders {
tempPath = searchPath + tryFolder + file
if resFile := gres.Get(tempPath); resFile != nil {
path = resFile.Name()
folder = searchPath + tryFolder
return
}
}
}
})
@ -336,16 +342,18 @@ func (view *View) searchFile(ctx context.Context, file string) (path string, fol
// Secondly checking the file system.
if path == "" {
view.paths.RLockFunc(func(array []string) {
for _, folderPath := range array {
folderPath = strings.TrimRight(folderPath, gfile.Separator)
if path, _ = gspath.Search(folderPath, file); path != "" {
folder = folderPath
break
}
if path, _ = gspath.Search(folderPath+gfile.Separator+"template", file); path != "" {
folder = folderPath + gfile.Separator + "template"
break
view.searchPaths.RLockFunc(func(array []string) {
for _, searchPath := range array {
searchPath = gstr.TrimRight(searchPath, `\/`)
for _, tryFolder := range localSystemTryFolders {
relativePath := gstr.TrimRight(
gfile.Join(tryFolder, file),
`\/`,
)
if path, _ = gspath.Search(searchPath, relativePath); path != "" {
folder = gfile.Dir(path)
return
}
}
}
})
@ -354,19 +362,19 @@ func (view *View) searchFile(ctx context.Context, file string) (path string, fol
// Error checking.
if path == "" {
buffer := bytes.NewBuffer(nil)
if view.paths.Len() > 0 {
if view.searchPaths.Len() > 0 {
buffer.WriteString(fmt.Sprintf("[gview] cannot find template file \"%s\" in following paths:", file))
view.paths.RLockFunc(func(array []string) {
view.searchPaths.RLockFunc(func(array []string) {
index := 1
for _, folderPath := range array {
folderPath = strings.TrimRight(folderPath, "/")
if folderPath == "" {
folderPath = "/"
for _, searchPath := range array {
searchPath = gstr.TrimRight(searchPath, `\/`)
for _, tryFolder := range localSystemTryFolders {
buffer.WriteString(fmt.Sprintf(
"\n%d. %s",
index, gfile.Join(searchPath, tryFolder),
))
index++
}
buffer.WriteString(fmt.Sprintf("\n%d. %s", index, folderPath))
index++
buffer.WriteString(fmt.Sprintf("\n%d. %s", index, strings.TrimRight(folderPath, "/")+gfile.Separator+"template"))
index++
}
})
} else {