-
-
Notifications
You must be signed in to change notification settings - Fork 427
Expand file tree
/
Copy pathGitHubCopilotInstallationManager.swift
More file actions
212 lines (184 loc) · 8.59 KB
/
GitHubCopilotInstallationManager.swift
File metadata and controls
212 lines (184 loc) · 8.59 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import Foundation
import Terminal
public struct GitHubCopilotInstallationManager {
@GitHubCopilotSuggestionActor
public private(set) static var isInstalling = false
static var downloadURL: URL {
let commitHash = "f89e977c87180519ba3b942200e3d05b17b1e2fc"
let link = "https://github.com/github/copilot.vim/archive/\(commitHash).zip"
return URL(string: link)!
}
/// The GitHub's version has quite a lot of changes about `watchedFiles` since the following
/// commit.
/// https://github.com/github/CopilotForXcode/commit/a50045aa3ab3b7d532cadf40c4c10bed32f81169#diff-678798cf677bcd1ce276809cfccd33da9ff594b1b0c557180210a4ed2bd27ffa
static let latestSupportedVersion = "1.57.0"
static let minimumSupportedVersion = "1.32.0"
public init() {}
public enum InstallationStatus {
case notInstalled
case installed(String)
case outdated(current: String, latest: String, mandatory: Bool)
case unsupported(current: String, latest: String)
}
public func checkInstallation() -> InstallationStatus {
guard let urls = try? GitHubCopilotBaseService.createFoldersIfNeeded()
else { return .notInstalled }
let executableFolderURL = urls.executableURL
let binaryURL = executableFolderURL.appendingPathComponent("copilot")
let versionFileURL = executableFolderURL.appendingPathComponent("version")
if !FileManager.default.fileExists(atPath: binaryURL.path) {
return .notInstalled
}
if FileManager.default.fileExists(atPath: versionFileURL.path),
let versionData = try? Data(contentsOf: versionFileURL),
let version = String(data: versionData, encoding: .utf8)
{
switch version.compare(Self.latestSupportedVersion, options: .numeric) {
case .orderedAscending:
switch version.compare(Self.minimumSupportedVersion) {
case .orderedAscending:
return .outdated(
current: version,
latest: Self.latestSupportedVersion,
mandatory: true
)
case .orderedSame:
return .outdated(
current: version,
latest: Self.latestSupportedVersion,
mandatory: false
)
case .orderedDescending:
return .outdated(
current: version,
latest: Self.latestSupportedVersion,
mandatory: false
)
}
case .orderedSame:
return .installed(version)
case .orderedDescending:
return .unsupported(current: version, latest: Self.latestSupportedVersion)
}
}
return .outdated(current: "Unknown", latest: Self.latestSupportedVersion, mandatory: false)
}
public enum InstallationStep {
case downloading
case uninstalling
case decompressing
case done
}
public enum Error: Swift.Error, LocalizedError {
case isInstalling
case failedToFindLanguageServer
case failedToInstallLanguageServer
public var errorDescription: String? {
switch self {
case .isInstalling:
return "Language server is installing."
case .failedToFindLanguageServer:
return "Failed to find language server. Please open an issue on GitHub."
case .failedToInstallLanguageServer:
return "Failed to install language server. Please open an issue on GitHub."
}
}
}
public func installLatestVersion() -> AsyncThrowingStream<InstallationStep, Swift.Error> {
AsyncThrowingStream<InstallationStep, Swift.Error> { continuation in
Task { @GitHubCopilotSuggestionActor in
guard !GitHubCopilotInstallationManager.isInstalling else {
continuation.finish(throwing: Error.isInstalling)
return
}
GitHubCopilotInstallationManager.isInstalling = true
defer { GitHubCopilotInstallationManager.isInstalling = false }
do {
continuation.yield(.downloading)
let urls = try GitHubCopilotBaseService.createFoldersIfNeeded()
// download
let (fileURL, _) = try await URLSession.shared.download(from: Self.downloadURL)
let targetURL = urls.executableURL.appendingPathComponent("archive")
.appendingPathExtension("zip")
try FileManager.default.copyItem(at: fileURL, to: targetURL)
defer { try? FileManager.default.removeItem(at: targetURL) }
// uninstall
continuation.yield(.uninstalling)
try await uninstall()
// decompress
continuation.yield(.decompressing)
let terminal = Terminal()
_ = try await terminal.runCommand(
"/usr/bin/unzip",
arguments: [targetURL.path],
currentDirectoryURL: urls.executableURL,
environment: [:]
)
let contentURLs = try FileManager.default.contentsOfDirectory(
at: urls.executableURL,
includingPropertiesForKeys: nil,
options: []
)
defer {
for url in contentURLs {
try? FileManager.default.removeItem(at: url)
}
}
guard let gitFolderURL = contentURLs
.first(where: { $0.lastPathComponent.hasPrefix("copilot.vim") })
else {
continuation.finish(throwing: Error.failedToInstallLanguageServer)
return
}
let lspURL = {
let caseA = gitFolderURL.appendingPathComponent("dist")
if FileManager.default.fileExists(atPath: caseA.path) {
return caseA
}
return gitFolderURL
.appendingPathComponent("copilot-language-server")
.appendingPathComponent("dist")
}()
let copilotURL = urls.executableURL.appendingPathComponent("copilot")
if !FileManager.default.fileExists(atPath: copilotURL.path) {
try FileManager.default.createDirectory(
at: copilotURL,
withIntermediateDirectories: true,
attributes: nil
)
}
let installationURL = copilotURL.appendingPathComponent("dist")
try FileManager.default.copyItem(at: lspURL, to: installationURL)
// update permission 755
try FileManager.default.setAttributes(
[.posixPermissions: 0o755],
ofItemAtPath: installationURL.path
)
// create version file
let data = Self.latestSupportedVersion.data(using: .utf8)
FileManager.default.createFile(
atPath: urls.executableURL.appendingPathComponent("version").path,
contents: data
)
continuation.yield(.done)
continuation.finish()
} catch {
continuation.finish(throwing: error)
}
}
}
}
public func uninstall() async throws {
guard let urls = try? GitHubCopilotBaseService.createFoldersIfNeeded()
else { return }
let executableFolderURL = urls.executableURL
let binaryURL = executableFolderURL.appendingPathComponent("copilot")
let versionFileURL = executableFolderURL.appendingPathComponent("version")
if FileManager.default.fileExists(atPath: binaryURL.path) {
try FileManager.default.removeItem(at: binaryURL)
}
if FileManager.default.fileExists(atPath: versionFileURL.path) {
try FileManager.default.removeItem(at: versionFileURL)
}
}
}