Skip to content

Commit

Permalink
Isolate Windows specific changes
Browse files Browse the repository at this point in the history
  • Loading branch information
fpseverino committed Nov 6, 2024
1 parent 567d515 commit 9c6ae98
Show file tree
Hide file tree
Showing 6 changed files with 377 additions and 149 deletions.
40 changes: 40 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,43 @@ jobs:
uses: vapor/ci/.github/workflows/run-unit-tests.yml@main
secrets:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}

windows-unit:
if: ${{ !(github.event.pull_request.draft || false) }}
strategy:
fail-fast: false
matrix:
swift-version:
- 5.9
- 5.10
- 6.0
include:
- { swift-version: 5.9, swift-branch: swift-5.9.2-release, swift-tag: 5.9.2-RELEASE }
- { swift-version: 5.10, swift-branch: swift-5.10.1-release, swift-tag: 5.10.1-RELEASE }
- { swift-version: 6.0, swift-branch: swift-6.0.1-release, swift-tag: 6.0.1-RELEASE }
runs-on: windows-latest
timeout-minutes: 60
steps:
- name: Install Windows Swift toolchain
uses: compnerd/gha-setup-swift@main
with:
branch: ${{ matrix.swift-branch }}
tag: ${{ matrix.swift-tag }}
- name: Download zlib
run: |
curl -L -o zlib.zip https://www.zlib.net/zlib131.zip
mkdir zlib-131
tar -xf zlib.zip -C zlib-131 --strip-components=1
- name: Build and install zlib
run: |
cd zlib-131
mkdir build
cd build
cmake ..
cmake --build . --config Release
cmake --install . --prefix ../install
- name: Check out code
uses: actions/checkout@v4
- name: Run unit tests
run: |
swift test -Xcc -I'C:/Program Files (x86)/zlib/include' -Xlinker -L'C:/Program Files (x86)/zlib/lib'
51 changes: 39 additions & 12 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
// swift-tools-version:5.8
// swift-tools-version:5.9
import PackageDescription

#if canImport(Darwin) || compiler(<6.0)
import Foundation
#else
import FoundationEssentials
#endif

let package = Package(
name: "Zip",
products: [
Expand All @@ -10,21 +16,20 @@ let package = Package(
.target(
name: "Minizip",
exclude: ["module"],
swiftSettings: [
.enableUpcomingFeature("ConciseMagicFile"),
cSettings: [
.define("_CRT_SECURE_NO_WARNINGS", .when(platforms: [.windows]))
],
linkerSettings: [
.linkedLibrary("z")
]
swiftSettings: swiftSettings
),
.target(
name: "Zip",
dependencies: [
.target(name: "Minizip"),
],
swiftSettings: [
.enableUpcomingFeature("ConciseMagicFile"),
]
cSettings: [
.define("_CRT_SECURE_NO_WARNINGS", .when(platforms: [.windows]))
],
swiftSettings: swiftSettings
),
.testTarget(
name: "ZipTests",
Expand All @@ -34,9 +39,31 @@ let package = Package(
resources: [
.copy("Resources"),
],
swiftSettings: [
.enableUpcomingFeature("ConciseMagicFile"),
]
swiftSettings: swiftSettings
),
]
)

var swiftSettings: [SwiftSetting] {
[
.enableUpcomingFeature("ExistentialAny"),
.enableUpcomingFeature("ConciseMagicFile"),
.enableUpcomingFeature("ForwardTrailingClosures"),
.enableUpcomingFeature("DisableOutwardActorInference"),
.enableUpcomingFeature("StrictConcurrency"),
.enableExperimentalFeature("StrictConcurrency=complete"),
]
}

if let target = package.targets.filter({ $0.name == "CMinizip" }).first {
#if os(Windows)
if ProcessInfo.processInfo.environment["ZIP_USE_DYNAMIC_ZLIB"] == nil {
target.cSettings?.append(contentsOf: [.define("ZLIB_STATIC")])
target.linkerSettings = [.linkedLibrary("zlibstatic")]
} else {
target.linkerSettings = [.linkedLibrary("zlib")]
}
#else
target.linkerSettings = [.linkedLibrary("z")]
#endif
}
4 changes: 1 addition & 3 deletions Sources/Zip/ArchiveFile.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,6 @@ extension Zip {
compression: ZipCompression = .DefaultCompression,
progress: ((_ progress: Double) -> ())? = nil
) throws {
let destinationPath = zipFilePath.path

// Progress handler set up
var currentPosition: Int = 0
var totalSize: Int = 0
Expand All @@ -71,7 +69,7 @@ extension Zip {
progressTracker.kind = ProgressKind.file

// Begin Zipping
let zip = zipOpen(destinationPath, APPEND_STATUS_CREATE)
let zip = zipOpen(zipFilePath.withUnsafeFileSystemRepresentation { String(cString: $0!) }, APPEND_STATUS_CREATE)

for archiveFile in archiveFiles {
// Skip empty data
Expand Down
64 changes: 64 additions & 0 deletions Sources/Zip/Zip+ProcessedFilePath.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import Foundation

extension Zip {
struct ProcessedFilePath {
let filePathURL: URL
let fileName: String?

var filePath: String {
filePathURL.withUnsafeFileSystemRepresentation { String(cString: $0!) }
}
}

/// Process zip paths.
///
/// - Parameter paths: Paths as `URL`.
///
/// - Returns: Array of ``ProcessedFilePath`` structs.
static func processZipPaths(_ paths: [URL]) -> [ProcessedFilePath] {
var processedFilePaths = [ProcessedFilePath]()
for pathURL in paths {
var isDirectory: ObjCBool = false
_ = FileManager.default.fileExists(
atPath: pathURL.withUnsafeFileSystemRepresentation { String(cString: $0!) },
isDirectory: &isDirectory
)

if !isDirectory.boolValue {
let processedPath = ProcessedFilePath(filePathURL: pathURL, fileName: pathURL.lastPathComponent)
processedFilePaths.append(processedPath)
} else {
let directoryContents = Self.expandDirectoryFilePath(pathURL)
processedFilePaths.append(contentsOf: directoryContents)
}
}
return processedFilePaths
}

/// Expand directory contents and parse them into ``ProcessedFilePath`` structs.
///
/// - Parameter directory: Path of folder as `URL`.
///
/// - Returns: Array of ``ProcessedFilePath`` structs.
private static func expandDirectoryFilePath(_ directory: URL) -> [ProcessedFilePath] {
var processedFilePaths = [ProcessedFilePath]()
if let enumerator = FileManager.default.enumerator(atPath: directory.withUnsafeFileSystemRepresentation { String(cString: $0!) }) {
while let filePathComponent = enumerator.nextObject() as? String {
let pathURL = directory.appendingPathComponent(filePathComponent)

var isDirectory: ObjCBool = false
_ = FileManager.default.fileExists(
atPath: pathURL.withUnsafeFileSystemRepresentation { String(cString: $0!) },
isDirectory: &isDirectory
)

if !isDirectory.boolValue {
let fileName = (directory.lastPathComponent as NSString).appendingPathComponent(filePathComponent)
let processedPath = ProcessedFilePath(filePathURL: pathURL, fileName: fileName)
processedFilePaths.append(processedPath)
}
}
}
return processedFilePaths
}
}
Loading

0 comments on commit 9c6ae98

Please sign in to comment.