Skip to content

Commit 9d55459

Browse files
author
CodeOcen
committed
feat: 导入导出文件完善
1 parent f3a2eb3 commit 9d55459

File tree

6 files changed

+136
-50
lines changed

6 files changed

+136
-50
lines changed

SwiftJSONModeler For Xcode.xcodeproj/project.pbxproj

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,18 @@
2727
C9CB2480241F559C00F4D5CE /* ErrorCenter.swift in Sources */ = {isa = PBXBuildFile; fileRef = C9CB247F241F559C00F4D5CE /* ErrorCenter.swift */; };
2828
C9CEE26D242B382900A04EA9 /* YApiCreator.swift in Sources */ = {isa = PBXBuildFile; fileRef = C9CEE26C242B382900A04EA9 /* YApiCreator.swift */; };
2929
C9D04F2B242F218200843DC4 /* SwiftJSONModeler_For_XcodeTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = C9D04F2A242F218200843DC4 /* SwiftJSONModeler_For_XcodeTests.swift */; };
30-
C9D9905325E742D800258865 /* CleanJSON in Frameworks */ = {isa = PBXBuildFile; productRef = C9D9905225E742D800258865 /* CleanJSON */; };
30+
C9D9905825E776A900258865 /* ConfigCenter.swift in Sources */ = {isa = PBXBuildFile; fileRef = C9D9905725E776A900258865 /* ConfigCenter.swift */; };
31+
C9D9905C25E776EF00258865 /* ConfigModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = C9E9C09125CFD94E00CD767D /* ConfigModel.swift */; };
32+
C9D9906725E7807300258865 /* CleanJSON in Frameworks */ = {isa = PBXBuildFile; productRef = C9D9906625E7807300258865 /* CleanJSON */; };
33+
C9D9906D25E7815F00258865 /* CleanJSON in Frameworks */ = {isa = PBXBuildFile; productRef = C9D9906C25E7815F00258865 /* CleanJSON */; };
34+
C9D9906E25E7816600258865 /* ConfigCenter.swift in Sources */ = {isa = PBXBuildFile; fileRef = C9D9905725E776A900258865 /* ConfigCenter.swift */; };
3135
C9DD88322431C630004E420F /* YApiRequest.swift in Sources */ = {isa = PBXBuildFile; fileRef = C9D04F20242EF99F00843DC4 /* YApiRequest.swift */; };
3236
C9DE045F242C8535008A0070 /* Config.swift in Sources */ = {isa = PBXBuildFile; fileRef = C9DE045E242C8535008A0070 /* Config.swift */; };
3337
C9DE0460242C853F008A0070 /* Config.swift in Sources */ = {isa = PBXBuildFile; fileRef = C9DE045E242C8535008A0070 /* Config.swift */; };
3438
C9E393E424EE248500D80B28 /* TokenView.xib in Resources */ = {isa = PBXBuildFile; fileRef = C9E393E324EE248500D80B28 /* TokenView.xib */; };
3539
C9E393E824EE255900D80B28 /* TokenView.swift in Sources */ = {isa = PBXBuildFile; fileRef = C9E393E724EE255900D80B28 /* TokenView.swift */; };
3640
C9E393EA24EE47D300D80B28 /* TokenViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = C9E393E924EE47D300D80B28 /* TokenViewController.swift */; };
37-
C9E9C08925CF907A00CD767D /* PanelManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = C9E9C08825CF907A00CD767D /* PanelManager.swift */; };
41+
C9E9C08925CF907A00CD767D /* OpenSavePanel.swift in Sources */ = {isa = PBXBuildFile; fileRef = C9E9C08825CF907A00CD767D /* OpenSavePanel.swift */; };
3842
C9E9C09225CFD94E00CD767D /* ConfigModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = C9E9C09125CFD94E00CD767D /* ConfigModel.swift */; };
3943
C9ED9CC623D7F9B3004567A1 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = C9ED9CC523D7F9B3004567A1 /* AppDelegate.swift */; };
4044
C9ED9CCA23D7F9B3004567A1 /* Document.swift in Sources */ = {isa = PBXBuildFile; fileRef = C9ED9CC923D7F9B3004567A1 /* Document.swift */; };
@@ -104,11 +108,12 @@
104108
C9D04F28242F218200843DC4 /* SwiftJSONModeler For XcodeTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "SwiftJSONModeler For XcodeTests.xctest"; sourceTree = BUILT_PRODUCTS_DIR; };
105109
C9D04F2A242F218200843DC4 /* SwiftJSONModeler_For_XcodeTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SwiftJSONModeler_For_XcodeTests.swift; sourceTree = "<group>"; };
106110
C9D04F2C242F218200843DC4 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
111+
C9D9905725E776A900258865 /* ConfigCenter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConfigCenter.swift; sourceTree = "<group>"; };
107112
C9DE045E242C8535008A0070 /* Config.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Config.swift; sourceTree = "<group>"; };
108113
C9E393E324EE248500D80B28 /* TokenView.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = TokenView.xib; sourceTree = "<group>"; };
109114
C9E393E724EE255900D80B28 /* TokenView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TokenView.swift; sourceTree = "<group>"; };
110115
C9E393E924EE47D300D80B28 /* TokenViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TokenViewController.swift; sourceTree = "<group>"; };
111-
C9E9C08825CF907A00CD767D /* PanelManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PanelManager.swift; sourceTree = "<group>"; };
116+
C9E9C08825CF907A00CD767D /* OpenSavePanel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OpenSavePanel.swift; sourceTree = "<group>"; };
112117
C9E9C09125CFD94E00CD767D /* ConfigModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConfigModel.swift; sourceTree = "<group>"; };
113118
C9ED9CC223D7F9B3004567A1 /* SwiftJSONModeler For Xcode.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "SwiftJSONModeler For Xcode.app"; sourceTree = BUILT_PRODUCTS_DIR; };
114119
C9ED9CC523D7F9B3004567A1 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
@@ -137,7 +142,7 @@
137142
isa = PBXFrameworksBuildPhase;
138143
buildActionMask = 2147483647;
139144
files = (
140-
C9D9905325E742D800258865 /* CleanJSON in Frameworks */,
145+
C9D9906725E7807300258865 /* CleanJSON in Frameworks */,
141146
);
142147
runOnlyForDeploymentPostprocessing = 0;
143148
};
@@ -146,6 +151,7 @@
146151
buildActionMask = 2147483647;
147152
files = (
148153
C9ED9CDE23D7F9D0004567A1 /* Cocoa.framework in Frameworks */,
154+
C9D9906D25E7815F00258865 /* CleanJSON in Frameworks */,
149155
C90EFE0C257E172E0008632B /* XcodeKit.framework in Frameworks */,
150156
);
151157
runOnlyForDeploymentPostprocessing = 0;
@@ -191,6 +197,15 @@
191197
path = SwiftJSONModelerTest;
192198
sourceTree = "<group>";
193199
};
200+
C9D9905625E7765C00258865 /* Config */ = {
201+
isa = PBXGroup;
202+
children = (
203+
C9E9C09025CFD93900CD767D /* Model */,
204+
C9D9905725E776A900258865 /* ConfigCenter.swift */,
205+
);
206+
path = Config;
207+
sourceTree = "<group>";
208+
};
194209
C9E393E224EE245D00D80B28 /* View */ = {
195210
isa = PBXGroup;
196211
children = (
@@ -212,7 +227,7 @@
212227
C9E9C08725CF905400CD767D /* Utils */ = {
213228
isa = PBXGroup;
214229
children = (
215-
C9E9C08825CF907A00CD767D /* PanelManager.swift */,
230+
C9E9C08825CF907A00CD767D /* OpenSavePanel.swift */,
216231
);
217232
path = Utils;
218233
sourceTree = "<group>";
@@ -249,7 +264,7 @@
249264
C9ED9CC423D7F9B3004567A1 /* SwiftJSONModeler For Xcode */ = {
250265
isa = PBXGroup;
251266
children = (
252-
C9E9C09025CFD93900CD767D /* Model */,
267+
C9D9905625E7765C00258865 /* Config */,
253268
C9E9C08725CF905400CD767D /* Utils */,
254269
C9E393EB24EF68E900D80B28 /* Controller */,
255270
C9E393E224EE245D00D80B28 /* View */,
@@ -326,7 +341,7 @@
326341
);
327342
name = "SwiftJSONModeler For Xcode";
328343
packageProductDependencies = (
329-
C9D9905225E742D800258865 /* CleanJSON */,
344+
C9D9906625E7807300258865 /* CleanJSON */,
330345
);
331346
productName = JSONSwiftModelApp;
332347
productReference = C9ED9CC223D7F9B3004567A1 /* SwiftJSONModeler For Xcode.app */;
@@ -346,6 +361,9 @@
346361
dependencies = (
347362
);
348363
name = SwiftJSONModeler;
364+
packageProductDependencies = (
365+
C9D9906C25E7815F00258865 /* CleanJSON */,
366+
);
349367
productName = JSONSwiftModel;
350368
productReference = C9ED9CDB23D7F9D0004567A1 /* SwiftJSONModeler.appex */;
351369
productType = "com.apple.product-type.xcode-extension";
@@ -382,7 +400,7 @@
382400
);
383401
mainGroup = C9ED9CB923D7F9B3004567A1;
384402
packageReferences = (
385-
C9D9905125E742D800258865 /* XCRemoteSwiftPackageReference "CleanJSON" */,
403+
C9D9906525E7807300258865 /* XCRemoteSwiftPackageReference "CleanJSON" */,
386404
);
387405
productRefGroup = C9ED9CC323D7F9B3004567A1 /* Products */;
388406
projectDirPath = "";
@@ -449,8 +467,9 @@
449467
C92A971D24E514FB00C87CCD /* Constants.swift in Sources */,
450468
C92A971424E513EA00C87CCD /* YApiHelper.swift in Sources */,
451469
C9ED9CCA23D7F9B3004567A1 /* Document.swift in Sources */,
452-
C9E9C08925CF907A00CD767D /* PanelManager.swift in Sources */,
470+
C9E9C08925CF907A00CD767D /* OpenSavePanel.swift in Sources */,
453471
C935B49C24EA24BE00E48231 /* JSONHelper.swift in Sources */,
472+
C9D9905825E776A900258865 /* ConfigCenter.swift in Sources */,
454473
C961C1DE23D82A8D008D0DE6 /* ConfigViewController.swift in Sources */,
455474
C92A971524E513EA00C87CCD /* YApiObject.swift in Sources */,
456475
);
@@ -467,6 +486,8 @@
467486
C94AFC04241F4E8A00389AF3 /* YApiObject.swift in Sources */,
468487
C9DE0460242C853F008A0070 /* Config.swift in Sources */,
469488
C9ED9CE323D7F9D0004567A1 /* SourceEditorCommand.swift in Sources */,
489+
C9D9905C25E776EF00258865 /* ConfigModel.swift in Sources */,
490+
C9D9906E25E7816600258865 /* ConfigCenter.swift in Sources */,
470491
C9CEE26D242B382900A04EA9 /* YApiCreator.swift in Sources */,
471492
C92A971E24E5150600C87CCD /* Constants.swift in Sources */,
472493
C935B49F24EA664A00E48231 /* JSONHelper.swift in Sources */,
@@ -787,7 +808,7 @@
787808
/* End XCConfigurationList section */
788809

789810
/* Begin XCRemoteSwiftPackageReference section */
790-
C9D9905125E742D800258865 /* XCRemoteSwiftPackageReference "CleanJSON" */ = {
811+
C9D9906525E7807300258865 /* XCRemoteSwiftPackageReference "CleanJSON" */ = {
791812
isa = XCRemoteSwiftPackageReference;
792813
repositoryURL = "https://github.com/Pircate/CleanJSON.git";
793814
requirement = {
@@ -798,9 +819,14 @@
798819
/* End XCRemoteSwiftPackageReference section */
799820

800821
/* Begin XCSwiftPackageProductDependency section */
801-
C9D9905225E742D800258865 /* CleanJSON */ = {
822+
C9D9906625E7807300258865 /* CleanJSON */ = {
823+
isa = XCSwiftPackageProductDependency;
824+
package = C9D9906525E7807300258865 /* XCRemoteSwiftPackageReference "CleanJSON" */;
825+
productName = CleanJSON;
826+
};
827+
C9D9906C25E7815F00258865 /* CleanJSON */ = {
802828
isa = XCSwiftPackageProductDependency;
803-
package = C9D9905125E742D800258865 /* XCRemoteSwiftPackageReference "CleanJSON" */;
829+
package = C9D9906525E7807300258865 /* XCRemoteSwiftPackageReference "CleanJSON" */;
804830
productName = CleanJSON;
805831
};
806832
/* End XCSwiftPackageProductDependency section */

SwiftJSONModeler For Xcode.xcodeproj/xcuserdata/yibin.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,8 @@
158158
filePath = "SwiftJSONModeler For Xcode/Utils/PanelManager.swift"
159159
startingColumnNumber = "9223372036854775807"
160160
endingColumnNumber = "9223372036854775807"
161-
startingLineNumber = "91"
162-
endingLineNumber = "91"
161+
startingLineNumber = "99"
162+
endingLineNumber = "99"
163163
landmarkName = "saveFile(url:)"
164164
landmarkType = "7">
165165
</BreakpointContent>
@@ -174,8 +174,8 @@
174174
filePath = "SwiftJSONModeler For Xcode/Utils/PanelManager.swift"
175175
startingColumnNumber = "9223372036854775807"
176176
endingColumnNumber = "9223372036854775807"
177-
startingLineNumber = "78"
178-
endingLineNumber = "78"
177+
startingLineNumber = "86"
178+
endingLineNumber = "86"
179179
landmarkName = "read(url:)"
180180
landmarkType = "7">
181181
</BreakpointContent>
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
//
2+
// ConfigCenter.swift
3+
// SwiftJSONModeler For Xcode
4+
//
5+
// Created by Sven on 2021/2/25.
6+
// Copyright © 2021 Sven. All rights reserved.
7+
//
8+
9+
import Foundation
10+
import CleanJSON
11+
/// 配置存在路径
12+
let configPath = (NSHomeDirectory() as NSString).appendingPathComponent("Config.plist")
13+
14+
class ConfigCenter {
15+
static let `default` = ConfigCenter()
16+
var config: ConfigModel
17+
18+
init() {
19+
config = Self.readConfig()
20+
}
21+
22+
}
23+
// MARK: - Private Static Method
24+
25+
private extension ConfigCenter {
26+
static func readConfig() -> ConfigModel {
27+
let fileManager = FileManager.default
28+
guard let data = fileManager.contents(atPath: configPath) else {
29+
return writeDefaultConfig()
30+
}
31+
32+
let decoder = CleanJSONDecoder()
33+
guard let model = try? decoder.decode(ConfigModel.self, from: data) else {
34+
return writeDefaultConfig()
35+
}
36+
return model
37+
}
38+
39+
static func writeDefaultConfig() -> ConfigModel {
40+
let defaultConfig = ConfigModel()
41+
let propertyListEncoder = PropertyListEncoder()
42+
propertyListEncoder.outputFormat = .xml
43+
44+
let plistData = try? propertyListEncoder.encode(defaultConfig)
45+
_ = FileManager.default.createFile(atPath: configPath, contents: plistData, attributes: nil)
46+
return defaultConfig
47+
}
48+
}

SwiftJSONModeler For Xcode/Model/ConfigModel.swift renamed to SwiftJSONModeler For Xcode/Config/Model/ConfigModel.swift

File renamed without changes.

SwiftJSONModeler For Xcode/Controller/ConfigViewController.swift

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -108,15 +108,16 @@ class ConfigViewController: NSViewController {
108108
}
109109

110110
@IBAction func saveButtonTap(_ sender: NSButton) {
111-
config.conform = confromTextField.stringValue
112-
config.module = moduleTextField.stringValue
113-
config.prefix = prefixTextField.stringValue
114-
config.subffix = subffixTextField.stringValue
115-
config.yapiPath = pathTextField.stringValue
116-
config.yapiToken = tokenForSave()
117-
config.yapiHost = yapiHostTextField.stringValue
118-
config.remark = remarkTextField.stringValue
119-
view.window?.close()
111+
112+
// config.conform = confromTextField.stringValue
113+
// config.module = moduleTextField.stringValue
114+
// config.prefix = prefixTextField.stringValue
115+
// config.subffix = subffixTextField.stringValue
116+
// config.yapiPath = pathTextField.stringValue
117+
// config.yapiToken = tokenForSave()
118+
// config.yapiHost = yapiHostTextField.stringValue
119+
// config.remark = remarkTextField.stringValue
120+
// view.window?.close()
120121
}
121122

122123
@IBAction func typeOptionalBtnTap(_ sender: NSButton) {
Lines changed: 36 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// Panel.swift
2+
// OpenSavePanel.swift
33
// SwiftJSONModeler For Xcode
44
//
55
// Created by Sven on 2021/2/7.
@@ -8,13 +8,10 @@
88
/// 文件导入和导出
99

1010
import AppKit
11-
/// 配置存在路径
12-
let configPath = (NSHomeDirectory() as NSString).appendingPathComponent("Config.plist")
13-
struct PanelManager {
14-
// static let `default` = PanelManager()
15-
// private init() {}
11+
12+
struct OpenSavePanel {
1613
/// 导入文件
17-
func openPanelImportFile(completion: @escaping (_ url: URL?) -> Void) -> Void {
14+
func importFile(completion: @escaping (_ error: Error?) -> Void) -> Void {
1815
let panel = NSOpenPanel()
1916
panel.prompt = "确定"
2017
panel.message = "选择配置"
@@ -25,18 +22,24 @@ struct PanelManager {
2522
//panel.directoryURL =
2623
panel.beginSheetModal(for: NSApp.mainWindow ?? NSWindow() ) { (response) in
2724
if response == .OK {
28-
let url = panel.url
29-
read(url: url)
30-
completion(url)
25+
if let url = panel.url {
26+
let isSuccess = read(url: url)
27+
let error = NSError(domain: "swiftjsonmodeler.error", code: 5000, userInfo: [NSLocalizedDescriptionKey: "导入保存失败"])
28+
completion(isSuccess ? nil : error )
29+
} else {
30+
let error = NSError(domain: "swiftjsonmodeler.error", code: 5000, userInfo: [NSLocalizedDescriptionKey: "导入文件 url不存在"])
31+
completion(error)
32+
}
33+
3134
} else {
3235
//取消
33-
completion(nil)
36+
completion(NSError(domain: "swiftjsonmodeler.error", code: 5000, userInfo: [NSLocalizedDescriptionKey: "取消选择"]))
3437
}
3538
}
3639
}
3740

3841
/// 导出文件
39-
func openPanelExportFile() -> Void {
42+
func exportFile(completion: @escaping (_ error: Error?) -> Void) -> Void {
4043
let savePanel = NSSavePanel()
4144
savePanel.nameFieldStringValue = "Config.plist"
4245
savePanel.message = "请选择路径保存配置"
@@ -47,15 +50,19 @@ struct PanelManager {
4750
savePanel.beginSheetModal(for: NSApp.mainWindow ?? NSWindow()) { (response) in
4851
if response == .OK {
4952
if let path = savePanel.url {
50-
saveFile(url: path)
53+
let isSuccess = saveFile(url: path)
54+
let error = NSError(domain: "swiftjsonmodeler.error", code: 5000, userInfo: [NSLocalizedDescriptionKey: "导入保存失败"])
55+
completion(isSuccess ? nil : error )
56+
} else {
57+
completion(NSError(domain: "swiftjsonmodeler.error", code: 5000, userInfo: [NSLocalizedDescriptionKey: "导入文件 url不存在"]))
5158
}
5259
} else {
53-
60+
completion(NSError(domain: "swiftjsonmodeler.error", code: 5000, userInfo: [NSLocalizedDescriptionKey: "取消选择"]))
5461
}
5562
}
5663
}
57-
58-
private func read(url: URL?) {
64+
/// 导入保存
65+
private func read(url: URL) -> Bool {
5966
// guard let url = url else {
6067
// return
6168
// }
@@ -70,22 +77,25 @@ struct PanelManager {
7077
// }
7178

7279

73-
let configModel = ConfigModel()
74-
let propertyListEncoder = PropertyListEncoder()
75-
propertyListEncoder.outputFormat = .xml
76-
77-
let plistData = try? propertyListEncoder.encode(configModel)
80+
// let configModel = ConfigModel()
81+
// let propertyListEncoder = PropertyListEncoder()
82+
// propertyListEncoder.outputFormat = .xml
83+
//
84+
// let plistData = try? propertyListEncoder.encode(configModel)
85+
7886
let fileManager = FileManager.default
79-
let isSuccess = fileManager.createFile(atPath: configPath, contents: plistData, attributes: nil) // 全覆盖写入
87+
let configData = fileManager.contents(atPath: url.path)
88+
let isSuccess = fileManager.createFile(atPath: configPath, contents: configData, attributes: nil) // 全覆盖写入
8089
if isSuccess {
81-
print("保存成功")
90+
print("保存成功:\(configPath)")
8291
} else {
8392
print("保存失败")
8493
}
94+
return isSuccess
8595
}
8696

87-
/// 保存文件
88-
func saveFile( url: URL) -> Void {
97+
/// 导出保存文件
98+
func saveFile( url: URL) -> Bool {
8999
let fileManager = FileManager.default
90100
let contents = fileManager.contents(atPath: configPath)
91101
let isSuccess = fileManager.createFile(atPath: url.path, contents: contents, attributes: [FileAttributeKey.appendOnly : false, FileAttributeKey.extensionHidden: false])
@@ -94,5 +104,6 @@ struct PanelManager {
94104
} else {
95105
print("保存失败")
96106
}
107+
return isSuccess
97108
}
98109
}

0 commit comments

Comments
 (0)