It seems as though I've dropped off from blogging, and that surely was not my intention. So without further ado, let's jump into another useful piece of code. People have been asking for this feature for quite some time now, and I'll be sure to push for it to be built-in to our next release. In the meantime, it's a devastatingly simple macro :) Just to clarify, here is the problem: while coding in Visual Studio, you may find that you only want to compile a project, instead of the entire solution. To this effect, there are a set of Build/Rebuild/Clean commands for the current project. However, the issue is that the commands apply to the current project. What is that anyway? It's the project corresponding to the file currently active in the editor. Crazy? It makes sense for a whole bunch of cases, but obviously not all. A lot of times, what users really want is to Build/Rebuild/Clean the startup project. And thus the following 3 macros.
Dim sb As SolutionBuild = DTE.Solution.SolutionBuild
Dim projName As String = sb.StartupProjects(0)
sb.BuildProject(sb.ActiveConfiguration.Name, projName, False)
Wait a sec. Didn't I just say I'd provide 3 macros? One for each command: Build, Rebuild and Clean? I did. But as I was coding these macros, I discovered an unfortunate deficiency in our current Extensibility API. We only provide a BuildProject function, and none of the other two. Until this is fixed, we can write the following hacky macros...
sb As SolutionBuild = DTE.Solution.SolutionBuild
String = sb.StartupProjects(0)
As SolutionContexts =
= New Dictionary(Of
' save current build states
Each scon As
SolutionContext In solContexts
stateDic(scon.ProjectName) = scon.ShouldBuild
scon.ShouldBuild = True
scon.ShouldBuild = False
' clean "solution" with only one project enabled
' restore previous state
scon.ShouldBuild = stateDic(scon.ProjectName)
What these virtually identical
macros do is Clean/Rebuild the entire solution but I temporarily mark all
projects not to be built, save for the startup project. There you have it. The
remaining flaw is my assumption that you only use a single startup
project, while VS supports multiple...