Skip to content

Commit 4187180

Browse files
author
shangkun
committed
support // comments in json.
1 parent 994c40b commit 4187180

File tree

6 files changed

+61
-33
lines changed

6 files changed

+61
-33
lines changed

SKGenerateModelTool/AppDelegate.swift

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,33 @@
99
import Cocoa
1010

1111
@NSApplicationMain
12-
class AppDelegate: NSObject, NSApplicationDelegate {
12+
class AppDelegate: NSObject, NSApplicationDelegate,NSWindowDelegate {
13+
14+
let statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.squareLength)
1315

1416
func applicationDidFinishLaunching(_ aNotification: Notification) {
15-
// Insert code here to initialize your application
17+
guard let button = statusItem.button else { return }
18+
button.image = NSImage(named: NSImage.Name("itemIcon"))
19+
button.action = #selector(applicationShouldHandleReopen(_:hasVisibleWindows:))
20+
NSApp.setActivationPolicy(NSApplication.ActivationPolicy.regular)
21+
configItemMenu()
1622
}
1723

18-
func applicationWillTerminate(_ aNotification: Notification) {
19-
// Insert code here to tear down your application
24+
func applicationShouldHandleReopen(_ sender: NSApplication, hasVisibleWindows flag: Bool) -> Bool {
25+
for window in NSApplication.shared.windows {
26+
window.makeKeyAndOrderFront(self)
27+
}
28+
return true
29+
}
30+
31+
func configItemMenu() {
32+
let menu = NSMenu()
33+
menu.addItem(NSMenuItem.separator())
34+
menu.addItem(NSMenuItem(title: "Show", action: #selector(NSApplicationDelegate.applicationShouldHandleReopen(_:hasVisibleWindows:)), keyEquivalent: "w"))
35+
menu.addItem(NSMenuItem(title: "Hide", action: #selector(NSApplication.hide(_:)), keyEquivalent: "e"))
36+
menu.addItem(NSMenuItem(title: "Quit", action: #selector(NSApplication.terminate(_:)), keyEquivalent: "q"))
37+
statusItem.menu = menu
2038
}
21-
2239

2340
}
2441

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"images" : [
3+
{
4+
"idiom" : "universal",
5+
"scale" : "1x"
6+
},
7+
{
8+
"idiom" : "universal",
9+
"filename" : "itemIcon@2x.png",
10+
"scale" : "2x"
11+
},
12+
{
13+
"idiom" : "universal",
14+
"scale" : "3x"
15+
}
16+
],
17+
"info" : {
18+
"version" : 1,
19+
"author" : "xcode"
20+
}
21+
}

SKGenerateModelTool/Base.lproj/Main.storyboard

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?xml version="1.0" encoding="UTF-8"?>
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">
2+
<document type="com.apple.InterfaceBuilder3.Cocoa.Storyboard.XIB" version="3.0" toolsVersion="16097.2" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" initialViewController="B8D-0N-5wS">
33
<dependencies>
44
<deployment identifier="macosx"/>
5-
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="15705"/>
5+
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="16097.2"/>
66
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
77
</dependencies>
88
<scenes>

SKGenerateModelTool/SKCodeBuilder.swift

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ class SKCodeBuilder: NSObject {
3939
return "h"
4040
}
4141
}
42+
4243
// MARK: - Public
44+
4345
func generateCode(with jsonObj:Any, complete:BuildComplete?){
4446

4547
allKeys.removeAll()
@@ -208,7 +210,7 @@ class SKCodeBuilder: NSObject {
208210
if config.codeType == .OC {
209211
hString.append("/** <#泛型#> */\n@property (nonatomic, strong) id \(key);\n")
210212
} else if config.codeType == .Swift {
211-
hString.append(" /// \(key)\n var \(key): Any?\n")
213+
hString.append(" /// <#泛型#>\n var \(key): Any?\n")
212214
}
213215
}
214216
}
@@ -253,22 +255,22 @@ class SKCodeBuilder: NSObject {
253255
if let firstObject = arrayValue.first {
254256
if firstObject is String {
255257
// String 类型
256-
hString.append("/** \(commentName(key, firstObject as! String)) */\n@property (nonatomic, strong) NSArray <NSString *> *\(key);\n")
258+
hString.append("/** eg. \(commentName(key, firstObject as! String)) */\n@property (nonatomic, strong) NSArray <NSString *> *\(key);\n")
257259
}
258260
else if (firstObject is [String:Any]) {
259261
// Dictionary 类型
260262
let key = handleMaybeSameKey(key)
261263
let modeName = modelClassName(with: key)
262264
self.handleDicts.setValue(firstObject, forKey: key)
263265
self.yymodelPropertyGenericClassDicts.setValue(modeName, forKey: key)
264-
hString.append("/** \(commentName(key, key)) */\n@property (nonatomic, strong) NSArray <\(modeName) *> *\(key);\n")
266+
hString.append("/** eg. \(commentName(key, "")) */\n@property (nonatomic, strong) NSArray <\(modeName) *> *\(key);\n")
265267
}
266268
else if (firstObject is [Any]) {
267269
// Array 类型
268270
handleArrayValue(arrayValue: firstObject as! [Any] , key: key, hString: hString)
269271
}
270272
else {
271-
hString.append("/** \(commentName(key, key)) */\n@property (nonatomic, strong) NSArray *\(key);\n")
273+
hString.append("/** eg. \(commentName(key, "")) */\n@property (nonatomic, strong) NSArray *\(key);\n")
272274
}
273275
}
274276
} else if config.codeType == .Swift {
@@ -282,23 +284,21 @@ class SKCodeBuilder: NSObject {
282284
let key = handleMaybeSameKey(key)
283285
let modeName = modelClassName(with: key)
284286
self.handleDicts.setValue(firstObject, forKey: key)
285-
hString.append(" /// \(commentName(key, key, false)) \n var \(key): [\(modeName)]?\n")
287+
hString.append(" /// \(commentName(key, "", false)) \n var \(key): [\(modeName)]?\n")
286288
}
287289
else if (firstObject is [Any]) {
288290
// Array 类型
289291
handleArrayValue(arrayValue: firstObject as! [Any] , key: key, hString: hString)
290292
}
291293
else {
292-
hString.append(" /// \(commentName(key, key, false)) \n var \(key): [Any]?\n")
294+
hString.append(" /// \(commentName(key, "", false)) \n var \(key): [Any]?\n")
293295
}
294296
}
295297
}
296298
}
297299

298300
private func handleIdNumberValue(numValue:NSNumber, key:String, hString:NSMutableString, ignoreIdValue:Bool) {
299-
300-
// let type = numValue.objCType
301-
301+
302302
let numType = CFNumberGetType(numValue as CFNumber)
303303

304304
switch numType {
@@ -353,7 +353,6 @@ class SKCodeBuilder: NSObject {
353353
}
354354

355355
/// String
356-
357356
private func handleIdStringValue(idValue: String, key:String, hString:NSMutableString, ignoreIdValue:Bool) {
358357

359358
if config.codeType == .OC {
@@ -378,9 +377,7 @@ class SKCodeBuilder: NSObject {
378377
}
379378
}
380379

381-
382-
/// 处理json解析
383-
380+
/// 处理json解析
384381
private func handleJsonType(hString:NSMutableString, mString:NSMutableString) {
385382

386383
if config.jsonType == .HandyJSON {

SKGenerateModelTool/ViewController.swift

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ import Cocoa
1111
class ViewController: NSViewController, NSControlTextEditingDelegate {
1212

1313
@IBOutlet weak var urlTF: NSTextField!
14-
@IBOutlet weak var jsonTextView: NSTextView!
15-
@IBOutlet weak var hTextView: NSTextView!
16-
@IBOutlet weak var mTextView: NSTextView!
14+
@IBOutlet var jsonTextView: NSTextView!
15+
@IBOutlet var hTextView: NSTextView!
16+
@IBOutlet var mTextView: NSTextView!
1717
@IBOutlet weak var hTextViewHeightPriority: NSLayoutConstraint!
1818
@IBOutlet weak var superClassNameTF: NSTextField! /// default 3:5
1919
@IBOutlet weak var modelNamePrefixTF: NSTextField!
@@ -128,13 +128,9 @@ class ViewController: NSViewController, NSControlTextEditingDelegate {
128128
if line.contains("//") {
129129
let substrings = line.components(separatedBy: "//")
130130
let hasHttpLink = line.contains("http://") || line.contains("https://")
131-
132131
// 只有图片链接 且没注释的情况下 不做截断操作
133132
let cannComment = !(substrings.count == 2 && hasHttpLink)
134-
guard cannComment else {
135-
return
136-
}
137-
133+
guard cannComment else { return }
138134
let trimmedLineStr = line.trimmingCharacters(in: .whitespacesAndNewlines)
139135
let position = trimmedLineStr.postionOf(sub: "//",backwards: true)
140136
if position >= 0 {
@@ -146,18 +142,15 @@ class ViewController: NSViewController, NSControlTextEditingDelegate {
146142
keystr = lines.first ?? ""
147143
keystr = keystr.replacingOccurrences(of: "\"", with: "")
148144
keystr = keystr.trimmingCharacters(in: .whitespacesAndNewlines)
149-
print("keystr = \(lines.first ?? "")")
150-
print("commentstr = \(commentstr)")
151-
commentDicts.updateValue(String(commentstr), forKey: keystr)
145+
let comment = String(commentstr).replacingOccurrences(of: "//", with: "")
146+
commentDicts.updateValue(comment, forKey: keystr)
152147
}
153148
let range = attriStr.range(of: String(commentstr))
154149
attriStr.replaceCharacters(in: range, with: "")
155150
}
156151
}
157152
}
158153

159-
print("commentDicts = \(commentDicts)")
160-
161154
let jsonStr = attriStr
162155
guard let jsonData = jsonStr.data(using: String.Encoding.utf8.rawValue) else {
163156
showAlertInfoWith("warn: input valid json string!", .warning)

0 commit comments

Comments
 (0)