-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
145 lines (132 loc) · 4.46 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
using System;
using System.Diagnostics;
using System.IO;
using System.Text.RegularExpressions;
using System.Linq;
using System.Collections.Generic;
namespace Normalize
{
class Program
{
static void Main(string[] args)
{
var options = ParseArgs(args);
if (options == null)
{
ConsoleMessage.WriteError("Invalid arguments!");
return;
}
NormalizeLineEndings(options);
}
private static Options ParseArgs(string[] args)
{
if (args == null || (args.Length != 4 && args.Length != 6))
{
return null;
}
var options = new Options();
for (int i = 0; i < args.Length; i += 2)
{
if (args[i].ToLower() == "-p" || args[i].ToLower() == "--path")
{
options.Path = args[i + 1];
}
else if (args[i].ToLower() == "-t" || args[i].ToLower() == "--type")
{
options.LineEndingType = args[i + 1];
}
else if (args[i].ToLower() == "-s" || args[i].ToLower() == "--search")
{
options.SearchPattern = args[i + 1];
}
else
{
return null;
}
}
return options;
}
private static void NormalizeLineEndings(Options opts)
{
var watch = new Stopwatch();
watch.Start();
var lineEnding = GetLineEnding(opts.LineEndingType);
if (lineEnding == string.Empty)
{
ConsoleMessage.WriteError($"{opts.LineEndingType} is not supported!");
return;
}
if (File.Exists(opts.Path))
{
ConsoleMessage.WriteInfo($"Normalizing file to {opts.LineEndingType.ToUpper()}...");
NormalizeFile(opts.Path, lineEnding);
watch.Stop();
ConsoleMessage.WriteSuccess($"Normalization finished in {watch.Elapsed.TotalSeconds} sec.");
}
else if (Directory.Exists(opts.Path))
{
ConsoleMessage.WriteInfo($"Normalizing directory to {opts.LineEndingType} line ending...");
NormalizeDirectory(opts.Path, opts.SearchPattern, lineEnding);
watch.Stop();
ConsoleMessage.WriteSuccess($"Normalization finished in {watch.Elapsed.TotalSeconds} sec.");
}
else
{
ConsoleMessage.WriteError($"Cannot find {opts.Path}");
}
}
private static void NormalizeDirectory(string path, string searchPattern, string lineEnding)
{
IEnumerable<string> files;
if (string.IsNullOrEmpty(searchPattern))
{
files = Directory.EnumerateFiles(path);
}
else
{
var regex = new Regex(searchPattern, RegexOptions.IgnoreCase);
files = Directory.EnumerateFiles(path, "*").Where(file => regex.IsMatch(Path.GetExtension(file)));
}
var filesCount = files.Count();
if (filesCount > 0)
{
ConsoleMessage.WriteInfo($"{filesCount} files found.");
}
else
{
ConsoleMessage.WriteError($"Cannot find files!");
return;
}
foreach (var file in files)
{
NormalizeFile(file, lineEnding);
}
}
private static void NormalizeFile(string path, string lineEnding)
{
ConsoleMessage.WriteInfo($"Normalizing file {path}");
var content = File.ReadAllText(path);
var normalized = Regex.Replace(content, @"\r\n|\n\r|\n|\r", lineEnding);
File.WriteAllText(path, normalized);
}
private static string GetLineEnding(string lineEndingType)
{
if (lineEndingType.ToUpper() == "CRLF")
{
return "\r\n";
}
else if (lineEndingType.ToUpper() == "CR")
{
return "\r";
}
else if (lineEndingType.ToUpper() == "LF")
{
return "\n";
}
else
{
return "";
}
}
}
}