Skip to content

Instantly share code, notes, and snippets.

@mloskot
Last active April 11, 2018 08:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mloskot/74b7f94c27cbaa3425b4855fda0f466b to your computer and use it in GitHub Desktop.
Save mloskot/74b7f94c27cbaa3425b4855fda0f466b to your computer and use it in GitHub Desktop.
PowerShell script to add toolbar with CMake commands to Visual Studio 2017
<#
.SYNOPSIS
Adds toolbar with CMake commands to Visual Studio 2017.
Workaround for "No toolbar or shortcuts for CMake integration", see
https://developercommunity.visualstudio.com/content/problem/199214/no-toolbar-or-shortcuts-for-cmake-integration.html
.DESCRIPTION
Visual Studio 2017 comes with integration for CMake,
with commands available from the dedicated top-level CMake menu.
The Add-CMakeToolbar adds complementary toolbar to improve CMake experience.
Author: Mateusz Loskot <mateusz@loskot.net>
.NOTES
CMake > Cache commands seem too volatile.
For example, OtherContextMenus.WorkspaceContextMenu.Cache.CMake.GenerateCache
if added to toolbar, it remains greyed iyt and never gets enabled.
.EXAMPLE
1. Visual Studio Installer > Individual components > Code tools > NuGet package manager
2. Launch Visual Studio 2017 > Tools > NuGet Package Manager > Package Manager Console
3. PM> . .\Add-CMakeToolbar.ps1
4. PM> Add-CMakeToolbar($DTE)
#>
function Add-CMakeToolbar
{
[CmdletBinding()]
Param([object] $VisualStudioDTE)
# CMake toolbar configuration
$cmakeBarName = "CMake Build"
$cmakeBarCommandsConfig = @(
@{n="File.CMake";c="CMake"},
# -----
@{n="CMake.CMake.BuildAll";c="Build";g=$true},
@{n="CMake.CMake.RebuildAll";c="Rebuild"},
@{n="CMake.CMake.CleanAll";c="Clean"},
@{n="CMake.CMake.CancelBuild";c="Cancel Build"},
# -----
@{n="CMake.CMake.ViewTests";c="View Tests";g=$true}
)
# end of configuration
if ($env:VisualStudioVersion -ne "15.0") {
throw "Visual Studio 2017 not found"
}
if (-not $DTE) {
throw "Visual Studio 2017 Development Tools Environment not found"
}
# Remove existing custom toolbar, skip the built-in CMake menu
$cmakeBar = $DTE.CommandBars[$cmakeBarName]
if ($cmakeBar -and -not $cmakeBar.BuiltIn) {
Write-Host "Removing custom '$cmakeBarName' toolbar..."
$DTE.Commands.RemoveCommandBar($cmakeBar)
}
Write-Host "Collecting '$cmakeBarName' commands..."
$cmakeCommands = $DTE.Commands | Where-Object { $_.Name -match "CMake" }
Write-Host "Creating custom '$cmakeBarName' toolbar..."
$cmakeBar = $DTE.Commands.AddCommandBar($cmakeBarName, [EnvDTE.vsCommandBarType]::vsCommandBarTypeToolbar)
foreach ($cmdConfig in $cmakeBarCommandsConfig.GetEnumerator()) {
$cmd = $cmakeCommands | Where-Object { $_.Name -eq $cmdConfig["n"] }
Write-Host ("Adding '{0}' command..." -f $cmd.Name)
$cmakeBarCmd = $cmd.AddControl($cmakeBar, $cmakeBar.Controls.Count + 1)
$cmakeBarCmd.Caption = $cmdConfig["c"]
$cmakeBarCmd.BeginGroup = $cmdConfig.ContainsKey("g")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment