-
Notifications
You must be signed in to change notification settings - Fork 40
/
Platform.fs
69 lines (59 loc) · 2.24 KB
/
Platform.fs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
namespace Microsoft.FSharpLu.Platform
open System
open Microsoft.FSharpLu.Parsing
/// All known OS platforms
type Platform =
| Windows
| Linux
/// Parse platform from a string
static member Parse (s: String) : Platform =
match Union.tryParse s with
| Some p -> p
| None -> failwithf "Unknown platform: %s" s
/// Gets the current platform
static member Current =
match Environment.OSVersion.Platform with
| PlatformID.Unix -> Platform.Linux
| PlatformID.Win32NT -> Platform.Windows
| _ -> failwithf "platform not supported %A" Environment.OSVersion.Platform
/// Generic type used to define a platform specific value that only gets evaluated when fetched
type PlatformSpecificDelayed<'a> =
{
WindowsEvaluation : unit -> 'a
LinuxEvaluation : unit -> 'a
}
member inline x.Linux = x.LinuxEvaluation()
member inline x.Windows = x.WindowsEvaluation()
member inline x.ForPlatform platform =
match platform with
| Linux -> x.Linux
| Windows -> x.Windows
member inline x.ForCurrentPlatform =
x.ForPlatform Platform.Current
/// Generic type used to define a platform specific value
type PlatformSpecific<'a> =
{
Windows : 'a
Linux : 'a
}
member inline x.ForPlatform platform =
match platform with
| Linux -> x.Linux
| Windows -> x.Windows
member inline x.ForCurrentPlatform =
x.ForPlatform Platform.Current
type Environment =
/// Return the name assigned to the current machine
/// Note: on Linux, this value is only initialized correctly after network
/// initialization has completed. We therefore define this value
/// as a static property instead of a constant.
static member MachineName
with get () =
System.Environment.MachineName
/// Path to the OS drive root: / on Linux, C:\ on Windows.
static member OSRootPath
with get () =
{
LinuxEvaluation = fun () -> "/"
WindowsEvaluation = fun () -> System.IO.Path.GetPathRoot(System.Environment.SystemDirectory)
}