-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHelpers.cs
52 lines (42 loc) · 1.69 KB
/
Helpers.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
using System.Diagnostics.CodeAnalysis;
namespace ConsoleHelpers;
public static class Helpers {
public static readonly CultureInfo cultureInfoEnUs = new("en-US", false);
public static readonly string? debugEnv = Environment.GetEnvironmentVariable("Base16384_Net_Debug");
public static readonly bool DEBUG = !string.IsNullOrWhiteSpace(debugEnv) && debugEnv.ToLower(cultureInfoEnUs) is not "false" and not "0";
public static int PrintErrorMessage(string? message, string? debugMessage = null, int exitCode = 0) {
if (message is not null) {
Console.WriteLine(message);
}
if (DEBUG && !string.IsNullOrWhiteSpace(debugMessage)) {
Console.Error.WriteLine(debugMessage);
}
return exitCode;
}
public static int PrintException(string? message, [NotNull] Exception e, int exitCode = 0) =>
PrintErrorMessage(message, e.ToString(), exitCode);
public static int Execute<TIn, TOut>(Func<TIn, TOut, long> func, TIn input, TOut output, string? inputName, string? outputName, bool isStdOut = false) {
if (!isStdOut) {
Console.Write($"{inputName} -> {outputName} ... ");
}
try {
_ = func(input, output);
} catch (Exception e) {
return PrintException(isStdOut ? null : "Failed.", e, 4);
}
if (!isStdOut) {
Console.WriteLine("Done.");
}
return 0;
}
public static int WriteToStdOut<T>(Func<T, Stream, long> func, T input) {
try {
using var stdout = Console.OpenStandardOutput();
return Execute(func, input, stdout, null, null, true);
} catch (Exception e) {
return PrintException(null, e, 3);
}
}
public static int WriteToFile<T>(Func<T, FileInfo, long> func, T input, FileInfo output, string inputName) =>
Execute(func, input, output, inputName, output.Name);
}