NGitVersion
NGitVersion copied to clipboard
Automatic versioning of C# and C++ DLLs (CLI and Native) based on Git Repository information
Current Status:
NGitVersion
Auto-increment your DLL versions based on git history.
Out-of-the-box NGitVersion populates your DLLs with the following versions:
- File Version:
1.0.0.34, where34is the Git auto-incremented version - Product Version:
1.0.0.34, Hash b716d1b, BuildConfig DEBUG, HasLocalChange True, where:Hashis the git "short hash" of the current commitBuildConfigis the build configuration (DEBUG/RELEASE)HasLocalChangetells whether some file has been locally edited
NGitVersion introduces a C# project called NGitVersion that generates on build two files GLobalAssemblyInfo.cs and GLobalAssemblyInfo.cpp. Just reference the NGitVersion project and GlobalAssemblyInfo.* files in your existing projects in order to get it work with your own DLLs.
NGitVersion is only a few lines of code. It is based on the template engine StringTemplate and the git-client LibGit2Sharp.
You can easily:
- Edit the templates, that generate the
GLobalAssemblyInfo.*files - Edit the underlying model
Model.csto change the major and minor version, product name... - Extend the underlying model
- to get more information from your Git repository (i.e. current branch, last commit description)
- to get additional information from your build environment, like OS version/architecture, tool version, date...
- Add more templates
- i.e. to generate a file containing the commit-history, the patch of the local changes
Usage
1) Get NGitVersion
Clone NGitVersion repository and copy its /src/NGitVersion/ subfolder to your target solution folder.
Alternatively, if you are confident with git subtree:
git subtree add --prefix my/target/folder https://github.com/jeromerg/NGitVersion master --squash
(update command)
git subtree pull --prefix my/target/folder https://github.com/jeromerg/NGitVersion master --squash
2) Add NGitVersion project to your Visual Studio solution
Add the /src/NGitVersion/NGitVersion.csproj to your solution.
3) Build
Typical issue: Antlr4.StringTemplate and LibGit2Sharp are not found: Fix the path to NuGet packages folder in the NGitVersion.csproj file:
- Either search and fix
..\packages\string within in the project file - Or remove/re-install nuget-dependencies
Antlr4.StringTemplateandLibGit2Sharp
Linux/mono issue: install 0.23-pre version of LibGit2Sharp with 'mono ~/bin/nuget/nuget.exe install LibGit2Sharp -Pre' to avoid dll not found errors.
The build generates files into the src\NGitVersion\Generated\ folder. Check that they exist and are properly generated from the templates located in src\NGitVersion\Templates\.
Upgrade your projects
Now you can upgrade your projects to support Git automatic DLL versioning.
C# Project
-
Add NGitVersion project as project reference
-
Add manually the
ReferenceOutputAssemblyelement to yourCSharp.csproj(optional):<ProjectReference Include="..\..\NGitVersion\NGitVersion.csproj"> <ReferenceOutputAssembly>false</ReferenceOutputAssembly> <Project>{badb59fb-1a14-42af-b314-152c4787f437}</Project> <Name>NGitVersion</Name> </ProjectReference>
From MSDN: If true, the assembly is used on the compiler command line during the build. ... We just reference the NGitVersion project to ensure the correct build-order, so we disable this functionality.
- Add file
src\NGitVersion\Generated\GlobalAssemblyInfo.csas a link (Add by using the Add as link button in Add Existing Item dialog) - Remove conflicting entries from the existing project
AssemblyInfo.cs. - Build: Check that the built DLL contains the metadata defined in the generated
GlobalAssemblyInfo.csfile.
Native and C++ CLI Project
-
Add NGitVersion project as project reference
- Uncheck "Properties>Reference Assembly Output" in Add Reference dialog
-
Copy and include following files into your project (eventually merge with existing):
- For C++ and C++ CLI:
NGitVersion\Resource\version.rc2as Resource Compiler - For C++ CLI:
NGitVersion\Resource\Assembly.cppas C/C++ Compiler
- For C++ and C++ CLI:
-
Fix all TODOs in both files, in particular the relative path to the generated
src\NGitVersion\Generated\GlobalAssemblyInfo.hfile. -
Build: Check that the built DLL contains the metadata defined in the generated
GlobalAssemblyInfo.hfile.
Customize
- Customize model file /src/NGitVersion/Model/Model.cs (major, minor, build versions, company name...)
- Edit/Add templates in /src/NGitVersion/Templates/