-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
146 lines (121 loc) · 4.48 KB
/
Program.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
using System.CommandLine;
using System.CommandLine.Parsing;
using System.Text.RegularExpressions;
using PreCompress;
var fileOption = new Option<FileInfo[]?>(
name: "-i",
description: "The file to compress.",
isDefault: true,
parseArgument: result =>
{
if (result.Tokens.Count == 0)
{
result.ErrorMessage = "No input sepcified";
return null;
}
char[] wildcards = [ '*', '?' ] ;
var res = result.Tokens
.Select(x => (pattern: x.Value, dir: EmptyCoalesce(Path.GetDirectoryName(x.Value), Directory.GetCurrentDirectory())!))
.SelectMany(x =>
x switch
{
_ when Directory.Exists(x.pattern) =>
new DirectoryInfo(x.pattern).GetFiles(),
_ when x.pattern.Intersect(wildcards).Any() && Directory.Exists(x.dir) =>
Directory.GetFiles(x.dir, Path.GetFileName(x.pattern)).Select(f => new FileInfo(f)),
_ when File.Exists(x.pattern) =>
[ new FileInfo(x.pattern) ],
_ =>
[]
}
).ToArray();
if(res.Length == 0)
{
result.ErrorMessage = "No files found";
return null;
}
return res;
})
{
AllowMultipleArgumentsPerToken = true,
IsRequired = true,
};
fileOption.AddAlias("--input");
var bratliOption = new Option<bool>(name: "-nb", description: "Omit Brotli compressed version.", getDefaultValue: () => false);
bratliOption.AddAlias("--NoBrotli");
var gzipOption = new Option<bool>(name: "-ng", description: "Omit GZip compressed version.", getDefaultValue: () => false);
gzipOption.AddAlias("--NoGzip");
var patternOption = new Option<string?>(
name: "-p",
description: "Use regular expression pattern to filter files",
parseArgument: result => {
var res = result.Tokens.FirstOrDefault()?.Value;
if (string.IsNullOrWhiteSpace(res)) return "*.";
if(!IsValidRegexPattern(res))
{
result.ErrorMessage = "Invalid pattern specified";
return null;
}
return res;
}
);
patternOption.AddAlias("--pattern");
var destinationOption = new Option<DirectoryInfo?>(name: "-d", description: "The output directory.");
destinationOption.AddAlias("--destination");
var rootCommand = new RootCommand("PreCompression tool for ASP.NET Core");
rootCommand.AddOption(fileOption);
rootCommand.AddOption(bratliOption);
rootCommand.AddOption(gzipOption);
rootCommand.AddOption(patternOption);
rootCommand.AddOption(destinationOption);
rootCommand.SetHandler(ReCompress, fileOption, bratliOption, gzipOption, patternOption, destinationOption);
rootCommand.AddValidator(result =>
{
if (result.GetValueForOption(destinationOption) is not null && result.GetValueForOption(fileOption)?.Length > 0)
{
result.ErrorMessage = "Destination can only be specified when a single file is being processed";
}
});
return await rootCommand.InvokeAsync(args);
static async Task ReCompress(FileInfo[]? files, bool omitBratli, bool omitGZip, string? pattern, DirectoryInfo? destination)
{
if(files is null) return;
var opts = RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.Compiled;
#if NET8_0_OR_GREATER
opts |= RegexOptions.NonBacktracking;
#endif
Predicate<string> match =
string.IsNullOrWhiteSpace(pattern) ?
_ => true :
new Regex(pattern, opts).IsMatch;
var list = files.Where(f => match(f.FullName));
var tasks = list.Select(async file =>
{
try
{
Console.WriteLine($"Compressing {file.FullName}");
await Compression.CompressAsync(file, !omitBratli, !omitGZip, destination, default);
}
catch (Exception ex)
{
Console.Error.WriteLine($"Error processing {file.FullName}: {ex.Message}");
}
});
await Task.WhenAll(tasks);
Console.WriteLine($"Compression finished");
}
static string? EmptyCoalesce(params string?[] values) => values.FirstOrDefault(x => !string.IsNullOrWhiteSpace(x));
static bool IsValidRegexPattern(string pattern, string testText = "", int maxMillisecondTimeOut = 10)
{
if (string.IsNullOrEmpty(pattern)) return false;
try
{
Regex re = new Regex(pattern, RegexOptions.None, TimeSpan.FromMilliseconds(maxMillisecondTimeOut));
re.IsMatch(testText);
}
catch
{
return false;
}
return true;
}