I'm looking for a simple way to include the output of git describe --t --dirty in the title bar of a WPF program.
I've looked at MSBuild.Community.Tasks but I don't understand the build process too well. I haven't been able to find much recent information or any tutorial that put all the pieces together for me.
I tried writing a batch file to add a property to AssemblyInfo.cs:
@echo off
echo "Append Git Version Property"
for /F "tokens=* USEBACKQ" %%F IN (`git describe --tags --dirty`) DO (
set var=%%F
)
echo Version: %var%
set statement=[assembly: AssemblyInformationalVersion(" %var% ")]
echo Property: %statement%
echo %statement% >> AssemblyInfo.cs
This seems to work if I run it from explorer. Adding this to my project file should run it after the build completes, I think.
<PropertyGroup>
<PostBuildEvent>cmd (SolutionDir)\Properties\gitVersion.bat</PostBuildEvent>
</PropertyGroup>
It doesn't seem to change the file at all, when I do a build. I need to get it to actually add the property.
I don't know if the build creates a new file with each build. If not what I have will append a new property to the end of the file each time. That will not be good.
Update-- Whatever other issues there might be there must at least be something wrong with the way I am running it from the project file. I changed the output to a file I created, and changing it to a prebuild event. The bat file just doesn't seem to run. There are no errors.
Update 2: I'm not sure if I should continue this or start a new question. Let me know if I am breaking etiquette.
per mister Dodd's comment I added a target to the project.
<Target Name="GitVersionAttribute" BeforeTargets="BeforeBuild">
<Exec Command="git describe --tags --dirty > $(IntermediateOutputPath)version.txt" />
<ReadLinesFromFile File="$(IntermediateOutputPath)version.txt">
<Output TaskParameter="Lines" PropertyName="VersionAttribute" />
</ReadLinesFromFile>
<WriteCodeFragment AssemblyAttributes="GitVersionAttribute" Language="C#" OutputDirectory="Properties" OutputFile="GitVersionAttribute.cs" />
<ItemGroup>
<Compile Include="Properties\GitVersionAttribute.cs" />
</ItemGroup>
</Target>
I added a RunCommand to to bring in the return value from git describe. I still can't figure out how to set the value for GitVersionAttribute.
The batch file is run in the
PostBuildEventwhich is after the build. i.e. it doesn't appear to do anything because it runs late in the process and, most importantly, after compilation.From discussion in the comments it would appear that the project is a 'Legacy' style project targeting .Net Framework.
C# project files (.csproj) are MSBuild XML files. The top level document root element in an msbuild file is
Project. An 'Sdk' style project will have an 'Sdk' attribute on theProjectelement. A 'Legacy' project will not.'Sdk' projects support a set of properties for specifying many of the common assembly attributes. Since properties can be set via environment variables and the command line, it is very easy for a build system to inject the appropriate values.
'Legacy' projects don't have the properties. (But we are not stuck.)
The code
is the syntax for an Attribute.
assembly:is specifying the target of the attribute.This line of code can appear in any source file. There is nothing special about the
AssemblyInfo.csfile or name.A given assembly attribute can only be specified once for an assembly. Repeating the same assembly attribute anywhere in the source is an error.
e.g. The following code is a compile time error. Putting these two lines in separate files in the same project is still an error.
The
AssemblyInfo.csfrom the project template doesn't includeAssemblyInformationalVersionso there shouldn't be a duplication issue. But it is something to be aware of, if you decide to dynamically update other attributes that do appear in theAssemblyInfo.csfile.With Visual Studio 2015 a task was added to MSBuild named
WriteCodeFragment. Despite the name this task is not (presently) capable of writing any generic code fragment. But that's okay because the one and only type of code fragment it is capable of writing is assembly attribute code fragments. This task was added to support 'Sdk' style projects and, thankfully, it was added to MSBuild itself as a general use task.To dynamically inject an
AssemblyInformationalVersion:BeforeCompileWriteCodeFragmentto create a new file that defines the attributeIncludethe new file in theCompileitemsUpdate with Example Code
Example code to add an AssemblyInformationalVersion:
In a CI build, a property value can be passed on the command line with the
-propertyswitch.-pis the short form.e.g.
or
As a design issue I would avoid coupling the build to a specific version control tool, i.e. I would not call
gitfrom within the build code.