Source code for this article: https://github.com/lukasz-lysik/teamcity-dependencies-tree.
Let's imagine, that we have an application which consists of many components. Each component consists of many projects (Visual Studio projects), e.g. Data Access, Data Model, Web, Test, etc. Each project can reference projects in other components. The hierarchy of dependencies can be deep and wide.
To speed up the build process, we would like to know which project was changed during the last commit (check-in) and build only this project along with projects one level higher in the hierarchy (projects that reference this one). There is no need to compile project that are lower in the hierarchy, neither the projects that are two or move levels above.
The first step to implement this would be to maintain a file which contains list of all the projects in the repository with their dependencies. This file could be used by build scripts to figure out which projects should be build.
In this article I'll show how to use Team City to track project dependencies. As an addition I'd like to visualize the dependency tree and display it in Team City. I'll not focus on the other parts, like deciding which projects to run (it will be covered in the next article).
a href="/assets/tree2.png"> Final Result: Project Dependencies in Team City.
- In a repository (I'll use Git) we have many Visual Studio projects (only C# projects).
- Build server stores all binary files in one folder. All the projects reference assemblies in this folder (there is no project-to-project reference). After a project is built, the assemblies are copied to this folder. This assumption will be important when we will be getting references from *.csproj files.
- We want to generate a file which will contain project dependency tree.
- After a developer adds or removes a reference, we want Team City to update the file.
- We want to use the dependencies file to generate a graphical representation of the tree.
We would like to have a separate build configuration which will run on each check-in. This configuration will create file with dependencies. The table below presents the problems we want to solve and possible solutions:
|Setup TeamCity to track changes in *.cproj files only.||Use VCS build trigger filter.|
|Check-out *.csproj only (there is no need to check-out full repository).||Unsolved yet. I've done quick research but I couldn't find a clean way to check-out files with specified extension. We will check-out whole repository.|
|Make the tasks be run by Team City.||I'll create two MSBuild tasks: BuildDependencies and DrawDependencies.|
|Parse *.csproj files||I'll use Project class from Microsoft.Build.Evaluation namespace. I've got the idea from this Stack Overflow answer, which suggests using Microsoft.Build.Engine.Project class (which is deprecated).|
|Draw a tree.||I'll use Graphviz. It has easy to use language, which requires specifying list of edges, rather than full tree.|
- Lines 2-3: I've defined two properties. InputFolder is the folder name which we will scan searching for *.csproj files.
- Line 6-18: The only method which should be overridden when creating custom MSBuild tasks is Execute. This method contains the code which is executed when the task is run. In our case we simply iterate through all the *.csproj files within the directory pointed by InputFolder property.
- Line 22: Use Project class. It is a wrapper for *.csproj files.
- Line 23: Get project's assembly name
- Lines 25-27: Create XML element "Project" and specify it's attributes: name and path
- Lines: 29-37: Iterate though "Reference" nodes in *.csproj file. As I stated before, we don't have project-to-project references so we can skip "ProjectReference" elements. We also skip assemblies which starts with "System" or "Microsoft".
As a result of this task execution I get the following XML file:
The tasks should take previously generated XML file with dependencies and produce *.gv file which will be later used by Graphviz.
Most of the code above doesn't need any explanation. However, there are part specific to Graphviz language, which should be described:
- Line 12: Graphviz file header.
- Line 13: Set the default graph appearance: filled, rounded rectangles with lighblue fill.
- Line 21: Attach edge in the form: "Source Node" -> "Destination Node";.
File produced by the above task:
Now that we have all needed tasks, let's create MSBuild script:
In the PropertyGroup section we define: directory where Team City checks-out repository, output directory, where XML, GV and PNG files should be placed, and path to Graphviz. Also, we need to include our tasks in the build script (using UsingTask).
Our build script have only 3 steps:
- BuildDependencies - which scans all *.csproj files and produces dependencies.xml file.
- DrawDependencies - which produces dependencies.gv file
- Exec command which compiles dependencies.gv into graphical representation: dependencies.png
Final Team City Configuration
- Create build configuration. Create VCS trigger.
- Add one build step which runs prepared MSBuild script.
- In order to see the the tree in Team City we will need to add single artifact which ofcourse will be our dependencies.png file.
- Use Report Tabs to add new tab
Let's now change project dependencies. Wait for the build to finish:
And see updated tree:
I hope you'll find this article useful.