-
Notifications
You must be signed in to change notification settings - Fork 0
/
Checks.cs
166 lines (150 loc) · 5.98 KB
/
Checks.cs
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
// Copyright (C) 2023 NexusKrop & contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
namespace NexusKrop.IceCube.Exceptions;
using System;
using System.Diagnostics;
using System.IO;
#if NET6_0_OR_GREATER
using System.Runtime.CompilerServices;
#endif
/// <summary>
/// Provides methods to enforce certain conditions.
/// </summary>
public static class Checks
{
/// <summary>
/// Throws <see cref="ArgumentException"/> if the specified <paramref name="process"/> is <see langword="null"/> or has exited.
/// </summary>
/// <param name="process">The process to check.</param>
/// <param name="argName">The name of the argument. On .NET 6+, the name of the argument that was checked for is automatically determined by runtime
/// and thus should not be passed explicitly.</param>
/// <returns></returns>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="ArgumentException"></exception>
#if NET6_0_OR_GREATER
public static Process ProcessRunning(Process process, [CallerArgumentExpression(nameof(process))] string argName = "???")
#else
public static Process ProcessRunning(Process process, string argName)
#endif
{
if (process == null) throw new ArgumentNullException(argName);
if (process.HasExited) throw new ArgumentException(ExceptionHelperResources.ProcessExited, argName);
return process;
}
/// <summary>
/// Throws <see cref="ArgumentNullException"/> if the specified <paramref name="value"/> is <see langword="null"/>.
/// </summary>
/// <typeparam name="T">The type to check.</typeparam>
/// <param name="value">The value to check.</param>
/// <param name="argName">The name of the argument.</param>
/// <returns>The value.</returns>
/// <exception cref="ArgumentNullException"></exception>
#if NET6_0_OR_GREATER
public static T ArgNotNull<T>(T value, [CallerArgumentExpression(nameof(value))] string argName = "???")
#else
public static T ArgNotNull<T>(T value, string argName)
#endif
{
if (value == null) throw Fails.ArgumentNull(argName);
return value;
}
/// <summary>
/// Throws <see cref="ArgumentException"/> if the <paramref name="value"/> is consisted sole of whitespaces,
/// or <see cref="ArgumentNullException"/> if the <paramref name="value"/> is <see langword="null"/>.
/// </summary>
/// <param name="value">The value to check.</param>
/// <param name="argName">The name of the argument.</param>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="ArgumentException"></exception>
#if NET6_0_OR_GREATER
public static void ArgNotNullOrWhitespace(string? value, [CallerArgumentExpression(nameof(value))] string? argName = "???")
#else
public static void ArgNotNullOrWhitespace(string value, string argName)
#endif
{
if (value == null) throw new ArgumentNullException(argName);
if (string.IsNullOrWhiteSpace(value))
{
throw new ArgumentException(string.Format(ExceptionHelperResources.StringWhitespace, argName), argName);
}
}
/// <summary>
/// Throws <see cref="FileNotFoundException"/> if the specified file does not exist.
/// </summary>
/// <param name="fileName">The name of the file.</param>
/// <exception cref="FileNotFoundException">The file specified does not exist.</exception>
public static void FileExists(string fileName)
{
if (!File.Exists(fileName))
{
throw Fails.FileNotFound(fileName);
}
}
/// <summary>
/// Throws <see cref="DirectoryNotFoundException"/> if the specified directory does not exist.
/// </summary>
/// <param name="directoryName">The name of the directory.</param>
/// <exception cref="DirectoryNotFoundException">The directory specified does not exist.</exception>
public static void DirectoryExists(string directoryName)
{
if (!Directory.Exists(directoryName))
{
throw Fails.DirectoryNotFound(directoryName);
}
}
/// <summary>
/// Throws <see cref="PlatformNotSupportedException"/> if the current platform is not Microsoft Windows.
/// </summary>
public static void OnWindows()
{
#if NET6_0_OR_GREATER
if (!OperatingSystem.IsWindows())
{
throw Fails.ExceptedPlatform("windows");
}
#else
if (Environment.OSVersion.Platform != PlatformID.Win32NT)
{
throw Fails.ExceptedPlatform(PlatformID.Win32NT);
}
#endif
}
#if NET6_0_OR_GREATER
/// <summary>
/// Throws <see cref="PlatformNotSupportedException"/> if the current platform is not Microsoft Windows
/// with version at least specified in <paramref name="major"/>.
/// </summary>
/// <param name="major">The version.</param>
/// <exception cref="PlatformNotSupportedException">The current platform is not Microsoft Windows, or the version is below the required.</exception>
public static void OnWindows(int major)
{
OnWindows();
if (!OperatingSystem.IsWindowsVersionAtLeast(major))
{
throw Fails.ExceptedPlatform("windows", major);
}
}
/// <summary>
/// Throws <see cref="PlatformNotSupportedException"/> if the current platform is not GNU/Linux or any other
/// Linux that is supported by .NET.
/// </summary>
public static void OnLinux()
{
if (!OperatingSystem.IsLinux())
{
throw Fails.ExceptedPlatform("linux");
}
}
#endif
}