Skip to content

Commit

Permalink
Merge pull request #22 from tuist/add-zip-api
Browse files Browse the repository at this point in the history
Add zipping APIs
  • Loading branch information
pepicrft authored Jul 8, 2024
2 parents 8cb7e5f + c570692 commit 4fbb98c
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 6 deletions.
21 changes: 15 additions & 6 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/tuist/Path",
"state" : {
"revision" : "61e3fa3b2fd56e0bd2496afd62a35c2cb98a6fca",
"version" : "0.2.0"
"revision" : "4490da629937fc3994f72dd787dcc3f50d6973fe",
"version" : "0.3.0"
}
},
{
Expand All @@ -32,17 +32,17 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-log",
"state" : {
"revision" : "e97a6fcb1ab07462881ac165fdbb37f067e205d5",
"version" : "1.5.4"
"revision" : "9cb486020ebf03bfa5b5df985387a14a98744537",
"version" : "1.6.1"
}
},
{
"identity" : "swift-nio",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-nio",
"state" : {
"revision" : "359c461e5561d22c6334828806cc25d759ca7aa6",
"version" : "2.65.0"
"revision" : "e5a216ba89deba84356bad9d4c2eab99071c745b",
"version" : "2.67.0"
}
},
{
Expand All @@ -53,6 +53,15 @@
"revision" : "025bcb1165deab2e20d4eaba79967ce73013f496",
"version" : "1.2.1"
}
},
{
"identity" : "zipfoundation",
"kind" : "remoteSourceControl",
"location" : "https://github.com/weichsel/ZIPFoundation",
"state" : {
"revision" : "02b6abe5f6eef7e3cbd5f247c5cc24e246efcfe0",
"version" : "0.9.19"
}
}
],
"version" : 2
Expand Down
2 changes: 2 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ let package = Package(
.package(url: "https://github.com/tuist/Path", .upToNextMajor(from: "0.3.0")),
.package(url: "https://github.com/apple/swift-nio", .upToNextMajor(from: "2.68.0")),
.package(url: "https://github.com/apple/swift-log", .upToNextMajor(from: "1.6.1")),
.package(url: "https://github.com/weichsel/ZIPFoundation", .upToNextMajor(from: "0.9.19")),
],
targets: [
.target(
Expand All @@ -33,6 +34,7 @@ let package = Package(
.product(name: "_NIOFileSystem", package: "swift-nio"),
.product(name: "Path", package: "Path"),
.product(name: "Logging", package: "swift-log"),
.product(name: "ZIPFoundation", package: "ZIPFoundation"),
],
swiftSettings: [
.define("MOCKING", .when(configuration: .debug)),
Expand Down
1 change: 1 addition & 0 deletions Project.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ let project = Project(name: "FileSystem", settings: .settings(base: ["SWIFT_STRI
.external(name: "_NIOFileSystem"),
.external(name: "Logging"),
.external(name: "Path"),
.external(name: "ZIPFoundation"),
],
settings: .settings(configurations: [
.debug(name: .debug, settings: ["SWIFT_ACTIVE_COMPILATION_CONDITIONS": "$(inherited) MOCKING"]),
Expand Down
25 changes: 25 additions & 0 deletions Sources/FileSystem/FileSystem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Logging
import NIOCore
import NIOFileSystem
import Path
import ZIPFoundation

public enum FileSystemItemType: CaseIterable, Equatable {
case directory
Expand Down Expand Up @@ -229,6 +230,9 @@ public protocol FileSysteming {
/// - Returns: The resolved path.
func resolveSymbolicLink(_ symlinkPath: AbsolutePath) async throws -> AbsolutePath

func zipFileOrDirectoryContent(at path: AbsolutePath, to: AbsolutePath) async throws
func unzip(_ zipePath: AbsolutePath, to: AbsolutePath) async throws

// TODO:
// func changeExtension(path: AbsolutePath, to newExtension: String) throws -> AbsolutePath
// func urlSafeBase64MD5(path: AbsolutePath) throws -> String
Expand Down Expand Up @@ -537,4 +541,25 @@ public struct FileSystem: FileSysteming {
let path = try await NIOFileSystem.FileSystem.shared.destinationOfSymbolicLink(at: FilePath(symlinkPath.pathString))
return try AbsolutePath(validating: path.string)
}

public func zipFileOrDirectoryContent(at path: Path.AbsolutePath, to: Path.AbsolutePath) async throws {
logger?.debug("Zipping the file or contents of directory at path \(path.pathString) into \(to.pathString)")
try await NIOSingletons.posixBlockingThreadPool.runIfActive {
try FileManager.default.zipItem(
at: URL(fileURLWithPath: path.pathString),
to: URL(fileURLWithPath: to.pathString),
shouldKeepParent: false
)
}
}

public func unzip(_ zipPath: Path.AbsolutePath, to: Path.AbsolutePath) async throws {
logger?.debug("Unzipping the file at path \(zipPath.pathString) to \(to.pathString)")
try await NIOSingletons.posixBlockingThreadPool.runIfActive {
try FileManager.default.unzipItem(
at: URL(fileURLWithPath: zipPath.pathString),
to: URL(fileURLWithPath: to.pathString)
)
}
}
}
19 changes: 19 additions & 0 deletions Tests/FileSystemTests/FileSystemTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -415,4 +415,23 @@ final class FileSystemTests: XCTestCase {
XCTAssertEqual(_error, FileSystemError.absentSymbolicLink(symbolicLinkPath))
}
}

func test_zipping() async throws {
try await subject.runInTemporaryDirectory(prefix: "FileSystem") { temporaryDirectory in
// Given
let filePath = temporaryDirectory.appending(component: "file")
let zipPath = temporaryDirectory.appending(component: "file.zip")
let unzippedPath = temporaryDirectory.appending(component: "unzipped")
try await subject.makeDirectory(at: unzippedPath)
try await subject.touch(filePath)

// When
try await subject.zipFileOrDirectoryContent(at: filePath, to: zipPath)
try await subject.unzip(zipPath, to: unzippedPath)

// Then
let exists = try await subject.exists(unzippedPath.appending(component: "file"))
XCTAssertTrue(exists)
}
}
}

0 comments on commit 4fbb98c

Please sign in to comment.