From 2d2884fdea2a7bee9f460d8d5e87937e7ffffd7c Mon Sep 17 00:00:00 2001 From: Davide De Rosa Date: Fri, 26 Oct 2018 09:59:05 +0200 Subject: [PATCH] Export profiles to separate JSONs Use id as contextual filename. --- .../Model/ConnectionService+Migration.swift | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/Passepartout/Sources/Model/ConnectionService+Migration.swift b/Passepartout/Sources/Model/ConnectionService+Migration.swift index ca2b9225..fbbd132d 100644 --- a/Passepartout/Sources/Model/ConnectionService+Migration.swift +++ b/Passepartout/Sources/Model/ConnectionService+Migration.swift @@ -53,6 +53,7 @@ extension ConnectionService { try migrateToBaseConfiguration(&json) try migrateToBuildNumber(&json) try migrateHostProfileConfigurations() + try migrateSplitProfileSerialization(&json) } return try JSONSerialization.data(withJSONObject: json, options: []) @@ -125,6 +126,42 @@ extension ConnectionService { } } + static func migrateSplitProfileSerialization(_ json: inout [String: Any]) throws { + guard let profiles = json["profiles"] as? [[String: Any]] else { + return + } + + let fm = FileManager.default + let providersParentURL = fm.userURL(for: .documentDirectory, appending: AppConstants.Store.providersDirectory) + let hostsParentURL = fm.userURL(for: .documentDirectory, appending: AppConstants.Store.hostsDirectory) + try? fm.createDirectory(at: providersParentURL, withIntermediateDirectories: false, attributes: nil) + try? fm.createDirectory(at: hostsParentURL, withIntermediateDirectories: false, attributes: nil) + + for p in profiles { + if var provider = p["provider"] as? [String: Any] { + guard let id = provider["name"] as? String else { + continue + } +// provider["id"] = id +// provider.removeValue(forKey: "name") + + let url = providersParentURL.appendingPathComponent("\(id).json") + let data = try JSONSerialization.data(withJSONObject: provider, options: []) + try data.write(to: url) + } else if var host = p["host"] as? [String: Any] { + guard let id = host["title"] as? String else { + continue + } +// host["id"] = id +// host.removeValue(forKey: "title") + + let url = hostsParentURL.appendingPathComponent("\(id).json") + let data = try JSONSerialization.data(withJSONObject: host, options: []) + try data.write(to: url) + } + } + } + // MARK: Helpers private static func migrateSessionConfiguration(in map: inout [String: Any]) {