forked from cloudfoundry/cli
/
remove-unused-translations.go
178 lines (144 loc) · 3.18 KB
/
remove-unused-translations.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
package main
import (
"encoding/json"
"fmt"
"go/ast"
"go/parser"
"go/token"
"io/ioutil"
"os"
"path/filepath"
)
// NB: this assumes that translation strings are globally unique
// as of the day we wrote this, they are not unique
func main() {
walkTranslationFilesAndPromptUser()
}
func walkTranslationFilesAndPromptUser() {
stringsFromCode := readSourceCode()
dir := "cf/i18n/resources"
filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
panic(err.Error())
}
if info.IsDir() {
return nil
}
if filepath.Ext(info.Name()) != ".json" {
return nil
}
file, err := os.Open(path)
if err != nil {
panic(err.Error())
}
data, err := ioutil.ReadAll(file)
if err != nil {
panic(err.Error())
}
maps := []map[string]string{}
err = json.Unmarshal(data, &maps)
if err != nil {
panic(err.Error())
}
indicesToRemove := []int{}
for index, tmap := range maps {
str := tmap["id"]
foundStr := false
for _, codeStr := range stringsFromCode {
if codeStr == str {
foundStr = true
break
}
}
if !foundStr {
fmt.Printf("Did not find this string in the source code:\n")
fmt.Printf("'%s'\n", str)
println()
answer := ""
fmt.Printf("Would you like to delete it from %s? [y|n]", path)
fmt.Fscanln(os.Stdin, &answer)
if answer == "y" {
indicesToRemove = append(indicesToRemove, index)
}
}
}
if len(indicesToRemove) > 0 {
println("Removing", len(indicesToRemove), "translations from", path)
newMaps := []map[string]string{}
for i, mapp := range maps {
foundIndex := false
for _, index := range indicesToRemove {
if index == i {
foundIndex = true
break
}
}
if !foundIndex {
newMaps = append(newMaps, mapp)
}
}
bytes, err := json.Marshal(newMaps) // consider json.MarshalIndent
if err != nil {
panic(err.Error())
}
newFile, err := os.Create(path)
if err != nil {
panic(err.Error())
}
_, err = newFile.Write(bytes)
if err != nil {
panic(err.Error())
}
}
return nil
})
}
func readSourceCode() []string {
strings := []string{}
dir, err := os.Getwd()
if err != nil {
panic(err.Error())
}
filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
panic(err.Error())
}
if info.IsDir() {
return nil
}
if filepath.Ext(info.Name()) != ".go" {
return nil
}
fileSet := token.NewFileSet()
astFile, err := parser.ParseFile(fileSet, path, nil, 0)
if err != nil {
panic(err.Error())
}
for _, declaration := range astFile.Decls {
ast.Inspect(declaration, func(node ast.Node) bool {
callExpr, ok := node.(*ast.CallExpr)
if !ok {
return true
}
funcNode, ok := callExpr.Fun.(*ast.Ident)
if !ok {
return true
}
if funcNode.Name != "T" {
return true
}
firstArg := callExpr.Args[0]
argAsString, ok := firstArg.(*ast.BasicLit)
if !ok {
return true
}
// remove quotes around string literal
end := len(argAsString.Value) - 1
strings = append(strings, argAsString.Value[1:end])
return true
})
}
return nil
})
return strings
}