-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOpenBinFolderCommand.cs
More file actions
126 lines (106 loc) · 3.9 KB
/
Copy pathOpenBinFolderCommand.cs
File metadata and controls
126 lines (106 loc) · 3.9 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
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
{
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();
var projectOutputPath = project
.ConfigurationManager.ActiveConfiguration.Properties.Item("OutputPath")
.Value.ToString();
var 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}"
);
}
}
}
}