Skip to content

Commit

Permalink
VS Extension now hot reloads its config (#1369)
Browse files Browse the repository at this point in the history
This fixes #1367. 

The `.vs/$(SolutionName)/v17/csharpier.json` file is now watched for
changes and will reload the configuration when the file is changed.

---------

Co-authored-by: Bela VanderVoort <[email protected]>
  • Loading branch information
scabana and belav authored Nov 1, 2024
1 parent 3563940 commit f2b2b98
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<PackageManifest Version="2.0.0" xmlns="http://schemas.microsoft.com/developer/vsx-schema/2011" xmlns:d="http://schemas.microsoft.com/developer/vsx-schema-design/2011">
<Metadata>
<Identity Id="83d6b6a0-9e25-4034-80f3-38445d8a8837" Version="1.8.0" Language="en-US" Publisher="CSharpier" />
<Identity Id="83d6b6a0-9e25-4034-80f3-38445d8a8837" Version="1.9.0" Language="en-US" Publisher="CSharpier" />
<DisplayName>CSharpier</DisplayName>
<Description xml:space="preserve">CSharpier is an opinionated code formatter for c#. It uses Roslyn to parse your code and re-prints it using its own rules.</Description>
<MoreInfo>https://github.com/belav/csharpier</MoreInfo>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<PackageManifest Version="2.0.0" xmlns="http://schemas.microsoft.com/developer/vsx-schema/2011" xmlns:d="http://schemas.microsoft.com/developer/vsx-schema-design/2011">
<Metadata>
<Identity Id="edd8b38c-baa1-46c6-b82e-1da7a0ba597b" Version="1.8.0" Language="en-US" Publisher="CSharpier" />
<Identity Id="edd8b38c-baa1-46c6-b82e-1da7a0ba597b" Version="1.9.0" Language="en-US" Publisher="CSharpier" />
<DisplayName>CSharpier 2019</DisplayName>
<Description xml:space="preserve">CSharpier is an opinionated code formatter for c#. It uses Roslyn to parse your code and re-prints it using its own rules.</Description>
<MoreInfo>https://github.com/belav/csharpier</MoreInfo>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ protected void LoadFrom(CSharpierOptions newInstance)
private static readonly AsyncLazy<CSharpierOptions> liveModel =
new(CreateAsync, ThreadHelper.JoinableTaskFactory);

private static FileSystemWatcher _hotReloadWatcher;

public static CSharpierOptions Instance
{
get
Expand All @@ -73,6 +75,7 @@ private static async Task<CSharpierOptions> CreateAsync()
{
var instance = new CSharpierOptions();
await instance.LoadAsync();
await InitializeHotReloadWatcherAsync();
return instance;
}

Expand Down Expand Up @@ -112,7 +115,7 @@ Action<OptionsDto> doStuff
}

await LoadOptionsFromFile(
this.GetSolutionOptionsFileNameAsync,
GetSolutionOptionsFileNameAsync,
o =>
{
newInstance.SolutionRunOnSave = o.RunOnSave;
Expand Down Expand Up @@ -170,7 +173,7 @@ async Task SaveOptions(Func<Task<string?>> getFilePath, OptionsDto optionsDto)
}

await SaveOptions(
this.GetSolutionOptionsFileNameAsync,
GetSolutionOptionsFileNameAsync,
new OptionsDto { RunOnSave = this.SolutionRunOnSave }
);

Expand All @@ -197,7 +200,7 @@ await SaveOptions(
);
}

private async Task<string?> GetSolutionOptionsFileNameAsync()
private static async Task<string?> GetSolutionOptionsFileNameAsync()
{
#pragma warning disable VSSDK006
var solution =
Expand All @@ -211,6 +214,33 @@ await AsyncServiceProvider.GlobalProvider.GetServiceAsync(typeof(SVsSolution))
: null;
}

private static async Task InitializeHotReloadWatcherAsync()
{
string filePath = await GetSolutionOptionsFileNameAsync();

_hotReloadWatcher = new FileSystemWatcher(
Path.GetDirectoryName(filePath),
Path.GetFileName(filePath)
);

static void OnFileChanged(object sender, FileSystemEventArgs e)
{
#pragma warning disable VSTHRD103 // Call async methods when in an async method
ThreadHelper.JoinableTaskFactory.Run(async () =>
{
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
await Instance.LoadAsync();
});
#pragma warning restore
}

_hotReloadWatcher.Changed += OnFileChanged;
_hotReloadWatcher.Created += OnFileChanged;
_hotReloadWatcher.Renamed += OnFileChanged;

_hotReloadWatcher.EnableRaisingEvents = true;
}

private class OptionsDto
{
public bool? RunOnSave { get; set; }
Expand Down
5 changes: 4 additions & 1 deletion Src/CSharpier.VisualStudio/ChangeLog.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
## [1.8.0]
## [1.9.0]
- Configuration will hot reload: changes to `.vs/$(SolutionName)/v17/csharpier.json` will trigger a reload of the configuration in an opened Visual Studio instance.

## [1.8.0]
- Use dotnet tool list to find both local and global installs of csharpier.

## [1.7.4]
Expand Down

0 comments on commit f2b2b98

Please sign in to comment.