Skip to content

Commit

Permalink
feat(cmd/web/enum): 如果未指定 -o 参数会自动生成
Browse files Browse the repository at this point in the history
  • Loading branch information
caixw committed Apr 29, 2024
1 parent d4ca3fe commit 74fce89
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 8 deletions.
9 changes: 8 additions & 1 deletion cmd/web/enum/enum.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"go/types"
"io"
"io/fs"
"path/filepath"
"slices"
"strings"

Expand Down Expand Up @@ -64,7 +65,7 @@ func Init(opt *cmdopt.CmdOpt, p *localeutil.Printer) {
return web.NewLocaleError("no input file")
}
if *o == "" {
return web.NewLocaleError("no output file")
*o = buildOutputFilename(*i)
}
if *t == "" {
return web.NewLocaleError("type not set")
Expand All @@ -79,6 +80,12 @@ func Init(opt *cmdopt.CmdOpt, p *localeutil.Printer) {
})
}

func buildOutputFilename(input string) string {
o := filepath.Base(input)
ext := filepath.Ext(o)
return strings.TrimSuffix(o, ext) + "_enums" + ext
}

func getValues(pkg *types.Package, types []string) (map[string][]string, error) {
vals := make(map[string][]string, len(types))
for _, t := range types {
Expand Down
8 changes: 8 additions & 0 deletions cmd/web/enum/enum_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,11 @@ func TestDump(t *testing.T) {
err := dump("header", "./testdir/testdir.go", "./testdir/testdata.out", []string{"t1", "t3"}, true, true)
a.NotError(err)
}

func TestBuildOutputFilename(t *testing.T) {
a := assert.New(t, false)

a.Equal(buildOutputFilename("abc.go"), "abc_enums.go")
a.Equal(buildOutputFilename("abc_test.g"), "abc_test_enums.g")
a.Equal(buildOutputFilename("abc"), "abc_enums")
}
3 changes: 0 additions & 3 deletions cmd/web/locales/und.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,6 @@ messages:
- key: no input file
message:
msg: no input file
- key: no output file
message:
msg: no output file
- key: no src dir
message:
msg: no src dir
Expand Down
5 changes: 1 addition & 4 deletions cmd/web/locales/zh-CN.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,6 @@ messages:
- key: no input file
message:
msg: 未指定输入文件
- key: no output file
message:
msg: 未指定输出文件
- key: no src dir
message:
msg: 未指定源码目录
Expand Down Expand Up @@ -334,7 +331,7 @@ messages:
msg: 指定输入文件
- key: set output file
message:
msg: 指定输出文件
msg: 指定输出文件,如果为空,则会根据输入文件名加上 _enums 作为后缀,比如 input.go 会以 input_enums.go 作为输出文件。
- key: set source dir
message:
msg: 设置源码的路径
Expand Down

0 comments on commit 74fce89

Please sign in to comment.