This question is a follow-up of this other one.
In that question, one mentions the usage of [assembly: AssemblyVersion(...)] to the file AssemblyInfo.cs file, and in the meanwhile I've found out that it's forbidden to execute any processing before such a line, the only thing which is allowed is something like:
[assembly: AssemblyVersion("1.0.0.0" + "-" + Namespace.Class.Attribute)], or:
[assembly: AssemblyVersion("1.0.0.0" + "-" + Namespace.Class.Method())]
Original question:
So my question: is there a Namespace.Class.Attribute or Namespace.Class.Method() which contains the commit hash (or sha or shortened sha) of a C# application?
Edit after more investigation
In the meantime I've learnt that the command git describe --always gives me the information I'm looking for, so what I need is something like:
[assembly: AssemblyVersion("1.0.0.0-" + Launch("git describe --always")]
... but how can I execute that Launch()?
I already know that I can launch a commandline command using System.Diagnostics.Process(), like this example:
System.Diagnostics.Process.Start(foldervar + "application.exe", "inputfile.txt");
... but this way does not catch the result of that command.
New question:
So, does anybody know a C# one-liner for launching commandline commands and getting their result?
Thanks in advance
Not a one liner, but this article https://galdin.dev/blog/show-git-commit-sha-in-net-applications/ shows a simple way to execute the command and access the result. The steps described in this article are as follows,
To do this we create a pre-build event that'll execute a git command and save it in a text file. We then add that generated text file as a string resource. The resource can will now be accessible from C#.
Right click on the web application and click on properties.
Go to the build events tab and type the following in the Pre-build event command line:
git rev-parse HEAD --short > "$(ProjectDir)\CurrentCommit.txt"The command can obviously be replaced by anything you see fit.
Save and build the project. (Ctrl+Shift+S, Ctrl+Shift+B)
A new file called
CurrentCommit.txtshould be created in the root of your project.Go ahead and exclude it from Source Control
In the projects properties page, go to the Resources tab and Add Existing File
CurrentCommit.txtas a resource.The contents of the generated file can now be accessed as:
Your.NameSpace.Properties.Resources.CurrentCommit;