Skip to content

Commit

Permalink
Merge pull request #442 from tonyhallett/restore-project-file-when-re…
Browse files Browse the repository at this point in the history
…moving-runsettings

Save project file removes from git changes
  • Loading branch information
tonyhallett authored Jul 25, 2024
2 parents e31d853 + 6ba4bcc commit aab53e4
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 5 deletions.
40 changes: 40 additions & 0 deletions SharedProject/Core/MsTestPlatform/CodeCoverage/ProjectSaver.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using Microsoft;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
using System;
using System.ComponentModel.Composition;

namespace FineCodeCoverage.Core.MsTestPlatform.CodeCoverage
{
interface IProjectSaver
{
void SaveProject(IVsHierarchy projectHierarchy);
}

[Export(typeof(IProjectSaver))]
internal class ProjectSaver : IProjectSaver
{
private IServiceProvider serviceProvider;

[ImportingConstructor]
public ProjectSaver(
[Import(typeof(SVsServiceProvider))]
IServiceProvider serviceProvider
)
{
this.serviceProvider = serviceProvider;
}

public void SaveProject(IVsHierarchy projectHierarchy)
{
ThreadHelper.ThrowIfNotOnUIThread();
var _solution = (IVsSolution)serviceProvider.GetService(typeof(SVsSolution));
Assumes.Present(_solution);
int hr = _solution.SaveSolutionElement((uint)__VSSLNSAVEOPTIONS.SLNSAVEOPT_SaveIfDirty, projectHierarchy, 0);
if (ErrorHandler.Failed(hr))
{
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
using Microsoft.VisualStudio.Shell.Interop;
using System;
using System.ComponentModel.Composition;
using Task = System.Threading.Tasks.Task;
using System.Threading.Tasks;
using FineCodeCoverage.Core.MsTestPlatform.CodeCoverage;

namespace FineCodeCoverage.Engine.MsTestPlatform.CodeCoverage
{
Expand All @@ -14,14 +14,17 @@ internal class VsRunSettingsWriter : IVsRunSettingsWriter
{
private const string projectRunSettingsFilePathElementName = "RunSettingsFilePath";
private readonly IServiceProvider serviceProvider;
private readonly IProjectSaver projectSaver;

[ImportingConstructor]
public VsRunSettingsWriter(
[Import(typeof(SVsServiceProvider))]
IServiceProvider serviceProvider
IServiceProvider serviceProvider,
IProjectSaver projectSaver
)
{
this.serviceProvider = serviceProvider;
this.projectSaver = projectSaver;
}

public async Task<bool> WriteRunSettingsFilePathAsync(Guid projectGuid, string projectRunSettingsFilePath)
Expand All @@ -34,15 +37,15 @@ public async Task<bool> WriteRunSettingsFilePathAsync(Guid projectGuid, string p
{
if (vsHierarchy is IVsBuildPropertyStorage vsBuildPropertyStorage)
{
// care not to use 2 !
success = vsBuildPropertyStorage.SetPropertyValue(projectRunSettingsFilePathElementName, null, 1, projectRunSettingsFilePath) == VSConstants.S_OK;
success = vsBuildPropertyStorage.SetPropertyValue(projectRunSettingsFilePathElementName, string.Empty, (uint)_PersistStorageType.PST_PROJECT_FILE, projectRunSettingsFilePath) == VSConstants.S_OK;
}
}
return success;
}

public async Task<bool> RemoveRunSettingsFilePathAsync(Guid projectGuid)
{

var ok = false;
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
var vsSolution = serviceProvider.GetService(typeof(SVsSolution)) as IVsSolution;
Expand All @@ -51,7 +54,12 @@ public async Task<bool> RemoveRunSettingsFilePathAsync(Guid projectGuid)
{
if (vsHierarchy is IVsBuildPropertyStorage vsBuildPropertyStorage)
{
ok = vsBuildPropertyStorage.RemoveProperty(projectRunSettingsFilePathElementName, null, 1) == VSConstants.S_OK;
ok = vsBuildPropertyStorage.RemoveProperty(projectRunSettingsFilePathElementName, string.Empty, (uint)_PersistStorageType.PST_PROJECT_FILE) == VSConstants.S_OK;

if (ok)
{
this.projectSaver.SaveProject(vsHierarchy);
}
}
}
return ok;
Expand Down
1 change: 1 addition & 0 deletions SharedProject/SharedProject.projitems
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Core\Model\VsBuildFCCSettingsProvider.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Core\MsTestPlatform\CodeCoverage\ITemplatedRunSettingsService.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Core\MsTestPlatform\CodeCoverage\MsTemplateReplacementException.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Core\MsTestPlatform\CodeCoverage\ProjectSaver.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Core\MsTestPlatform\CodeCoverage\RunSettingsTemplate.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Core\MsTestPlatform\CodeCoverage\CustomRunSettingsTemplateProvider.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Core\MsTestPlatform\CodeCoverage\IRunSettingsTemplate.cs" />
Expand Down

0 comments on commit aab53e4

Please sign in to comment.