Commit bba9ad95 authored by 陈业泓's avatar 陈业泓

feat(swift模板):支持根据HandyJSON模板生成

parent a84bd09c
File added
# Created by https://www.gitignore.io/api/xcode
### Xcode ###
# Xcode
#
# gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore
## Build generated
build/
DerivedData/
## Various settings
*.pbxuser
!default.pbxuser
*.mode1v3
!default.mode1v3
*.mode2v3
!default.mode2v3
*.perspectivev3
!default.perspectivev3
xcuserdata/
## Other
*.moved-aside
*.xccheckout
*.xcscmblueprint
### Xcode Patch ###
*.xcodeproj/*
!*.xcodeproj/project.pbxproj
!*.xcodeproj/xcshareddat
!*/xcuserdata/admin.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlista/
!*.xcworkspace/contents.xcworkspacedata
/*.gcno
# End of https://www.gitignore.io/api/xcode
# Created by https://www.gitignore.io/api/cocoapods
### CocoaPods ###
## CocoaPods GitIgnore Template
# CocoaPods - Only use to conserve bandwidth / Save time on Pushing
# - Also handy if you have a large number of dependant pods
# - AS PER https://guides.cocoapods.org/using/using-cocoapods.html NEVER IGNORE THE LOCK FILE
Pods/
Podfile.lock
.DS_Store
# End of https://www.gitignore.io/api/cocoapods
xcode_build_ipa_appstore
xcode_build_ipa_dev
xcode_build_ipa_dis
Pods
......@@ -14,6 +14,8 @@
813500D627C33F480082F0FD /* FileUtil.swift in Sources */ = {isa = PBXBuildFile; fileRef = 813500D527C33F480082F0FD /* FileUtil.swift */; };
813500D827C35FDE0082F0FD /* FileModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 813500D727C35FDE0082F0FD /* FileModel.swift */; };
813500DA27C37F710082F0FD /* ObjcUtil.swift in Sources */ = {isa = PBXBuildFile; fileRef = 813500D927C37F710082F0FD /* ObjcUtil.swift */; };
8173774027C5C0D90041A913 /* SwiftUtil.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8173773F27C5C0D90041A913 /* SwiftUtil.swift */; };
8173774227C5C11E0041A913 /* CommonConfig.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8173774127C5C11E0041A913 /* CommonConfig.swift */; };
8187698127C4761A00FB6E9C /* OcTool.m in Sources */ = {isa = PBXBuildFile; fileRef = 8187698027C4761A00FB6E9C /* OcTool.m */; };
/* End PBXBuildFile section */
......@@ -27,6 +29,8 @@
813500D527C33F480082F0FD /* FileUtil.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FileUtil.swift; sourceTree = "<group>"; };
813500D727C35FDE0082F0FD /* FileModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FileModel.swift; sourceTree = "<group>"; };
813500D927C37F710082F0FD /* ObjcUtil.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ObjcUtil.swift; sourceTree = "<group>"; };
8173773F27C5C0D90041A913 /* SwiftUtil.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SwiftUtil.swift; sourceTree = "<group>"; };
8173774127C5C11E0041A913 /* CommonConfig.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CommonConfig.swift; sourceTree = "<group>"; };
8187697E27C4761A00FB6E9C /* MacJsonToModelFile-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "MacJsonToModelFile-Bridging-Header.h"; sourceTree = "<group>"; };
8187697F27C4761A00FB6E9C /* OcTool.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = OcTool.h; sourceTree = "<group>"; };
8187698027C4761A00FB6E9C /* OcTool.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = OcTool.m; sourceTree = "<group>"; };
......@@ -79,6 +83,8 @@
813500D527C33F480082F0FD /* FileUtil.swift */,
813500D727C35FDE0082F0FD /* FileModel.swift */,
813500D927C37F710082F0FD /* ObjcUtil.swift */,
8173773F27C5C0D90041A913 /* SwiftUtil.swift */,
8173774127C5C11E0041A913 /* CommonConfig.swift */,
);
path = FileManager;
sourceTree = "<group>";
......@@ -164,9 +170,11 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
8173774027C5C0D90041A913 /* SwiftUtil.swift in Sources */,
813500D627C33F480082F0FD /* FileUtil.swift in Sources */,
8130590227C330B000820D6A /* ViewController.swift in Sources */,
8130590027C330B000820D6A /* AppDelegate.swift in Sources */,
8173774227C5C11E0041A913 /* CommonConfig.swift in Sources */,
813500DA27C37F710082F0FD /* ObjcUtil.swift in Sources */,
813500D827C35FDE0082F0FD /* FileModel.swift in Sources */,
8187698127C4761A00FB6E9C /* OcTool.m in Sources */,
......@@ -311,7 +319,7 @@
CODE_SIGN_ENTITLEMENTS = MacJsonToModelFile/MacJsonToModelFile.entitlements;
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 1;
CURRENT_PROJECT_VERSION = 2;
DEVELOPMENT_TEAM = C77C4Z7YUK;
ENABLE_HARDENED_RUNTIME = YES;
GENERATE_INFOPLIST_FILE = YES;
......@@ -322,7 +330,7 @@
"$(inherited)",
"@executable_path/../Frameworks",
);
MARKETING_VERSION = 1.0;
MARKETING_VERSION = 1.0.1;
PRODUCT_BUNDLE_IDENTIFIER = com.syc.saas.MacJsonToModelFile;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_EMIT_LOC_STRINGS = YES;
......@@ -341,7 +349,7 @@
CODE_SIGN_ENTITLEMENTS = MacJsonToModelFile/MacJsonToModelFile.entitlements;
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 1;
CURRENT_PROJECT_VERSION = 2;
DEVELOPMENT_TEAM = C77C4Z7YUK;
ENABLE_HARDENED_RUNTIME = YES;
GENERATE_INFOPLIST_FILE = YES;
......@@ -352,7 +360,7 @@
"$(inherited)",
"@executable_path/../Frameworks",
);
MARKETING_VERSION = 1.0;
MARKETING_VERSION = 1.0.1;
PRODUCT_BUNDLE_IDENTIFIER = com.syc.saas.MacJsonToModelFile;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_EMIT_LOC_STRINGS = YES;
......
//
// CommonConfig.swift
// MacJsonToModelFile
//
// Created by chenyehong on 2022/2/23.
//
import Foundation
let replacedDic = ["id":"ID"]
func replacedPropertyName(_ key : String) -> String {
if let name = replacedDic[key] {
return name
}
return key
}
......@@ -39,6 +39,11 @@ class FileUtil: NSObject {
createFile("\(model.fileName!).m", implement, callback)
}
func createSwFile(_ model : FileModel, _ callback : ((_ msg : String) -> Void)){
let content = SwiftUtil.classTextByModel(model)
createFile("\(model.fileName!).swift", content, callback)
}
func createFile(_ fileName : String, _ content : String, _ callback : ((_ msg : String) -> Void)){
let file = fileDir.appendingPathComponent(fileName)
let exist = manager.fileExists(atPath: file.path)
......
......@@ -122,13 +122,4 @@ class ObjcUtil: NSObject {
"// Copyright © 2022 mac. All rights reserved.\n" +
"//\n\n"
}
static func replacedPropertyName(_ key : String) -> String {
if let name = replacedDic[key] {
return name
}
return key
}
static let replacedDic = ["id":"ID"]
}
//
// SwiftUtil.swift
// MacJsonToModelFile
//
// Created by chenyehong on 2022/2/23.
//
import Cocoa
class SwiftUtil: NSObject {
static func classTextByModel(_ model : FileModel) -> String {
var content = ""
for pt in model.propertys {
let propertyName = replacedPropertyName(pt.propertyName!)
switch pt.propertyType {
case .id:
content += " var \(propertyName) : Any?\n"
case .int:
content += " var \(propertyName) : Int?\n"
case .string:
content += " var \(propertyName) : String?\n"
case .float:
content += " var \(propertyName) : Float?\n"
case .bool:
content += " var \(propertyName) : Bool?\n"
case .model(let pName):
content += " var \(propertyName) : \(pName)?\n"
break
case .list(let itemType):
switch itemType {
case .string:
content += " var \(propertyName) : [String]?\n"
case .model(let pName):
content += " var \(propertyName) : [\(pName)]?\n"
default:
content += " var \(propertyName) : [Any]?\n"
}
break
default:
break
}
}
var includeTxt = getIncludeTxt(model.fileName!)
includeTxt += "import UIKit\n"
includeTxt += "\nclass \(model.fileName!): NSObject, HandyJSON {\n\n"
content = includeTxt + content + "\n"
content = content + " required override init() {\n super.init()\n }\n\n"
var replaceCode = ""
for pt in model.propertys {
let propertyName = replacedPropertyName(pt.propertyName!)
if propertyName != pt.propertyName! {
replaceCode += " mapper.specify(property: &\(propertyName), name: \"\(pt.propertyName!)\")\n"
}
}
if replaceCode.count > 0 {
replaceCode =
" func mapping(mapper: HelpingMapper) {\n" +
replaceCode +
" }\n\n"
content += replaceCode
}
content = content + "}"
return content
}
static func getIncludeTxt(_ fileName : String) -> String {
return "//\n" +
"// \(fileName).swift\n" +
"// MacJsonToModelFile\n" +
"//\n" +
"// Created by mac on 2022/xx/xx.\n" +
"//\n\n"
}
}
......@@ -30,11 +30,6 @@ class ViewController: NSViewController {
msgTextView.textStorage?.setAttributedString(NSAttributedString.init(string: ""))
if menuItemHandJson.state == .on {
appendMessage("HandyJSON模板(.swift),功能未完成,敬请期待")
return
}
let classname = tfClassName.stringValue.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
guard classname.count > 0 else {
appendMessage("请输入类名")
......@@ -54,9 +49,15 @@ class ViewController: NSViewController {
fileMap = [:]
_ = handleDic(classname, dict as NSDictionary)
let blockFunc = {(_ msg : String) in
self.appendMessage(msg)
}
for (_, value) in fileMap {
fileUtil.createOcFile(value) { msg in
appendMessage(msg)
if menuItemMJ.state == .on {
fileUtil.createOcFile(value, blockFunc)
} else {
fileUtil.createSwFile(value, blockFunc)
}
}
......
......@@ -17,13 +17,13 @@
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<string>1.0.1</string>
<key>CFBundleSupportedPlatforms</key>
<array>
<string>MacOSX</string>
</array>
<key>CFBundleVersion</key>
<string>1</string>
<string>2</string>
<key>DTCompiler</key>
<string>com.apple.compilers.llvm.clang.1_0</string>
<key>DTPlatformBuild</key>
......
......@@ -6,19 +6,19 @@
<dict>
<key>Resources/Base.lproj/Main.storyboardc/Info.plist</key>
<data>
RxlJS1WxAPeGfvozrWFd7OaUkeM=
XJQBCRfO+KYTDPyYfPANI/ewINE=
</data>
<key>Resources/Base.lproj/Main.storyboardc/MainMenu.nib</key>
<data>
0QjjmlBucfphyMhpt75XTeQ0aIc=
zxp3MYGWubE2Upwa563xqquYrwU=
</data>
<key>Resources/Base.lproj/Main.storyboardc/NSWindowController-B8D-0N-5wS.nib</key>
<data>
WIREypm4xQpK0Znkj7yaXvVF/+Y=
qaUI3Z0yMgCpTW1vdeBuAnPOL9c=
</data>
<key>Resources/Base.lproj/Main.storyboardc/XfG-lQ-9wD-view-m2S-Jp-Qdl.nib</key>
<data>
3uI+CiKq7HTfY69i5y7J/G1P/E0=
QvHDTsA19IooBYP5pGY7LieiXcw=
</data>
</dict>
<key>files2</key>
......@@ -27,28 +27,28 @@
<dict>
<key>hash2</key>
<data>
qAJ3rqw37ee9lqlgEUCWnFELjd3YlPq9xIvvQ2r5kMc=
KkAs8+5cSxgQ4DtAuKTRZCRErIUA6QvA8kfbJcQaqjE=
</data>
</dict>
<key>Resources/Base.lproj/Main.storyboardc/MainMenu.nib</key>
<dict>
<key>hash2</key>
<data>
+p13BdZB4xx/TKoGYjSUI/NEgsyTxRJQww2K1QwuPqU=
hz6hdbZ5+iORtz728nVw+mRXR7DELxkUyTpyPF9NNQA=
</data>
</dict>
<key>Resources/Base.lproj/Main.storyboardc/NSWindowController-B8D-0N-5wS.nib</key>
<dict>
<key>hash2</key>
<data>
45en/P8XQ0e1/SFoZbLcIJHGecHgG67bwdS64ZOsWvU=
D6A84BwNQoLVWg78IBgBOQr+pIyvNsKxmRMa1YNlg/0=
</data>
</dict>
<key>Resources/Base.lproj/Main.storyboardc/XfG-lQ-9wD-view-m2S-Jp-Qdl.nib</key>
<dict>
<key>hash2</key>
<data>
rqKcBKwPh8+vhBn5wnLiXcLr+vyb9xAgNfbxwh/T11A=
RfR94wtIbSdJVC6P2XlbC1FC0CSyHIgQszFt/wlCI4k=
</data>
</dict>
</dict>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment