@@ -37,27 +37,107 @@ class SKCodeBuilder: NSObject {
37
37
let hString = NSMutableString ( )
38
38
let mString = NSMutableString ( )
39
39
handleDictValue ( dictValue: jsonObj, key: " " , hString: hString, mString: mString)
40
+ if config. superClassName == " NSObject " {
41
+ hString. insert ( " \n #import <Foundation/Foundation.h> \n \n " , at: 0 )
42
+ } else {
43
+ hString. insert ( " \n #import \" \( config. superClassName) .h \" \n \n " , at: 0 )
44
+ }
45
+ mString. insert ( " \n #import \" \( config. rootModelName) .h \" \n \n " , at: 0 )
46
+
47
+ let dateFormatter = DateFormatter ( )
48
+ dateFormatter. dateFormat = " yyyy/MM/dd "
49
+ let time = dateFormatter. string ( from: Date ( ) )
50
+ let year = time. components ( separatedBy: " / " ) . first ?? " 2020 "
51
+
52
+ let hCommentString =
53
+ """
54
+ //
55
+ // \( config. rootModelName) .h
56
+ // SKCodeBuilder
57
+ //
58
+ // Created by \( config. authorName) on \( time) .
59
+ // Copyright © \( year) SKCodeBuilder. All rights reserved.
60
+ // \n
61
+ """
62
+
63
+ let mCommentString =
64
+ """
65
+ //
66
+ // \( config. rootModelName) .m
67
+ // SKCodeBuilder
68
+ //
69
+ // Created by \( config. authorName) on \( time) .
70
+ // Copyright © \( year) SKCodeBuilder. All rights reserved.
71
+ // \n
72
+ """
73
+
74
+ hString. insert ( hCommentString, at: 0 )
75
+ mString. insert ( mCommentString, at: 0 )
76
+
40
77
if let handler = complete {
41
78
handler ( hString, mString)
42
79
}
43
80
}
44
81
45
- func generate_OC_File( with filePath: String , hString: NSMutableString , mString: NSMutableString , complete: GenerateFileComplete ) {
46
-
82
+ func generate_OC_File( with filePath: String ? , hString: NSMutableString , mString: NSMutableString , complete: GenerateFileComplete ? ) {
83
+ if hString. length > 0 && mString. length > 0 {
84
+
85
+ var filePath = filePath
86
+ var success = false
87
+
88
+ if filePath == nil {
89
+ if let desktopPath = NSSearchPathForDirectoriesInDomains ( . desktopDirectory, . userDomainMask, false ) . last {
90
+ let path = desktopPath. appending ( " /SKGenerateModelToolFiles " )
91
+ print ( " path = \( path) " )
92
+ var isDir = ObjCBool . init ( false )
93
+ let isExists = FileManager . default. fileExists ( atPath: path, isDirectory: & isDir)
94
+ if isDir. boolValue && isExists {
95
+ filePath = path
96
+ } else {
97
+ do {
98
+ try FileManager . default. createDirectory ( atPath: path, withIntermediateDirectories: true , attributes: nil )
99
+ filePath = path
100
+ } catch let error {
101
+ print ( " createDirectory error = \( error) " )
102
+ success = false
103
+ }
104
+ }
105
+ }
106
+ }
107
+
108
+ let fileNameH = filePath? . appending ( " / \( config. rootModelName) .h " )
109
+ let fileNameM = filePath? . appending ( " / \( config. rootModelName) .m " )
110
+
111
+ do {
112
+ try hString. write ( toFile: fileNameH!, atomically: true , encoding: String . Encoding. utf8. rawValue)
113
+ try mString. write ( toFile: fileNameM!, atomically: true , encoding: String . Encoding. utf8. rawValue)
114
+ success = true
115
+ } catch {
116
+ success = false
117
+ }
118
+
119
+ print ( " fileNameH = \( fileNameH!) " )
120
+ print ( " fileNameM = \( fileNameM!) " )
121
+
122
+ if let complete = complete {
123
+ complete ( success, filePath!)
124
+ }
125
+ }
47
126
}
48
127
49
128
// MARK: - Private Handler
50
129
51
130
private func handleDictValue( dictValue: Any , key: String , hString: NSMutableString , mString: NSMutableString ) {
52
131
53
132
if key. isBlank { // Root model
54
- hString. append ( " \n \n @interface \( self . config. rootModelName) : \( self . config. superClassName) \n \n " )
55
- mString. append ( " \n \n @implementation \( self . config. rootModelName) \n \n " )
133
+ hString. append ( " \n @interface \( self . config. rootModelName) : \( self . config. superClassName) \n \n " )
134
+ mString. append ( " \n @implementation \( self . config. rootModelName) \n \n " )
56
135
57
136
} else { // sub model
58
137
let modeName = modelClassName ( with: key)
59
- hString. append ( " \n \n @interface \( modeName) : \( self . config. superClassName) \n \n " )
60
- mString. append ( " \n \n @implementation \( modeName) \n \n " )
138
+ hString. insert ( " @class \( modeName) ; \n " , at: 0 )
139
+ hString. append ( " \n @interface \( modeName) : \( self . config. superClassName) \n \n " )
140
+ mString. append ( " \n @implementation \( modeName) \n \n " )
61
141
}
62
142
63
143
switch dictValue {
0 commit comments