-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathBridgeVsExtension.cs
175 lines (153 loc) · 6.6 KB
/
BridgeVsExtension.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
#region License
// Copyright (c) 2013 - 2018 Coding Adventures
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit psticlersons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#endregion
using System;
using System.Collections.Generic;
using System.ComponentModel.Design;
using System.Diagnostics;
using System.IO;
using System.Linq;
using BridgeVs.VsPackage.Helper;
using BridgeVs.VsPackage.Helper.Command;
using BridgeVs.VsPackage.Helper.Configuration;
using EnvDTE;
using Microsoft.VisualStudio;
using Project = EnvDTE.Project;
namespace BridgeVs.VisualStudio.AsyncExtension
{
public class BridgeVsExtension
{
private readonly DTE _application;
public BridgeVsExtension(DTE app)
{
_application = app;
}
private static bool IsSupported(string uniqueName)
{
return
uniqueName.EndsWith(".csproj", StringComparison.InvariantCultureIgnoreCase) || uniqueName.EndsWith(".vbproj", StringComparison.InvariantCultureIgnoreCase);
}
private IEnumerable<Project> AllProjects
{
get
{
List<Project> projects = new List<Project>();//AllProjects.ToList();
foreach (Project proj in _application.Solution.Projects)
{
_findProjects(projects, proj);
}
Microsoft.VisualStudio.Shell.ThreadHelper.ThrowIfNotOnUIThread();
if (projects.Count == 0)
return Enumerable.Empty<Project>();
return from project in projects.Cast<Project>()
where (IsSupported(project.UniqueName) || IsSupported(project.FullName))
select project;
}
}
IEnumerable<Project> _findProjects(IList<Project> projects, Object projectItem)
{
var proj = projectItem as Project;
var projItm = projectItem as ProjectItem;
if (proj != null)
{
Debug.WriteLine($"Project => {proj.Name} {proj.Kind}");
}
if (projItm != null)
{
Debug.WriteLine($"ProjectItem => {projItm.Name} {projItm.Kind}");
}
if (proj == null && projItm != null)
{
proj = projItm.SubProject;
}
if (proj != null)
{
Debug.WriteLine($"Resolved Project => {proj.Name} {proj.Kind}");
}
if (proj != null && (proj.Kind == VSConstants.UICONTEXT.CSharpProject_string
|| proj.Kind == VSConstants.UICONTEXT.VBProject_string
|| IsSupported(proj.UniqueName)))
{
projects.Add(proj);
return projects;
}
else if (proj != null && (proj.Kind == VSConstants.ItemTypeGuid.VirtualFolder_string
|| proj.Kind == "{66A26720-8FB5-11D2-AA7E-00C04F688DDE}")
)
{
foreach (ProjectItem itm in proj.ProjectItems)
{
_findProjects(projects, itm);
}
return projects;
}
else if (proj == null && projItm != null && projItm.ProjectItems != null)
{
foreach (ProjectItem pi in projItm.ProjectItems)
{
_findProjects(projects, pi);
}
}
return projects;
}
private string SolutionName => Path.GetFileNameWithoutExtension(_application.Solution.FileName);
public void Execute(CommandAction action)
{
Microsoft.VisualStudio.Shell.ThreadHelper.ThrowIfNotOnUIThread();
List<Project> projects = AllProjects.ToList();
if (projects.Count == 0)
return;
if (BridgeCommand.IsEveryProjectSupported(projects, _application.Version, _application.Edition))
{
BridgeCommand.ActivateBridgeVsOnSolution(action, projects, SolutionName, _application.Version,
_application.Edition, Path.GetDirectoryName(_application.Solution.FileName));
}
else
{
string message = $@"Solution {SolutionName} contains one or more un-supported projects. ASP.NET Core, .NET Core, .NET standard and UAP are not supported by LINQBridgeVs.";
System.Windows.MessageBox.Show(message);
}
}
public void UpdateCommand(MenuCommand cmd, CommandAction action)
{
CommandStates states = GetStatus(action);
cmd.Visible = (CommandStates.Visible & states) == CommandStates.Visible;
cmd.Enabled = (CommandStates.Enabled & states) == CommandStates.Enabled;
}
private CommandStates GetStatus(CommandAction action)
{
Microsoft.VisualStudio.Shell.ThreadHelper.ThrowIfNotOnUIThread();
CommandStates result = CommandStates.Visible;
bool isBridgeVsConfigured = PackageConfigurator.IsBridgeVsConfigured(_application.Version);
if (!isBridgeVsConfigured)
return result; //just show it as visible
string solutionDir = Path.GetDirectoryName(_application.Solution.FileName);
string directoryTarget = Path.Combine(solutionDir, "Directory.Build.targets");
bool isSolutionEnabled = File.Exists(directoryTarget);
if (isSolutionEnabled && action == CommandAction.Disable || !isSolutionEnabled && action == CommandAction.Enable)
result |= CommandStates.Enabled;
return result;
}
}
}