-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtableau.go
98 lines (87 loc) · 3.77 KB
/
tableau.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package tableau
import (
"github.com/davecgh/go-spew/spew"
"github.com/tableauio/tableau/internal/confgen"
"github.com/tableauio/tableau/internal/importer"
"github.com/tableauio/tableau/internal/importer/book"
"github.com/tableauio/tableau/internal/localizer"
"github.com/tableauio/tableau/internal/protogen"
"github.com/tableauio/tableau/internal/xlsxgen"
"github.com/tableauio/tableau/internal/xproto"
"github.com/tableauio/tableau/log"
"github.com/tableauio/tableau/options"
"github.com/tableauio/tableau/xerrors"
)
// Generate can convert Excel/CSV/XML/YAML files to protoconf files and
// different configuration files: JSON, Text, and Bin at the same time.
func Generate(protoPackage, indir, outdir string, setters ...options.Option) error {
if err := GenProto(protoPackage, indir, outdir, setters...); err != nil {
return xerrors.Wrapf(err, "failed to generate proto files")
}
if err := GenConf(protoPackage, indir, outdir, setters...); err != nil {
return xerrors.Wrapf(err, "failed to generate conf files")
}
return nil
}
// GenProto can convert Excel/CSV/XML/YAML files to protoconf files.
func GenProto(protoPackage, indir, outdir string, setters ...options.Option) (err error) {
opts := options.ParseOptions(setters...)
if err := localizer.SetLang(opts.Lang); err != nil {
return err
}
if err := log.Init(opts.Log); err != nil {
return err
}
g := protogen.NewGenerator(protoPackage, indir, outdir, setters...)
log.Debugf("options inited: %+v", spew.Sdump(opts))
return g.Generate()
}
// GenConf can convert Excel/CSV/XML/YAML files to different configuration files: JSON, Text, and Bin.
func GenConf(protoPackage, indir, outdir string, setters ...options.Option) error {
opts := options.ParseOptions(setters...)
if err := localizer.SetLang(opts.Lang); err != nil {
return err
}
if err := log.Init(opts.Log); err != nil {
return err
}
g := confgen.NewGenerator(protoPackage, indir, outdir, setters...)
log.Debugf("options inited: %+v", spew.Sdump(opts))
return g.Generate()
}
// NewProtoGenerator creates a new proto generator.
func NewProtoGenerator(protoPackage, indir, outdir string, options ...options.Option) *protogen.Generator {
return protogen.NewGenerator(protoPackage, indir, outdir, options...)
}
// NewProtoGeneratorWithOptions creates a new proto generator with options.
func NewProtoGeneratorWithOptions(protoPackage, indir, outdir string, options *options.Options) *protogen.Generator {
return protogen.NewGeneratorWithOptions(protoPackage, indir, outdir, options)
}
// NewConfGenerator creates a new conf generator.
func NewConfGenerator(protoPackage, indir, outdir string, options ...options.Option) *confgen.Generator {
return confgen.NewGenerator(protoPackage, indir, outdir, options...)
}
// NewConfGeneratorWithOptions creates a new conf generator with options.
func NewConfGeneratorWithOptions(protoPackage, indir, outdir string, options *options.Options) *confgen.Generator {
return confgen.NewGeneratorWithOptions(protoPackage, indir, outdir, options)
}
// SetLang sets the default language.
// E.g: en, zh.
func SetLang(lang string) error {
return localizer.SetLang(lang)
}
// Proto2Excel converts protoconf files to excel files (with tableau header).
// TODO: fully usable generation of excel templates.
func Proto2Excel(protoPackage, indir, outdir string) {
g := xlsxgen.Generator{
ProtoPackage: protoPackage,
InputDir: indir,
OutputDir: outdir,
}
g.Generate()
}
// NewImporter creates a new importer of the specified workbook.
func NewImporter(workbookPath string) (importer.Importer, error) {
parser := confgen.NewSheetParser(xproto.InternalProtoPackage, "", book.MetasheetOptions())
return importer.New(workbookPath, importer.Parser(parser))
}