mirror of
https://github.com/gogf/gf.git
synced 2025-04-05 11:18:50 +08:00
improve Export feature for package gres
This commit is contained in:
parent
bc2ce19876
commit
36e05561cc
@ -78,8 +78,8 @@ func ScanDirFile(path string, pattern string, recursive ...bool) []*File {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Export exports and saves specified path `src` and all its sub files to specified system path `dst` recursively.
|
// Export exports and saves specified path `src` and all its sub files to specified system path `dst` recursively.
|
||||||
func Export(src, dst string) error {
|
func Export(src, dst string, option ...ExportOption) error {
|
||||||
return defaultResource.Export(src, dst)
|
return defaultResource.Export(src, dst, option...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Dump prints the files of the default resource object.
|
// Dump prints the files of the default resource object.
|
||||||
|
@ -17,6 +17,7 @@ import (
|
|||||||
"github.com/gogf/gf/v2/internal/intlog"
|
"github.com/gogf/gf/v2/internal/intlog"
|
||||||
"github.com/gogf/gf/v2/os/gfile"
|
"github.com/gogf/gf/v2/os/gfile"
|
||||||
"github.com/gogf/gf/v2/os/gtime"
|
"github.com/gogf/gf/v2/os/gtime"
|
||||||
|
"github.com/gogf/gf/v2/text/gstr"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Resource struct {
|
type Resource struct {
|
||||||
@ -224,15 +225,33 @@ func (r *Resource) doScanDir(path string, pattern string, recursive bool, onlyFi
|
|||||||
return files
|
return files
|
||||||
}
|
}
|
||||||
|
|
||||||
// Export exports and saves specified path `src` and all its sub files to specified system path `dst` recursively.
|
// ExportOption is the option for function Export.
|
||||||
func (r *Resource) Export(src, dst string) error {
|
type ExportOption struct {
|
||||||
|
RemovePrefix string // Remove the prefix of file name from resource.
|
||||||
|
}
|
||||||
|
|
||||||
|
// Export exports and saves specified path `srcPath` and all its sub files to specified system path `dstPath` recursively.
|
||||||
|
func (r *Resource) Export(src, dst string, option ...ExportOption) error {
|
||||||
var (
|
var (
|
||||||
err error
|
err error
|
||||||
path string
|
name string
|
||||||
files = r.doScanDir(src, "*", true, false)
|
path string
|
||||||
|
exportOption ExportOption
|
||||||
|
files = r.doScanDir(src, "*", true, false)
|
||||||
)
|
)
|
||||||
|
if len(option) > 0 {
|
||||||
|
exportOption = option[0]
|
||||||
|
}
|
||||||
for _, file := range files {
|
for _, file := range files {
|
||||||
path = gfile.Join(dst, file.Name())
|
name = file.Name()
|
||||||
|
if exportOption.RemovePrefix != "" {
|
||||||
|
name = gstr.TrimLeftStr(name, exportOption.RemovePrefix)
|
||||||
|
}
|
||||||
|
name = gstr.Trim(name, `\/`)
|
||||||
|
if name == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
path = gfile.Join(dst, name)
|
||||||
if file.FileInfo().IsDir() {
|
if file.FileInfo().IsDir() {
|
||||||
err = gfile.Mkdir(path)
|
err = gfile.Mkdir(path)
|
||||||
} else {
|
} else {
|
||||||
|
@ -249,4 +249,22 @@ func Test_Export(t *testing.T) {
|
|||||||
name := `template/index.html`
|
name := `template/index.html`
|
||||||
t.Assert(gfile.GetContents(gfile.Join(dst, name)), gres.GetContent(name))
|
t.Assert(gfile.GetContents(gfile.Join(dst, name)), gres.GetContent(name))
|
||||||
})
|
})
|
||||||
|
gtest.C(t, func(t *gtest.T) {
|
||||||
|
var (
|
||||||
|
src = `template`
|
||||||
|
dst = gfile.TempDir(gtime.TimestampNanoStr())
|
||||||
|
err = gres.Export(src, dst, gres.ExportOption{
|
||||||
|
RemovePrefix: `template`,
|
||||||
|
})
|
||||||
|
)
|
||||||
|
defer gfile.Remove(dst)
|
||||||
|
t.AssertNil(err)
|
||||||
|
files, err := gfile.ScanDir(dst, "*", true)
|
||||||
|
t.AssertNil(err)
|
||||||
|
t.Assert(len(files), 13)
|
||||||
|
|
||||||
|
nameInRes := `template/index.html`
|
||||||
|
nameInSys := `index.html`
|
||||||
|
t.Assert(gfile.GetContents(gfile.Join(dst, nameInSys)), gres.GetContent(nameInRes))
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user