-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
executable file
·151 lines (122 loc) · 3.33 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
package main
import (
"fmt"
"bufio"
"os"
"strings"
"github.com/jessevdk/go-flags"
)
var opts struct {
INPUT_FILE_PATH string `short:"l" long:"path" default:"./Localizable.strings" description:"enumに変換するLocalizeStringsファイルへのパス" value-name:"FILE_PATH" required:"true"`
OUTPUT_PATH string `short:"o" long:"output" default:"./" description:"ファイルの出力先" value-name:"OUTPUT_PATH" required:"true"`
OUTPUT_FILE_NAME string `short:"n" long:"name" default:"Localize.swift" description:"基本的にはLocalize.swift" value-name:"FILE_NAME" required:"false"`
ENUM_NAME string `short:"e" long:"ename" default:"LocalizableStrings" description:"Enumの名前" value-name:"ENUM_NAME" required:"false"`
}
func main() {
_, err := flags.Parse(&opts)
CheckIfError(err)
texts := []string{}
LocalisableStringsAnalyzer(opts.INPUT_FILE_PATH, &texts)
filePath := opts.OUTPUT_PATH + opts.OUTPUT_FILE_NAME
err = os.RemoveAll(filePath)
CheckIfError(err)
fp, err := os.OpenFile(filePath, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0666)
CheckIfError(err)
fmt.Fprintln(fp, "import Foundation")
fmt.Fprintln(fp, "enum " + opts.ENUM_NAME + ": String {")
for _, text := range texts {
if text == "" {
continue
}
// 空白はアンダースコアに置換
keyword := strings.Replace(text, " ", "_", -1)
// ハイフンはアンダースコアに置き換え
keyword = strings.Replace(text, "-", "_", -1)
// CamelCase
keyword = convertToCamelCase(keyword)
str := fmt.Sprintf(" case %s = \"%s\"", keyword, text)
fmt.Fprintln(fp,str)
fmt.Println(str)
}
fmt.Fprintln(fp, "}")
fmt.Print("success!")
}
func LocalisableStringsAnalyzer(path string, texts *[]string ){
// xxx.strings のみを解析
if !strings.HasSuffix(path, ".strings") {
return
}
// ファイルをOpenする
file, err := os.Open(path)
// 読み取り時の例外処理
if err != nil {
fmt.Println("error")
}
// 関数が終了した際に確実に閉じるようにする
defer file.Close()
sc := bufio.NewScanner(file)
for i := 1; sc.Scan(); i++ {
if err := sc.Err(); err != nil {
// エラー処理
break
}
text := sc.Text()
text = strings.TrimSpace(text)
index := strings.Index(text, "//")
if index > 0 {
text = text[:index]
text = strings.TrimSpace(text)
}
if strings.HasSuffix(text, ";") {
index := strings.Index(text, "=")
if index > 0 {
text = text[:index]
text = strings.TrimSpace(text)
text = strings.Trim(text, "\"")
}
var result bool = false
for _, element := range *texts {
if element == text {
result = true
break
}
}
if result == false {
*texts = append(*texts, text)
}
}
}
}
func output(text string) {
fmt.Print(text)
}
//
func convertToCamelCase(text string) string {
var keyword string
var foundUnderScore = false
for i := 0; i < len(text); i++ {
letter := text[i : i+1]
if letter == "_" {
foundUnderScore = true
continue
}
if foundUnderScore {
foundUnderScore = false
keyword = keyword + strings.ToUpper(letter)
} else {
keyword = keyword + letter
}
}
return keyword
}
func CheckIfError(err error) {
if err == nil {
return
}
fmt.Printf("\x1b[31;1m%s\x1b[0m\n", fmt.Sprintf("error: %s", err))
os.Exit(1)
}
func Exists(filename string) bool {
_, err := os.Stat(filename)
return err == nil
}