Bläddra i källkod

Add project files.

Veloe 1 år sedan
förälder
incheckning
ea5d7a6210
3 ändrade filer med 177 tillägg och 0 borttagningar
  1. 25 0
      StringReplacer.sln
  2. 134 0
      StringReplacer/Program.cs
  3. 18 0
      StringReplacer/StringReplacer.csproj

+ 25 - 0
StringReplacer.sln

@@ -0,0 +1,25 @@
+
+Microsoft Visual Studio Solution File, Format Version 12.00
+# Visual Studio Version 17
+VisualStudioVersion = 17.8.34322.80
+MinimumVisualStudioVersion = 10.0.40219.1
+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StringReplacer", "StringReplacer\StringReplacer.csproj", "{9C0AF8FD-37E6-46AD-83AD-2281D18FEAAF}"
+EndProject
+Global
+	GlobalSection(SolutionConfigurationPlatforms) = preSolution
+		Debug|Any CPU = Debug|Any CPU
+		Release|Any CPU = Release|Any CPU
+	EndGlobalSection
+	GlobalSection(ProjectConfigurationPlatforms) = postSolution
+		{9C0AF8FD-37E6-46AD-83AD-2281D18FEAAF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+		{9C0AF8FD-37E6-46AD-83AD-2281D18FEAAF}.Debug|Any CPU.Build.0 = Debug|Any CPU
+		{9C0AF8FD-37E6-46AD-83AD-2281D18FEAAF}.Release|Any CPU.ActiveCfg = Release|Any CPU
+		{9C0AF8FD-37E6-46AD-83AD-2281D18FEAAF}.Release|Any CPU.Build.0 = Release|Any CPU
+	EndGlobalSection
+	GlobalSection(SolutionProperties) = preSolution
+		HideSolutionNode = FALSE
+	EndGlobalSection
+	GlobalSection(ExtensibilityGlobals) = postSolution
+		SolutionGuid = {043DE239-96A5-4175-B667-5CE75453C4E9}
+	EndGlobalSection
+EndGlobal

+ 134 - 0
StringReplacer/Program.cs

@@ -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);
+        }
+    }
+}

+ 18 - 0
StringReplacer/StringReplacer.csproj

@@ -0,0 +1,18 @@
+<Project Sdk="Microsoft.NET.Sdk">
+
+  <PropertyGroup>
+    <OutputType>Exe</OutputType>
+    <TargetFramework>net7.0</TargetFramework>
+    <ImplicitUsings>enable</ImplicitUsings>
+    <Nullable>enable</Nullable>
+    <AssemblyVersion>1.0.0.1</AssemblyVersion>
+    <FileVersion>1.0.0.1</FileVersion>
+  </PropertyGroup>
+
+  <ItemGroup>
+    <PackageReference Include="Serilog" Version="3.1.1" />
+    <PackageReference Include="Serilog.Sinks.Console" Version="5.0.1" />
+    <PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
+  </ItemGroup>
+
+</Project>