Skip to content

Commit 6c127d0

Browse files
Support for non-Apple platforms (#27)
* Conditional import of Combine and SwiftUI * Conditionally import swift ui * Added #if canImport(os) to ReducerInstrumentation.swift * More changes for supporting non-Apple platforms. * Add CI job for Linux * Added Linux test discovery * Added LinuxMain.swift for testing * Add back debugCaseOutput for Linux * Exclude Identified and IdentifiedArray for Linux * Exclude use of IdenfitiedArray for Linux * Workaround for DipatchQueue is not Equatable in Linux * Allow tests to build on Linux. * Another attempt to limit tests on Linux * Only include ComposableArchitecture for Android OS Co-authored-by: Andrei Vidrasco <[email protected]>
1 parent 191122e commit 6c127d0

20 files changed

+97
-3
lines changed

.github/workflows/ci.yml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,27 @@ jobs:
2222
run: sudo xcode-select -s /Applications/Xcode_${{ matrix.xcode }}.app
2323
- name: Run tests
2424
run: make test
25+
26+
swiftpm-linux:
27+
name: SwiftPM Linux
28+
runs-on: ubuntu-18.04
29+
steps:
30+
- name: Install Swift
31+
run: |
32+
eval "$(curl -sL https://swiftenv.fuller.li/install.sh)"
33+
- name: Checkout
34+
uses: actions/checkout@v2
35+
- name: Recover cached dependencies
36+
uses: actions/cache@v1
37+
id: dependency-cache
38+
with:
39+
path: .build
40+
key: swiftpm-linux-${{ hashFiles('Package.resolved') }}
41+
- name: Pull dependencies
42+
if: steps.dependency-cache.outputs.cache-hit != 'true'
43+
run: |
44+
swift package resolve
45+
- name: Test via SwiftPM
46+
run: |
47+
swift --version
48+
swift test --enable-test-discovery

Package.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,8 @@ let package = Package(
6969
),
7070
]
7171
)
72+
73+
#if os(Linux) || os(Android)
74+
package.products.removeAll(where: { $0.name != "ComposableArchitecture" })
75+
package.targets.removeAll(where: { $0.name != "ComposableArchitecture" && $0.name != "ComposableArchitectureTests" })
76+
#endif

Sources/ComposableArchitecture/Debugging/ReducerInstrumentation.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#if canImport(os)
12
import os.signpost
23

34
extension Reducer {
@@ -82,6 +83,7 @@ extension Effect where Error == Never {
8283
})
8384
}
8485
}
86+
#endif
8587

8688
func debugCaseOutput(_ value: Any) -> String {
8789
func debugCaseOutputHelp(_ value: Any) -> String {

Sources/ComposableArchitecture/Internal/Debug.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import Foundation
22

3+
#if os(Linux) || os(Android)
4+
import let CDispatch.NSEC_PER_USEC
5+
import let CDispatch.NSEC_PER_SEC
6+
#endif
7+
38
func debugOutput(_ value: Any, indent: Int = 0) -> String {
49
var visitedItems: Set<ObjectIdentifier> = []
510

@@ -173,6 +178,18 @@ private let dateFormatter: ISO8601DateFormatter = {
173178
}()
174179

175180
extension DispatchQueue: CustomDebugOutputConvertible {
181+
#if os(Linux) || os(Android)
182+
public var debugOutput: String {
183+
switch self.label {
184+
case "com.apple.main-thread": return "DispatchQueue.main"
185+
case "com.apple.root.default-qos": return "DispatchQueue.global()"
186+
case _ where self.label == "com.apple.root.\(self.qos.qosClass)-qos":
187+
return "DispatchQueue.global(qos: .\(self.qos.qosClass))"
188+
default:
189+
return "DispatchQueue(label: \(self.label.debugDescription), qos: .\(self.qos.qosClass))"
190+
}
191+
}
192+
#else
176193
public var debugOutput: String {
177194
switch (self, self.label) {
178195
case (.main, _): return "DispatchQueue.main"
@@ -183,6 +200,7 @@ extension DispatchQueue: CustomDebugOutputConvertible {
183200
return "DispatchQueue(label: \(self.label.debugDescription), qos: .\(self.qos.qosClass))"
184201
}
185202
}
203+
#endif
186204
}
187205

188206
extension Effect: CustomDebugOutputConvertible {

Sources/ComposableArchitecture/Internal/LocalizedStringKey.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#if canImport(SwiftUI)
12
import SwiftUI
23

34
@available(iOS 13.0, macOS 10.15, macCatalyst 13, tvOS 13.0, watchOS 6.0, *)
@@ -29,3 +30,4 @@ extension LocalizedStringKey: CustomDebugOutputConvertible {
2930
self.formatted().debugDescription
3031
}
3132
}
33+
#endif

Sources/ComposableArchitecture/Internal/Locking.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import Foundation
22

3+
#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
34
extension UnsafeMutablePointer where Pointee == os_unfair_lock_s {
45
@inlinable @discardableResult
56
func sync<R>(_ work: () -> R) -> R {
@@ -8,6 +9,7 @@ extension UnsafeMutablePointer where Pointee == os_unfair_lock_s {
89
return work()
910
}
1011
}
12+
#endif
1113

1214
extension NSRecursiveLock {
1315
@inlinable @discardableResult

Sources/ComposableArchitecture/Reducer.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,7 @@ public struct Reducer<State, Action, Environment> {
406406
}
407407
}
408408

409+
#if canImport(SwiftUI)
409410
/// A version of `pullback` that transforms a reducer that works on an element into one that works
410411
/// on an identified array of elements.
411412
///
@@ -487,7 +488,8 @@ public struct Reducer<State, Action, Environment> {
487488
.map { toLocalAction.embed((id, $0)) }
488489
}
489490
}
490-
491+
#endif
492+
491493
/// A version of `pullback` that transforms a reducer that works on an element into one that works
492494
/// on a dictionary of element values.
493495
///

Sources/ComposableArchitecture/SwiftUI/ActionSheet.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#if canImport(SwiftUI)
12
import SwiftUI
23

34
/// A data type that describes the state of an action sheet that can be shown to the user. The
@@ -221,3 +222,4 @@ extension ActionSheetState {
221222
)
222223
}
223224
}
225+
#endif

Sources/ComposableArchitecture/SwiftUI/Alert.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#if canImport(SwiftUI)
12
import SwiftUI
23

34
/// A data type that describes the state of an alert that can be shown to the user. The `Action`
@@ -298,3 +299,4 @@ extension AlertState {
298299
}
299300
}
300301
}
302+
#endif

Sources/ComposableArchitecture/SwiftUI/ForEachStore.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#if canImport(SwiftUI)
12
import SwiftUI
23

34
/// A structure that computes views on demand from a store on a collection of data.
@@ -119,3 +120,4 @@ where Data: Collection, ID: Hashable, Content: View {
119120
self.content()
120121
}
121122
}
123+
#endif

0 commit comments

Comments
 (0)