Useful Code: Build Startup Project


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.

    Sub BuildStartupProject()

        Dim sb As SolutionBuild = DTE.Solution.SolutionBuild

        Dim projName As String = sb.StartupProjects(0)

        sb.BuildProject(sb.ActiveConfiguration.Name, projName, False)

    End Sub

 

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… 

 

   
Sub
CleanStartupProject()

       
Dim
sb As SolutionBuild = DTE.Solution.SolutionBuild

       
Dim

startupProjectName As
String
= sb.StartupProjects(0)

       
Dim
solContexts
As SolutionContexts =
sb.ActiveConfiguration.SolutionContexts

       
Dim
stateDic
As Dictionary(Of
String, Boolean)
= New Dictionary(Of
String, Boolean)

 

       
‘ save current build states

       
For

Each scon As
SolutionContext In solContexts

           
stateDic(scon.ProjectName) = scon.ShouldBuild

           
If

scon.ProjectName.Equals(startupProjectName) Then

               
scon.ShouldBuild = True

           
Else

               
scon.ShouldBuild = False

           
End

If

       
Next

 

       
‘ clean “solution” with only one project enabled

       
sb.Clean(True)

 

       
‘ restore previous state

       
For

Each scon As
SolutionContext In solContexts

           
scon.ShouldBuild = stateDic(scon.ProjectName)

       
Next

   
End
Sub

 

 

   
Sub
RebuildStartupProject()

       
Dim
sb As SolutionBuild = DTE.Solution.SolutionBuild

       
Dim

startupProjectName As
String
= sb.StartupProjects(0)

       
Dim
solContexts
As SolutionContexts =
sb.ActiveConfiguration.SolutionContexts

       
Dim
stateDic
As Dictionary(Of
String, Boolean)
= New Dictionary(Of
String, Boolean)

 

       
‘ save current build states

       
For

Each scon As
SolutionContext In solContexts

           
stateDic(scon.ProjectName) = scon.ShouldBuild

           
If

scon.ProjectName.Equals(startupProjectName) Then

               
scon.ShouldBuild = True

           
Else

               
scon.ShouldBuild = False

           
End

If

       
Next

 

       
‘ clean “solution” with only one project enabled

       
sb.Clean(True)

       
sb.Build(True)

 

       
‘ restore previous state

       
For

Each scon As
SolutionContext In solContexts

           
scon.ShouldBuild = stateDic(scon.ProjectName)

       
Next

   
End
Sub

 

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…


Comments (10)

  1. Anonymous says:

    I cannot run the first macro.

    I get a "Cannot perform this action a Build, Debug, Deploy, Run or Clean action is in progress" message?

  2. Anonymous says:

    VS2005 have additional extention in this behaviour…

    SolutionBuild depends on VisualStudio options:

    VStudio|Options|Project and Solutions|Build and Run

    Check box – "Only build startup project and dependencies on Run"

    For some reason it is driving SolutionBuild.Build() behaviour.

    If mentioned check box is checked then SolutonBuild.Build() will build only startup (or current) proect.

    If not checked – all proects will be build.

  3. Anonymous says:

    mmm.. nice design, I must say..

  4. Anonymous says:

    Du musst ein Fachmann sein – wirklich guter Aufstellungsort, den du hast!

  5. Anonymous says:

    Chi ha fatto questo? E un buon posto per trovare le informazioni importanti!:)

  6. Anonymous says:

    <p>Welcome to our website for you World of Warcraft Gold,Wow Gold,Cheap World of Warcraft Gold,cheap wow gold,buy cheap wow gold,real wow gold,sell wow gold, …<a href="http://powerlevels.mgsale.com/"><strong>wow powerleveling</strong></a>  Here wow gold of 1000 gold at $68.99-$80.99  ,World Of Warcraft Gold,buy wow gold,sell world of warcraft gold(wow gold),buy euro gold wow Cheap wow gold,cheapest wow gold store … buy euro gold wow wow gold–buy cheap wow gold,sell wow gold.<strong>wow powerleveling</strong> welcome to buy cheap wow gold–cheap, easy, <a href="http://powerlevels.mgsale.com/"><strong>powerleveling</strong></a&gt; wow gold purchasing.World of Warcraft,wow gold Super …

    We can have your wow gold,buy wow gold,wow gold game,world of warcraft gold, wow Gold Cheap wow, Cheap wow gold,world of warcraft gold deal,Cheap WOW Gold …

    Welcome to our website for you World of Warcraft Gold,Wow Gold,Cheap World of Warcraft Gold,wow gold,buy cheap wow gold,real wow gold,sell wow gold, …

    Here wow gold of 1000 gold at $68.99-$80.99,<strong>powerleveling</strong> World Of Warcraft Gold,buy wow gold,sell world of warcraft gold(wow gold),buy gold wow lightninghoof instock Cheap wow gold,cheapest wow gold store …

    wow gold–buy cheap wow gold,sell wow gold.welcome to buy cheap wow gold–cheap, easy, wow gold purchasing.World of Warcraft,wow gold Super …

    Wow gold- Gold for buy gold wow lightninghoof instock EU-Server: …wow Gold EU: starting from 84,99?; 3000 WoW Gold EU: starting from 119,99?. wow Gold- Leveling Services: …

    We can have your wow Gold,buy wow Gold,wow Gold game,wow gold, Cheap wow Gold, Cheap World of Warcraft Gold,world of warcraft gold deal,buy cheap wow gold,Cheap WOW Gold …

    Here wow Gold of 1000 gold at $68.99-$80.99,World Of Warcraft Gold,buy wow Gold,sell world of warcraft gold(wow gold),Cheap wow gold,cheapest World of Warcraft Gold store … </p>