-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
executable file
·264 lines (230 loc) · 7.73 KB
/
main.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
package main
import (
"os"
"io/ioutil"
"fmt"
spec "github.com/Sam-Izdat/pogo/gtspec"
"github.com/Sam-Izdat/pogo/po"
"github.com/Sam-Izdat/pogo/deps/odin/cli"
"time"
"strings"
"runtime"
"errors"
)
var (
o spec.Config
CLI = cli.New("0.0.3", "pogo command line utility", exec)
ps = string(os.PathSeparator)
cmdInit, cmdBuild *cli.SubCommand
)
func init() {
cmdInit = CLI.DefineSubCommand("init", "initialize pogo in this directory", pinit)
cmdBuild = CLI.DefineSubCommand("build", "scan source and compile .pot or .po files", build, "filetype")
cmdBuild.DefineBoolFlag("overwrite", false, "overwrite existing files")
cmdBuild.AliasFlag('o', "overwrite")
}
func main() {
CLI.Start()
}
func exec(c cli.Command) {
fmt.Println(pWarn, "missing command \n", `See "pogo --help" for usage information`)
os.Exit(1)
}
func pinit(c cli.Command) {
wd, err := os.Getwd()
clid := getCallerDir()
fnFrom, fnTo := "tmpl.toml", "POGO.toml"
if _, err := os.Stat(wd+ps+fnTo); err == nil {
fmt.Println(pNotice,
fStr("aborting").s("bold"), "- configuration file already exists \n", wd+ps+fnTo)
os.Exit(1)
}
tmpl, err := ioutil.ReadFile(clid+ps+fnFrom)
if err != nil {
fmt.Println(pWarn, "could not read default configuration file:\n", err)
os.Exit(1)
}
err = ioutil.WriteFile(wd+ps+fnTo, tmpl, 0755)
if err != nil {
fmt.Println(pWarn, "could not write configuration file:\n", err)
os.Exit(1)
}
fmt.Println(pSuccess, "default configuration file created - edit POGO.toml to configure")
os.Exit(1)
}
func build(c cli.Command) {
loadOptions()
if c.Params()["filetype"] == nil {
fmt.Println("\n", pWarn, "missing filetype parameter \n",
`Specify what to build ("pot"/"po") - e.g. "pogo build pot"`)
os.Exit(1)
}
switch fmt.Sprintf("%s", c.Param("filetype")) {
case "pot":
verifyLocaleDir()
// Verify no file or active overwrite flag
fn := o.General.DirLocale+ps+o.General.ProjectFN+".pot"
if _, err := os.Stat(fn); err == nil && c.Flag("overwrite").Get() == false {
fmt.Println(pNotice, fStr("skipping").s("bold"),
`template - file already exists; use "-o" flag to overwrite`)
os.Exit(1)
}
// Scan
defer un(trace("scan/build"))
fmt.Println("Parsing...")
pdir := o.General.DirProject
msgs := []spec.Msg{}
msgs = append(po.ScanGo(pdir), po.ScanTmpl(pdir)...)
po.RemoveDuplicates(&msgs)
fmt.Println(len(msgs), "unique message(s) extracted")
fmt.Println("Compiling", o.General.ProjectFN+".pot...")
// Write
err := WritePOT(msgs)
if err != nil {
fmt.Println(pWarn, err)
}
case "po":
verifyLocaleDir()
defer un(trace("scan/build"))
fmt.Println("Parsing...")
pdir := o.General.DirProject
msgs := []spec.Msg{}
msgs = append(po.ScanGo(pdir), po.ScanTmpl(pdir)...)
po.RemoveDuplicates(&msgs)
fmt.Println(len(msgs), "unique message(s) extracted")
if c.Flag("overwrite").Get() == true {
fmt.Println(pNotice, fStr("WARNING!").s("bold"))
fmt.Println(fStr("OVERWRITING EXISTING CATALOGS").s("red"))
fmt.Println("You will be asked to confirm overwrites individually.")
fmt.Println("This is a destructive and not an additive process.")
fmt.Println("Any completed translations in these files will be", fStr("LOST!").s("bold"))
}
for _, target := range o.General.Targets {
path := o.General.DirLocale+ps+target+ps+o.General.DirMessages
dir, err := os.Stat(path)
if err != nil || !dir.IsDir(){
err = os.MkdirAll(path, os.FileMode(0755))
if err != nil {
fmt.Println(pWarn, fStr("skipping").s("bold"), target,
`could not create locale directory`)
}
}
file := o.General.ProjectFN+"."+target+".po"
fn := path+ps+file
if _, err := os.Stat(fn); err == nil {
if c.Flag("overwrite").Get() == true {
var confirm string
fmt.Print("Are you sure you want to overwrite ", file, "? (y/N) : ")
fmt.Scanln(&confirm)
if confirm != "y" && confirm != "Y" {
continue
}
} else {
fmt.Println(pNotice, fStr("skipping").s("bold"), file,
`- file already exists; use "-o" flag to overwrite`)
continue
}
}
fmt.Println("Compiling", file+"...")
// Write
err = WritePO(msgs, target, path)
if err != nil {
fmt.Println(pWarn, "ERROR compiling", file, "-", err)
}
}
default:
fmt.Println(pWarn, "invalid filetype parameter \n",
`Expecting "pot" or "po" - e.g. "pogo build pot"`)
os.Exit(1)
}
}
func loadOptions() {
var err error
o, err = spec.LoadOptions()
if err != nil {
fmt.Println(pWarn, "could not locate or parse POGO.toml configuration file \n" +
`Run "pogo init" to intialize i18n in this directory`)
os.Exit(1)
}
}
func verifyLocaleDir() {
dir, err := os.Stat(o.General.DirLocale)
if err != nil || !dir.IsDir(){
fmt.Println(pWarn,
"locale directory does not exist")
os.Exit(1)
}
}
func WritePOT(msgs []spec.Msg) error {
pofile := po.Compile(msgs, "", "", "")
// open output file
fn := o.General.DirLocale+ps+o.General.ProjectFN+".pot"
fo, err := os.Create(fn)
if err != nil {
return err
}
// close fo on exit and check for its returned error
defer func() error {
if err := fo.Close(); err != nil {
return err
}
return nil
}()
// write a chunk
if _, err := fo.Write([]byte(pofile)); err != nil {
return err
}
return nil
}
func WritePO(msgs []spec.Msg, target string, path string) error {
prule := spec.Plurals[target]
if prule == nil {
prule = spec.Plurals[strings.Split(target, "_")[0]]
if prule == nil {
return errors.New("unknown locale")
}
}
name := prule.Name()
pf := prule.Header()
pofile := po.Compile(msgs, target, name, pf)
// open output file
fn := path+ps+o.General.ProjectFN+"."+target+".po"
fo, err := os.Create(fn)
if err != nil {
return err
}
// close fo on exit and check for its returned error
defer func() error {
if err := fo.Close(); err != nil {
return err
}
return nil
}()
// write a chunk
if _, err := fo.Write([]byte(pofile)); err != nil {
return err
}
return nil
}
func getCallerDir() string {
_, fn, _, _ := runtime.Caller(1)
tmp := strings.Split(fn, "/") // always "/" -- do NOT use OS-specific path separator
dir := strings.Join(tmp[:len(tmp)-1], ps)
return dir
}
func trace(s string) (string, time.Time) {
fmt.Println(fStr("Starting").s("green"), fStr(s).s("green"))
return s, time.Now()
}
func un(s string, startTime time.Time) {
endTime := time.Now()
fmt.Println(
fStr("Ending").s("green"),
fStr(s).s("green"),
fStr("- took").s("green"),
fStr(endTime.Sub(startTime).String()).s("green"),
)
}
func isWindows() bool { // <!--[if IE 6]> lalalalalala
return os.PathSeparator == '\\' && os.PathListSeparator == ';'
}