forked from intitni/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServiceUpdateMigrator.swift
More file actions
94 lines (76 loc) · 3.1 KB
/
ServiceUpdateMigrator.swift
File metadata and controls
94 lines (76 loc) · 3.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import Configs
import Foundation
import GitHubCopilotService
import KeychainAccess
import Preferences
extension UserDefaultPreferenceKeys {
struct OldMigrationVersion: UserDefaultPreferenceKey {
var defaultValue: String = "0"
let key = "OldMigrationVersion"
}
var oldMigrationVersion: OldMigrationVersion { .init() }
}
public struct ServiceUpdateMigrator {
public init() {}
public func migrate() async throws {
let version = Bundle.main.object(forInfoDictionaryKey: "CFBundleVersion") as? String ?? "0"
try await migrate(from: UserDefaults.shared.value(for: \.oldMigrationVersion), to: version)
UserDefaults.shared.set(version, for: \.oldMigrationVersion)
}
func migrate(from oldVersion: String, to currentVersion: String) async throws {
guard let old = Int(oldVersion) else { return }
if old <= 135 {
try migrateFromLowerThanOrEqualToVersion135()
}
if old < 170 {
try migrateFromLowerThanOrEqualToVersion170()
}
}
}
func migrateFromLowerThanOrEqualToVersion135() throws {
// 0. Create the application support folder if it doesn't exist
let urls = try GitHubCopilotBaseService.createFoldersIfNeeded()
// 1. Move the undefined folder in application support into a sub folder called `GitHub
// Copilot/support`
let undefinedFolderURL = urls.applicationSupportURL.appendingPathComponent("undefined")
var isUndefinedADirectory: ObjCBool = false
let isUndefinedExisted = FileManager.default.fileExists(
atPath: undefinedFolderURL.path,
isDirectory: &isUndefinedADirectory
)
if isUndefinedExisted, isUndefinedADirectory.boolValue {
try FileManager.default.moveItem(
at: undefinedFolderURL,
to: urls.supportURL.appendingPathComponent("undefined")
)
}
// 2. Copy the GitHub copilot language service to `GitHub Copilot/executable`
let copilotFolderURL = urls.executableURL.appendingPathComponent("copilot")
var copilotIsFolder: ObjCBool = false
let executable = Bundle.main.resourceURL?.appendingPathComponent("copilot")
if let executable,
FileManager.default.fileExists(atPath: executable.path, isDirectory: &copilotIsFolder),
!FileManager.default.fileExists(atPath: copilotFolderURL.path)
{
try FileManager.default.copyItem(
at: executable,
to: urls.executableURL.appendingPathComponent("copilot")
)
}
// 3. Use chmod to change the permission of the executable to 755
try FileManager.default.setAttributes(
[.posixPermissions: 0o755],
ofItemAtPath: copilotFolderURL.path
)
}
func migrateFromLowerThanOrEqualToVersion170() throws {
let oldKeychain = Keychain(service: keychainService, accessGroup: keychainAccessGroup)
let newKeychain = oldKeychain.attributes([
kSecUseDataProtectionKeychain as String: true,
])
if (try? oldKeychain.contains("codeiumKey")) ?? false,
let key = try? oldKeychain.getString("codeiumKey")
{
try newKeychain.set(key, key: "codeiumAuthKey")
}
}