Skip to content

Commit

Permalink
Better errors and comments
Browse files Browse the repository at this point in the history
  • Loading branch information
dfed committed Sep 3, 2024
1 parent 755087b commit 0cd2442
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
18 changes: 10 additions & 8 deletions Plugins/InstallSafeDITool/InstallCLIPluginCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,17 @@ struct InstallSafeDITool: CommandPlugin {
arguments _: [String]
) async throws {
guard let safeDIOrigin = context.package.dependencies.first(where: { $0.package.displayName == "SafeDI" })?.package.origin else {
print("No package origin found for SafeDI package")
Diagnostics.error("No package origin found for SafeDI package")
return
}
switch safeDIOrigin {
case let .repository(url, displayVersion, _):
// As of Xcode 16.0 Beta 6, the display version is of the form "Optional(version)".
// This regular expression is duplicated by SafeDIGenerateDependencyTree since plugins can not share code.
guard let versionMatch = try /Optional\((.*?)\)|^(.*?)$/.firstMatch(in: displayVersion),
let versionSubstring = versionMatch.output.1 ?? versionMatch.output.2
else {
print("could not extract version for SafeDI")
Diagnostics.error("Could not extract version for SafeDI")
return
}
let version = String(versionSubstring)
Expand All @@ -49,33 +51,33 @@ struct InstallSafeDITool: CommandPlugin {
let expectedToolLocation = expectedToolFolder.appending(component: "safeditool")

guard let url = URL(string: url)?.deletingPathExtension() else {
print("No package url found for SafeDI package")
Diagnostics.error("No package url found for SafeDI package")
return
}
#if arch(arm64)
let toolName = "SafeDITool-arm64"
#elseif arch(x86_64)
let toolName = "SafeDITool-x86_64"
#else
print("Unexpected architecture type")
Diagnostics.error("Unexpected architecture type")
return
#endif

let downloadURL = url.appending(
let githubDownloadURL = url.appending(
components: "releases",
"download",
version,
toolName
)
let (downloadedURL, _) = try await URLSession.shared.download(
for: URLRequest(url: downloadURL)
for: URLRequest(url: githubDownloadURL)
)
let downloadedFileAttributes = try FileManager.default.attributesOfItem(atPath: downloadedURL.path())
guard let currentPermissions = downloadedFileAttributes[.posixPermissions] as? NSNumber,
// Add executable attributes to the downloaded file.
chmod(downloadedURL.path(), mode_t(currentPermissions.uint16Value | S_IXUSR | S_IXGRP | S_IXOTH)) == 0
else {
print("Failed to make downloaded file executable")
Diagnostics.error("Failed to make downloaded file \(downloadedURL.path()) executable")
return
}
try FileManager.default.createDirectory(
Expand All @@ -101,7 +103,7 @@ struct InstallSafeDITool: CommandPlugin {
fallthrough

@unknown default:
print("Cannot download SafeDITool from \(safeDIOrigin)")
Diagnostics.error("Cannot download SafeDITool from \(safeDIOrigin) – downloading only works when using a versioned release of SafeDI")
}
}
}
1 change: 1 addition & 0 deletions Plugins/SafeDIGenerator/SafeDIGenerateDependencyTree.swift
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,7 @@ extension Data {
}
switch safeDIOrigin {
case let .repository(_, displayVersion, _):
// This regular expression is duplicated by InstallSafeDITool since plugins can not share code.
guard let versionMatch = try? /Optional\((.*?)\)|^(.*?)$/.firstMatch(in: displayVersion),
let version = versionMatch.output.1 ?? versionMatch.output.2
else {
Expand Down

0 comments on commit 0cd2442

Please sign in to comment.