Skip to content

Instantly share code, notes, and snippets.

@istupakov
Last active June 14, 2016 19:18
Show Gist options
  • Save istupakov/3dffe4ec304f9323c20b to your computer and use it in GitHub Desktop.
Save istupakov/3dffe4ec304f9323c20b to your computer and use it in GitHub Desktop.
MSBuild target for automatic update AssemblyInfo.cs.
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<UsingTask TaskName="RegexTransform" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
<ParameterGroup>
<InputFile ParameterType="System.String" Required="true" />
<OutputFile ParameterType="System.String" Required="true" />
<Items ParameterType="Microsoft.Build.Framework.ITaskItem[]" Required="true" />
</ParameterGroup>
<Task>
<Using Namespace="System.IO" />
<Using Namespace="System.Text.RegularExpressions" />
<Using Namespace="Microsoft.Build.Framework" />
<Code Type="Fragment" Language="cs">
<![CDATA[
if(!File.Exists(InputFile)) {
Log.LogError(null, null, null, null, 0, 0, 0, 0, String.Format("Could not find version file: {0}", InputFile), new object[0]);
}
string content = File.ReadAllText(InputFile);
foreach(ITaskItem item in Items) {
string find = item.GetMetadata("Find");
string replaceWith = item.GetMetadata("ReplaceWith");
content = Regex.Replace(content, find, replaceWith);
}
File.WriteAllText(OutputFile, content);
]]>
</Code>
</Task>
</UsingTask>
<Target Name="CreateDir" BeforeTargets="UpdateAssemblyVersion">
<MakeDir Directories="$(IntermediateOutputPath)"/>
</Target>
<Target Name="UpdateAssemblyVersion" AfterTargets="BeforeBuild">
<Exec Command="git describe --tags --long --match v[0-9]*.[0-9]* --dirty" ConsoleToMSBuild="true" ContinueOnError="true"
StandardErrorImportance="low" StandardOutputImportance="low">
<Output TaskParameter="ConsoleOutput" PropertyName="GitDescribe" />
<Output TaskParameter="ExitCode" PropertyName="GitDescribeExitCode"/>
</Exec>
<Warning Condition=" '$(GitDescribeExitCode)' != '0' " Text="Can't get version from git!" />
<PropertyGroup Condition=" '$(GitDescribeExitCode)' != '0' ">
<Version>2.0.*</Version>
<InformationalVersion>2.0 (unknown git revision)</InformationalVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(GitDescribeExitCode)' == '0' ">
<GitParse>$([System.Text.RegularExpressions.Regex]::Match($(GitDescribe), `v(\d+\.\d+)(.*)-(\d+)-g([0-9a-z]+)-?(.*)`).Groups)</GitParse>
<GitBaseVer>$(GitParse.Split(';')[1])</GitBaseVer>
<GitRevision>$(GitParse.Split(';')[3])</GitRevision>
<GitHash>$(GitParse.Split(';')[4])</GitHash>
<GitDirty>$(GitParse.Split(';')[5])</GitDirty>
<Version>$(GitBaseVer).$(GitRevision).*</Version>
<InformationalVersion Condition=" '$(GitDirty)' == '' ">$(GitBaseVer).$(GitRevision)</InformationalVersion>
<InformationalVersion Condition=" '$(GitDirty)' != '' ">$(GitBaseVer).$(GitRevision) beta</InformationalVersion>
</PropertyGroup>
<Message Text="Version: $(Version)" Importance="high" />
<Message Text="InformationalVersion: $(InformationalVersion)" Importance="high" />
<ItemGroup>
<Regex Include="Version">
<Find>#version</Find>
<ReplaceWith>$(Version)</ReplaceWith>
</Regex>
<Regex Include="InformationalVersion">
<Find>#infoversion</Find>
<ReplaceWith>$(InformationalVersion)</ReplaceWith>
</Regex>
<Regex Include="Date">
<Find>#date</Find>
<ReplaceWith>$([System.DateTime]::Now)</ReplaceWith>
</Regex>
<Regex Include="Year">
<Find>#year</Find>
<ReplaceWith>$([System.DateTime]::Now.Year)</ReplaceWith>
</Regex>
<Regex Include="Config">
<Find>#config</Find>
<ReplaceWith>$(Configuration)</ReplaceWith>
</Regex>
<Regex Include="Hash">
<Find>#hash</Find>
<ReplaceWith>$(GitHash)</ReplaceWith>
</Regex>
</ItemGroup>
<RegexTransform InputFile="$(MSBuildThisFileDirectory)\CommonAssemblyInfo.cs" OutputFile="$(IntermediateOutputPath)\TempCommonAssemblyInfo.cs" Items="@(Regex)" />
<ItemGroup>
<Compile Include="$(IntermediateOutputPath)\TempCommonAssemblyInfo.cs" />
</ItemGroup>
</Target>
</Project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment