From b70d2587a246624cdbb3d032cad2a5efb33c5c97 Mon Sep 17 00:00:00 2001 From: Jaex Date: Wed, 2 Nov 2022 20:04:28 +0300 Subject: [PATCH] Code refactoring --- ShareX.HelpersLib/Helpers/FileHelpers.cs | 98 +++++++++++++++++------- ShareX.Setup/Program.cs | 26 +++---- ShareX.Setup/SetupHelpers.cs | 80 ------------------- ShareX.Setup/ShareX.Setup.csproj | 1 - 4 files changed, 84 insertions(+), 121 deletions(-) delete mode 100644 ShareX.Setup/SetupHelpers.cs diff --git a/ShareX.HelpersLib/Helpers/FileHelpers.cs b/ShareX.HelpersLib/Helpers/FileHelpers.cs index 7b6dd6954..59fa3dc8d 100644 --- a/ShareX.HelpersLib/Helpers/FileHelpers.cs +++ b/ShareX.HelpersLib/Helpers/FileHelpers.cs @@ -635,6 +635,77 @@ public static string CopyFile(string filePath, string destinationFolder, bool ov return null; } + public static void CopyFiles(string filePath, string destinationFolder) + { + CopyFiles(new string[] { filePath }, destinationFolder); + } + + public static void CopyFiles(IEnumerable files, string destinationFolder) + { + if (!Directory.Exists(destinationFolder)) + { + Directory.CreateDirectory(destinationFolder); + } + + foreach (string filePath in files) + { + string fileName = Path.GetFileName(filePath); + string destinationFilePath = Path.Combine(destinationFolder, fileName); + File.Copy(filePath, destinationFilePath); + } + } + + public static void CopyFiles(string sourceFolder, string destinationFolder, string searchPattern = "*", string[] ignoreFiles = null) + { + string[] files = Directory.GetFiles(sourceFolder, searchPattern); + + if (ignoreFiles != null) + { + List newFiles = new List(); + + foreach (string file in files) + { + string fileName = Path.GetFileName(file); + + if (ignoreFiles.All(x => !fileName.Equals(x, StringComparison.OrdinalIgnoreCase))) + { + newFiles.Add(file); + } + } + + files = newFiles.ToArray(); + } + + CopyFiles(files, destinationFolder); + } + + public static void CopyAll(string sourceDirectory, string targetDirectory) + { + DirectoryInfo diSource = new DirectoryInfo(sourceDirectory); + DirectoryInfo diTarget = new DirectoryInfo(targetDirectory); + + CopyAll(diSource, diTarget); + } + + public static void CopyAll(DirectoryInfo source, DirectoryInfo target) + { + if (!Directory.Exists(target.FullName)) + { + Directory.CreateDirectory(target.FullName); + } + + foreach (FileInfo fi in source.GetFiles()) + { + fi.CopyTo(Path.Combine(target.FullName, fi.Name), true); + } + + foreach (DirectoryInfo diSourceSubDir in source.GetDirectories()) + { + DirectoryInfo nextTargetSubDir = target.CreateSubdirectory(diSourceSubDir.Name); + CopyAll(diSourceSubDir, nextTargetSubDir); + } + } + public static string MoveFile(string filePath, string destinationFolder, bool overwrite = true) { if (!string.IsNullOrEmpty(filePath) && File.Exists(filePath) && !string.IsNullOrEmpty(destinationFolder)) @@ -745,33 +816,6 @@ public static string GetTempFilePath(string extension) return Path.ChangeExtension(path, extension); } - public static void CopyAll(string sourceDirectory, string targetDirectory) - { - DirectoryInfo diSource = new DirectoryInfo(sourceDirectory); - DirectoryInfo diTarget = new DirectoryInfo(targetDirectory); - - CopyAll(diSource, diTarget); - } - - public static void CopyAll(DirectoryInfo source, DirectoryInfo target) - { - if (!Directory.Exists(target.FullName)) - { - Directory.CreateDirectory(target.FullName); - } - - foreach (FileInfo fi in source.GetFiles()) - { - fi.CopyTo(Path.Combine(target.FullName, fi.Name), true); - } - - foreach (DirectoryInfo diSourceSubDir in source.GetDirectories()) - { - DirectoryInfo nextTargetSubDir = target.CreateSubdirectory(diSourceSubDir.Name); - CopyAll(diSourceSubDir, nextTargetSubDir); - } - } - public static void CreateEmptyFile(string filePath) { File.Create(filePath).Dispose(); diff --git a/ShareX.Setup/Program.cs b/ShareX.Setup/Program.cs index ace63b9cf..79a0baaa1 100644 --- a/ShareX.Setup/Program.cs +++ b/ShareX.Setup/Program.cs @@ -312,10 +312,10 @@ private static void CreateSteamFolder() Directory.CreateDirectory(SteamOutputDir); - SetupHelpers.CopyFile(Path.Combine(SteamLauncherDir, "ShareX_Launcher.exe"), SteamOutputDir); - SetupHelpers.CopyFile(Path.Combine(SteamLauncherDir, "steam_appid.txt"), SteamOutputDir); - SetupHelpers.CopyFile(Path.Combine(SteamLauncherDir, "installscript.vdf"), SteamOutputDir); - SetupHelpers.CopyFiles(SteamLauncherDir, "*.dll", SteamOutputDir); + FileHelpers.CopyFiles(Path.Combine(SteamLauncherDir, "ShareX_Launcher.exe"), SteamOutputDir); + FileHelpers.CopyFiles(Path.Combine(SteamLauncherDir, "steam_appid.txt"), SteamOutputDir); + FileHelpers.CopyFiles(Path.Combine(SteamLauncherDir, "installscript.vdf"), SteamOutputDir); + FileHelpers.CopyFiles(SteamLauncherDir, SteamOutputDir, "*.dll"); CreateFolder(BinDir, SteamUpdatesDir, SetupJobs.CreateSteamFolder); } @@ -331,16 +331,16 @@ private static void CreateFolder(string source, string destination, SetupJobs jo Directory.CreateDirectory(destination); - SetupHelpers.CopyFile(Path.Combine(source, "ShareX.exe"), destination); - SetupHelpers.CopyFile(Path.Combine(source, "ShareX.exe.config"), destination); - SetupHelpers.CopyFiles(source, "*.dll", destination); + FileHelpers.CopyFiles(Path.Combine(source, "ShareX.exe"), destination); + FileHelpers.CopyFiles(Path.Combine(source, "ShareX.exe.config"), destination); + FileHelpers.CopyFiles(source, destination, "*.dll"); if (job == SetupJobs.CreateDebug || job == SetupJobs.CreateMicrosoftStoreDebugFolder) { - SetupHelpers.CopyFiles(source, "*.pdb", destination); + FileHelpers.CopyFiles(source, destination, "*.pdb"); } - SetupHelpers.CopyFiles(Path.Combine(ParentDir, "Licenses"), "*.txt", Path.Combine(destination, "Licenses")); + FileHelpers.CopyFiles(Path.Combine(ParentDir, "Licenses"), Path.Combine(destination, "Licenses"), "*.txt"); if (job != SetupJobs.CreateMicrosoftStoreFolder && job != SetupJobs.CreateMicrosoftStoreDebugFolder) { @@ -349,9 +349,9 @@ private static void CreateFolder(string source, string destination, SetupJobs jo CompileISSFile("Recorder-devices-setup.iss"); } - SetupHelpers.CopyFile(RecorderDevicesSetupPath, destination); + FileHelpers.CopyFiles(RecorderDevicesSetupPath, destination); - SetupHelpers.CopyFile(Path.Combine(NativeMessagingHostDir, "ShareX_NativeMessagingHost.exe"), destination); + FileHelpers.CopyFiles(Path.Combine(NativeMessagingHostDir, "ShareX_NativeMessagingHost.exe"), destination); } string[] languages = new string[] { "de", "es", "es-MX", "fa-IR", "fr", "hu", "id-ID", "it-IT", "ja-JP", "ko-KR", "nl-NL", "pl", "pt-BR", "pt-PT", @@ -359,12 +359,12 @@ private static void CreateFolder(string source, string destination, SetupJobs jo foreach (string language in languages) { - SetupHelpers.CopyFiles(Path.Combine(source, language), "*.resources.dll", Path.Combine(destination, "Languages", language)); + FileHelpers.CopyFiles(Path.Combine(source, language), Path.Combine(destination, "Languages", language), "*.resources.dll"); } if (File.Exists(FFmpegPath)) { - SetupHelpers.CopyFile(FFmpegPath, destination); + FileHelpers.CopyFiles(FFmpegPath, destination); } FileHelpers.CopyAll(Path.Combine(ParentDir, @"ShareX.ScreenCaptureLib\Stickers"), Path.Combine(destination, "Stickers")); diff --git a/ShareX.Setup/SetupHelpers.cs b/ShareX.Setup/SetupHelpers.cs deleted file mode 100644 index b9a0e0268..000000000 --- a/ShareX.Setup/SetupHelpers.cs +++ /dev/null @@ -1,80 +0,0 @@ -#region License Information (GPL v3) - -/* - ShareX - A program that allows you to take screenshots and share any file type - Copyright (c) 2007-2022 ShareX Team - - This program is free software; you can redistribute it and/or - modify it under the terms of the GNU General Public License - as published by the Free Software Foundation; either version 2 - of the License, or (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - - Optionally you can also view the license at . -*/ - -#endregion License Information (GPL v3) - -using ShareX.HelpersLib; -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; - -namespace ShareX.Setup -{ - internal class SetupHelpers - { - public static void CopyFile(string path, string toFolder) - { - CopyFiles(new string[] { path }, toFolder); - } - - public static void CopyFiles(IEnumerable files, string toFolder) - { - if (!Directory.Exists(toFolder)) - { - Directory.CreateDirectory(toFolder); - } - - foreach (string filePath in files) - { - string fileName = Path.GetFileName(filePath); - string dest = Path.Combine(toFolder, fileName); - File.Copy(filePath, dest); - } - } - - public static void CopyFiles(string directory, string searchPattern, string toFolder, string[] ignoreFiles = null) - { - string[] files = Directory.GetFiles(directory, searchPattern); - - if (ignoreFiles != null) - { - List newFiles = new List(); - - foreach (string file in files) - { - string fileName = Path.GetFileName(file); - - if (ignoreFiles.All(x => !fileName.Equals(x, StringComparison.OrdinalIgnoreCase))) - { - newFiles.Add(file); - } - } - - files = newFiles.ToArray(); - } - - CopyFiles(files, toFolder); - } - } -} \ No newline at end of file diff --git a/ShareX.Setup/ShareX.Setup.csproj b/ShareX.Setup/ShareX.Setup.csproj index 5aa97a049..7c210cc51 100644 --- a/ShareX.Setup/ShareX.Setup.csproj +++ b/ShareX.Setup/ShareX.Setup.csproj @@ -73,7 +73,6 @@ Properties\SharedAssemblyInfo.cs -