forked from FortuneN/FineCodeCoverage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVsRunSettingsWriter.cs
66 lines (60 loc) · 2.64 KB
/
VsRunSettingsWriter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
using Microsoft;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
using System;
using System.ComponentModel.Composition;
using System.Threading.Tasks;
using FineCodeCoverage.Core.MsTestPlatform.CodeCoverage;
using FineCodeCoverage.Core.Utilities;
namespace FineCodeCoverage.Engine.MsTestPlatform.CodeCoverage
{
[Export(typeof(IVsRunSettingsWriter))]
internal class VsRunSettingsWriter : IVsRunSettingsWriter
{
private const string projectRunSettingsFilePathElementName = "RunSettingsFilePath";
private readonly IServiceProvider serviceProvider;
private readonly IProjectSaver projectSaver;
private readonly IProjectFilePropertyWriter projectFilePropertyWriter;
[ImportingConstructor]
public VsRunSettingsWriter(
[Import(typeof(SVsServiceProvider))]
IServiceProvider serviceProvider,
IProjectSaver projectSaver,
IProjectFilePropertyWriter projectFilePropertyWriter
)
{
this.serviceProvider = serviceProvider;
this.projectSaver = projectSaver;
this.projectFilePropertyWriter = projectFilePropertyWriter;
}
public async Task<bool> WriteRunSettingsFilePathAsync(Guid projectGuid, string projectRunSettingsFilePath)
{
var success = false;
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
var vsSolution = serviceProvider.GetService(typeof(SVsSolution)) as IVsSolution;
Assumes.Present(vsSolution);
if (vsSolution.GetProjectOfGuid(ref projectGuid, out var vsHierarchy) == VSConstants.S_OK)
{
success = await projectFilePropertyWriter.WritePropertyAsync(vsHierarchy, projectRunSettingsFilePathElementName, projectRunSettingsFilePath);
}
return success;
}
public async Task<bool> RemoveRunSettingsFilePathAsync(Guid projectGuid)
{
var ok = false;
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
var vsSolution = serviceProvider.GetService(typeof(SVsSolution)) as IVsSolution;
Assumes.Present(vsSolution);
if (vsSolution.GetProjectOfGuid(ref projectGuid, out var vsHierarchy) == VSConstants.S_OK)
{
ok = await projectFilePropertyWriter.RemovePropertyAsync(vsHierarchy, projectRunSettingsFilePathElementName);
if (ok)
{
await this.projectSaver.SaveProjectAsync(vsHierarchy);
}
}
return ok;
}
}
}