-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOpenBinFolderCommand.cs
More file actions
164 lines (138 loc) · 5.42 KB
/
OpenBinFolderCommand.cs
File metadata and controls
164 lines (138 loc) · 5.42 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
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
using System;
using System.Collections.Generic;
using System.ComponentModel.Design;
using System.IO;
using System.Windows.Forms;
using CodingWithCalvin.Otel4Vsix;
using EnvDTE;
using EnvDTE80;
using Microsoft.VisualStudio.Shell;
using Project = EnvDTE.Project;
namespace CodingWithCalvin.OpenBinFolder.Commands
{
internal class OpenBinFolderCommand
{
// Project type GUIDs
private const string CSharpProjectKind = "{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}";
private const string VbNetProjectKind = "{F184B08F-C81C-45F6-A57F-5ABD9991F28F}";
private const string FSharpProjectKind = "{F2A71F9B-5D33-465A-A702-920D77279786}";
private const string CppProjectKind = "{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}";
private readonly Package _package;
private OpenBinFolderCommand(Package package)
{
_package = package;
var commandService = (OleMenuCommandService)
ServiceProvider.GetService(typeof(IMenuCommandService));
if (commandService == null)
{
return;
}
var menuCommandId = new CommandID(
PackageGuids.CommandSetGuid,
PackageIds.OpenBinCommandId
);
var menuItem = new MenuCommand(OpenPath, menuCommandId);
commandService.AddCommand(menuItem);
}
private IServiceProvider ServiceProvider => _package;
public static void Initialize(Package package)
{
_ = new OpenBinFolderCommand(package);
}
private void OpenPath(object sender, EventArgs e)
{
ThreadHelper.ThrowIfNotOnUIThread();
using var activity = VsixTelemetry.StartCommandActivity("OpenBinFolder.OpenPath");
try
{
if (!(ServiceProvider.GetService(typeof(DTE)) is DTE2 dte))
{
throw new ArgumentNullException(nameof(dte));
}
foreach (
UIHierarchyItem selectedItem in (Array)
dte.ToolWindows.SolutionExplorer.SelectedItems
)
{
switch (selectedItem.Object)
{
case Project project:
OpenProjectBinFolder(project);
break;
}
}
VsixTelemetry.LogInformation("Bin folder opened successfully");
}
catch (Exception ex)
{
activity?.RecordError(ex);
VsixTelemetry.TrackException(ex, new Dictionary<string, object>
{
{ "operation.name", "OpenPath" }
});
throw;
}
}
private void OpenProjectBinFolder(Project project)
{
ThreadHelper.ThrowIfNotOnUIThread();
using var activity = VsixTelemetry.StartCommandActivity("OpenBinFolder.OpenProjectBinFolder");
try
{
var projectPath =
Path.GetDirectoryName(project.FullName)
?? throw new InvalidOperationException();
string projectBinPath;
if (IsCppProject(project.Kind))
{
projectBinPath = GetCppOutputPath(project.Object, projectPath);
}
else
{
var projectOutputPath = project
.ConfigurationManager.ActiveConfiguration.Properties.Item("OutputPath")
.Value.ToString();
projectBinPath = Path.Combine(projectPath, projectOutputPath);
}
System.Diagnostics.Process.Start(
Directory.Exists(projectBinPath) ? projectBinPath : projectPath
);
VsixTelemetry.LogInformation("Opened bin folder for project");
}
catch (Exception ex)
{
activity?.RecordError(ex);
VsixTelemetry.TrackException(ex, new Dictionary<string, object>
{
{ "operation.name", "OpenProjectBinFolder" },
});
MessageBox.Show(
$@"
Unable to determine output path for selected project
{Environment.NewLine}
{Environment.NewLine}
Exception: {ex.Message}"
);
}
bool IsCppProject(string projectKind)
{
return string.Equals(projectKind, CppProjectKind, StringComparison.OrdinalIgnoreCase);
}
string GetCppOutputPath(dynamic vcProject, string projectPath)
{
dynamic activeConfig = vcProject.ActiveConfiguration;
if (activeConfig == null)
{
throw new InvalidOperationException("Unable to get active configuration for C++ project");
}
// Evaluate expands macros like $(OutDir), $(Configuration), $(Platform), etc.
string outDir = activeConfig.Evaluate("$(OutDir)");
if (Path.IsPathRooted(outDir))
{
return outDir;
}
return Path.Combine(projectPath, outDir);
}
}
}
}