Skip to content

Commit

Permalink
Support Linux (#52)
Browse files Browse the repository at this point in the history
* Update test.yml
* import FoundationNetworking
* Update MIMEType.swift
* Fix tests with MIMEType
* Update OSLogging for Linux
  • Loading branch information
Alexander-Ignition authored Jan 15, 2022
1 parent d20a816 commit 787b830
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 6 deletions.
14 changes: 12 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,21 @@ on:
- '**'

jobs:
test:
name: Run tests
test-macos:
name: Test on macOS
runs-on: macOS-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Build and test
run: make test
test-linux:
name: Test on Linux
runs-on: ubuntu-20.04
container:
image: swift:5.5.1-focal
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Test catbird app
run: swift test --package-path Packages/CatbirdApp --disable-automatic-resolution
4 changes: 4 additions & 0 deletions Packages/CatbirdAPI/Sources/CatbirdAPI/Catbird.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import Foundation

#if canImport(FoundationNetworking)
import FoundationNetworking
#endif

/// API Client to mock server.
public final class Catbird {

Expand Down
4 changes: 4 additions & 0 deletions Packages/CatbirdAPI/Sources/CatbirdAPI/CatbirdAction.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import Foundation

#if canImport(FoundationNetworking)
import FoundationNetworking
#endif

/// Catbird API action.
public enum CatbirdAction: Equatable {
/// Add, or insert `ResponseMock` for `RequestPattern`.
Expand Down
4 changes: 4 additions & 0 deletions Packages/CatbirdAPI/Sources/CatbirdAPI/CatbirdError.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import Foundation

#if canImport(FoundationNetworking)
import FoundationNetworking
#endif

/// From `Vapor.ErrorMiddleware`.
private struct ErrorResponse: Codable {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import Foundation

#if canImport(FoundationNetworking)
import FoundationNetworking
#endif

extension URLSessionTask {

/// Wait until task completed.
Expand Down
4 changes: 2 additions & 2 deletions Packages/CatbirdApp/Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@
"repositoryURL": "https://github.com/Alexander-Ignition/OSLogging",
"state": {
"branch": null,
"revision": "295e9816de70fdea51c9ad9e674c18b01a0acfd6",
"version": "1.0.2"
"revision": "2370acda786974f59d1726c4662620b9d036a817",
"version": "1.0.3"
}
},
{
Expand Down
2 changes: 1 addition & 1 deletion Packages/CatbirdApp/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ let package = Package(
.package(url: "https://github.com/stencilproject/Stencil.git", from: "0.14.0"),

// macOS system logger
.package(url: "https://github.com/Alexander-Ignition/OSLogging", from: "1.0.0"),
.package(url: "https://github.com/Alexander-Ignition/OSLogging", from: "1.0.3"),

// CatbirdAPI
.package(name: "Catbird", path: "../../")
Expand Down
11 changes: 10 additions & 1 deletion Packages/CatbirdApp/Sources/CatbirdApp/Common/Loggers.swift
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
import Logging

#if canImport(OSLogging)
import OSLogging
#endif

enum Loggers {
static let inMemoryStore = logger(category: "InMemory")
static let fileStore = logger(category: "File")

private static func logger(category: String) -> Logging.Logger {
Logging.Logger(label: CatbirdInfo.current.domain) {
#if os(Linux)
return Logging.Logger(label: CatbirdInfo.current.domain)
#else
return Logging.Logger(label: CatbirdInfo.current.domain) {
OSLogHandler(subsystem: $0, category: category)
}
#endif
}

}
30 changes: 30 additions & 0 deletions Packages/CatbirdApp/Sources/CatbirdApp/Common/MIMEType.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import Vapor
import Foundation

#if canImport(CoreServices)
import CoreServices
#endif

#if canImport(UniformTypeIdentifiers)
import UniformTypeIdentifiers
#endif

struct MIMEType: Equatable {
private let string: String
Expand All @@ -11,11 +17,15 @@ struct MIMEType: Equatable {
}

var preferredFilenameExtension: String? {
#if os(Linux)
return _UTType(mimeType: string)?.preferredFilenameExtension
#else
if #available(macOS 11.0, *) {
return UTType(mimeType: string)?.preferredFilenameExtension
} else {
return _UTType(mimeType: string)?.preferredFilenameExtension
}
#endif
}
}

Expand All @@ -25,6 +35,25 @@ extension Vapor.Request {
}
}

#if os(Linux)
private struct _UTType {
private static let filenameExtension: [String: String] = [
"application/json": "json",
"text/html": "html",
"text/plain": "txt"
]

var preferredFilenameExtension: String? {
Self.filenameExtension[mimeType]
}

private let mimeType: String

init?(mimeType: String) {
self.mimeType = mimeType
}
}
#else
private struct _UTType {
private let identifier: CFString

Expand All @@ -47,3 +76,4 @@ private struct _UTType {
UTTypeCopyPreferredTagWithClass(identifier, tagClass)?.takeRetainedValue() as String?
}
}
#endif

0 comments on commit 787b830

Please sign in to comment.