Skip to content

Commit 3ac6547

Browse files
committed
Shift platform detection to utility
1 parent baf7033 commit 3ac6547

File tree

2 files changed

+49
-20
lines changed

2 files changed

+49
-20
lines changed

Sources/Build/misc.swift

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -185,29 +185,23 @@ extension SystemPackageProvider {
185185
}
186186
}
187187

188-
static func providerForCurrentPlatform(providers: [SystemPackageProvider]) -> SystemPackageProvider? {
189-
guard let uname = try? popen(["uname"]).chomp().lowercased() else { return nil }
190-
switch uname {
191-
case "darwin":
192-
for provider in providers {
193-
if case .Brew = provider {
194-
return provider
195-
}
188+
var isAvailable: Bool {
189+
guard let platform = Platform.currentPlatform() else { return false }
190+
switch self {
191+
case .Brew(_):
192+
if case .Darwin = platform {
193+
return true
196194
}
197-
case "linux":
198-
if "/etc/debian_version".isFile {
199-
for provider in providers {
200-
if case .Apt = provider {
201-
return provider
202-
}
203-
}
195+
case .Apt(_):
196+
if case .Linux(.Debian) = platform {
197+
return true
204198
}
205-
break
206-
207-
default:
208-
return nil
209199
}
210-
return nil
200+
return false
201+
}
202+
203+
static func providerForCurrentPlatform(providers: [SystemPackageProvider]) -> SystemPackageProvider? {
204+
return providers.filter{ $0.isAvailable }.first
211205
}
212206
}
213207

Sources/Utility/Platform.swift

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
This source file is part of the Swift.org open source project
3+
4+
Copyright 2015 - 2016 Apple Inc. and the Swift project authors
5+
Licensed under Apache License v2.0 with Runtime Library Exception
6+
7+
See http://swift.org/LICENSE.txt for license information
8+
See http://swift.org/CONTRIBUTORS.txt for Swift project authors
9+
*/
10+
11+
import func POSIX.popen
12+
13+
public enum Platform {
14+
case Darwin
15+
case Linux(LinuxFlavor)
16+
17+
public enum LinuxFlavor {
18+
case Debian
19+
}
20+
21+
public static func currentPlatform() -> Platform? {
22+
guard let uname = try? popen(["uname"]).chomp().lowercased() else { return nil }
23+
switch uname {
24+
case "darwin":
25+
return .Darwin
26+
case "linux":
27+
if "/etc/debian_version".isFile {
28+
return .Linux(.Debian)
29+
}
30+
default:
31+
return nil
32+
}
33+
return nil
34+
}
35+
}

0 commit comments

Comments
 (0)