Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Destinations: use UniversalArchiver in install command #6392

Merged
merged 6 commits into from
Apr 11, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions Sources/Basics/Archiver/UniversalArchiver.swift
Original file line number Diff line number Diff line change
@@ -20,13 +20,13 @@ public struct UniversalArchiver: Archiver {

/// Errors specific to the implementation of ``UniversalArchiver``.
enum Error: Swift.Error {
case unknownFormat(String, AbsolutePath)
case unknownFormat([String], AbsolutePath)
case noFileNameExtension(AbsolutePath)

var description: String {
switch self {
case .unknownFormat(let ext, let path):
return "unknown format with extension \(ext) at path `\(path)`"
return "unknown format with extension \(ext.joined(separator: ".")) at path `\(path)`"
case .noFileNameExtension(let path):
return "file at path `\(path)` has no extension to detect archival format from"
}
@@ -55,15 +55,22 @@ public struct UniversalArchiver: Archiver {
}

private func archiver(for archivePath: AbsolutePath) throws -> any Archiver {
guard let extensions = archivePath.allExtensions else {
guard var extensions = archivePath.allExtensions, extensions.count > 0 else {
throw Error.noFileNameExtension(archivePath)
}

guard let archiver = self.formatMapping[extensions] else {
throw Error.unknownFormat(extensions, archivePath)
// None of the archivers support extensions with more than 2 extension components
if extensions.count > 2 {
extensions = extensions.suffix(2)
}

return archiver
if let archiver = self.formatMapping[extensions.joined(separator: ".")] {
return archiver
} else if let lastExtension = extensions.last, let archiver = self.formatMapping[lastExtension] {
return archiver
} else {
throw Error.unknownFormat(extensions, archivePath)
}
}

public func extract(
6 changes: 3 additions & 3 deletions Sources/Basics/FileSystem/Path+Extensions.swift
Original file line number Diff line number Diff line change
@@ -40,8 +40,8 @@ extension AbsolutePath {
}

/// Unlike ``AbsolutePath//extension``, this property returns all characters after the first `.` character in a
/// `````. If no dot character is present in the filename or first dot is the last character, `nil` is returned.
var allExtensions: String? {
/// filename. If no dot character is present in the filename or first dot is the last character, `nil` is returned.
var allExtensions: [String]? {
guard let firstDot = basename.firstIndex(of: ".") else {
return nil
}
@@ -54,6 +54,6 @@ extension AbsolutePath {

extensions.removeFirst()

return extensions
return extensions.split(separator: ".").map(String.init)
}
}
Original file line number Diff line number Diff line change
@@ -43,11 +43,13 @@ public struct InstallDestination: DestinationCommand {
_ destinationsDirectory: AbsolutePath,
_ observabilityScope: ObservabilityScope
) throws {
let cancellator = Cancellator(observabilityScope: observabilityScope)
cancellator.installSignalHandlers()
try DestinationBundle.install(
bundlePathOrURL: bundlePathOrURL,
destinationsDirectory: destinationsDirectory,
self.fileSystem,
ZipArchiver(fileSystem: self.fileSystem),
UniversalArchiver(self.fileSystem, cancellator),
observabilityScope
)
}
4 changes: 2 additions & 2 deletions Sources/PackageModel/DestinationBundle.swift
Original file line number Diff line number Diff line change
@@ -225,13 +225,13 @@ public struct DestinationBundle {
throw DestinationError.destinationBundleAlreadyInstalled(bundleName: unpackedBundleName)
}

print("\(bundleName) is assumed to be an archive, unpacking...")

// If there's no archive extension on the bundle name, assuming it's not archived and returning the same path.
guard unpackedBundleName != bundleName else {
return bundlePath
}

print("\(bundleName) is assumed to be an archive, unpacking...")

try tsc_await { archiver.extract(from: bundlePath, to: temporaryDirectory, completion: $0) }

return temporaryDirectory.appending(component: unpackedBundleName)