diff --git a/.github/workflows/CI.yml b/.github/workflows/macOS.yml similarity index 85% rename from .github/workflows/CI.yml rename to .github/workflows/macOS.yml index a15b567..7353c39 100644 --- a/.github/workflows/CI.yml +++ b/.github/workflows/macOS.yml @@ -1,4 +1,4 @@ -name: CI +name: macOS on: push: @@ -8,7 +8,7 @@ on: jobs: build: - runs-on: macos-12 + runs-on: macos-latest steps: - uses: actions/checkout@v3 diff --git a/.github/workflows/ubuntu.yml b/.github/workflows/ubuntu.yml new file mode 100644 index 0000000..374be6c --- /dev/null +++ b/.github/workflows/ubuntu.yml @@ -0,0 +1,18 @@ +name: Ubuntu + +on: + push: + branches: [ "main" ] + pull_request: + branches: [ "main" ] + +jobs: + build: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v3 + - name: Build + run: swift build -v + - name: Run tests + run: swift test -v diff --git a/.github/workflows/windows.yml b/.github/workflows/windows.yml new file mode 100644 index 0000000..44a5d46 --- /dev/null +++ b/.github/workflows/windows.yml @@ -0,0 +1,20 @@ +name: Windows + +on: + push: + branches: [ "main" ] + pull_request: + branches: [ "main" ] + +jobs: + build: + runs-on: windows-latest + steps: + - uses: compnerd/gha-setup-swift@main + with: + branch: swift-5.6-release + tag: 5.6-RELEASE + + - uses: actions/checkout@v2 + - run: swift build + - run: swift test diff --git a/Sources/Cache/Cache/Cache+subscript.swift b/Sources/Cache/Cache/Cache+subscript.swift index a704a40..043f97a 100644 --- a/Sources/Cache/Cache/Cache+subscript.swift +++ b/Sources/Cache/Cache/Cache+subscript.swift @@ -12,7 +12,7 @@ extension Cache { get(key, as: Value.self) } set(newValue) { - guard let newValue else { + guard let newValue = newValue else { return remove(key) } diff --git a/Sources/Cache/Cache/Cache.swift b/Sources/Cache/Cache/Cache.swift index d6c4d2e..d351aef 100644 --- a/Sources/Cache/Cache/Cache.swift +++ b/Sources/Cache/Cache/Cache.swift @@ -13,13 +13,17 @@ import Foundation let age = cache.get("age") // age is now 100 ``` */ -open class Cache: Cacheable, ObservableObject { +open class Cache: Cacheable { /// Lock to synchronize the access to the cache dictionary. fileprivate var lock: NSLock + #if os(Linux) || os(Windows) + fileprivate var cache: [Key: Value] = [:] + #else /// The actual cache dictionary of key-value pairs. @Published fileprivate var cache: [Key: Value] = [:] + #endif /** Initializes a new `Cache` instance with an optional dictionary of initial key-value pairs. @@ -154,3 +158,7 @@ open class Cache: Cacheable, ObservableObject { return cache.values(ofType: Output.self) } } + +#if !os(Linux) && !os(Windows) +extension Cache: ObservableObject { } +#endif diff --git a/Sources/Cache/Cache/ExpiringCache+subscript.swift b/Sources/Cache/Cache/ExpiringCache+subscript.swift index 7331090..c8438e7 100644 --- a/Sources/Cache/Cache/ExpiringCache+subscript.swift +++ b/Sources/Cache/Cache/ExpiringCache+subscript.swift @@ -12,7 +12,7 @@ extension ExpiringCache { get(key, as: Value.self) } set(newValue) { - guard let newValue else { + guard let newValue = newValue else { return remove(key) } diff --git a/Sources/Cache/Cache/PersistableCache.swift b/Sources/Cache/Cache/PersistableCache.swift index ea25363..0beca3a 100644 --- a/Sources/Cache/Cache/PersistableCache.swift +++ b/Sources/Cache/Cache/PersistableCache.swift @@ -1,3 +1,4 @@ +#if !os(Linux) && !os(Windows) import Foundation /** @@ -142,3 +143,4 @@ private extension URL { return appending(path: name) } } +#endif diff --git a/Sources/Cache/JSON/JSON+subscript.swift b/Sources/Cache/JSON/JSON+subscript.swift index de064b4..8d4b2eb 100644 --- a/Sources/Cache/JSON/JSON+subscript.swift +++ b/Sources/Cache/JSON/JSON+subscript.swift @@ -12,7 +12,7 @@ extension JSON { get(key, as: Any.self) } set(newValue) { - guard let newValue else { + guard let newValue = newValue else { return remove(key) } diff --git a/Sources/Cache/PropertyWrappers/Cached.swift b/Sources/Cache/PropertyWrappers/Cached.swift index e69d11b..6ea062b 100644 --- a/Sources/Cache/PropertyWrappers/Cached.swift +++ b/Sources/Cache/PropertyWrappers/Cached.swift @@ -42,6 +42,8 @@ } } + + #if !os(Windows) /** Initializes a new instance of the `Cached` property wrapper. @@ -59,4 +61,23 @@ self.cache = cache self.defaultValue = defaultValue } + #else + /** + Initializes a new instance of the `Cached` property wrapper. + + - Parameters: + - key: The key associated with the value in the cache. + - cache: The cache instance to retrieve the value from. + - defaultValue: The default value to be used if the value is not present in the cache. + */ + public init( + key: Key, + using cache: Cache, + defaultValue: Value + ) { + self.key = key + self.cache = cache + self.defaultValue = defaultValue + } + #endif } diff --git a/Sources/Cache/PropertyWrappers/OptionallyCached.swift b/Sources/Cache/PropertyWrappers/OptionallyCached.swift index ee6c8ae..d53893e 100644 --- a/Sources/Cache/PropertyWrappers/OptionallyCached.swift +++ b/Sources/Cache/PropertyWrappers/OptionallyCached.swift @@ -38,7 +38,7 @@ cache.get(key, as: Value.self) } set { - guard let newValue else { + guard let newValue = newValue else { return cache.remove(key) } @@ -46,9 +46,10 @@ } } + #if !os(Windows) /** Initializes a new instance of the `OptionallyCached` property wrapper. - + - Parameters: - key: The key associated with the value in the cache. - cache: The cache instance to retrieve the value from. The default is `Global.cache`. @@ -60,4 +61,20 @@ self.key = key self.cache = cache } + #else + /** + Initializes a new instance of the `OptionallyCached` property wrapper. + + - Parameters: + - key: The key associated with the value in the cache. + - cache: The cache instance to retrieve the value from. + */ + public init( + key: Key, + using cache: Cache + ) { + self.key = key + self.cache = cache + } + #endif } diff --git a/Sources/Cache/PropertyWrappers/Resolved.swift b/Sources/Cache/PropertyWrappers/Resolved.swift index 266b927..ee83f0c 100644 --- a/Sources/Cache/PropertyWrappers/Resolved.swift +++ b/Sources/Cache/PropertyWrappers/Resolved.swift @@ -32,9 +32,12 @@ } } + + + #if !os(Windows) /** Initializes a new instance of the `Resolved` property wrapper. - + - Parameters: - key: The key associated with the dependency in the cache. - cache: The `RequiredKeysCache` instance to resolve the dependency from. The default is `Global.dependencies`. @@ -45,7 +48,25 @@ ) { self.key = key self.cache = cache - + _ = self.cache.requiredKeys.insert(key) } + #else + /** + Initializes a new instance of the `Resolved` property wrapper. + + - Parameters: + - key: The key associated with the dependency in the cache. + - cache: The `RequiredKeysCache` instance to resolve the dependency from. The default is `Global.dependencies`. + */ + public init( + key: Key, + using cache: RequiredKeysCache + ) { + self.key = key + self.cache = cache + + _ = self.cache.requiredKeys.insert(key) + } + #endif } diff --git a/Tests/CacheTests/ExampleTests.swift b/Tests/CacheTests/ExampleTests.swift index e68a6a4..b233a22 100644 --- a/Tests/CacheTests/ExampleTests.swift +++ b/Tests/CacheTests/ExampleTests.swift @@ -1,3 +1,4 @@ +#if canImport(SwiftUI) import SwiftUI import XCTest @testable import Cache @@ -58,3 +59,4 @@ final class ExampleTests: XCTestCase { _ = ExampleView().body } } +#endif diff --git a/Tests/CacheTests/ExpiringCacheTests.swift b/Tests/CacheTests/ExpiringCacheTests.swift index 9107bad..f942736 100644 --- a/Tests/CacheTests/ExpiringCacheTests.swift +++ b/Tests/CacheTests/ExpiringCacheTests.swift @@ -344,6 +344,7 @@ final class ExpiringCacheTests: XCTestCase { } } + #if !os(Windows) func testExpiration_secondSuccess() { enum Key { case text @@ -375,4 +376,5 @@ final class ExpiringCacheTests: XCTestCase { XCTAssertNotNil(cache.get(.text)) } + #endif } diff --git a/Tests/CacheTests/PersistableCacheTests.swift b/Tests/CacheTests/PersistableCacheTests.swift index a165ed2..127a58f 100644 --- a/Tests/CacheTests/PersistableCacheTests.swift +++ b/Tests/CacheTests/PersistableCacheTests.swift @@ -1,3 +1,4 @@ +#if !os(Linux) && !os(Windows) import XCTest @testable import Cache @@ -117,3 +118,4 @@ final class PersistableCacheTests: XCTestCase { ) } } +#endif