Skip to content

Commit daae205

Browse files
Refactors
1 parent f358ab9 commit daae205

File tree

2 files changed

+31
-10
lines changed

2 files changed

+31
-10
lines changed

CodeEdit/Features/LSP/Registry/PackageManagers/GithubPackageManager.swift

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,17 @@ final class GithubPackageManager: PackageManagerProtocol {
3030
}
3131

3232
func install(method: InstallationMethod) async throws {
33-
guard case .standardPackage(let source) = method else {
34-
throw PackageManagerError.invalidConfiguration
35-
}
36-
37-
let packagePath = installationDirectory.appending(path: source.entryName)
38-
try await initialize(in: packagePath)
39-
4033
switch method {
4134
case let .binaryDownload(source, url):
35+
let packagePath = installationDirectory.appending(path: source.entryName)
36+
try await initialize(in: packagePath)
4237
try await downloadBinary(source, url)
38+
4339
case let .sourceBuild(source, command):
40+
let packagePath = installationDirectory.appending(path: source.entryName)
41+
try await initialize(in: packagePath)
4442
try await installFromSource(source, command)
43+
4544
case .standardPackage, .unknown:
4645
throw PackageManagerError.invalidConfiguration
4746
}
@@ -64,11 +63,29 @@ final class GithubPackageManager: PackageManagerProtocol {
6463
}
6564

6665
private func downloadBinary(_ source: PackageSource, _ url: URL) async throws {
67-
_ = try await URLSession.shared.data(from: url)
66+
let (data, response) = try await URLSession.shared.data(from: url)
67+
68+
guard let httpResponse = response as? HTTPURLResponse,
69+
(200...299).contains(httpResponse.statusCode) else {
70+
throw RegistryManagerError.downloadFailed(
71+
url: url,
72+
error: NSError(domain: "HTTP error", code: (response as? HTTPURLResponse)?.statusCode ?? -1)
73+
)
74+
}
75+
6876
let fileName = url.lastPathComponent
6977
let downloadPath = installationDirectory.appending(path: source.entryName)
7078
let packagePath = downloadPath.appending(path: fileName)
7179

80+
do {
81+
try data.write(to: packagePath, options: .atomic)
82+
} catch {
83+
throw RegistryManagerError.downloadFailed(
84+
url: url,
85+
error: error
86+
)
87+
}
88+
7289
if !FileManager.default.fileExists(atPath: packagePath.path) {
7390
throw RegistryManagerError.downloadFailed(
7491
url: url,
@@ -84,7 +101,11 @@ final class GithubPackageManager: PackageManagerProtocol {
84101
private func installFromSource(_ source: PackageSource, _ command: String) async throws {
85102
let installPath = installationDirectory.appending(path: source.entryName, directoryHint: .isDirectory)
86103
do {
87-
_ = try await executeInDirectory(in: installPath.path, ["git", "clone", source.repositoryUrl!])
104+
guard let repoURL = source.repositoryUrl else {
105+
throw PackageManagerError.invalidConfiguration
106+
}
107+
108+
_ = try await executeInDirectory(in: installPath.path, ["git", "clone", repoURL])
88109
let repoPath = installPath.appending(path: source.pkgName, directoryHint: .isDirectory)
89110
_ = try await executeInDirectory(in: repoPath.path, [command])
90111
} catch {

CodeEdit/Features/LSP/Registry/PackageSourceParser/PackageSourceParser.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ enum PackageSourceParser {
4040
false
4141
}
4242

43-
let source = PackageSource(
43+
var source = PackageSource(
4444
sourceId: sourceId,
4545
type: isSourceBuild ? .sourceBuild : .github,
4646
pkgName: packageName,

0 commit comments

Comments
 (0)