Skip to content

Commit f00e937

Browse files
authored
fix: add C++ project support for Open Bin Folder command (#25)
- Add Microsoft.VisualStudio.VCProjectEngine package reference - Detect C++ projects using Project.Kind GUID - Use VCProject/VCConfiguration APIs to get output directory - Use Evaluate("$(OutDir)") to properly expand MSBuild macros Fixes #4
1 parent 0c67c33 commit f00e937

3 files changed

Lines changed: 55 additions & 5 deletions

File tree

src/CodingWithCalvin.OpenBinFolder/CodingWithCalvin.OpenBinFolder.csproj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="CodingWithCalvin.VsixSdk/0.3.0">
1+
<Project Sdk="CodingWithCalvin.VsixSdk/0.4.0">
22

33
<PropertyGroup>
44
<TargetFramework>net48</TargetFramework>
@@ -11,6 +11,8 @@
1111
<ItemGroup>
1212
<PackageReference Include="CodingWithCalvin.Otel4Vsix" Version="0.2.2" />
1313
<PackageReference Include="Microsoft.VisualStudio.SDK" Version="17.14.40265" />
14+
<PackageReference Include="Microsoft.VSSDK.BuildTools" Version="17.*" PrivateAssets="all" />
15+
<PackageReference Include="Microsoft.VisualStudio.VCProjectEngine" Version="17.14.40264" />
1416
</ItemGroup>
1517

1618
<ItemGroup>

src/CodingWithCalvin.OpenBinFolder/Commands/OpenBinFolderCommand.cs

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,19 @@
77
using EnvDTE;
88
using EnvDTE80;
99
using Microsoft.VisualStudio.Shell;
10+
using Microsoft.VisualStudio.VCProjectEngine;
1011
using Project = EnvDTE.Project;
1112

1213
namespace CodingWithCalvin.OpenBinFolder.Commands
1314
{
1415
internal class OpenBinFolderCommand
1516
{
17+
// Project type GUIDs
18+
private const string CSharpProjectKind = "{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}";
19+
private const string VbNetProjectKind = "{F184B08F-C81C-45F6-A57F-5ABD9991F28F}";
20+
private const string FSharpProjectKind = "{F2A71F9B-5D33-465A-A702-920D77279786}";
21+
private const string CppProjectKind = "{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}";
22+
1623
private readonly Package _package;
1724

1825
private OpenBinFolderCommand(Package package)
@@ -93,11 +100,19 @@ private void OpenProjectBinFolder(Project project)
93100
Path.GetDirectoryName(project.FullName)
94101
?? throw new InvalidOperationException();
95102

96-
var projectOutputPath = project
97-
.ConfigurationManager.ActiveConfiguration.Properties.Item("OutputPath")
98-
.Value.ToString();
103+
string projectBinPath;
99104

100-
var projectBinPath = Path.Combine(projectPath, projectOutputPath);
105+
if (IsCppProject(project.Kind) && project.Object is VCProject vcProject)
106+
{
107+
projectBinPath = GetCppOutputPath(vcProject, projectPath);
108+
}
109+
else
110+
{
111+
var projectOutputPath = project
112+
.ConfigurationManager.ActiveConfiguration.Properties.Item("OutputPath")
113+
.Value.ToString();
114+
projectBinPath = Path.Combine(projectPath, projectOutputPath);
115+
}
101116

102117
System.Diagnostics.Process.Start(
103118
Directory.Exists(projectBinPath) ? projectBinPath : projectPath
@@ -121,6 +136,30 @@ private void OpenProjectBinFolder(Project project)
121136
Exception: {ex.Message}"
122137
);
123138
}
139+
140+
bool IsCppProject(string projectKind)
141+
{
142+
return string.Equals(projectKind, CppProjectKind, StringComparison.OrdinalIgnoreCase);
143+
}
144+
145+
string GetCppOutputPath(VCProject vcProject, string projectPath)
146+
{
147+
var activeConfig = vcProject.ActiveConfiguration as VCConfiguration;
148+
if (activeConfig == null)
149+
{
150+
throw new InvalidOperationException("Unable to get active configuration for C++ project");
151+
}
152+
153+
// Evaluate expands macros like $(OutDir), $(Configuration), $(Platform), etc.
154+
var outDir = activeConfig.Evaluate("$(OutDir)");
155+
156+
if (Path.IsPathRooted(outDir))
157+
{
158+
return outDir;
159+
}
160+
161+
return Path.Combine(projectPath, outDir);
162+
}
124163
}
125164
}
126165
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"profiles": {
3+
"Debug Extension": {
4+
"commandName": "Executable",
5+
"executablePath": "C:\\Program Files\\Microsoft Visual Studio\\18\\Community\\Common7\\IDE\\devenv.exe",
6+
"commandLineArgs": "/rootSuffix Exp"
7+
}
8+
}
9+
}

0 commit comments

Comments
 (0)