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

Isolate Windows specific changes #12

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
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
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 == "Minizip" }).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
}
8 changes: 4 additions & 4 deletions Sources/Minizip/include/Minizip.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
#ifndef Minizip_h
#define Minizip_h

#import "ioapi.h"
#import "crypt.h"
#import "unzip.h"
#import "zip.h"
#include "ioapi.h"
#include "crypt.h"
#include "unzip.h"
#include "zip.h"

#endif /* Minizip_h */
1 change: 0 additions & 1 deletion Sources/Minizip/module/module.modulemap
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
module Minizip [system][extern_c] {
header "../include/Minizip.h"
link "z"
export *
}
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.nativePath, APPEND_STATUS_CREATE)

for archiveFile in archiveFiles {
// Skip empty data
Expand Down
11 changes: 11 additions & 0 deletions Sources/Zip/URL+nativePath.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#if canImport(Darwin) || compiler(<6.0)
import Foundation
#else
import FoundationEssentials
#endif

extension URL {
var nativePath: String {
return withUnsafeFileSystemRepresentation { String(cString: $0!) }
}
}
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 FileManager {
struct ProcessedFilePath {
let filePathURL: URL
let fileName: String?

var filePath: String {
filePathURL.nativePath
}
}

/// Process zip paths.
///
/// - Parameter roots: Paths as `URL`.
///
/// - Returns: Array of ``ProcessedFilePath`` structs.
static func fileSubPaths(from roots: [URL]) -> [ProcessedFilePath] {
var processedFilePaths = [ProcessedFilePath]()
for pathURL in roots {
var isDirectory: ObjCBool = false
_ = FileManager.default.fileExists(
atPath: pathURL.nativePath,
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.nativePath) {
while let filePathComponent = enumerator.nextObject() as? String {
let pathURL = directory.appendingPathComponent(filePathComponent)

var isDirectory: ObjCBool = false
_ = FileManager.default.fileExists(
atPath: pathURL.nativePath,
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