Skip to content

Commit

Permalink
fix issue gogf#1971 (gogf#2203)
Browse files Browse the repository at this point in the history
Co-authored-by: houseme <housemecn@gmail.com>
  • Loading branch information
gqcn and houseme authored Oct 14, 2022
1 parent e756f28 commit 2598745
Show file tree
Hide file tree
Showing 14 changed files with 195 additions and 64 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ cbuild
cmd/gf/main
cmd/gf/gf
go.work
temp/
4 changes: 3 additions & 1 deletion cmd/gf/internal/cmd/cmd_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,9 @@ func (c cBuild) Index(ctx context.Context, in cBuildInput) (out *cBuildOutput, e
mlog.Printf(`remove the automatically generated resource go file: %s`, in.PackDst)
}()
}
packCmd := fmt.Sprintf(`gf pack %s %s`, in.PackSrc, in.PackDst)
// remove black space in separator.
in.PackSrc, _ = gregex.ReplaceString(`,\s+`, `,`, in.PackSrc)
packCmd := fmt.Sprintf(`gf pack %s %s --keepPath=true`, in.PackSrc, in.PackDst)
mlog.Print(packCmd)
gproc.MustShellRun(ctx, packCmd)
}
Expand Down
39 changes: 23 additions & 16 deletions cmd/gf/internal/cmd/cmd_pack.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,28 +37,31 @@ gf pack /var/www/public packed/data.go -n=packed
destination file path for packed file. if extension of the filename is ".go" and "-n" option is given,
it enables packing SRC to go file, or else it packs SRC into a binary file.
`
cPackNameBrief = `package name for output go file, it's set as its directory name if no name passed`
cPackPrefixBrief = `prefix for each file packed into the resource file`
cPackNameBrief = `package name for output go file, it's set as its directory name if no name passed`
cPackPrefixBrief = `prefix for each file packed into the resource file`
cPackKeepPathBrief = `keep the source path from system to resource file, usually for relative path`
)

func init() {
gtag.Sets(g.MapStrStr{
`cPackUsage`: cPackUsage,
`cPackBrief`: cPackBrief,
`cPackEg`: cPackEg,
`cPackSrcBrief`: cPackSrcBrief,
`cPackDstBrief`: cPackDstBrief,
`cPackNameBrief`: cPackNameBrief,
`cPackPrefixBrief`: cPackPrefixBrief,
`cPackUsage`: cPackUsage,
`cPackBrief`: cPackBrief,
`cPackEg`: cPackEg,
`cPackSrcBrief`: cPackSrcBrief,
`cPackDstBrief`: cPackDstBrief,
`cPackNameBrief`: cPackNameBrief,
`cPackPrefixBrief`: cPackPrefixBrief,
`cPackKeepPathBrief`: cPackKeepPathBrief,
})
}

type cPackInput struct {
g.Meta `name:"pack"`
Src string `name:"SRC" arg:"true" v:"required" brief:"{cPackSrcBrief}"`
Dst string `name:"DST" arg:"true" v:"required" brief:"{cPackDstBrief}"`
Name string `name:"name" short:"n" brief:"{cPackNameBrief}"`
Prefix string `name:"prefix" short:"p" brief:"{cPackPrefixBrief}"`
g.Meta `name:"pack"`
Src string `name:"SRC" arg:"true" v:"required" brief:"{cPackSrcBrief}"`
Dst string `name:"DST" arg:"true" v:"required" brief:"{cPackDstBrief}"`
Name string `name:"name" short:"n" brief:"{cPackNameBrief}"`
Prefix string `name:"prefix" short:"p" brief:"{cPackPrefixBrief}"`
KeepPath bool `name:"keepPath" short:"k" brief:"{cPackKeepPathBrief}" orphan:"true"`
}
type cPackOutput struct{}

