1
- // <copyright file="ResourceUtilities.cs" company="WebDriver Committers">
1
+ // <copyright file="ResourceUtilities.cs" company="WebDriver Committers">
2
2
// Licensed to the Software Freedom Conservancy (SFC) under one
3
3
// or more contributor license agreements. See the NOTICE file
4
4
// distributed with this work for additional information
16
16
// limitations under the License.
17
17
// </copyright>
18
18
19
+ using System ;
19
20
using System . Collections . Generic ;
21
+ using System . Diagnostics ;
20
22
using System . Globalization ;
21
23
using System . IO ;
22
24
using System . Reflection ;
25
+ using System . Runtime . InteropServices ;
23
26
24
27
namespace OpenQA . Selenium . Internal
25
28
{
@@ -28,6 +31,43 @@ namespace OpenQA.Selenium.Internal
28
31
/// </summary>
29
32
public static class ResourceUtilities
30
33
{
34
+ private static string assemblyVersion ;
35
+ private static string platformFamily ;
36
+
37
+ /// <summary>
38
+ /// Gets a string representing the version of the Selenium assembly.
39
+ /// </summary>
40
+ public static string AssemblyVersion
41
+ {
42
+ get
43
+ {
44
+ if ( string . IsNullOrEmpty ( assemblyVersion ) )
45
+ {
46
+ Assembly executingAssembly = Assembly . GetCallingAssembly ( ) ;
47
+ Version versionResource = executingAssembly . GetName ( ) . Version ;
48
+ assemblyVersion = string . Format ( CultureInfo . InvariantCulture , "{0}.{1}.{2}" , versionResource . Major , versionResource . Minor , versionResource . Revision ) ;
49
+ }
50
+
51
+ return assemblyVersion ;
52
+ }
53
+ }
54
+
55
+ /// <summary>
56
+ /// Gets a string representing the platform family on which the Selenium assembly is executing.
57
+ /// </summary>
58
+ public static string PlatformFamily
59
+ {
60
+ get
61
+ {
62
+ if ( string . IsNullOrEmpty ( platformFamily ) )
63
+ {
64
+ platformFamily = GetPlatformString ( ) ;
65
+ }
66
+
67
+ return platformFamily ;
68
+ }
69
+ }
70
+
31
71
/// <summary>
32
72
/// Gets a <see cref="Stream"/> that contains the resource to use.
33
73
/// </summary>
@@ -89,5 +129,56 @@ public static bool IsValidResourceName(string resourceId)
89
129
List < string > resourceNames = new List < string > ( executingAssembly . GetManifestResourceNames ( ) ) ;
90
130
return resourceNames . Contains ( resourceId ) ;
91
131
}
132
+
133
+ private static string GetPlatformString ( )
134
+ {
135
+ string platformName = "unknown" ;
136
+ #if NETSTANDARD2_0 || NETCOREAPP2_0
137
+ if ( RuntimeInformation . IsOSPlatform ( OSPlatform . Linux ) )
138
+ {
139
+ platformName = "windows" ;
140
+ }
141
+ else if ( RuntimeInformation . IsOSPlatform ( OSPlatform . Linux ) )
142
+ {
143
+ platformName = "linux" ;
144
+ }
145
+ else if ( RuntimeInformation . IsOSPlatform ( OSPlatform . OSX ) )
146
+ {
147
+ platformName = "mac" ;
148
+ }
149
+ #else
150
+ // Unfortunately, detecting the currently running platform isn't as
151
+ // straightforward as you might hope.
152
+ // See: http://mono.wikia.com/wiki/Detecting_the_execution_platform
153
+ // and https://msdn.microsoft.com/en-us/library/3a8hyw88(v=vs.110).aspx
154
+ const int PlatformMonoUnixValue = 128 ;
155
+ PlatformID platformId = Environment . OSVersion . Platform ;
156
+ if ( platformId == PlatformID . Unix || platformId == PlatformID . MacOSX || ( int ) platformId == PlatformMonoUnixValue )
157
+ {
158
+ using ( Process unameProcess = new Process ( ) )
159
+ {
160
+ unameProcess . StartInfo . FileName = "uname" ;
161
+ unameProcess . StartInfo . UseShellExecute = false ;
162
+ unameProcess . StartInfo . RedirectStandardOutput = true ;
163
+ unameProcess . Start ( ) ;
164
+ unameProcess . WaitForExit ( 1000 ) ;
165
+ string output = unameProcess . StandardOutput . ReadToEnd ( ) ;
166
+ if ( output . ToLowerInvariant ( ) . StartsWith ( "darwin" ) )
167
+ {
168
+ platformName = "mac" ;
169
+ }
170
+ else
171
+ {
172
+ platformName = "linux" ;
173
+ }
174
+ }
175
+ }
176
+ else if ( platformId == PlatformID . Win32NT || platformId == PlatformID . Win32S || platformId == PlatformID . Win32Windows || platformId == PlatformID . WinCE )
177
+ {
178
+ platformName = "windows" ;
179
+ }
180
+ #endif
181
+ return platformName ;
182
+ }
92
183
}
93
184
}
0 commit comments