From 612f07cd430ad4659da82b6633a48c2d07a92134 Mon Sep 17 00:00:00 2001 From: Eric Date: Thu, 9 May 2024 15:10:08 +0200 Subject: [PATCH] Update plugin to facilitate support for more types of files to update --- NamespaceAdjuster/NamespaceAdjuster.csproj | 1 + .../NamespaceAdjusterController.cs | 9 ++--- .../CsNamespaceUpdaterService.cs | 2 ++ .../FileNamespaceUpdaterProvider.cs | 31 ++++++++++++++++ .../NamespaceUpdater/IFileNamespaceUpdater.cs | 4 +-- .../LogicFileNamespaceUpdaterService.cs | 6 ++++ NamespaceAdjuster/VSCommandTable.cs | 36 +++++++++---------- .../Windows/InputFieldWindow.xaml.cs | 14 +++++--- NamespaceAdjuster/source.extension.cs | 20 +++++------ README.md | 23 ++++++++++++ 10 files changed, 106 insertions(+), 40 deletions(-) create mode 100644 NamespaceAdjuster/NamespaceUpdater/FileNamespaceUpdaterProvider.cs diff --git a/NamespaceAdjuster/NamespaceAdjuster.csproj b/NamespaceAdjuster/NamespaceAdjuster.csproj index e4aa4ed..d0e364c 100644 --- a/NamespaceAdjuster/NamespaceAdjuster.csproj +++ b/NamespaceAdjuster/NamespaceAdjuster.csproj @@ -51,6 +51,7 @@ + diff --git a/NamespaceAdjuster/NamespaceAdjusterController.cs b/NamespaceAdjuster/NamespaceAdjusterController.cs index b379cdf..2ca5841 100644 --- a/NamespaceAdjuster/NamespaceAdjusterController.cs +++ b/NamespaceAdjuster/NamespaceAdjusterController.cs @@ -3,10 +3,11 @@ class NamespaceAdjusterController { - private IFileNamespaceUpdater namespaceUpdater; + FileNamespaceUpdaterProvider namespaceUpdaterProvider; + public NamespaceAdjusterController() { - namespaceUpdater = new CsNamespaceUpdaterService(); + namespaceUpdaterProvider = new FileNamespaceUpdaterProvider(); } public void FixNamespace(string desiredNamespace, string filePath) @@ -16,10 +17,10 @@ public void FixNamespace(string desiredNamespace, string filePath) return; } - var encoding = Extensions.PathExtensions.GetEncoding(filePath); + IFileNamespaceUpdater namespaceUpdater = namespaceUpdaterProvider.GetUpdater(filePath); + var encoding = Extensions.PathExtensions.GetEncoding(filePath); var fileContent = File.ReadAllText(filePath, encoding); - var updated = namespaceUpdater.UpdateFileNamespace(ref fileContent, desiredNamespace); if (updated) diff --git a/NamespaceAdjuster/NamespaceUpdater/CsNamespaceUpdaterService.cs b/NamespaceAdjuster/NamespaceUpdater/CsNamespaceUpdaterService.cs index a39eea1..1647cad 100644 --- a/NamespaceAdjuster/NamespaceUpdater/CsNamespaceUpdaterService.cs +++ b/NamespaceAdjuster/NamespaceUpdater/CsNamespaceUpdaterService.cs @@ -4,6 +4,8 @@ namespace NamespaceUpdater { internal class CsNamespaceUpdaterService : LogicFileNamespaceUpdaterService { + protected override string SupportedFileExtension => ".cs"; + protected override string NamespaceStartLimiter => "{" + NewLine; protected override string NamespaceEndLimiter => "}"; diff --git a/NamespaceAdjuster/NamespaceUpdater/FileNamespaceUpdaterProvider.cs b/NamespaceAdjuster/NamespaceUpdater/FileNamespaceUpdaterProvider.cs new file mode 100644 index 0000000..98223fe --- /dev/null +++ b/NamespaceAdjuster/NamespaceUpdater/FileNamespaceUpdaterProvider.cs @@ -0,0 +1,31 @@ +using System; +using System.Collections.Generic; +using System.IO; + +namespace NamespaceUpdater +{ + internal class FileNamespaceUpdaterProvider + { + private List updaterServices = new List(); + + public FileNamespaceUpdaterProvider() + { + updaterServices.Add(new CsNamespaceUpdaterService()); + } + + public IFileNamespaceUpdater GetUpdater(string filePath) + { + string fileExtension = Path.GetExtension(filePath); + + foreach (var updaterService in updaterServices) + { + if (updaterService.SupportsFileExtension(fileExtension)) + { + return updaterService; + } + } + + throw new Exception($"Error updating file {filePath}:\nFile extension {fileExtension} not supported."); + } + } +} diff --git a/NamespaceAdjuster/NamespaceUpdater/IFileNamespaceUpdater.cs b/NamespaceAdjuster/NamespaceUpdater/IFileNamespaceUpdater.cs index 7799491..8547c36 100644 --- a/NamespaceAdjuster/NamespaceUpdater/IFileNamespaceUpdater.cs +++ b/NamespaceAdjuster/NamespaceUpdater/IFileNamespaceUpdater.cs @@ -1,6 +1,4 @@ -using System.IO; - -namespace NamespaceUpdater +namespace NamespaceUpdater { internal interface IFileNamespaceUpdater { diff --git a/NamespaceAdjuster/NamespaceUpdater/LogicFileNamespaceUpdaterService.cs b/NamespaceAdjuster/NamespaceUpdater/LogicFileNamespaceUpdaterService.cs index c30fa36..5eaf50c 100644 --- a/NamespaceAdjuster/NamespaceUpdater/LogicFileNamespaceUpdaterService.cs +++ b/NamespaceAdjuster/NamespaceUpdater/LogicFileNamespaceUpdaterService.cs @@ -6,6 +6,7 @@ namespace NamespaceUpdater { internal abstract class LogicFileNamespaceUpdaterService : IFileNamespaceUpdater { + protected abstract string SupportedFileExtension { get; } protected abstract string NamespaceStartLimiter { get; } protected abstract string NamespaceEndLimiter { get; } protected abstract Match FindNamespaceMatch(string fileContent); @@ -22,6 +23,11 @@ private void SetNewLine(string fileContent) else NewLine = "\n"; } + public bool SupportsFileExtension(string fileExtension) + { + return fileExtension.Equals(SupportedFileExtension); + } + public virtual bool UpdateFileNamespace(ref string fileContent, string desiredNamespace) { if (string.IsNullOrEmpty(desiredNamespace)) return false; diff --git a/NamespaceAdjuster/VSCommandTable.cs b/NamespaceAdjuster/VSCommandTable.cs index 253c475..d4a1b68 100644 --- a/NamespaceAdjuster/VSCommandTable.cs +++ b/NamespaceAdjuster/VSCommandTable.cs @@ -5,22 +5,22 @@ // ------------------------------------------------------------------------------ namespace NamespaceAdjuster { - using System; - - /// - /// Helper class that exposes all GUIDs used across VS Package. - /// - internal sealed partial class PackageGuids - { - public const string NamespaceAdjusterString = "f5019467-54ba-4373-a6de-d3b5857b0545"; - public static Guid NamespaceAdjuster = new Guid(NamespaceAdjusterString); - } - /// - /// Helper class that encapsulates all CommandIDs uses across VS Package. - /// - internal sealed partial class PackageIds - { - public const int MyMenuGroup = 0x0001; - public const int AdjustNamespaceCommand = 0x0100; - } + using System; + + /// + /// Helper class that exposes all GUIDs used across VS Package. + /// + internal sealed partial class PackageGuids + { + public const string NamespaceAdjusterString = "f5019467-54ba-4373-a6de-d3b5857b0545"; + public static Guid NamespaceAdjuster = new Guid(NamespaceAdjusterString); + } + /// + /// Helper class that encapsulates all CommandIDs uses across VS Package. + /// + internal sealed partial class PackageIds + { + public const int MyMenuGroup = 0x0001; + public const int AdjustNamespaceCommand = 0x0100; + } } \ No newline at end of file diff --git a/NamespaceAdjuster/Windows/InputFieldWindow.xaml.cs b/NamespaceAdjuster/Windows/InputFieldWindow.xaml.cs index eb79ef3..ff6d724 100644 --- a/NamespaceAdjuster/Windows/InputFieldWindow.xaml.cs +++ b/NamespaceAdjuster/Windows/InputFieldWindow.xaml.cs @@ -7,9 +7,6 @@ namespace NamespaceAdjuster.Windows { - /// - /// Lógica de interacción para Window1.xaml - /// public partial class InputFieldWindow : Window { ISolutionSelectionService solutionSelectionService; @@ -32,7 +29,14 @@ private void RenameFiles(string desiredNamespace) NamespaceAdjusterController namespaceAdjuster = new NamespaceAdjusterController(); foreach (string filePath in solutionSelectionService.GetSelectedItemsPaths()) { - namespaceAdjuster.FixNamespace(desiredNamespace, filePath); + try + { + namespaceAdjuster.FixNamespace(desiredNamespace, filePath); + } + catch(Exception e) + { + MessageBox.Show(e.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error); + } } } @@ -52,7 +56,7 @@ public async System.Threading.Tasks.Task ShowAsync() } catch (Exception exc) { - System.Windows.MessageBox.Show("Opening failed: " + exc.Message, "Error", MessageBoxButton.OK, System.Windows.MessageBoxImage.Error); + MessageBox.Show("Opening failed: " + exc.Message, "Error", MessageBoxButton.OK, System.Windows.MessageBoxImage.Error); } finally { diff --git a/NamespaceAdjuster/source.extension.cs b/NamespaceAdjuster/source.extension.cs index 4d27df4..6b17f97 100644 --- a/NamespaceAdjuster/source.extension.cs +++ b/NamespaceAdjuster/source.extension.cs @@ -5,14 +5,14 @@ // ------------------------------------------------------------------------------ namespace NamespaceAdjuster { - internal sealed partial class Vsix - { - public const string Id = "NamespaceAdjuster.897e9043-7d4c-48ac-a147-97f2dcb2be9f"; - public const string Name = "Namespace Adjuster"; - public const string Description = @"Select one or multiple .cs files and adjust them with the specified Namespace"; - public const string Language = "en-US"; - public const string Version = "1.2"; - public const string Author = "Eric Batlle Clavero"; - public const string Tags = ""; - } + internal sealed partial class Vsix + { + public const string Id = "NamespaceAdjuster.897e9043-7d4c-48ac-a147-97f2dcb2be9f"; + public const string Name = "Namespace Adjuster"; + public const string Description = @"Select one or multiple .cs files and adjust them with the specified Namespace"; + public const string Language = "en-US"; + public const string Version = "1.2"; + public const string Author = "Eric Batlle Clavero"; + public const string Tags = ""; + } } diff --git a/README.md b/README.md index b566d69..6793f26 100644 --- a/README.md +++ b/README.md @@ -22,3 +22,26 @@ If this extension has been helpful remember to star the repository and consider If you like my general work and contributions consider [sponsoring me on Github](https://github.com/sponsors/EricBatlle). But if you just want to donate straightforward, I also have [PayPal.me](https://paypal.me/EricBatlleClavero?locale.x=es_ES). + +## Supported file extensions + +- .cs + +## Contribute and add support for more file extensions + +If you want to open a PR and contribute to this plugin I'll be more than happy. + +Just remember that if you want to add support for new file extensions please follow the current code pattern: + +- Inside ``NamespaceUpdater`` directory create your own ``XXNamespaceUpdaterService.cs`` class. +- Make sure the name starts with the file extension that it supports, like ``CsNamespaceUpdaterService.cs``, clearly indicating that it updates ``Csharp (.cs)`` files. +- Make your class inherit from ``LogicFileNamespaceUpdaterService`` and implement how the Updater should behave. +- Last but not less important, add your service to provider ``FileNamespaceUpdaterProvider`` list, inside it's constructor. +```csharp +public FileNamespaceUpdaterProvider() +{ + updaterServices.Add(new CsNamespaceUpdaterService()); +} +``` + +I'll appreciate any contribution! \ No newline at end of file