Skip to content

Commit 49db58c

Browse files
author
shangkun
committed
Update header comment
1 parent 8fd7edd commit 49db58c

File tree

3 files changed

+151
-9
lines changed

3 files changed

+151
-9
lines changed

SKGenerateModelTool/SKCodeBuilder.swift

Lines changed: 86 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,27 +37,107 @@ class SKCodeBuilder: NSObject {
3737
let hString = NSMutableString()
3838
let mString = NSMutableString()
3939
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+
4077
if let handler = complete {
4178
handler(hString, mString)
4279
}
4380
}
4481

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+
}
47126
}
48127

49128
// MARK: - Private Handler
50129

51130
private func handleDictValue(dictValue:Any, key:String, hString:NSMutableString, mString:NSMutableString) {
52131

53132
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")
56135

57136
} else { // sub model
58137
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")
61141
}
62142

63143
switch dictValue {

SKGenerateModelTool/SKGenerateModelTool.entitlements

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,15 @@
44
<dict>
55
<key>com.apple.security.app-sandbox</key>
66
<true/>
7-
<key>com.apple.security.files.user-selected.read-only</key>
7+
<key>com.apple.security.assets.movies.read-write</key>
8+
<true/>
9+
<key>com.apple.security.assets.music.read-write</key>
10+
<true/>
11+
<key>com.apple.security.assets.pictures.read-write</key>
12+
<true/>
13+
<key>com.apple.security.files.downloads.read-write</key>
14+
<true/>
15+
<key>com.apple.security.files.user-selected.read-write</key>
816
<true/>
917
<key>com.apple.security.network.client</key>
1018
<true/>

SKGenerateModelTool/ViewController.swift

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,22 +118,76 @@ class ViewController: NSViewController {
118118
print(" error = \(error)")
119119
}
120120

121-
if builder.config.codeType == .OC {
121+
switch builder.config.codeType {
122+
case .OC:
122123
builder.build_OC_code(with: jsonObj) { [weak self] (hString, mString) in
124+
self?.hTextViewHeightPriority = self?.modifyConstraint(self?.hTextViewHeightPriority, 3/5.0)
123125
let color = NSColor(red: 215/255.0, green: 0/255.0 , blue: 143/255.0, alpha: 1.0)
124126
self?.configJsonTextView(text: hString as String, textView: self!.hTextView, color: color)
125127
self?.configJsonTextView(text: mString as String, textView: self!.mTextView, color: color)
128+
129+
guard let state = self?.generateFileBtn.state else { return }
130+
guard state == .on else { return }
131+
let outputFilePath = self?.outputFilePath
132+
self?.builder.generate_OC_File(with: outputFilePath, hString: hString, mString: mString, complete: { (success, filePath) in
133+
if success {
134+
self?.showAlertInfoWith("生成文件路径在:\(filePath)", .informational)
135+
self?.outputFilePath = filePath
136+
self?.saveUserInputContent()
137+
}
138+
})
139+
}
140+
case .Swift:
141+
builder.build_OC_code(with: jsonObj) { [weak self] (hString, mString) in
142+
self?.hTextViewHeightPriority = self?.modifyConstraint(self?.hTextViewHeightPriority, 1.0)
143+
let color = NSColor(red: 215/255.0, green: 0/255.0 , blue: 143/255.0, alpha: 1.0)
144+
self?.configJsonTextView(text: hString as String, textView: self!.hTextView, color: color)
145+
self?.configJsonTextView(text: mString as String, textView: self!.mTextView, color: color)
146+
147+
guard let state = self?.generateFileBtn.state else { return }
148+
guard state == .on else { return }
149+
let outputFilePath = self?.outputFilePath
150+
self?.builder.generate_OC_File(with: outputFilePath, hString: hString, mString: mString, complete: { (success, filePath) in
151+
if success {
152+
self?.showAlertInfoWith("生成文件路径在:\(filePath)", .informational)
153+
self?.outputFilePath = filePath
154+
self?.saveUserInputContent()
155+
}
156+
})
126157
}
158+
default: break
127159
}
128160
}
129161

130162

131163
@IBAction func chooseOutputFilePath(_ sender: NSButton) {
132-
164+
let openPanel = NSOpenPanel()
165+
openPanel.canChooseFiles = false
166+
openPanel.canChooseDirectories = true
167+
let modal = openPanel.runModal()
168+
if modal == .OK {
169+
if let fileUrl = openPanel.urls.first{
170+
outputFilePath = fileUrl.path
171+
}
172+
}
133173
}
134174

135175
// MARK: - Private Method
136176

177+
private func modifyConstraint( _ constraint: NSLayoutConstraint?, _ multiplier: CGFloat) -> NSLayoutConstraint? {
178+
179+
guard let constraint = constraint else {
180+
return nil
181+
}
182+
NSLayoutConstraint.deactivate([constraint])
183+
let newConstraint = NSLayoutConstraint.init(item: constraint.firstItem as Any, attribute: constraint.firstAttribute, relatedBy: constraint.relation, toItem: constraint.secondItem, attribute: constraint.secondAttribute, multiplier: multiplier, constant: 0)
184+
newConstraint.identifier = constraint.identifier;
185+
newConstraint.priority = constraint.priority;
186+
newConstraint.shouldBeArchived = constraint.shouldBeArchived;
187+
NSLayoutConstraint .activate([newConstraint])
188+
return newConstraint
189+
}
190+
137191
private func showAlertInfoWith( _ info: String, _ style:NSAlert.Style) {
138192
let alert = NSAlert()
139193
alert.messageText = info

0 commit comments

Comments
 (0)