Repository navigation
Expand file tree
/
Copy pathProjectHelper.cs
More file actions
107 lines (96 loc) · 4.26 KB
/
Copy pathProjectHelper.cs
File metadata and controls
107 lines (96 loc) · 4.26 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.VisualStudio.Shell.Interop;
using MsBuildProject = Microsoft.Build.Evaluation.Project;
using EnvDTE;
#if VS12
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.ProjectSystem;
using Microsoft.VisualStudio.ProjectSystem.Designers;
using NuGet;
using System.Threading;
#endif
namespace NuGet.VisualStudio12
{
public static class ProjectHelper
{
#if VS12
/// <summary>
/// Performs an action inside a VS internal writer lock. If called from the UI thread this method will
/// run async to avoid deadlocks.
/// </summary>
public static void DoWorkInWriterLock(Project project, IVsHierarchy hierarchy, Action<MsBuildProject> action)
{
// Perform this work on a new thread to avoid moving our current thread, and so it can be done async if needed.
var task = Task.Run(() => DoWorkInWriterLockInternal(project, hierarchy, action));
// Check if we are running on the UI thread. If we are we cannot risk blocking and holding the lock.
// Ideally all calls involving the lock should be done on a background thread from the start to
// keep the call as synchronous as possible within NuGet.
if (!Microsoft.VisualStudio.Shell.ThreadHelper.CheckAccess())
{
// If we are on a background thread we can safely run synchronously.
task.Wait();
}
}
private static async Task DoWorkInWriterLockInternal(Project project, IVsHierarchy hierarchy, Action<MsBuildProject> action)
{
UnconfiguredProject unconfiguredProject = GetUnconfiguredProject((IVsProject)hierarchy);
if (unconfiguredProject != null)
{
var service = unconfiguredProject.ProjectService.Services.ProjectLockService;
if (service != null)
{
// WriteLockAsync will move us to a background thread.
using (ProjectWriteLockReleaser x = await service.WriteLockAsync())
{
await x.CheckoutAsync(unconfiguredProject.FullPath);
ConfiguredProject configuredProject = await unconfiguredProject.GetSuggestedConfiguredProjectAsync();
MsBuildProject buildProject = await x.GetProjectAsync(configuredProject);
if (buildProject != null)
{
action(buildProject);
}
await x.ReleaseAsync();
}
// perform the save synchronously
await Task.Run(() =>
{
// move to the UI thread for the rest of this method
unconfiguredProject.ProjectService.Services.ThreadingPolicy.SwitchToUIThread();
var fileSystem = new PhysicalFileSystem(@"c:\");
fileSystem.MakeFileWritable(project.FullName);
project.Save();
}
);
}
}
}
private static UnconfiguredProject GetUnconfiguredProject(IVsProject project)
{
IVsBrowseObjectContext context = project as IVsBrowseObjectContext;
if (context == null)
{
IVsHierarchy hierarchy = project as IVsHierarchy;
if (hierarchy != null)
{
object extObject;
if (ErrorHandler.Succeeded(hierarchy.GetProperty((uint)VSConstants.VSITEMID.Root, (int)__VSHPROPID.VSHPROPID_ExtObject, out extObject)))
{
Project dteProject = extObject as Project;
if (dteProject != null)
{
context = dteProject.Object as IVsBrowseObjectContext;
}
}
}
}
return context != null ? context.UnconfiguredProject : null;
}
#else
public static void DoWorkInWriterLock(Project project, IVsHierarchy hierarchy, Action<MsBuildProject> action)
{
}
#endif
}
}