forked from canonical/snapd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
348 lines (299 loc) · 9.27 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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
package main
import (
"bytes"
"fmt"
"go/ast"
"go/parser"
"go/token"
"io"
"log"
"os"
"path/filepath"
"sort"
"strings"
"time"
"github.com/jessevdk/go-flags"
)
type msgID struct {
msgidPlural string
comment string
fname string
line int
formatHint string
}
var msgIDs map[string][]msgID
func formatComment(com string) string {
out := ""
for _, rawline := range strings.Split(com, "\n") {
line := rawline
line = strings.TrimPrefix(line, "//")
line = strings.TrimPrefix(line, "/*")
line = strings.TrimSuffix(line, "*/")
line = strings.TrimSpace(line)
if line != "" {
out += fmt.Sprintf("#. %s\n", line)
}
}
return out
}
func findCommentsForTranslation(fset *token.FileSet, f *ast.File, posCall token.Position) string {
com := ""
for _, cg := range f.Comments {
// search for all comments in the previous line
for i := len(cg.List) - 1; i >= 0; i-- {
c := cg.List[i]
posComment := fset.Position(c.End())
//println(posCall.Line, posComment.Line, c.Text)
if posCall.Line == posComment.Line+1 {
posCall = posComment
com = fmt.Sprintf("%s\n%s", c.Text, com)
}
}
}
// only return if we have a matching prefix
formatedComment := formatComment(com)
needle := fmt.Sprintf("#. %s", opts.AddCommentsTag)
if !strings.HasPrefix(formatedComment, needle) {
formatedComment = ""
}
return formatedComment
}
func constructValue(val interface{}) string {
switch val.(type) {
case *ast.BasicLit:
return val.(*ast.BasicLit).Value
// this happens for constructs like:
// gettext.Gettext("foo" + "bar")
case *ast.BinaryExpr:
// we only support string concat
if val.(*ast.BinaryExpr).Op != token.ADD {
return ""
}
left := constructValue(val.(*ast.BinaryExpr).X)
// strip right " (or `)
left = left[0 : len(left)-1]
right := constructValue(val.(*ast.BinaryExpr).Y)
// strip left " (or `)
right = right[1:]
return left + right
default:
panic(fmt.Sprintf("unknown type: %v", val))
}
}
func inspectNodeForTranslations(fset *token.FileSet, f *ast.File, n ast.Node) bool {
// FIXME: this assume we always have a "gettext.Gettext" style keyword
l := strings.Split(opts.Keyword, ".")
gettextSelector := l[0]
gettextFuncName := l[1]
l = strings.Split(opts.KeywordPlural, ".")
gettextSelectorPlural := l[0]
gettextFuncNamePlural := l[1]
switch x := n.(type) {
case *ast.CallExpr:
if sel, ok := x.Fun.(*ast.SelectorExpr); ok {
i18nStr := ""
i18nStrPlural := ""
if sel.Sel.Name == gettextFuncNamePlural && sel.X.(*ast.Ident).Name == gettextSelectorPlural {
i18nStr = x.Args[0].(*ast.BasicLit).Value
i18nStrPlural = x.Args[1].(*ast.BasicLit).Value
}
if sel.Sel.Name == gettextFuncName && sel.X.(*ast.Ident).Name == gettextSelector {
i18nStr = constructValue(x.Args[0])
}
formatI18nStr := func(s string) string {
if s == "" {
return ""
}
// the "`" is special
if s[0] == '`' {
// keep escaped ", replace inner " with \", replace \n with \\n
rep := strings.NewReplacer(`\"`, `\"`, `"`, `\"`, "\n", "\\n")
s = rep.Replace(s)
}
// strip leading and trailing " (or `)
s = s[1 : len(s)-1]
return s
}
// FIXME: too simplistic(?), no %% is considered
formatHint := ""
if strings.Contains(i18nStr, "%") || strings.Contains(i18nStrPlural, "%") {
// well, not quite correct but close enough
formatHint = "c-format"
}
if i18nStr != "" {
msgidStr := formatI18nStr(i18nStr)
posCall := fset.Position(n.Pos())
msgIDs[msgidStr] = append(msgIDs[msgidStr], msgID{
formatHint: formatHint,
msgidPlural: formatI18nStr(i18nStrPlural),
fname: posCall.Filename,
line: posCall.Line,
comment: findCommentsForTranslation(fset, f, posCall),
})
}
}
}
return true
}
func processFiles(args []string) error {
// go over the input files
msgIDs = make(map[string][]msgID)
fset := token.NewFileSet()
for _, fname := range args {
if err := processSingleGoSource(fset, fname); err != nil {
return err
}
}
return nil
}
func readContent(fname string) (content []byte, err error) {
// If no search directories have been specified or we have an
// absolute path, just try to read the contents directly.
if len(opts.Directories) == 0 || filepath.IsAbs(fname) {
return os.ReadFile(fname)
}
// Otherwise, search for the file in each of the configured
// directories.
for _, dir := range opts.Directories {
content, err = os.ReadFile(filepath.Join(dir, fname))
if !os.IsNotExist(err) {
break
}
}
return content, err
}
func processSingleGoSource(fset *token.FileSet, fname string) error {
fnameContent, err := readContent(fname)
if err != nil {
return err
}
// Create the AST by parsing src.
f, err := parser.ParseFile(fset, fname, fnameContent, parser.ParseComments)
if err != nil {
return err
}
ast.Inspect(f, func(n ast.Node) bool {
return inspectNodeForTranslations(fset, f, n)
})
return nil
}
var formatTime = func() string {
return time.Now().Format("2006-01-02 15:04-0700")
}
// mustFprintf will write the given format string to the given
// writer. Any error will make it panic.
func mustFprintf(w io.Writer, format string, a ...interface{}) {
_, err := fmt.Fprintf(w, format, a...)
if err != nil {
panic(fmt.Sprintf("cannot write output: %v", err))
}
}
func writePotFile(out io.Writer) {
header := fmt.Sprintf(`# SOME DESCRIPTIVE TITLE.
# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
# This file is distributed under the same license as the PACKAGE package.
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#
#, fuzzy
msgid ""
msgstr "Project-Id-Version: %s\n"
"Report-Msgid-Bugs-To: %s\n"
"POT-Creation-Date: %s\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
"Language: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=CHARSET\n"
"Content-Transfer-Encoding: 8bit\n"
`, opts.PackageName, opts.MsgIDBugsAddress, formatTime())
mustFprintf(out, "%s", header)
// yes, this is the way to do it in go
sortedKeys := []string{}
for k := range msgIDs {
sortedKeys = append(sortedKeys, k)
}
if opts.SortOutput {
sort.Strings(sortedKeys)
}
// FIXME: use template here?
for _, k := range sortedKeys {
msgidList := msgIDs[k]
for _, msgid := range msgidList {
if opts.AddComments || opts.AddCommentsTag != "" {
mustFprintf(out, "%s", msgid.comment)
}
}
if !opts.NoLocation {
mustFprintf(out, "#:")
for _, msgid := range msgidList {
mustFprintf(out, " %s:%d", msgid.fname, msgid.line)
}
mustFprintf(out, "\n")
}
msgid := msgidList[0]
if msgid.formatHint != "" {
mustFprintf(out, "#, %s\n", msgid.formatHint)
}
var formatOutput = func(in string) string {
// split string with \n into multiple lines
// to make the output nicer
out := strings.Replace(in, "\\n", "\\n\"\n \"", -1)
// cleanup too aggressive splitting (empty "" lines)
return strings.TrimSuffix(out, "\"\n \"")
}
mustFprintf(out, "msgid \"%v\"\n", formatOutput(k))
if msgid.msgidPlural != "" {
mustFprintf(out, "msgid_plural \"%v\"\n", formatOutput(msgid.msgidPlural))
mustFprintf(out, "msgstr[0] \"\"\n")
mustFprintf(out, "msgstr[1] \"\"\n")
} else {
mustFprintf(out, "msgstr \"\"\n")
}
mustFprintf(out, "\n")
}
}
// FIXME: this must be setable via go-flags
var opts struct {
FilesFrom string `short:"f" long:"files-from" description:"get list of input files from FILE"`
Directories []string `short:"D" long:"directory" description:"add DIRECTORY to list for input files search"`
Output string `short:"o" long:"output" description:"output to specified file"`
AddComments bool `short:"c" long:"add-comments" description:"place all comment blocks preceding keyword lines in output file"`
AddCommentsTag string `long:"add-comments-tag" description:"place comment blocks starting with TAG and prceding keyword lines in output file"`
SortOutput bool `short:"s" long:"sort-output" description:"generate sorted output"`
NoLocation bool `long:"no-location" description:"do not write '#: filename:line' lines"`
MsgIDBugsAddress string `long:"msgid-bugs-address" default:"EMAIL" description:"set report address for msgid bugs"`
PackageName string `long:"package-name" description:"set package name in output"`
Keyword string `short:"k" long:"keyword" default:"gettext.Gettext" description:"look for WORD as the keyword for singular strings"`
KeywordPlural string `long:"keyword-plural" default:"gettext.NGettext" description:"look for WORD as the keyword for plural strings"`
}
func main() {
// parse args
args, err := flags.ParseArgs(&opts, os.Args)
if err != nil {
log.Fatalf("ParseArgs failed %s", err)
}
var files []string
if opts.FilesFrom != "" {
content, err := os.ReadFile(opts.FilesFrom)
if err != nil {
log.Fatalf("cannot read file %v: %v", opts.FilesFrom, err)
}
content = bytes.TrimSpace(content)
files = strings.Split(string(content), "\n")
} else {
files = args[1:]
}
if err := processFiles(files); err != nil {
log.Fatalf("processFiles failed with: %s", err)
}
out := os.Stdout
if opts.Output != "" {
var err error
out, err = os.Create(opts.Output)
if err != nil {
log.Fatalf("failed to create %s: %s", opts.Output, err)
}
}
writePotFile(out)
}