Skip to content

Commit 4e4ce53

Browse files
author
shangkun
committed
Select app-sanbox && network.client
1 parent 767616b commit 4e4ce53

File tree

5 files changed

+179
-15
lines changed

5 files changed

+179
-15
lines changed

SKGenerateModelTool/Base.lproj/Main.storyboard

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<document type="com.apple.InterfaceBuilder3.Cocoa.Storyboard.XIB" version="3.0" toolsVersion="15702" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" initialViewController="B8D-0N-5wS">
2+
<document type="com.apple.InterfaceBuilder3.Cocoa.Storyboard.XIB" version="3.0" toolsVersion="15705" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" initialViewController="B8D-0N-5wS">
33
<dependencies>
4-
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="15702"/>
4+
<deployment identifier="macosx"/>
5+
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="15705"/>
56
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
67
</dependencies>
78
<scenes>
@@ -748,7 +749,7 @@
748749
</buttonCell>
749750
<color key="contentTintColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
750751
<connections>
751-
<action selector="requestURLBtnClicked:" target="LHr-yW-btL" id="yuo-iN-k4q"/>
752+
<action selector="requestURLBtnClicked:" target="LHr-yW-btL" id="uh4-Ee-owB"/>
752753
</connections>
753754
</button>
754755
<button verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="IZJ-nR-Rro">
@@ -763,7 +764,7 @@
763764
</buttonCell>
764765
<color key="contentTintColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
765766
<connections>
766-
<action selector="startMakeCode:" target="LHr-yW-btL" id="Xo0-U5-s7k"/>
767+
<action selector="startMakeCode:" target="LHr-yW-btL" id="7N9-BI-3Yh"/>
767768
</connections>
768769
</button>
769770
<textField verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="zmG-Je-iVh">
@@ -991,7 +992,7 @@
991992
<font key="font" metaFont="system"/>
992993
</buttonCell>
993994
<connections>
994-
<action selector="chooseOutputFilePath:" target="LHr-yW-btL" id="atj-fQ-Uuc"/>
995+
<action selector="chooseOutputFilePath:" target="LHr-yW-btL" id="XRE-5w-zi8"/>
995996
</connections>
996997
</button>
997998
<button verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="4Sj-xp-ugz">

SKGenerateModelTool/Info.plist

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,10 @@
3232
<true/>
3333
<key>NSSupportsSuddenTermination</key>
3434
<true/>
35+
<key>NSAppTransportSecurity</key>
36+
<dict>
37+
<key>NSAllowsArbitraryLoads</key>
38+
<true/>
39+
</dict>
3540
</dict>
3641
</plist>

SKGenerateModelTool/SKCodeBuilder.swift

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,12 @@ class SKCodeBuilder: NSObject {
4545

4646
if key.isBlank { // Root model
4747
let modeName = modelName(with: key)
48-
hString.appending("\n\n@interface \(modeName) : \(self.config.superClassName)\n\n")
49-
mString.appending("\n\n@implementation \(modeName)\n\n")
48+
hString.append("\n\n@interface \(modeName) : \(self.config.superClassName)\n\n")
49+
mString.append("\n\n@implementation \(modeName)\n\n")
5050

5151
} else { // sub model
52-
hString.appending("\n\n@interface \(self.config.rootModelName) : \(self.config.superClassName)\n\n")
53-
mString.appending("\n\n@implementation \(self.config.rootModelName)\n\n")
52+
hString.append("\n\n@interface \(self.config.rootModelName) : \(self.config.superClassName)\n\n")
53+
mString.append("\n\n@implementation \(self.config.rootModelName)\n\n")
5454
}
5555
}
5656

@@ -73,13 +73,38 @@ class SKCodeBuilderConfig: NSObject {
7373
var superClassName = "NSObject"
7474
var rootModelName = "NSRootModel"
7575
var modelNamePrefix = "NS"
76+
var authorName = "SKGenerateModelTool"
7677
var codeType: SKCodeBuilderCodeType = .OC
7778
var jsonType: SKCodeBuilderJSONModelType = .None
7879
}
7980

