add absolute file path support for package gcfg/gview

This commit is contained in:
John Guo 2022-01-14 17:16:38 +08:00
parent fe93d7b332
commit 6468d55a81
2 changed files with 28 additions and 18 deletions

View File

@ -174,25 +174,30 @@ func (c *AdapterFile) GetFilePath(fileName ...string) (path string, err error) {
c.autoCheckAndAddMainPkgPathToSearchPaths() c.autoCheckAndAddMainPkgPathToSearchPaths()
// Searching local file system. // Searching local file system.
c.searchPaths.RLockFunc(func(array []string) { if path == "" {
for _, searchPath := range array { // Absolute path.
searchPath = gstr.TrimRight(searchPath, `\/`) if path = gfile.RealPath(usedFileName); path != "" {
for _, tryFolder := range localSystemTryFolders { return
relativePath := gstr.TrimRight( }
gfile.Join(tryFolder, usedFileName), c.searchPaths.RLockFunc(func(array []string) {
`\/`, for _, searchPath := range array {
) searchPath = gstr.TrimRight(searchPath, `\/`)
if path, _ = gspath.Search(searchPath, relativePath); path != "" { for _, tryFolder := range localSystemTryFolders {
return relativePath := gstr.TrimRight(
gfile.Join(tryFolder, usedFileName),
`\/`,
)
if path, _ = gspath.Search(searchPath, relativePath); path != "" {
return
}
} }
} }
} })
}) }
// If it cannot find the path of `file`, it formats and returns a detailed error. // If it cannot find the path of `file`, it formats and returns a detailed error.
if path == "" { if path == "" {
var ( var buffer = bytes.NewBuffer(nil)
buffer = bytes.NewBuffer(nil)
)
if c.searchPaths.Len() > 0 { if c.searchPaths.Len() > 0 {
buffer.WriteString(fmt.Sprintf( buffer.WriteString(fmt.Sprintf(
`config file "%s" not found in resource manager or the following system searching paths:`, `config file "%s" not found in resource manager or the following system searching paths:`,

View File

@ -319,9 +319,7 @@ func (view *View) formatTemplateObjectCreatingError(filePath, tplName string, er
// Note that, the returned `folder` is the template folder path, but not the folder of // Note that, the returned `folder` is the template folder path, but not the folder of
// the returned template file `path`. // the returned template file `path`.
func (view *View) searchFile(ctx context.Context, file string) (path string, folder string, resource *gres.File, err error) { func (view *View) searchFile(ctx context.Context, file string) (path string, folder string, resource *gres.File, err error) {
var ( var tempPath string
tempPath string
)
// Firstly checking the resource manager. // Firstly checking the resource manager.
if !gres.IsEmpty() { if !gres.IsEmpty() {
// Try folders. // Try folders.
@ -350,6 +348,13 @@ func (view *View) searchFile(ctx context.Context, file string) (path string, fol
// Secondly checking the file system. // Secondly checking the file system.
if path == "" { if path == "" {
// Absolute path.
path = gfile.RealPath(file)
if path != "" {
folder = gfile.Dir(path)
return
}
// In search paths.
view.searchPaths.RLockFunc(func(array []string) { view.searchPaths.RLockFunc(func(array []string) {
for _, searchPath := range array { for _, searchPath := range array {
searchPath = gstr.TrimRight(searchPath, `\/`) searchPath = gstr.TrimRight(searchPath, `\/`)