@@ -12,44 +12,59 @@ import TSCBasic
12
12
import Foundation
13
13
14
14
/// Recognized Platform types.
15
- public enum Platform {
15
+ public enum Platform : Equatable {
16
16
case android
17
17
case darwin
18
18
case linux( LinuxFlavor )
19
19
20
20
/// Recognized flavors of linux.
21
- public enum LinuxFlavor {
21
+ public enum LinuxFlavor : Equatable {
22
22
case debian
23
23
case fedora
24
24
}
25
25
26
26
/// Lazily checked current platform.
27
- public static var currentPlatform = Platform . findCurrentPlatform ( )
27
+ public static var currentPlatform = Platform . _findCurrentPlatform ( localFileSystem )
28
28
/// Attempt to match `uname` with recognized platforms.
29
- private static func findCurrentPlatform ( ) -> Platform ? {
29
+ public static func _findCurrentPlatform ( _ fs : FileSystem ) -> Platform ? {
30
30
guard let uname = try ? Process . checkNonZeroExit ( args: " uname " ) . spm_chomp ( ) . lowercased ( ) else { return nil }
31
31
switch uname {
32
32
case " darwin " :
33
33
return . darwin
34
34
case " linux " :
35
- if localFileSystem. isFile ( AbsolutePath ( " /etc/debian_version " ) ) {
36
- return . linux( . debian)
37
- }
38
- if localFileSystem. isFile ( AbsolutePath ( " /system/bin/toolbox " ) ) ||
39
- localFileSystem. isFile ( AbsolutePath ( " /system/bin/toybox " ) ) {
40
- return . android
41
- }
42
- if localFileSystem. isFile ( AbsolutePath ( " /etc/redhat-release " ) ) ||
43
- localFileSystem. isFile ( AbsolutePath ( " /etc/centos-release " ) ) ||
44
- localFileSystem. isFile ( AbsolutePath ( " /etc/fedora-release " ) ) {
45
- return . linux( . fedora)
46
- }
35
+ return Platform . _findCurrentPlatformLinux ( fs)
47
36
default :
48
37
return nil
49
38
}
39
+ }
40
+
41
+ public static func _findCurrentPlatformLinux( _ fs: FileSystem ) -> Platform ? {
42
+ if fs. isFile ( AbsolutePath ( " /etc/debian_version " ) ) {
43
+ return . linux( . debian)
44
+ }
45
+ if fs. isFile ( AbsolutePath ( " /system/bin/toolbox " ) ) ||
46
+ fs. isFile ( AbsolutePath ( " /system/bin/toybox " ) ) {
47
+ return . android
48
+ }
49
+ if fs. isFile ( AbsolutePath ( " /etc/redhat-release " ) ) ||
50
+ fs. isFile ( AbsolutePath ( " /etc/centos-release " ) ) ||
51
+ fs. isFile ( AbsolutePath ( " /etc/fedora-release " ) ) ||
52
+ Platform . isAmazonLinux2 ( fs) {
53
+ return . linux( . fedora)
54
+ }
55
+
50
56
return nil
51
57
}
52
58
59
+ private static func isAmazonLinux2( _ fs: FileSystem ) -> Bool {
60
+ do {
61
+ let release = try fs. readFileContents ( AbsolutePath ( " /etc/system-release " ) ) . cString
62
+ return release. hasPrefix ( " Amazon Linux release 2 " )
63
+ } catch {
64
+ return false
65
+ }
66
+ }
67
+
53
68
/// Returns the cache directories used in Darwin.
54
69
public static func darwinCacheDirectories( ) -> [ AbsolutePath ] {
55
70
if let value = Platform . _darwinCacheDirectories {
0 commit comments