8081
extension String {
82+
8183
var isBlank: Bool {
8284
let trimmedStr = self.trimmingCharacters(in: .whitespacesAndNewlines)
8385
return trimmedStr.isEmpty
8486
}
87+
88+
/// url 编码
89+
func urlEncoding() -> String {
90+
if self.isBlank { return self }
91+
if let encodeUrl = self.addingPercentEncoding(withAllowedCharacters: .urlFragmentAllowed) {
92+
return encodeUrl
93+
}
94+
return self
95+
}
96+
97+
/// string -> jsonObj
98+
func _toJsonObj() -> Any? {
99+
if self.isBlank { return nil }
100+
if let jsonData = self.data(using: String.Encoding.utf8) {
101+
do {
102+
let jsonObj = try JSONSerialization.jsonObject(with: jsonData, options: .mutableContainers)
103+
return jsonObj
104+
} catch let error {
105+
print(error)
106+
}
107+
}
108+
return nil
109+
}
85110
}

SKGenerateModelTool/SKGenerateModelTool.entitlements

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@
22
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
33
<plist version="1.0">
44
<dict>
5-
<key>com.apple.security.app-sandbox</key>
6-
<true/>
7-
<key>com.apple.security.files.user-selected.read-only</key>
8-
<true/>
5+
<key>com.apple.security.app-sandbox</key>
6+
<true/>
7+
<key>com.apple.security.files.user-selected.read-only</key>
8+
<true/>
9+
<key>com.apple.security.network.client</key>
10+
<true/>
11+
<key>com.apple.security.network.server</key>
12+
<true/>
913
</dict>
1014
</plist>

SKGenerateModelTool/ViewController.swift

Lines changed: 131 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,105 @@ class ViewController: NSViewController {
6262
loadUserLastInputContent()
6363
}
6464

