Skip to content

Commit f34791e

Browse files
committed
Add update migration
1 parent 10da585 commit f34791e

File tree

2 files changed

+72
-18
lines changed

2 files changed

+72
-18
lines changed
Lines changed: 60 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,75 @@
11
import Foundation
2+
import GitHubCopilotService
23
import Preferences
34

45
extension UserDefaultPreferenceKeys {
56
struct OldMigrationVersion: UserDefaultPreferenceKey {
6-
typealias PreferenceValueType = String
7-
static let key = "OldMigrationVersion"
7+
var defaultValue: String = "0"
8+
let key = "OldMigrationVersion"
89
}
9-
10+
1011
var oldMigrationVersion: OldMigrationVersion { .init() }
1112
}
1213

13-
struct ServiceUpdateMigrator {
14-
func migrate() {
15-
migrate(
16-
from: UserDefaults.shared.value(for: \.oldMigrationVersion),
17-
to: Bundle.main.object(forInfoDictionaryKey: "CFBundleVersion")
18-
)
19-
}
14+
public struct ServiceUpdateMigrator {
15+
public init() {}
2016

21-
func migrate(from oldVersion: String, to currentVersion: String) {
22-
guard let old = Int(oldVersion), let new = Int(currentVersion) else { return }
23-
guard old != new else { return }
17+
public func migrate() async throws {
18+
let version = Bundle.main.object(forInfoDictionaryKey: "CFBundleVersion") as? String ?? "0"
19+
20+
try await migrate(from: UserDefaults.shared.value(for: \.oldMigrationVersion), to: version)
21+
UserDefaults.shared.set(version, for: \.oldMigrationVersion)
22+
}
23+
24+
func migrate(from oldVersion: String, to currentVersion: String) async throws {
25+
guard let old = Int(oldVersion) else { return }
2426
if old <= 135 {
25-
migrateFromLowerThanOrEqualToVersion135()
27+
try migrateFromLowerThanOrEqualToVersion135()
2628
}
2729
}
2830
}
2931

30-
func migrateFromLowerThanOrEqualToVersion135() {}
32+
func migrateFromLowerThanOrEqualToVersion135() throws {
33+
// 0. Create the application support folder if it doesn't exist
34+
35+
let urls = try GitHubCopilotBaseService.createFoldersIfNeeded()
36+
37+
// 1. Move the undefined folder in application support into a sub folder called `GitHub
38+
// Copilot/support`
39+
40+
let undefinedFolderURL = urls.applicationSupportURL.appendingPathComponent("undefined")
41+
var isUndefinedADirectory: ObjCBool = false
42+
let isUndefinedExisted = FileManager.default.fileExists(
43+
atPath: undefinedFolderURL.path,
44+
isDirectory: &isUndefinedADirectory
45+
)
46+
if isUndefinedExisted, isUndefinedADirectory.boolValue {
47+
try FileManager.default.moveItem(
48+
at: undefinedFolderURL,
49+
to: urls.supportURL.appendingPathComponent("undefined")
50+
)
51+
}
52+
53+
// 2. Copy the GitHub copilot language service to `GitHub Copilot/executable`
54+
55+
let copilotFolderURL = urls.executableURL.appendingPathComponent("copilot")
56+
var copilotIsFolder: ObjCBool = false
57+
let executable = Bundle.main.resourceURL?.appendingPathComponent("copilot")
58+
if let executable,
59+
FileManager.default.fileExists(atPath: executable.path, isDirectory: &copilotIsFolder),
60+
!FileManager.default.fileExists(atPath: copilotFolderURL.path)
61+
{
62+
try FileManager.default.copyItem(
63+
at: executable,
64+
to: urls.executableURL.appendingPathComponent("copilot")
65+
)
66+
}
67+
68+
// 3. Use chmod to change the permission of the executable to 755
69+
70+
try FileManager.default.setAttributes(
71+
[.posixPermissions: 0o755],
72+
ofItemAtPath: copilotFolderURL.path
73+
)
74+
}
75+

ExtensionService/AppDelegate.swift

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import AppKit
2+
import Environment
23
import FileChangeChecker
34
import LaunchAgentManager
45
import Logger
56
import Preferences
67
import Service
78
import ServiceManagement
9+
import ServiceUpdateMigration
810
import SwiftUI
911
import UpdateChecker
1012
import UserNotifications
11-
import Environment
1213

1314
let bundleIdentifierBase = Bundle.main
1415
.object(forInfoDictionaryKey: "BUNDLE_IDENTIFIER_BASE") as! String
@@ -36,6 +37,13 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate {
3637
Logger.service.info("XPC Service started.")
3738
NSApp.setActivationPolicy(.accessory)
3839
buildStatusBarMenu()
40+
Task {
41+
do {
42+
try await ServiceUpdateMigrator().migrate()
43+
} catch {
44+
Logger.service.error(error.localizedDescription)
45+
}
46+
}
3947
}
4048

4149
@objc private func buildStatusBarMenu() {
@@ -64,7 +72,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate {
6472
keyEquivalent: ""
6573
)
6674
#endif
67-
75+
6876
let checkForUpdate = NSMenuItem(
6977
title: "Check for Updates",
7078
action: #selector(checkForUpdate),
@@ -219,7 +227,7 @@ private class UserDefaultsObserver: NSObject {
219227
) {
220228
onChange?(keyPath)
221229
}
222-
230+
223231
deinit {
224232
removeObserver(self, forKeyPath: UserDefaultPreferenceKeys().realtimeSuggestionToggle.key)
225233
}
@@ -256,3 +264,4 @@ func locateHostBundleURL(url: URL) -> URL? {
256264
.appendingPathComponent("Copilot for Xcode Dev.app")
257265
return devAppURL
258266
}
267+

0 commit comments

Comments
 (0)