@@ -10,85 +10,97 @@ import Foundation
10
10
11
11
struct FileGenerator {
12
12
13
- /**
13
+ /**
14
14
Fetch the template for creating model.swift files.
15
15
16
16
- parameter filename: Name of the file to be loaded
17
17
18
18
- returns: String containing the template.
19
19
*/
20
- static func loadFileWith( _ filename: String ) -> String {
20
+ static func loadFileWith( _ filename: String ) -> String {
21
21
22
- let bundle = Bundle . main
23
- let path = bundle. path ( forResource: filename, ofType: " txt " )
22
+ let bundle = Bundle . main
23
+ let path = bundle. path ( forResource: filename, ofType: " txt " )
24
24
25
- do {
26
- let content = try String . init ( contentsOfFile: path!)
27
- return content
28
- } catch { }
25
+ do {
26
+ let content = try String . init ( contentsOfFile: path!)
27
+ return content
28
+ } catch { }
29
29
30
- return " "
31
- }
32
-
33
- static func generateFileContentWith( _ modelFile: ModelFile , configuration: ModelGenerationConfiguration ) -> String {
34
-
35
- var content = loadFileWith ( " BaseTemplate " )
36
- content = content. replacingOccurrences ( of: " {OBJECT_NAME} " , with: modelFile. fileName)
37
- content = content. replacingOccurrences ( of: " {DATE} " , with: todayDateString ( ) )
38
- content = content. replacingOccurrences ( of: " {OBJECT_KIND} " , with: modelFile. type. rawValue)
39
- content = content. replacingOccurrences ( of: " {JSON_PARSER_LIBRARY_BODY} " , with: loadFileWith ( modelFile. mainBodyFileName ( ) ) )
40
- if let authorName = configuration. authorName {
41
- content = content. replacingOccurrences ( of: " __NAME__ " , with: authorName)
42
- }
43
- if let companyName = configuration. companyName {
44
- content = content. replacingOccurrences ( of: " __MyCompanyName__ " , with: companyName)
45
- }
46
- content = content. replacingOccurrences ( of: " {INCLUDE_HEADER} " , with: " \n import \( modelFile. moduleName ( ) ) " )
47
-
48
- var classesExtendFrom : [ String ] = [ ]
49
- if let extendFrom = modelFile. baseElementName ( ) {
50
- classesExtendFrom = [ extendFrom]
51
- }
52
- if configuration. supportNSCoding && configuration. constructType == . ClassType {
53
- classesExtendFrom = classesExtendFrom + [ " NSCoding " ]
54
- }
55
-
56
- if classesExtendFrom. count > 0 {
57
- content = content. replacingOccurrences ( of: " {EXTEND_FROM} " , with: classesExtendFrom. joined ( separator: " , " ) )
58
- content = content. replacingOccurrences ( of: " {EXTENDED_OBJECT_COLON} " , with: " : " )
59
- } else {
60
- content = content. replacingOccurrences ( of: " {EXTEND_FROM} " , with: " " )
61
- content = content. replacingOccurrences ( of: " {EXTENDED_OBJECT_COLON} " , with: " " )
30
+ return " "
62
31
}
63
32
64
- let stringConstants = modelFile. component. stringConstants. map ( { " " + $0 } ) . joined ( separator: " \n " )
65
- let declarations = modelFile. component. declarations. map ( { " " + $0 } ) . joined ( separator: " \n " )
66
- let initialisers = modelFile. component. initialisers. map ( { " " + $0 } ) . joined ( separator: " \n " )
67
- let description = modelFile. component. description. map ( { " " + $0 } ) . joined ( separator: " \n " )
68
-
69
- content = content. replacingOccurrences ( of: " {STRING_CONSTANT} " , with: stringConstants)
70
- content = content. replacingOccurrences ( of: " {DECLARATION} " , with: declarations)
71
- content = content. replacingOccurrences ( of: " {INITALIZER} " , with: initialisers)
72
- content = content. replacingOccurrences ( of: " {DESCRIPTION} " , with: description)
73
-
74
- if configuration. constructType == . StructType {
75
- content = content. replacingOccurrences ( of: " convenience " , with: " " )
33
+ static func generateFileContentWith( _ modelFile: ModelFile , configuration: ModelGenerationConfiguration ) -> String {
34
+
35
+ var content = loadFileWith ( " BaseTemplate " )
36
+ content = content. replacingOccurrences ( of: " {OBJECT_NAME} " , with: modelFile. fileName)
37
+ content = content. replacingOccurrences ( of: " {DATE} " , with: todayDateString ( ) )
38
+ content = content. replacingOccurrences ( of: " {OBJECT_KIND} " , with: modelFile. type. rawValue)
39
+ content = content. replacingOccurrences ( of: " {JSON_PARSER_LIBRARY_BODY} " , with: loadFileWith ( modelFile. mainBodyFileName ( ) ) )
40
+
41
+ if modelFile. type == . ClassType {
42
+ content = content. replacingOccurrences ( of: " {REQUIRED} " , with: " required " )
43
+ } else {
44
+ content = content. replacingOccurrences ( of: " {REQUIRED} " , with: " " )
45
+ }
46
+ if let authorName = configuration. authorName {
47
+ content = content. replacingOccurrences ( of: " __NAME__ " , with: authorName)
48
+ }
49
+ if let companyName = configuration. companyName {
50
+ content = content. replacingOccurrences ( of: " __MyCompanyName__ " , with: companyName)
51
+ }
52
+ content = content. replacingOccurrences ( of: " {INCLUDE_HEADER} " , with: " \n import \( modelFile. moduleName ( ) ) " )
53
+
54
+ var classesExtendFrom : [ String ] = [ ]
55
+ if let extendFrom = modelFile. baseElementName ( ) {
56
+ classesExtendFrom = [ extendFrom]
57
+ }
58
+ if configuration. supportNSCoding && configuration. constructType == . ClassType {
59
+ classesExtendFrom = classesExtendFrom + [ " NSCoding " ]
60
+ }
61
+
62
+ if configuration. isFinalRequired && configuration. constructType == . ClassType {
63
+ content = content. replacingOccurrences ( of: " {IS_FINAL} " , with: " final " )
64
+ } else {
65
+ content = content. replacingOccurrences ( of: " {IS_FINAL} " , with: " " )
66
+ }
67
+
68
+ if classesExtendFrom. count > 0 {
69
+ content = content. replacingOccurrences ( of: " {EXTEND_FROM} " , with: classesExtendFrom. joined ( separator: " , " ) )
70
+ content = content. replacingOccurrences ( of: " {EXTENDED_OBJECT_COLON} " , with: " : " )
71
+ } else {
72
+ content = content. replacingOccurrences ( of: " {EXTEND_FROM} " , with: " " )
73
+ content = content. replacingOccurrences ( of: " {EXTENDED_OBJECT_COLON} " , with: " " )
74
+ }
75
+
76
+ let stringConstants = modelFile. component. stringConstants. map ( { " " + $0 } ) . joined ( separator: " \n " )
77
+ let declarations = modelFile. component. declarations. map ( { " " + $0 } ) . joined ( separator: " \n " )
78
+ let initialisers = modelFile. component. initialisers. map ( { " " + $0 } ) . joined ( separator: " \n " )
79
+ let description = modelFile. component. description. map ( { " " + $0 } ) . joined ( separator: " \n " )
80
+
81
+ content = content. replacingOccurrences ( of: " {STRING_CONSTANT} " , with: stringConstants)
82
+ content = content. replacingOccurrences ( of: " {DECLARATION} " , with: declarations)
83
+ content = content. replacingOccurrences ( of: " {INITIALIZER} " , with: initialisers)
84
+ content = content. replacingOccurrences ( of: " {DESCRIPTION} " , with: description)
85
+
86
+ if configuration. constructType == . StructType {
87
+ content = content. replacingOccurrences ( of: " convenience " , with: " " )
88
+ }
89
+
90
+ if configuration. supportNSCoding && configuration. constructType == . ClassType {
91
+ content = content. replacingOccurrences ( of: " {NSCODING_SUPPORT} " , with: loadFileWith ( " NSCodingTemplate " ) )
92
+ let encoders = modelFile. component. encoders. map ( { " " + $0 } ) . joined ( separator: " \n " )
93
+ let decoders = modelFile. component. decoders. map ( { " " + $0 } ) . joined ( separator: " \n " )
94
+ content = content. replacingOccurrences ( of: " {DECODERS} " , with: decoders)
95
+ content = content. replacingOccurrences ( of: " {ENCODERS} " , with: encoders)
96
+ } else {
97
+ content = content. replacingOccurrences ( of: " {NSCODING_SUPPORT} " , with: " " )
98
+ }
99
+
100
+ return content
76
101
}
77
102
78
- if configuration. supportNSCoding && configuration. constructType == . ClassType {
79
- content = content. replacingOccurrences ( of: " {NSCODING_SUPPORT} " , with: loadFileWith ( " NSCodingTemplate " ) )
80
- let encoders = modelFile. component. encoders. map ( { " " + $0 } ) . joined ( separator: " \n " )
81
- let decoders = modelFile. component. decoders. map ( { " " + $0 } ) . joined ( separator: " \n " )
82
- content = content. replacingOccurrences ( of: " {DECODERS} " , with: decoders)
83
- content = content. replacingOccurrences ( of: " {ENCODERS} " , with: encoders)
84
- } else {
85
- content = content. replacingOccurrences ( of: " {NSCODING_SUPPORT} " , with: " " )
86
- }
87
-
88
- return content
89
- }
90
-
91
- /**
103
+ /**
92
104
Write the given content to a file at the mentioned path.
93
105
94
106
- parameter name: The name of the file.
@@ -97,24 +109,24 @@ struct FileGenerator {
97
109
98
110
- returns: Boolean indicating if the process was successful.
99
111
*/
100
- static internal func writeToFileWith( _ name: String , content: String , path: String ) -> Bool {
101
- let filename = path. appendingFormat ( " %@ " , ( name + " .swift " ) )
102
- do {
103
- try FileManager . default. createDirectory ( at: URL . init ( fileURLWithPath: path) ,
104
- withIntermediateDirectories: true ,
105
- attributes: nil )
106
- try content. write ( toFile: filename, atomically: true , encoding: String . Encoding. utf8)
107
- return true
108
- } catch let error as NSError {
109
- print ( error)
110
- return false
112
+ static internal func writeToFileWith( _ name: String , content: String , path: String ) -> Bool {
113
+ let filename = path. appendingFormat ( " %@ " , ( name + " .swift " ) )
114
+ do {
115
+ try FileManager . default. createDirectory ( at: URL . init ( fileURLWithPath: path) ,
116
+ withIntermediateDirectories: true ,
117
+ attributes: nil )
118
+ try content. write ( toFile: filename, atomically: true , encoding: String . Encoding. utf8)
119
+ return true
120
+ } catch let error as NSError {
121
+ print ( error)
122
+ return false
123
+ }
111
124
}
112
- }
113
125
114
- static fileprivate func todayDateString( ) -> String {
115
- let formatter = DateFormatter . init ( )
116
- formatter. dateStyle = . short
117
- return formatter. string ( from: Date . init ( ) )
118
- }
126
+ static fileprivate func todayDateString( ) -> String {
127
+ let formatter = DateFormatter . init ( )
128
+ formatter. dateStyle = . short
129
+ return formatter. string ( from: Date . init ( ) )
130
+ }
119
131
120
132
}
0 commit comments