jens-schaller.de

Visual Studio solutions - set project build order automatically

When you combine multiple projects into one (big) solution and the different projects have dependencies between each other, you quickly go nuts, when setting the project build order or rather the project dependencies manually on the solution.

I know what you will suggest: "Just set the dependencies using project references."

You're right, but this has one major disadvantage: When using project references, you have to set these dependencies again, when combining some of the projects into another solution for example.

Yeah, I know what comes now: "You noob, no one sets an assembly reference to a project output dir!"

You're right again, but if you copy the output of your project (assembly and documentation) into a central directory, using the post build step of a project, and set an assembly folder to this directory, you get clean assembly references and a central location for your assemblies. Of course this mostly makes sense for larger development projects.

This is one reason, why I prefer direct references to the resulting project assemblies.

So, here is what you get: SetBuildOrder is a macro, which sets the project build order automatically. It was tested under Visual Studio 2003 and 2005 using C# solutions/projects.

Installation

In Visual Studio

  • open the Macros IDE by pressing "Alt+F11", or by selecting "Tools - Macros - Macros IDE"
  • right-click on "MyMacros" and select "Add - Add Existing Item"
  • select and add SetBuildOrder.vb
  • assign a key (Tools - Options - Keyboard) or start it using the Macro Explorer (Alt+F8)

Source Code

 

Imports EnvDTE

Imports System.Diagnostics

Imports System.Windows.Forms

Imports VSLangProj

Imports System.Collections

Imports System.Text.RegularExpressions

Public Module SetBuildOrder

Sub SetBuildOrder()

Dim objVSProject As VSProject

Dim objDependency As BuildDependency

Dim hstProjects As Hashtable

' clear all existing dependencies

For Each objDependency In DTE.Solution.SolutionBuild.BuildDependencies

objDependency.RemoveAllProjects()

Next

' build hasttable containing project informations

hstProjects = New Hashtable

For Each solProject As Project In DTE.Solution.Projects

If solProject.Kind = PrjKind.prjKindCSharpProject Then

hstProjects.Add(solProject.Properties.Item("AssemblyName").Value, solProject.UniqueName)

End If

Next

' process all projects

For Each objDependency In DTE.Solution.SolutionBuild.BuildDependencies

If objDependency.Project.Kind = PrjKind.prjKindCSharpProject Then

objVSProject = CType(objDependency.Project.Object, VSProject)

' add references

For Each objReference As Reference In objVSProject.References

If hstProjects.ContainsKey(objReference.Name) Then

objDependency.AddProject(hstProjects(objReference.Name))

End If

Next

End If

Next

End Sub

End Module