Skip to content

Commit

Permalink
Add ubuntu and windows CI actions (#8)
Browse files Browse the repository at this point in the history
* Add ubuntu and windows CI actions

* Update file spacing

* Update windows.yml and fix multiple platform cache

* Use correct boolean operator

* Limit PersitableCache to apple platforms

* Limit ExampleTests

* Limit PersistableCacheTest

* Update code for Windows support

* Make Windows PropertyWrapper unique inits

* Limit some tests in ExpiringCacheTest from windows
  • Loading branch information
0xLeif authored Jun 29, 2023
1 parent 936865e commit a3f81b1
Show file tree
Hide file tree
Showing 14 changed files with 123 additions and 10 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/CI.yml → .github/workflows/macOS.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: CI
name: macOS

on:
push:
Expand All @@ -8,7 +8,7 @@ on:

jobs:
build:
runs-on: macos-12
runs-on: macos-latest

steps:
- uses: actions/checkout@v3
Expand Down
18 changes: 18 additions & 0 deletions .github/workflows/ubuntu.yml
Original file line number Diff line number Diff line change
@@ -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
20 changes: 20 additions & 0 deletions .github/workflows/windows.yml
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion Sources/Cache/Cache/Cache+subscript.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
10 changes: 9 additions & 1 deletion Sources/Cache/Cache/Cache.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,17 @@ import Foundation
let age = cache.get("age") // age is now 100
```
*/
open class Cache<Key: Hashable, Value>: Cacheable, ObservableObject {
open class Cache<Key: Hashable, Value>: 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.
Expand Down Expand Up @@ -154,3 +158,7 @@ open class Cache<Key: Hashable, Value>: Cacheable, ObservableObject {
return cache.values(ofType: Output.self)
}
}

#if !os(Linux) && !os(Windows)
extension Cache: ObservableObject { }
#endif
2 changes: 1 addition & 1 deletion Sources/Cache/Cache/ExpiringCache+subscript.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
2 changes: 2 additions & 0 deletions Sources/Cache/Cache/PersistableCache.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#if !os(Linux) && !os(Windows)
import Foundation

/**
Expand Down Expand Up @@ -142,3 +143,4 @@ private extension URL {
return appending(path: name)
}
}
#endif
2 changes: 1 addition & 1 deletion Sources/Cache/JSON/JSON+subscript.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
21 changes: 21 additions & 0 deletions Sources/Cache/PropertyWrappers/Cached.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
}
}


#if !os(Windows)
/**
Initializes a new instance of the `Cached` property wrapper.

Expand All @@ -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<Key, Any>,
defaultValue: Value
) {
self.key = key
self.cache = cache
self.defaultValue = defaultValue
}
#endif
}
21 changes: 19 additions & 2 deletions Sources/Cache/PropertyWrappers/OptionallyCached.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,18 @@
cache.get(key, as: Value.self)
}
set {
guard let newValue else {
guard let newValue = newValue else {
return cache.remove(key)
}

cache.set(value: newValue, forKey: key)
}
}

#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`.
Expand All @@ -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<Key, Any>
) {
self.key = key
self.cache = cache
}
#endif
}
25 changes: 23 additions & 2 deletions Sources/Cache/PropertyWrappers/Resolved.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand All @@ -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<Key, Any>
) {
self.key = key
self.cache = cache

_ = self.cache.requiredKeys.insert(key)
}
#endif
}
2 changes: 2 additions & 0 deletions Tests/CacheTests/ExampleTests.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#if canImport(SwiftUI)
import SwiftUI
import XCTest
@testable import Cache
Expand Down Expand Up @@ -58,3 +59,4 @@ final class ExampleTests: XCTestCase {
_ = ExampleView().body
}
}
#endif
2 changes: 2 additions & 0 deletions Tests/CacheTests/ExpiringCacheTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,7 @@ final class ExpiringCacheTests: XCTestCase {
}
}

#if !os(Windows)
func testExpiration_secondSuccess() {
enum Key {
case text
Expand Down Expand Up @@ -375,4 +376,5 @@ final class ExpiringCacheTests: XCTestCase {

XCTAssertNotNil(cache.get(.text))
}
#endif
}
2 changes: 2 additions & 0 deletions Tests/CacheTests/PersistableCacheTests.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#if !os(Linux) && !os(Windows)
import XCTest
@testable import Cache

Expand Down Expand Up @@ -117,3 +118,4 @@ final class PersistableCacheTests: XCTestCase {
)
}
}
#endif

0 comments on commit a3f81b1

Please sign in to comment.