@@ -10,85 +10,97 @@ import Foundation
1010
1111struct FileGenerator {
1212
13- /**
13+ /**
1414 Fetch the template for creating model.swift files.
1515
1616 - parameter filename: Name of the file to be loaded
1717
1818 - returns: String containing the template.
1919 */
20- static func loadFileWith( _ filename: String ) -> String {
20+ static func loadFileWith( _ filename: String ) -> String {
2121
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 " )
2424
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 { }
2929
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 " "
6231 }
6332
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
76101 }
77102
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+ /**
92104 Write the given content to a file at the mentioned path.
93105
94106 - parameter name: The name of the file.
@@ -97,24 +109,24 @@ struct FileGenerator {
97109
98110 - returns: Boolean indicating if the process was successful.
99111 */
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+ }
111124 }
112- }
113125
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+ }
119131
120132}
0 commit comments