|
@@ -6,11 +6,13 @@ public static class Program
|
|
|
{
|
|
|
private static HashSet<string> _fileNames = new HashSet<string>();
|
|
|
private static Dictionary<string, Encoding> _fileEncodings = new Dictionary<string, Encoding>();
|
|
|
+ private static Logger _logger;
|
|
|
private static Logger _proccess_find_logger;
|
|
|
private static Logger _proccess_encode_logger;
|
|
|
- public static void Main()
|
|
|
+
|
|
|
+ static Program()
|
|
|
{
|
|
|
- var logger = new LoggerConfiguration()
|
|
|
+ _logger = new LoggerConfiguration()
|
|
|
.MinimumLevel.Debug()
|
|
|
.WriteTo.File("output.log", Serilog.Events.LogEventLevel.Debug, fileSizeLimitBytes: 1024 * 1024, rollOnFileSizeLimit: true)
|
|
|
.WriteTo.Console(Serilog.Events.LogEventLevel.Debug)
|
|
@@ -27,79 +29,183 @@ public static class Program
|
|
|
.WriteTo.File("proccess_encode.log", Serilog.Events.LogEventLevel.Debug, fileSizeLimitBytes: 1024 * 1024, rollOnFileSizeLimit: true)
|
|
|
.WriteTo.Console(Serilog.Events.LogEventLevel.Debug)
|
|
|
.CreateLogger();
|
|
|
+ }
|
|
|
|
|
|
- string path = "";
|
|
|
-
|
|
|
- path = Console.ReadLine();
|
|
|
- logger.Information($"Path: {path}");
|
|
|
-
|
|
|
- string word_Find, word_Replace;
|
|
|
-
|
|
|
- word_Find = Console.ReadLine();
|
|
|
- logger.Information($"Find: {word_Find}");
|
|
|
- word_Replace = Console.ReadLine();
|
|
|
- logger.Information($"Find: {word_Replace}");
|
|
|
-
|
|
|
- Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
|
|
|
-
|
|
|
- Process processFind = new Process();
|
|
|
- ProcessStartInfo startInfoFind = new ProcessStartInfo();
|
|
|
- startInfoFind.WindowStyle = ProcessWindowStyle.Hidden;
|
|
|
- startInfoFind.RedirectStandardOutput = true;
|
|
|
- startInfoFind.WorkingDirectory = path;
|
|
|
- startInfoFind.FileName = "cmd.exe";
|
|
|
- startInfoFind.Arguments = $"/C findstr /s /i {word_Find} *.pas *.dfm *.dpr";
|
|
|
- processFind.StartInfo = startInfoFind;
|
|
|
- processFind.OutputDataReceived += ProcessFind_OutputDataReceived;
|
|
|
- processFind.Start();
|
|
|
- processFind.BeginOutputReadLine();
|
|
|
-
|
|
|
- processFind.WaitForExit();
|
|
|
- processFind.CancelOutputRead();
|
|
|
- processFind.OutputDataReceived -= ProcessFind_OutputDataReceived;
|
|
|
- processFind.Close();
|
|
|
-
|
|
|
- Process processEncode = new Process();
|
|
|
- ProcessStartInfo startInfoEncode = new ProcessStartInfo();
|
|
|
- startInfoEncode.WindowStyle = ProcessWindowStyle.Hidden;
|
|
|
- startInfoEncode.RedirectStandardOutput = true;
|
|
|
- startInfoEncode.WorkingDirectory = path;
|
|
|
- startInfoEncode.FileName = "cmd.exe";
|
|
|
- startInfoEncode.Arguments = $"/C \"C:\\Program Files\\Git\\usr\\bin\\file.exe\" --mime-encoding {string.Join(' ', _fileNames)}";
|
|
|
- processEncode.StartInfo = startInfoEncode;
|
|
|
- processEncode.OutputDataReceived += ProcessEncode_OutputDataReceived;
|
|
|
- processEncode.Start();
|
|
|
- processEncode.BeginOutputReadLine();
|
|
|
-
|
|
|
- processEncode.WaitForExit();
|
|
|
- processEncode.CancelOutputRead();
|
|
|
- processEncode.OutputDataReceived -= ProcessEncode_OutputDataReceived;
|
|
|
- processEncode.Close();
|
|
|
-
|
|
|
- foreach (var filename in _fileNames)
|
|
|
+ public static void Main(string[] args)
|
|
|
+ {
|
|
|
+ try
|
|
|
{
|
|
|
- StringBuilder stringBuilder = new StringBuilder();
|
|
|
- stringBuilder.Append(File.ReadAllText($"{path}/{filename}", _fileEncodings[filename]));
|
|
|
- stringBuilder.Replace(word_Find, word_Replace);
|
|
|
- File.WriteAllText($"{path}/{filename}", stringBuilder.ToString(), _fileEncodings[filename]);
|
|
|
+ string path = "", word_Find = "", word_Replace = "", files_exts = "";
|
|
|
+
|
|
|
+ if (args.Length != 0)
|
|
|
+ {
|
|
|
+ int argIndex = 0;
|
|
|
+ while (argIndex < args.Length)
|
|
|
+ {
|
|
|
+ switch (args[argIndex])
|
|
|
+ {
|
|
|
+ case "-p":
|
|
|
+ if (!string.IsNullOrEmpty(path))
|
|
|
+ _logger.Warning("Duplicating argument {0} {1}", args[argIndex], args[argIndex + 1]);
|
|
|
+ path = args[++argIndex];
|
|
|
+
|
|
|
+ if (!Directory.Exists(path))
|
|
|
+ throw new DirectoryNotFoundException(path);
|
|
|
+
|
|
|
+ _logger.Information($"Path: {path}");
|
|
|
+ argIndex++;
|
|
|
+ break;
|
|
|
+ case "-f":
|
|
|
+ if (!string.IsNullOrEmpty(word_Find))
|
|
|
+ _logger.Warning("Duplicating argument {0} {1}", args[argIndex], args[argIndex + 1]);
|
|
|
+ word_Find = args[++argIndex];
|
|
|
+
|
|
|
+ if (word_Find.StartsWith('-') || string.IsNullOrEmpty(word_Find))
|
|
|
+ throw new ArgumentException(word_Find);
|
|
|
+
|
|
|
+ _logger.Information($"Find: {word_Find}");
|
|
|
+ argIndex++;
|
|
|
+ break;
|
|
|
+ case "-r":
|
|
|
+ if (!string.IsNullOrEmpty(word_Replace))
|
|
|
+ _logger.Warning("Duplicating argument {0} {1}", args[argIndex], args[argIndex + 1]);
|
|
|
+ word_Replace = args[++argIndex];
|
|
|
+
|
|
|
+ if (word_Replace.StartsWith('-') || string.IsNullOrEmpty(word_Replace))
|
|
|
+ throw new ArgumentException(word_Replace);
|
|
|
+
|
|
|
+ _logger.Information($"Replace to: {word_Replace}");
|
|
|
+ argIndex++;
|
|
|
+ break;
|
|
|
+ case "-e":
|
|
|
+ if (!string.IsNullOrEmpty(files_exts))
|
|
|
+ _logger.Warning("Duplicating argument {0} {1}", args[argIndex], args[argIndex + 1]);
|
|
|
+ files_exts = args[++argIndex];
|
|
|
+
|
|
|
+ if (files_exts.StartsWith('-') || string.IsNullOrEmpty(files_exts))
|
|
|
+ throw new ArgumentException(files_exts);
|
|
|
+
|
|
|
+ _logger.Information($"Files extentions: {files_exts}");
|
|
|
+ argIndex++;
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ _logger.Warning("Skipped unrecognized argument {0}", argIndex);
|
|
|
+ argIndex++;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- logger.Information($"Replaced in {path}\\{filename}");
|
|
|
+ if (string.IsNullOrEmpty(path))
|
|
|
+ {
|
|
|
+ do
|
|
|
+ {
|
|
|
+ Console.Write("Path:");
|
|
|
+ path = Console.ReadLine() ?? string.Empty;
|
|
|
+ }
|
|
|
+ while (!string.IsNullOrEmpty(path) && Directory.Exists(path));
|
|
|
+ _logger.Information($"Path: {path}");
|
|
|
+ }
|
|
|
+
|
|
|
+ if (string.IsNullOrEmpty(word_Find))
|
|
|
+ {
|
|
|
+ do
|
|
|
+ {
|
|
|
+ Console.Write("Find:");
|
|
|
+ word_Find = Console.ReadLine() ?? string.Empty;
|
|
|
+ }
|
|
|
+ while (!string.IsNullOrEmpty(word_Find));
|
|
|
+ _logger.Information($"Find: {word_Find}");
|
|
|
+ }
|
|
|
+
|
|
|
+ if (string.IsNullOrEmpty(word_Replace))
|
|
|
+ {
|
|
|
+ do
|
|
|
+ {
|
|
|
+ Console.Write("Replace:");
|
|
|
+ word_Replace = Console.ReadLine() ?? string.Empty;
|
|
|
+ }
|
|
|
+ while (string.IsNullOrEmpty(word_Replace));
|
|
|
+ _logger.Information($"Replace to: {word_Replace}");
|
|
|
+ }
|
|
|
+
|
|
|
+ if (string.IsNullOrEmpty(files_exts))
|
|
|
+ files_exts = "*.pas *.dfm *.dpr";
|
|
|
+
|
|
|
+ Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
|
|
|
+
|
|
|
+ Process processFind = new Process();
|
|
|
+ ProcessStartInfo startInfoFind = new ProcessStartInfo();
|
|
|
+ startInfoFind.WindowStyle = ProcessWindowStyle.Hidden;
|
|
|
+ startInfoFind.RedirectStandardOutput = true;
|
|
|
+ startInfoFind.WorkingDirectory = path;
|
|
|
+ startInfoFind.FileName = "cmd.exe";
|
|
|
+ startInfoFind.Arguments = $"/C findstr /s /i {word_Find} {files_exts}";
|
|
|
+ processFind.StartInfo = startInfoFind;
|
|
|
+ processFind.OutputDataReceived += ProcessFind_OutputDataReceived;
|
|
|
+ processFind.Start();
|
|
|
+ processFind.BeginOutputReadLine();
|
|
|
+
|
|
|
+ processFind.WaitForExit();
|
|
|
+ processFind.CancelOutputRead();
|
|
|
+ processFind.OutputDataReceived -= ProcessFind_OutputDataReceived;
|
|
|
+ processFind.Close();
|
|
|
+
|
|
|
+ Process processEncode = new Process();
|
|
|
+ ProcessStartInfo startInfoEncode = new ProcessStartInfo();
|
|
|
+ startInfoEncode.WindowStyle = ProcessWindowStyle.Hidden;
|
|
|
+ startInfoEncode.RedirectStandardOutput = true;
|
|
|
+ startInfoEncode.WorkingDirectory = path;
|
|
|
+ startInfoEncode.FileName = "cmd.exe";
|
|
|
+ startInfoEncode.Arguments = $"/C \"C:\\Program Files\\Git\\usr\\bin\\file.exe\" --mime-encoding {string.Join(' ', _fileNames)}";
|
|
|
+ processEncode.StartInfo = startInfoEncode;
|
|
|
+ processEncode.OutputDataReceived += ProcessEncode_OutputDataReceived;
|
|
|
+ processEncode.Start();
|
|
|
+ processEncode.BeginOutputReadLine();
|
|
|
+
|
|
|
+ processEncode.WaitForExit();
|
|
|
+ processEncode.CancelOutputRead();
|
|
|
+ processEncode.OutputDataReceived -= ProcessEncode_OutputDataReceived;
|
|
|
+ processEncode.Close();
|
|
|
+
|
|
|
+ foreach (var filename in _fileNames)
|
|
|
+ {
|
|
|
+ StringBuilder stringBuilder = new StringBuilder();
|
|
|
+ stringBuilder.Append(File.ReadAllText($"{path}/{filename}", _fileEncodings[filename]));
|
|
|
+ stringBuilder.Replace(word_Find, word_Replace);
|
|
|
+ File.WriteAllText($"{path}/{filename}", stringBuilder.ToString(), _fileEncodings[filename]);
|
|
|
+
|
|
|
+ _logger.Information($"Replaced in {path}\\{filename}");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ var innerException = ex;
|
|
|
+
|
|
|
+ while ( innerException != null )
|
|
|
+ {
|
|
|
+ _logger.Fatal(ex.Message);
|
|
|
+ if (!string.IsNullOrEmpty(ex.StackTrace))
|
|
|
+ _logger.Error(ex.StackTrace);
|
|
|
+
|
|
|
+ innerException = innerException.InnerException;
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private static void ProcessEncode_OutputDataReceived(object sender, DataReceivedEventArgs e)
|
|
|
{
|
|
|
- _proccess_encode_logger.Information(e.Data);
|
|
|
var filepath = e.Data;
|
|
|
if (!string.IsNullOrEmpty(filepath))
|
|
|
{
|
|
|
+ _proccess_encode_logger.Information(filepath);
|
|
|
var index = filepath.IndexOf(':');
|
|
|
filepath = filepath.Substring(0, index != -1 ? index : 0);
|
|
|
|
|
|
if (!string.IsNullOrEmpty(filepath))
|
|
|
{
|
|
|
|
|
|
- var encoding = e.Data.Substring(index + 1).Trim();
|
|
|
+ var encoding = e.Data?.Substring(index + 1).Trim();
|
|
|
|
|
|
if (!string.IsNullOrEmpty(encoding))
|
|
|
{
|
|
@@ -120,10 +226,10 @@ public static class Program
|
|
|
|
|
|
private static void ProcessFind_OutputDataReceived(object sender, System.Diagnostics.DataReceivedEventArgs e)
|
|
|
{
|
|
|
- _proccess_find_logger.Information(e.Data);
|
|
|
var filepath = e.Data;
|
|
|
if (!string.IsNullOrEmpty(filepath))
|
|
|
{
|
|
|
+ _proccess_find_logger.Information(filepath);
|
|
|
var index = filepath.IndexOf(':');
|
|
|
filepath = filepath.Substring(0, index != -1 ? index : 0);
|
|
|
|