forked from intitni/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGitHubCopilotInstallationManager.swift
More file actions
180 lines (152 loc) · 7.04 KB
/
GitHubCopilotInstallationManager.swift
File metadata and controls
180 lines (152 loc) · 7.04 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
import Foundation
import Terminal
public struct GitHubCopilotInstallationManager {
private static var isInstalling = false
static var downloadURL: URL {
let commitHash = "a4a6d6b3f9e284e7f5c849619e06cd228cad8abd"
let link = "https://github.com/github/copilot.vim/archive/\(commitHash).zip"
return URL(string: link)!
}
static let latestSupportedVersion = "1.12.1"
public init() {}
public enum InstallationStatus {
case notInstalled
case installed(String)
case outdated(current: String, latest: String)
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) {
case .orderedAscending:
return .outdated(current: version, latest: Self.latestSupportedVersion)
case .orderedSame:
return .installed(version)
case .orderedDescending:
return .unsupported(current: version, latest: Self.latestSupportedVersion)
}
}
return .outdated(current: "Unknown", latest: Self.latestSupportedVersion)
}
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 {
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],
currentDirectoryPath: urls.executableURL.path,
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 = gitFolderURL.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)
}
}
}