forked from Taritsyn/JavaScriptEngineSwitcher
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtils.cs
More file actions
206 lines (180 loc) · 5.47 KB
/
Utils.cs
File metadata and controls
206 lines (180 loc) · 5.47 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
using System;
#if NET40
using System.Diagnostics;
#endif
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text;
using JavaScriptEngineSwitcher.Core.Resources;
namespace JavaScriptEngineSwitcher.Core.Utilities
{
public static class Utils
{
/// <summary>
/// Flag indicating whether the current runtime is Mono
/// </summary>
private static readonly bool _isMonoRuntime;
/// <summary>
/// Static constructor
/// </summary>
static Utils()
{
_isMonoRuntime = Type.GetType("Mono.Runtime") is not null;
}
/// <summary>
/// Determines whether the current runtime is Mono
/// </summary>
/// <returns><c>true</c> if the runtime is Mono; otherwise, <c>false</c></returns>
public static bool IsMonoRuntime()
{
return _isMonoRuntime;
}
/// <summary>
/// Determines whether the current process is a 64-bit process
/// </summary>
/// <returns><c>true</c> if the process is 64-bit; otherwise, <c>false</c></returns>
[MethodImpl((MethodImplOptions)256 /* AggressiveInlining */)]
public static bool Is64BitProcess()
{
#if NETSTANDARD1_3
bool is64Bit = IntPtr.Size == 8;
#else
bool is64Bit = Environment.Is64BitProcess;
#endif
return is64Bit;
}
/// <summary>
/// Gets a content of the embedded resource as string
/// </summary>
/// <param name="resourceName">The case-sensitive resource name without the namespace of the specified type</param>
/// <param name="type">The type, that determines the assembly and whose namespace is used to scope
/// the resource name</param>
/// <returns>Content of the embedded resource as string</returns>
public static string GetResourceAsString(string resourceName, Type type)
{
if (resourceName is null)
{
throw new ArgumentNullException(
nameof(resourceName),
string.Format(Strings.Common_ArgumentIsNull, nameof(resourceName))
);
}
if (type is null)
{
throw new ArgumentNullException(
nameof(type),
string.Format(Strings.Common_ArgumentIsNull, nameof(type))
);
}
if (string.IsNullOrWhiteSpace(resourceName))
{
throw new ArgumentException(
string.Format(Strings.Common_ArgumentIsEmpty, nameof(resourceName)),
nameof(resourceName)
);
}
#if NET40
Assembly assembly = type.Assembly;
#else
Assembly assembly = type.GetTypeInfo().Assembly;
#endif
string nameSpace = type.Namespace;
string resourceFullName = nameSpace is not null ? nameSpace + "." + resourceName : resourceName;
return InnerGetResourceAsString(resourceFullName, assembly);
}
/// <summary>
/// Gets a content of the embedded resource as string
/// </summary>
/// <param name="resourceName">The case-sensitive resource name</param>
/// <param name="assembly">The assembly, which contains the embedded resource</param>
/// <returns>Content of the embedded resource as string</returns>
public static string GetResourceAsString(string resourceName, Assembly assembly)
{
if (resourceName is null)
{
throw new ArgumentNullException(
nameof(resourceName),
string.Format(Strings.Common_ArgumentIsNull, nameof(resourceName))
);
}
if (assembly is null)
{
throw new ArgumentNullException(
nameof(assembly),
string.Format(Strings.Common_ArgumentIsNull, nameof(assembly))
);
}
if (string.IsNullOrWhiteSpace(resourceName))
{
throw new ArgumentException(
string.Format(Strings.Common_ArgumentIsEmpty, nameof(resourceName)),
nameof(resourceName)
);
}
return InnerGetResourceAsString(resourceName, assembly);
}
private static string InnerGetResourceAsString(string resourceName, Assembly assembly)
{
using (Stream stream = assembly.GetManifestResourceStream(resourceName))
{
if (stream is null)
{
throw new NullReferenceException(
string.Format(Strings.Common_ResourceIsNull, resourceName)
);
}
using (var reader = new StreamReader(stream))
{
return reader.ReadToEnd();
}
}
}
/// <summary>
/// Gets a text content of the specified file
/// </summary>
/// <param name="path">File path</param>
/// <param name="encoding">Content encoding</param>
/// <returns>Text content</returns>
public static string GetFileTextContent(string path, Encoding encoding = null)
{
if (!File.Exists(path))
{
throw new FileNotFoundException(
string.Format(Strings.Common_FileNotExist, path),
path
);
}
string content;
using (var stream = File.OpenRead(path))
using (var reader = new StreamReader(stream, encoding ?? Encoding.UTF8))
{
content = reader.ReadToEnd();
}
return content;
}
/// <summary>
/// Converts a value of source enumeration type to value of destination enumeration type
/// </summary>
/// <typeparam name="TSource">Source enumeration type</typeparam>
/// <typeparam name="TDest">Destination enumeration type</typeparam>
/// <param name="value">Value of source enumeration type</param>
/// <returns>Value of destination enumeration type</returns>
public static TDest GetEnumFromOtherEnum<TSource, TDest>(TSource value)
{
string name = value.ToString();
var destEnumValues = (TDest[])Enum.GetValues(typeof(TDest));
foreach (var destEnum in destEnumValues)
{
if (string.Equals(destEnum.ToString(), name, StringComparison.OrdinalIgnoreCase))
{
return destEnum;
}
}
throw new InvalidCastException(
string.Format(Strings.Common_EnumValueConversionFailed,
name, typeof(TSource), typeof(TDest))
);
}
}
}