Skip to content

Commit

Permalink
Update plugin to facilitate support for more types of files to update
Browse files Browse the repository at this point in the history
  • Loading branch information
EricBatlle committed May 9, 2024
1 parent 24013ab commit 612f07c
Show file tree
Hide file tree
Showing 10 changed files with 106 additions and 40 deletions.
1 change: 1 addition & 0 deletions NamespaceAdjuster/NamespaceAdjuster.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
<Compile Include="NamespaceUpdater\CsNamespaceUpdaterService.cs" />
<Compile Include="NamespaceUpdater\IFileNamespaceUpdater.cs" />
<Compile Include="NamespaceUpdater\LogicFileNamespaceUpdaterService.cs" />
<Compile Include="NamespaceUpdater\FileNamespaceUpdaterProvider.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="NamespaceAdjusterPackage.cs" />
<Compile Include="SolutionSelection\ISolutionSelectionService.cs" />
Expand Down
9 changes: 5 additions & 4 deletions NamespaceAdjuster/NamespaceAdjusterController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ namespace NamespaceUpdater
{
internal class CsNamespaceUpdaterService : LogicFileNamespaceUpdaterService
{
protected override string SupportedFileExtension => ".cs";

protected override string NamespaceStartLimiter => "{" + NewLine;

protected override string NamespaceEndLimiter => "}";
Expand Down
31 changes: 31 additions & 0 deletions NamespaceAdjuster/NamespaceUpdater/FileNamespaceUpdaterProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;
using System.IO;

namespace NamespaceUpdater
{
internal class FileNamespaceUpdaterProvider
{
private List<LogicFileNamespaceUpdaterService> updaterServices = new List<LogicFileNamespaceUpdaterService>();

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.");
}
}
}
4 changes: 1 addition & 3 deletions NamespaceAdjuster/NamespaceUpdater/IFileNamespaceUpdater.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.IO;

namespace NamespaceUpdater
namespace NamespaceUpdater
{
internal interface IFileNamespaceUpdater
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
Expand Down
36 changes: 18 additions & 18 deletions NamespaceAdjuster/VSCommandTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,22 @@
// ------------------------------------------------------------------------------
namespace NamespaceAdjuster
{
using System;
/// <summary>
/// Helper class that exposes all GUIDs used across VS Package.
/// </summary>
internal sealed partial class PackageGuids
{
public const string NamespaceAdjusterString = "f5019467-54ba-4373-a6de-d3b5857b0545";
public static Guid NamespaceAdjuster = new Guid(NamespaceAdjusterString);
}
/// <summary>
/// Helper class that encapsulates all CommandIDs uses across VS Package.
/// </summary>
internal sealed partial class PackageIds
{
public const int MyMenuGroup = 0x0001;
public const int AdjustNamespaceCommand = 0x0100;
}
using System;

/// <summary>
/// Helper class that exposes all GUIDs used across VS Package.
/// </summary>
internal sealed partial class PackageGuids
{
public const string NamespaceAdjusterString = "f5019467-54ba-4373-a6de-d3b5857b0545";
public static Guid NamespaceAdjuster = new Guid(NamespaceAdjusterString);
}
/// <summary>
/// Helper class that encapsulates all CommandIDs uses across VS Package.
/// </summary>
internal sealed partial class PackageIds
{
public const int MyMenuGroup = 0x0001;
public const int AdjustNamespaceCommand = 0x0100;
}
}
14 changes: 9 additions & 5 deletions NamespaceAdjuster/Windows/InputFieldWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@

namespace NamespaceAdjuster.Windows
{
/// <summary>
/// Lógica de interacción para Window1.xaml
/// </summary>
public partial class InputFieldWindow : Window
{
ISolutionSelectionService solutionSelectionService;
Expand All @@ -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);
}
}
}

Expand All @@ -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
{
Expand Down
20 changes: 10 additions & 10 deletions NamespaceAdjuster/source.extension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "";
}
}
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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!

0 comments on commit 612f07c

Please sign in to comment.