forked from PhilJollans/ProjectDepender2019
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProjectDependencies.cs
229 lines (199 loc) · 7.02 KB
/
ProjectDependencies.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
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
//------------------------------------------------------------------------------
// <copyright file="ProjectDependencies.cs" company="Company">
// Copyright (c) Company. All rights reserved.
// </copyright>
//------------------------------------------------------------------------------
using System;
using System.Linq;
using System.ComponentModel.Design;
using System.Globalization;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
using EnvDTE;
using VSLangProj;
using System.Diagnostics;
using System.Collections;
using System.Collections.Generic;
using System.Windows;
namespace ProjectDepender
{
/// <summary>
/// Command handler
/// </summary>
internal sealed class ProjectDependencies
{
/// <summary>
/// Command ID.
/// </summary>
public const int CommandId = 0x0100;
/// <summary>
/// Command menu group (command set GUID).
/// </summary>
public static readonly Guid CommandSet = new Guid("be92087d-77ad-4325-b413-ec0139a64587");
/// <summary>
/// VS Package that provides this command, not null.
/// </summary>
private readonly Package package;
/// <summary>
/// Initializes a new instance of the <see cref="ProjectDependencies"/> class.
/// Adds our command handlers for menu (commands must exist in the command table file)
/// </summary>
/// <param name="package">Owner package, not null.</param>
private ProjectDependencies(Package package)
{
if (package == null)
{
throw new ArgumentNullException("package");
}
this.package = package;
OleMenuCommandService commandService = this.ServiceProvider.GetService(typeof(IMenuCommandService)) as OleMenuCommandService;
if (commandService != null)
{
var menuCommandID = new CommandID(CommandSet, CommandId);
var menuItem = new MenuCommand(this.MenuItemCallback, menuCommandID);
commandService.AddCommand(menuItem);
}
}
/// <summary>
/// Gets the instance of the command.
/// </summary>
public static ProjectDependencies Instance
{
get;
private set;
}
/// <summary>
/// Gets the service provider from the owner package.
/// </summary>
private IServiceProvider ServiceProvider
{
get
{
return this.package;
}
}
/// <summary>
/// Initializes the singleton instance of the command.
/// </summary>
/// <param name="package">Owner package, not null.</param>
public static void Initialize(Package package)
{
Instance = new ProjectDependencies(package);
}
/// <summary>
/// This function is the callback used to execute the command when the menu item is clicked.
/// See the constructor to see how the menu item is associated with this function using
/// OleMenuCommandService service and MenuCommand class.
/// </summary>
/// <param name="sender">Event sender.</param>
/// <param name="e">Event args.</param>
private void MenuItemCallback(object sender, EventArgs e)
{
//string message = string.Format(CultureInfo.CurrentCulture, "Inside {0}.MenuItemCallback()", this.GetType().FullName);
//string title = "ProjectDependencies";
//// Show a message box to prove we were here
//VsShellUtilities.ShowMessageBox(
// this.ServiceProvider,
// message,
// title,
// OLEMSGICON.OLEMSGICON_INFO,
// OLEMSGBUTTON.OLEMSGBUTTON_OK,
// OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST);
try
{
DTE dte_object = (package as ProjectDependenciesPackage).dte_object ;
var vm = new DependenciesViewModel ( dte_object ) ;
// Version 1.3
// Get a flat list of projects, recursing into solution folders
var ProjectList = GetFlatListOfProjects ( dte_object.Solution ) ;
// Version 1.1
// Start by building a list of project dependencies which we can show in a grid.
foreach ( BuildDependency dep in dte_object.Solution.SolutionBuild.BuildDependencies )
{
// Build a list of references, which we will use below
var refs = new List<string>() ;
var prj = dep.Project ;
var vsproj = prj.Object as VSProject ;
if ( vsproj != null )
{
foreach ( Reference refer in vsproj.References )
{
string UsedName = refer.Name ;
// Ad hoc rules to handle interop components
if ( UsedName.StartsWith ( "Interop." ) )
{
UsedName = UsedName.Substring(8) ;
if ( UsedName.EndsWith ( "Lib" ) )
{
UsedName = UsedName.Substring ( 0, UsedName.Length-3 ) ;
}
}
// Add to the list
refs.Add ( UsedName ) ;
}
}
// Loop over all of the projects and determine if the 'parent' project
// has a build dependency or a reference to each one
foreach ( Project p2 in ProjectList )
{
var deps = dep.RequiredProjects as IEnumerable ;
var IsBuildDependency = deps.Cast<Project>().Contains ( p2 ) ;
var IsReference = refs.Contains ( p2.Name ) ;
Debug.WriteLine ( "{0},{1},{2},{3}", prj.Name, p2.Name, IsBuildDependency, IsReference ) ;
if ( IsBuildDependency || IsReference )
{
// Add an reference item to show
vm.ReferenceItems.Add ( new ReferenceItem ( dep, prj, p2, IsBuildDependency, IsReference ) ) ;
}
}
}
if ( vm.ReferenceItems.Count == 0 )
{
MessageBox.Show ( "No project dependencies or references found." ) ;
}
else
{
var dv = new DependenciesView ( vm ) ;
dv.ShowDialog() ;
}
}
catch ( Exception ex )
{
string msg = string.Format ( "(0)\n{1}\n{2}", ex.GetType().ToString(), ex.Message, ex.StackTrace ) ;
VsShellUtilities.ShowMessageBox(
this.ServiceProvider,
msg,
"Project Depender",
OLEMSGICON.OLEMSGICON_INFO,
OLEMSGBUTTON.OLEMSGBUTTON_OK,
OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST);
}
}
private List<Project> GetFlatListOfProjects ( Solution s )
{
var results = new List<Project>() ;
foreach ( Project p in s.Projects )
{
if ( p.Kind == EnvDTE.Constants.vsProjectKindSolutionItems )
ProcessSolutionFolder ( p, results ) ;
else
results.Add ( p ) ;
}
return results ;
}
private void ProcessSolutionFolder ( Project p, List<Project> results )
{
foreach ( ProjectItem pi in p.ProjectItems )
{
Project pp = pi.Object as Project;
if ( pp != null )
{
if (pp.Kind == EnvDTE.Constants.vsProjectsKindSolution )
ProcessSolutionFolder ( pp, results ) ;
else
results.Add ( pp ) ;
}
}
}
}
}