Skip to content

Instantly share code, notes, and snippets.

@shvydky
Last active December 13, 2023 05:28
Show Gist options
  • Save shvydky/dceb83d34c942a3a95e336250c708334 to your computer and use it in GitHub Desktop.
Save shvydky/dceb83d34c942a3a95e336250c708334 to your computer and use it in GitHub Desktop.
MSBuild/GIT/SVN auto-increment build number
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"
DefaultTargets="Settings">
<PropertyGroup>
<productVersion>0.1.1</productVersion>
</PropertyGroup>
<UsingTask
TaskName="ExtractRevisionHash"
TaskFactory="CodeTaskFactory"
AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll" >
<ParameterGroup>
<WorkCopy ParameterType="System.String" Required="true" />
<RevisionHash ParameterType="System.String" Output="true" />
</ParameterGroup>
<Task>
<Using Namespace="System" />
<Using Namespace="System.Diagnostics" />
<Using Namespace="System.IO" />
<Code Type="Fragment" Language="cs">
<![CDATA[
try {
ProcessStartInfo psi = new ProcessStartInfo("git", "rev-parse HEAD");
psi.WorkingDirectory = WorkCopy;
psi.RedirectStandardOutput = true;
psi.RedirectStandardError = true;
psi.UseShellExecute = false;
Process p = Process.Start(psi);
string line;
while ((line = p.StandardOutput.ReadLine()) != null) {
RevisionHash = line;
Log.LogMessage("Last Git Revision Hash: {0}", RevisionHash);
}
p.WaitForExit();
if (p.ExitCode != 0)
Log.LogError(p.StandardError.ReadLine());
return p.ExitCode == 0;
} catch (Exception ex) {
Log.LogError(ex.Message);
return false;
}
]]>
</Code>
</Task>
</UsingTask>
<UsingTask
TaskName="ExtractGITRevision"
TaskFactory="CodeTaskFactory"
AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll" >
<ParameterGroup>
<WorkCopy ParameterType="System.String" Required="true" />
<Revision ParameterType="System.String" Output="true" />
</ParameterGroup>
<Task>
<Using Namespace="System" />
<Using Namespace="System.Diagnostics" />
<Using Namespace="System.IO" />
<Code Type="Fragment" Language="cs">
<![CDATA[
try {
ProcessStartInfo psi = new ProcessStartInfo("git", "rev-list HEAD --count");
psi.WorkingDirectory = WorkCopy;
psi.RedirectStandardOutput = true;
psi.RedirectStandardError = true;
psi.UseShellExecute = false;
Process p = Process.Start(psi);
string line;
while ((line = p.StandardOutput.ReadLine()) != null) {
Revision = line;
Log.LogMessage("Last Git Revision: {0}", Revision);
}
p.WaitForExit();
if (p.ExitCode != 0)
Log.LogError(p.StandardError.ReadLine());
return p.ExitCode == 0;
} catch (Exception ex) {
Log.LogError(ex.Message);
return false;
}
]]>
</Code>
</Task>
</UsingTask>
<UsingTask
TaskName="ExtractSVNRevision"
TaskFactory="CodeTaskFactory"
AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll" >
<ParameterGroup>
<WorkCopy ParameterType="System.String" Required="true" />
<Revision ParameterType="System.String" Output="true" />
</ParameterGroup>
<Task>
<Using Namespace="System" />
<Using Namespace="System.Diagnostics" />
<Using Namespace="System.IO" />
<Code Type="Fragment" Language="cs">
<![CDATA[
try {
ProcessStartInfo psi = new ProcessStartInfo("svn", "info");
psi.WorkingDirectory = WorkCopy;
psi.RedirectStandardOutput = true;
psi.RedirectStandardError = true;
psi.UseShellExecute = false;
Process p = Process.Start(psi);
string line;
while ((line = p.StandardOutput.ReadLine()) != null) {
if (line.ToLower().StartsWith("url:"))
Log.LogMessage("Svn Url: {0}", line.Substring(5));
if (line.ToLower().StartsWith("last changed rev:")) {
Revision = line.Substring(18);
Log.LogMessage("Last Revision: {0}", Revision);
}
}
p.WaitForExit();
if (p.ExitCode != 0)
Log.LogError(p.StandardError.ReadLine());
return p.ExitCode == 0;
} catch (Exception ex) {
Log.LogError(ex.Message);
return false;
}
]]>
</Code>
</Task>
</UsingTask>
<UsingTask
TaskName="ReplaceText"
TaskFactory="CodeTaskFactory"
AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll" >
<ParameterGroup>
<InputFile ParameterType="System.String" Required="true" />
<OutputFile ParameterType="System.String" Required="true" />
<FindFor ParameterType="System.String" Required="true" />
<ReplaceBy ParameterType="System.String" Required="true" />
<Revision ParameterType="System.String" Output="true" />
</ParameterGroup>
<Task>
<Using Namespace="System" />
<Using Namespace="System.Diagnostics" />
<Using Namespace="System.IO" />
<Using Namespace="System.Text.RegularExpressions" />
<Code Type="Fragment" Language="cs">
<![CDATA[
try {
StringBuilder sb = new StringBuilder();
// in-memory replace
Regex rx = new Regex(FindFor);
string text = File.ReadAllText(InputFile);
text = rx.Replace(text, ReplaceBy);
if (File.Exists(OutputFile)) {
string oldText = File.ReadAllText(OutputFile);
if (oldText != text)
File.WriteAllText(OutputFile, text, Encoding.UTF8);
} else
File.WriteAllText(OutputFile, text, Encoding.UTF8);
return true;
} catch (Exception ex) {
Log.LogErrorFromException(ex);
return false;
}
]]>
</Code>
</Task>
</UsingTask>
</Project>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0"
DefaultTargets="Build">
<Import Project="global.targets" />
<PropertyGroup>
<BuildRoot>$(MSBuildProjectDirectory)\build</BuildRoot>
<Configuration>Debug</Configuration>
<gitRevision>1</gitRevision>
<gitRevisionHash>Unknown commit</gitRevisionHash>
</PropertyGroup>
<ItemGroup>
<BuildProject Include="src\Solution.sln" />
</ItemGroup>
<Target Name="Build" DependsOnTargets="Version">
<MsBuild
Projects="@(BuildProject)"
Targets="Build"
Properties=""
StopOnFirstFailure="true"
/>
</Target>
<Target Name="Version">
<!--
If .git folder is found than CommonAssemblyInfo.cs.part will be regenerated with new hash value.
-->
<ExtractRevisionHash WorkCopy="$(MSBuildProjectDirectory)" Condition="Exists('.git')">
<Output TaskParameter="RevisionHash" PropertyName="gitRevisionHash"/>
</ExtractRevisionHash>
<ExtractGITRevision WorkCopy="$(MSBuildProjectDirectory)" Condition="Exists('.git')">
<Output TaskParameter="Revision" PropertyName="gitRevision"/>
</ExtractGITRevision>
<ReplaceText
InputFile="src\Common\CommonAssemblyInfo.cs.template"
OutputFile="src\Common\CommonAssemblyInfo.cs.part"
FindFor="\[REVISION\]"
ReplaceBy="$(gitRevisionHash)" Condition="Exists('.git')"/>
<ReplaceText
InputFile="src\Common\CommonAssemblyInfo.cs.part"
OutputFile="src\Common\CommonAssemblyInfo.cs"
FindFor="\[VERSION\]"
ReplaceBy="$(productVersion).$(gitRevision)" />
</Target>
</Project>
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
#if DEBUG
[assembly: AssemblyConfiguration("DEBUG")]
#else
[assembly: AssemblyConfiguration("RELEASE")]
#endif
[assembly: AssemblyCompany("BREEZE Software, Ltd")]
[assembly: AssemblyProduct("Product Name")]
[assembly: AssemblyCopyright("Copyright @ 2016 Breeze Software")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyVersion("[VERSION]")]
[assembly: AssemblyFileVersion("[VERSION]")]
[assembly: AssemblyInformationalVersion("[REVISION]")]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment