-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextract.go
119 lines (102 loc) · 2.44 KB
/
extract.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
package main
import (
"encoding/json"
"flag"
"fmt"
"go/ast"
"go/importer"
"go/parser"
"go/token"
"go/types"
"os"
"path/filepath"
"strings"
//"io/ioutil"
//"github.com/maximilien/i18n4go/common"
)
type Translation struct {
ID string `json:"id"`
Translation string `json:"translation"`
Modified bool `json:"modified"`
}
type builder struct {
str []string
}
func main() {
var pkg = flag.String("pkg", "", "help message for flagname")
var dir = flag.String("dir", "", "help message for dir")
var out = flag.String("o", "", "help message for out")
flag.Parse()
var pkgName = *pkg
var absDirPath = *dir
if !filepath.IsAbs(absDirPath) {
absDirPath = filepath.Join(os.Getenv("PWD"), absDirPath)
}
fset := token.NewFileSet()
pkgs, err := parser.ParseDir(fset, absDirPath, nil, parser.ParseComments|parser.AllErrors)
info := types.Info{
Types: make(map[ast.Expr]types.TypeAndValue),
Defs: make(map[*ast.Ident]types.Object),
Uses: make(map[*ast.Ident]types.Object),
}
var conf types.Config
conf.Importer = importer.Default()
files := make([]*ast.File, 0, len(pkgs[pkgName].Files))
for _, file := range pkgs[pkgName].Files {
files = append(files, file)
}
_, err = conf.Check(pkgName, fset, files, &info)
if err != nil {
fmt.Println("checked", err)
return
}
b := builder{}
for _, astFile := range pkgs[pkgName].Files {
err = b.extractString(astFile, &info, fset)
if err != nil {
fmt.Println(err)
}
}
err = b.write(*out)
if err != nil {
fmt.Errorf("got error during write (%v)\n", err)
}
}
func (b *builder) extractString(f *ast.File, info *types.Info, fset *token.FileSet) error {
ast.Inspect(f, func(n ast.Node) bool {
switch x := n.(type) {
case *ast.CallExpr:
if len(x.Args) > 0 {
tc := info.TypeOf(x.Fun)
//fmt.Println(tc.String())
if strings.HasSuffix(tc.String(), "github.com/nicksnyder/go-i18n/i18n.TranslateFunc") {
str := x.Args[0].(*ast.BasicLit)
b.str = append(b.str, str.Value[1:len(str.Value)-1])
//fmt.Printf("got string %s\n", str.Value[1:len(str.Value)-1])
}
}
}
return true
})
return nil
}
func (b *builder) write(out string) error {
tr := []Translation{}
for _, str := range b.str {
tr = append(tr, Translation{
ID: str,
Translation: str,
})
}
f, err := os.Create(out)
if err != nil {
return err
}
defer f.Close()
m, err := json.MarshalIndent(tr, "", " ")
if err != nil {
return err
}
f.Write(m)
return nil
}