forked from ServiceStack/ServiceStack.Text
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEnv.cs
More file actions
171 lines (140 loc) · 7.15 KB
/
Env.cs
File metadata and controls
171 lines (140 loc) · 7.15 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
//Copyright (c) ServiceStack, Inc. All Rights Reserved.
//License: https://raw.github.com/ServiceStack/ServiceStack/master/license.txt
using System;
using System.Globalization;
using System.IO;
namespace ServiceStack.Text
{
public static class Env
{
static Env()
{
if (PclExport.Instance == null)
throw new ArgumentException("PclExport.Instance needs to be initialized");
var platformName = PclExport.Instance.PlatformName;
if (platformName != PclExport.Platforms.Uwp)
{
IsMono = AssemblyUtils.FindType("Mono.Runtime") != null;
IsIOS = AssemblyUtils.FindType("MonoTouch.Foundation.NSObject") != null
|| AssemblyUtils.FindType("Foundation.NSObject") != null;
IsAndroid = AssemblyUtils.FindType("Android.Manifest") != null;
try
{
IsOSX = AssemblyUtils.FindType("Mono.AppKit") != null;
#if NET45
IsWindows = !string.IsNullOrEmpty(Environment.GetEnvironmentVariable("windir"));
if (File.Exists(@"/System/Library/CoreServices/SystemVersion.plist"))
IsOSX = true;
string osType = File.Exists(@"/proc/sys/kernel/ostype") ? File.ReadAllText(@"/proc/sys/kernel/ostype") : null;
IsLinux = osType?.IndexOf("Linux", StringComparison.OrdinalIgnoreCase) >= 0;
#endif
}
catch (Exception) {}
}
else
{
IsUWP = true;
}
#if NETSTANDARD2_0
IsNetStandard = true;
try
{
IsLinux = System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.Linux);
IsWindows = System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.Windows);
IsOSX = System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.OSX);
if (!IsIOS && IsOSX && System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription.Contains("Mono"))
{
var runtimeDir = System.Runtime.InteropServices.RuntimeEnvironment.GetRuntimeDirectory();
//iOS detection no longer trustworthy so assuming iOS based on some current heuristics. TODO: improve iOS detection
IsIOS = runtimeDir.StartsWith("/private/var") ||
runtimeDir.Contains("/CoreSimulator/Devices/");
}
}
catch (Exception) {} //throws PlatformNotSupportedException in AWS lambda
IsUnix = IsOSX || IsLinux;
HasMultiplePlatformTargets = true;
#elif NET45
IsNetFramework = true;
var platform = (int)Environment.OSVersion.Platform;
IsUnix = platform == 4 || platform == 6 || platform == 128;
IsLinux = IsUnix;
if (Environment.GetEnvironmentVariable("OS")?.IndexOf("Windows", StringComparison.OrdinalIgnoreCase) >= 0)
IsWindows = true;
#endif
SupportsExpressions = !IsIOS;
SupportsEmit = !IsIOS;
ServerUserAgent = "ServiceStack/" +
ServiceStackVersion + " "
+ platformName
+ (IsMono ? "/Mono" : "/.NET");
VersionString = ServiceStackVersion.ToString(CultureInfo.InvariantCulture);
__releaseDate = new DateTime(2001,01,01);
PclExport.Instance.SupportsEmit = SupportsEmit;
PclExport.Instance.SupportsExpression = SupportsExpressions;
}
public static string VersionString { get; set; }
public static decimal ServiceStackVersion = 5.00m;
public static bool IsLinux { get; set; }
public static bool IsOSX { get; set; }
public static bool IsUnix { get; set; }
public static bool IsWindows { get; set; }
public static bool IsMono { get; set; }
public static bool IsIOS { get; set; }
public static bool IsAndroid { get; set; }
public static bool IsUWP { get; set; }
public static bool IsNetStandard { get; set; }
public static bool IsNetFramework { get; set; }
public static bool SupportsExpressions { get; set; }
public static bool SupportsEmit { get; set; }
public static bool StrictMode { get; set; }
public static string ServerUserAgent { get; set; }
public static bool HasMultiplePlatformTargets { get; set; }
private static readonly DateTime __releaseDate;
public static DateTime GetReleaseDate()
{
return __releaseDate;
}
private static string referenceAssembyPath;
public static string ReferenceAssembyPath
{
get
{
if (!IsMono && referenceAssembyPath == null)
{
var programFilesPath = PclExport.Instance.GetEnvironmentVariable("ProgramFiles(x86)") ?? @"C:\Program Files (x86)";
var netFxReferenceBasePath = programFilesPath + @"\Reference Assemblies\Microsoft\Framework\.NETFramework\";
if ((netFxReferenceBasePath + @"v4.5.2\").DirectoryExists())
referenceAssembyPath = netFxReferenceBasePath + @"v4.5.2\";
else if ((netFxReferenceBasePath + @"v4.5.1\").DirectoryExists())
referenceAssembyPath = netFxReferenceBasePath + @"v4.5.1\";
else if ((netFxReferenceBasePath + @"v4.5\").DirectoryExists())
referenceAssembyPath = netFxReferenceBasePath + @"v4.5\";
else if ((netFxReferenceBasePath + @"v4.0\").DirectoryExists())
referenceAssembyPath = netFxReferenceBasePath + @"v4.0\";
else
{
var v4Dirs = PclExport.Instance.GetDirectoryNames(netFxReferenceBasePath, "v4*");
if (v4Dirs.Length == 0)
{
var winPath = PclExport.Instance.GetEnvironmentVariable("SYSTEMROOT") ?? @"C:\Windows";
var gacPath = winPath + @"\Microsoft.NET\Framework\";
v4Dirs = PclExport.Instance.GetDirectoryNames(gacPath, "v4*");
}
if (v4Dirs.Length > 0)
{
referenceAssembyPath = v4Dirs[v4Dirs.Length - 1] + @"\"; //latest v4
}
else
{
throw new FileNotFoundException(
"Could not infer .NET Reference Assemblies path, e.g '{0}'.\n".Fmt(netFxReferenceBasePath + @"v4.0\") +
"Provide path manually 'Env.ReferenceAssembyPath'.");
}
}
}
return referenceAssembyPath;
}
set => referenceAssembyPath = value;
}
}
}