|
@@ -0,0 +1,134 @@
|
|
|
|
+using Serilog;
|
|
|
|
+using Serilog.Core;
|
|
|
|
+using System.Diagnostics;
|
|
|
|
+using System.Text;
|
|
|
|
+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 _proccess_find_logger;
|
|
|
|
+ private static Logger _proccess_encode_logger;
|
|
|
|
+ public static void Main()
|
|
|
|
+ {
|
|
|
|
+ var logger = new LoggerConfiguration()
|
|
|
|
+ .MinimumLevel.Debug()
|
|
|
|
+ .WriteTo.File("output.log", Serilog.Events.LogEventLevel.Debug, fileSizeLimitBytes: 1024 * 1024, rollOnFileSizeLimit: true)
|
|
|
|
+ .WriteTo.Console(Serilog.Events.LogEventLevel.Debug)
|
|
|
|
+ .CreateLogger();
|
|
|
|
+
|
|
|
|
+ _proccess_find_logger = new LoggerConfiguration()
|
|
|
|
+ .MinimumLevel.Debug()
|
|
|
|
+ .WriteTo.File("proccess_find.log", Serilog.Events.LogEventLevel.Debug, fileSizeLimitBytes: 1024 * 1024, rollOnFileSizeLimit: true)
|
|
|
|
+ .WriteTo.Console(Serilog.Events.LogEventLevel.Debug)
|
|
|
|
+ .CreateLogger();
|
|
|
|
+
|
|
|
|
+ _proccess_encode_logger = new LoggerConfiguration()
|
|
|
|
+ .MinimumLevel.Debug()
|
|
|
|
+ .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)
|
|
|
|
+ {
|
|
|
|
+ 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}");
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private static void ProcessEncode_OutputDataReceived(object sender, DataReceivedEventArgs e)
|
|
|
|
+ {
|
|
|
|
+ _proccess_encode_logger.Information(e.Data);
|
|
|
|
+ var filepath = e.Data;
|
|
|
|
+ if (!string.IsNullOrEmpty(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();
|
|
|
|
+
|
|
|
|
+ if (!string.IsNullOrEmpty(encoding))
|
|
|
|
+ {
|
|
|
|
+ switch (encoding)
|
|
|
|
+ {
|
|
|
|
+ case "utf-8":
|
|
|
|
+ _fileEncodings.Add(filepath, Encoding.UTF8);
|
|
|
|
+ break;
|
|
|
|
+ case "us-ascii":
|
|
|
|
+ case "iso-8859-1":
|
|
|
|
+ _fileEncodings.Add(filepath, Encoding.GetEncoding("windows-1251"));
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ 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))
|
|
|
|
+ {
|
|
|
|
+ var index = filepath.IndexOf(':');
|
|
|
|
+ filepath = filepath.Substring(0, index != -1 ? index : 0);
|
|
|
|
+
|
|
|
|
+ if (!string.IsNullOrEmpty(filepath))
|
|
|
|
+ _fileNames.Add(filepath);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+}
|