Expand All @@ -75,12 +78,16 @@ func (c cPack) Index(ctx context.Context, in cPackInput) (out *cPackOutput, err
if in.Name == "" && gfile.ExtName(in.Dst) == "go" {
in.Name = gfile.Basename(gfile.Dir(in.Dst))
}
var option = gres.Option{
Prefix: in.Prefix,
KeepPath: in.KeepPath,
}
if in.Name != "" {
if err = gres.PackToGoFile(in.Src, in.Dst, in.Name, in.Prefix); err != nil {
if err = gres.PackToGoFileWithOption(in.Src, in.Dst, in.Name, option); err != nil {
mlog.Fatalf("pack failed: %v", err)
}
} else {
if err = gres.PackToFile(in.Src, in.Dst, in.Prefix); err != nil {
if err = gres.PackToFileWithOption(in.Src, in.Dst, option); err != nil {
mlog.Fatalf("pack failed: %v", err)
}
}
Expand Down
9 changes: 9 additions & 0 deletions example/pack/hack/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
gfcli:
build:
name: "pack"
arch: "amd64"
system: "darwin"
packSrc: "resource/public,manifest/i18n"
packDst: "packed/build_packed_data.go"
mod: ""
cgo: 0
28 changes: 28 additions & 0 deletions example/pack/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package main

import (
_ "github.com/gogf/gf/example/pack/packed"

"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/i18n/gi18n"
"github.com/gogf/gf/v2/net/ghttp"
"github.com/gogf/gf/v2/os/gres"
)

func main() {
gres.Dump()

s := g.Server()
s.SetPort(8199)
s.SetServerRoot("resource/public")
s.BindHandler("/i18n", func(r *ghttp.Request) {
var (
lang = r.Get("lang", "zh-CN").String()
ctx = gi18n.WithLanguage(r.Context(), lang)
content string
)
content = g.I18n().T(ctx, `{#hello} {#world}!`)
r.Response.Write(content)
})
s.Run()
}
2 changes: 2 additions & 0 deletions example/pack/manifest/i18n/ja.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
hello: "こんにちは"
world: "世界"
2 changes: 2 additions & 0 deletions example/pack/manifest/i18n/ru.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
hello: "Привет"
world: "мир"
2 changes: 2 additions & 0 deletions example/pack/manifest/i18n/zh-CN.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
hello: "你好"
world: "世界"
1 change: 1 addition & 0 deletions example/pack/packed/paked.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package packed
8 changes: 8 additions & 0 deletions example/pack/resource/public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<html>
<head>
<title>Hello World</title>
</head>
<body>
<h1>Hello World!</h1>
</body>
</html>
26 changes: 17 additions & 9 deletions i18n/gi18n/gi18n_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,14 @@ type Options struct {
}

var (
defaultLanguage = "en" // defaultDelimiters defines the default language if user does not specified in options.
defaultDelimiters = []string{"{#", "}"} // defaultDelimiters defines the default key variable delimiters.
// defaultDelimiters defines the default language if user does not specify in options.
defaultLanguage = "en"

// defaultDelimiters defines the default key variable delimiters.
defaultDelimiters = []string{"{#", "}"}

// i18n files searching folders.
searchFolders = []string{"manifest/i18n", "manifest/config/i18n", "i18n"}
)

// New creates and returns a new i18n manager.
Expand Down Expand Up @@ -73,13 +79,15 @@ func New(options ...Options) *Manager {

// DefaultOptions creates and returns a default options for i18n manager.
func DefaultOptions() Options {
var (
path = "i18n"
realPath, _ = gfile.Search(path)
)
if realPath != "" {
path = realPath
// To avoid of the source path of GF: github.com/gogf/i18n/gi18n
var path string
for _, folder := range searchFolders {
path, _ = gfile.Search(folder)
if path != "" {
break
}
}
if path != "" {
// To avoid of the source path of GoFrame: github.com/gogf/i18n/gi18n
if gfile.Exists(path + gfile.Separator + "gi18n") {
path = ""
}
Expand Down
55 changes: 49 additions & 6 deletions os/gres/gres_func.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,33 @@ func init() {
`
)

// Option contains the extra options for Pack functions.
type Option struct {
Prefix string // The file path prefix for each file item in resource manager.
KeepPath bool // Keep the passed path when packing, usually for relative path.
}

// Pack packs the path specified by `srcPaths` into bytes.
// The unnecessary parameter `keyPrefix` indicates the prefix for each file
// packed into the result bytes.
//
// Note that parameter `srcPaths` supports multiple paths join with ','.
//
// Deprecated, use PackWithOption instead.
func Pack(srcPaths string, keyPrefix ...string) ([]byte, error) {
var (
buffer = bytes.NewBuffer(nil)
headerPrefix = ""
)
option := Option{}
if len(keyPrefix) > 0 && keyPrefix[0] != "" {
headerPrefix = keyPrefix[0]
option.Prefix = keyPrefix[0]
}
err := zipPathWriter(srcPaths, buffer, headerPrefix)
return PackWithOption(srcPaths, option)
}

// PackWithOption packs the path specified by `srcPaths` into bytes.
//
// Note that parameter `srcPaths` supports multiple paths join with ','.
func PackWithOption(srcPaths string, option Option) ([]byte, error) {
var buffer = bytes.NewBuffer(nil)
err := zipPathWriter(srcPaths, buffer, option)
if err != nil {
return nil, err
}
Expand All @@ -59,6 +72,8 @@ func Pack(srcPaths string, keyPrefix ...string) ([]byte, error) {
// packed into the result bytes.
//
// Note that parameter `srcPaths` supports multiple paths join with ','.
//
// Deprecated, use PackToFileWithOption instead.
func PackToFile(srcPaths, dstPath string, keyPrefix ...string) error {
data, err := Pack(srcPaths, keyPrefix...)
if err != nil {
Expand All @@ -67,13 +82,26 @@ func PackToFile(srcPaths, dstPath string, keyPrefix ...string) error {
return gfile.PutBytes(dstPath, data)
}

// PackToFileWithOption packs the path specified by `srcPaths` to target file `dstPath`.
//
// Note that parameter `srcPaths` supports multiple paths join with ','.
func PackToFileWithOption(srcPaths, dstPath string, option Option) error {
data, err := PackWithOption(srcPaths, option)
if err != nil {
return err
}
return gfile.PutBytes(dstPath, data)
}

// PackToGoFile packs the path specified by `srcPaths` to target go file `goFilePath`
// with given package name `pkgName`.
//
// The unnecessary parameter `keyPrefix` indicates the prefix for each file
// packed into the result bytes.
//
// Note that parameter `srcPaths` supports multiple paths join with ','.
//
// Deprecated, use PackToGoFileWithOption instead.
func PackToGoFile(srcPath, goFilePath, pkgName string, keyPrefix ...string) error {
data, err := Pack(srcPath, keyPrefix...)
if err != nil {
Expand All @@ -85,6 +113,21 @@ func PackToGoFile(srcPath, goFilePath, pkgName string, keyPrefix ...string) erro
)
}

// PackToGoFileWithOption packs the path specified by `srcPaths` to target go file `goFilePath`
// with given package name `pkgName`.
//
// Note that parameter `srcPaths` supports multiple paths join with ','.
func PackToGoFileWithOption(srcPath, goFilePath, pkgName string, option Option) error {
data, err := PackWithOption(srcPath, option)
if err != nil {
return err
}
return gfile.PutContents(
goFilePath,
fmt.Sprintf(gstr.TrimLeft(packedGoSourceTemplate), pkgName, gbase64.EncodeToString(data)),
)
}

// Unpack unpacks the content specified by `path` to []*File.
func Unpack(path string) ([]*File, error) {
realPath, err := gfile.Search(path)
Expand Down
Loading

0 comments on commit 2598745

Please sign in to comment.