-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSwiftWriter.go
92 lines (81 loc) · 2.74 KB
/
SwiftWriter.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
package main
import (
"os"
"regexp"
"strings"
)
/*
Used to transform a key of a strings.xml to an idiomatic Swift var name
*/
func swiftTransformKeyToSwiftVarName(key string) string {
reg, _ := regexp.Compile(`(_).`)
workingKey := reg.ReplaceAllStringFunc(key, func(rKey string) string {
return strings.ToUpper(rKey)
})
workingKey = strings.Replace(workingKey, "_", "", -1)
workingKey = strings.Replace(workingKey, ".", "_", -1)
return workingKey
}
/*
Writes the Swift StringKey api for a given StringKey struct
*/
func writeSwiftKeyFile(value *StringKeys, config *StringCheeseConfig) error {
pathToSwiftKey := config.pathToIOSProject + config.pathToSwiftKey + "/" + config.className + ".swift"
_ = os.Remove(pathToSwiftKey) //skipped err check
file, err := os.Create(pathToSwiftKey)
if err != nil {
return err
}
file.WriteString(config.timeStampString +
"//This will be deleted and generated each time you run StringCheese.\n" +
"import Foundation\n" +
"\n" +
"class " + config.className)
if config.objCSupport {
file.WriteString(": NSObject")
}
file.WriteString(" { \n")
if config.createStaticKeyClass {
file.WriteString(" private static var _shared: " + config.className + "? = nil\n" +
" static var shared: " + config.className + " {\n" +
" if let s = _shared {\n" +
" return s\n" +
" }\n" +
" let s = " + config.className + "()\n" +
" _shared = s\n" +
" return s\n" +
" }\n\n")
}
file.WriteString(" private func localize(_ key: String) -> String { \n" +
" return NSLocalizedString(key, comment: \"\")\n" +
" }\n")
values := value.getSortedValues()
writeArgSwiftFuncs := config.shouldCreateArguments
var objCStringTag = ""
if config.objCSupport {
objCStringTag = "@objc "
}
for _, value := range values {
if value.translatable == false {
file.WriteString(" " + objCStringTag + "let " + swiftTransformKeyToSwiftVarName(value.originalKey) + ": String = " +
"\"" + value.value + "\"\n")
} else if writeArgSwiftFuncs && value.numberOfArguments > 0 {
//I added the raw string just incase
file.WriteString(" " + objCStringTag + "var raw_" + swiftTransformKeyToSwiftVarName(value.originalKey) + ": String {\n" +
" localize(\"" + value.key + "\")\n" +
" }\n")
file.WriteString(" " + objCStringTag + "func " + swiftTransformKeyToSwiftVarName(value.originalKey) + "(" +
value.argumentString + ") -> String {\n" +
" let s = localize(\"" + value.key + "\")\n" +
" return String(format: s, " + value.formatString + ")\n" +
" }\n")
} else {
file.WriteString(" " + objCStringTag + "var " + swiftTransformKeyToSwiftVarName(value.originalKey) + ": String {\n" +
" localize(\"" + value.key + "\")\n" +
" }\n")
}
}
file.WriteString("}")
file.Close()
return nil
}