65+
/// GET request URL
66+
/// example url:
67+
///
68+
/// 今日热榜(微博):https://v1.alapi.cn/api/tophub/get?type=weibo
69+
70+
@IBAction func requestURLBtnClicked(_ sender: NSButton) {
71+
72+
var urlString = urlTF.stringValue
73+
if urlString.isBlank { return }
74+
urlString = urlString.urlEncoding()
75+
print("encode URL = \(urlTF.stringValue)")
76+
77+
UserDefaults.standard.setValue(urlString, forKey: LastInputURLCacheKey)
78+
79+
let session = URLSession.shared
80+
let task = session.dataTask(with: URL(string: urlString)!) { [weak self] (data, response, error) in
81+
82+
guard let data = data, error == nil else { return }
83+
84+
do {
85+
let jsonObj = try JSONSerialization.jsonObject(with: data, options: .mutableContainers)
86+
if JSONSerialization.isValidJSONObject(jsonObj) {
87+
let formatJsonData = try JSONSerialization.data(withJSONObject: jsonObj, options: .prettyPrinted)
88+
if let jsonString = String(data: formatJsonData, encoding: String.Encoding.utf8) {
89+
self?.configJsonTextView(text: jsonString, textView: self!.jsonTextView, color: NSColor.blue)
90+
}
91+
}
92+
} catch let error {
93+
print(" error = \(error)")
94+
}
95+
}
96+
task.resume()
97+
}
98+
99+
/// config ui on main queue.
100+
101+
func configJsonTextView(text:String, textView:NSTextView, color:NSColor) {
102+
let attrString = NSAttributedString(string: text)
103+
DispatchQueue.main.async {
104+
textView.textStorage?.setAttributedString(attrString)
105+
textView.textStorage?.font = NSFont.systemFont(ofSize: 15)
106+
textView.textStorage?.foregroundColor = color
107+
}
108+
}
109+
110+
111+
/// start generate code....
112+
113+
@IBAction func startMakeCode(_ sender: NSButton) {
114+
115+
let jsonString = jsonTextView.textStorage?.string
116+
117+
guard let jsonObj = jsonString?._toJsonObj() else {
118+
showAlertInfoWith("warn: input valid json string!", .warning)
119+
return
120+
}
121+
122+
guard JSONSerialization.isValidJSONObject(jsonObj) else {
123+
showAlertInfoWith("warn: is not a valid JSON !!!", .warning)
124+
return
125+
}
126+
127+
saveUserInputContent()
128+
129+
do {
130+
let formatJsonData = try JSONSerialization.data(withJSONObject: jsonObj, options: .prettyPrinted)
131+
if let jsonString = String(data: formatJsonData, encoding: String.Encoding.utf8) {
132+
configJsonTextView(text: jsonString, textView: jsonTextView, color: NSColor.blue)
133+
}
134+
} catch let error {
135+
print(" error = \(error)")
136+
}
137+
138+
if builder.config.codeType == .OC {
139+
builder.build_OC_code(with: jsonObj) { [weak self] (hString, mString) in
140+
print(" hString = \(hString)")
141+
print(" mString = \(mString)")
142+
143+
self?.configJsonTextView(text: hString as String, textView: self!.hTextView, color: NSColor.red)
144+
self?.configJsonTextView(text: mString as String, textView: self!.mTextView, color: NSColor.red)
145+
}
146+
}
147+
}
148+
149+
150+
@IBAction func chooseOutputFilePath(_ sender: NSButton) {
151+
152+
}
153+
154+
155+
func showAlertInfoWith( _ info: String, _ style:NSAlert.Style) {
156+
let alert = NSAlert()
157+
alert.messageText = info
158+
alert.alertStyle = style
159+
alert.beginSheetModal(for: self.view.window!, completionHandler: nil)
160+
}
161+
65162
/// load cache
163+
66164
func loadUserLastInputContent() {
67165

68166
if let lastUrl = UserDefaults.standard.string(forKey: LastInputURLCacheKey) {
@@ -80,7 +178,7 @@ class ViewController: NSViewController {
80178
if let authorName = UserDefaults.standard.string(forKey: AuthorNameCacheKey) {
81179
authorNameTF.stringValue = authorName
82180
}
83-
if let outFilePath = UserDefaults.standard.string(forKey: LastInputURLCacheKey) {
181+
if let outFilePath = UserDefaults.standard.string(forKey: GenerateFilePathCacheKey) {
84182
outputFilePath = outFilePath
85183
}
86184

@@ -93,10 +191,41 @@ class ViewController: NSViewController {
93191
generateFileBtn.state = UserDefaults.standard.bool(forKey: SupportJSONModelTypeCacheKey) ? .on : .off
94192
}
95193

96-
/// save cache
194+
/// MARK: save cache
97195
func saveUserInputContent() {
196+
98197
let superClassName = superClassNameTF.stringValue.isBlank ? "NSObject" : superClassNameTF.stringValue
99198
UserDefaults.standard.setValue(superClassName, forKey: SuperClassNameCacheKey)
199+
builder.config.superClassName = superClassName
200+
201+
let modelNamePrefix = modelNamePrefixTF.stringValue.isBlank ? "NS" : modelNamePrefixTF.stringValue
202+
UserDefaults.standard.setValue(modelNamePrefix, forKey: ModelNamePrefixCacheKey)
203+
builder.config.modelNamePrefix = modelNamePrefix
204+
205+
let rootModelName = rootModelNameTF.stringValue.isBlank ? "NSRootModel" : rootModelNameTF.stringValue
206+
UserDefaults.standard.setValue(rootModelName, forKey: RootModelNameCacheKey)
207+
builder.config.rootModelName = rootModelName
208+
209+
let authorName = authorNameTF.stringValue.isBlank ? "SKGenerateModelTool" : authorNameTF.stringValue
210+
UserDefaults.standard.setValue(authorName, forKey: AuthorNameCacheKey)
211+
builder.config.authorName = authorName
212+
213+
builder.config.codeType = SKCodeBuilderCodeType(rawValue: codeTypeBtn.indexOfSelectedItem + 1)!
214+
UserDefaults.standard.set(codeTypeBtn.indexOfSelectedItem + 1, forKey: BuildCodeTypeCacheKey)
215+
216+
builder.config.jsonType = SKCodeBuilderJSONModelType(rawValue: jsonTypeBtn.indexOfSelectedItem)!
217+
UserDefaults.standard.set(jsonTypeBtn.indexOfSelectedItem, forKey: SupportJSONModelTypeCacheKey)
218+
219+
if builder.config.superClassName.compare("NSObject") == .orderedSame {
220+
if builder.config.jsonType == .HandyJSON {
221+
builder.config.superClassName = "HandyJSON"
222+
} else if builder.config.jsonType == .YYModel {
223+
builder.config.superClassName = "YYModel"
224+
}
225+
}
226+
227+
UserDefaults.standard.setValue(outputFilePath, forKey: GenerateFilePathCacheKey)
228+
UserDefaults.standard.set(generateFileBtn.state == .on , forKey: ShouldGenerateFileCacheKey)
100229
}
101230

102231

0 commit comments

Comments
 (0)