Skip to content

Instantly share code, notes, and snippets.

@george-polevoy
Created May 31, 2016 14:01
Show Gist options
  • Save george-polevoy/b46a3f3461153253d8ef6466a27c8bdd to your computer and use it in GitHub Desktop.
Save george-polevoy/b46a3f3461153253d8ef6466a27c8bdd to your computer and use it in GitHub Desktop.
Creates AssemblyInfo.cs by template patching for msbuild.
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<!--
============================================================
RegexTransform
Transforms the input Items parameter by evaluating the
regular expression in their Find metadata and
replacing with their ReplaceWith metadata. Optional, the
options for the regular expression evaluation can be specified.
Example input item:
<RegexTransform Include="$(BuildRoot)Src\GlobalAssemblyInfo.cs">
<Find>AssemblyFileVersion\(".*?"\)</Find>
<ReplaceWith>AssemblyFileVersion("$(FileVersion)")</ReplaceWith>
<Options>Multiline | IgnorePatternWhitespace</Options>
</RegexTransform>
Invoking the target:
<RegexTransform Items="@(RegexTransform)" />
============================================================
-->
<PropertyGroup>
<TasksDllPath Condition="'$(MSBuildToolsVersion)' == '14'">$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll</TasksDllPath>
<TasksDllPath Condition="'$(MSBuildToolsVersion)' != '14'">$(MSBuildToolsPath)\Microsoft.Build.Tasks.v$(MSBuildToolsVersion).dll</TasksDllPath>
</PropertyGroup>
<UsingTask TaskName="RegexTransform"
TaskFactory="CodeTaskFactory"
AssemblyFile="$(TasksDllPath)">
<ParameterGroup>
<Items ParameterType="Microsoft.Build.Framework.ITaskItem[]" />
</ParameterGroup>
<Task>
<Using Namespace="System.IO" />
<Using Namespace="System.Text.RegularExpressions" />
<Using Namespace="Microsoft.Build.Framework" />
<Code Type="Fragment"
Language="cs">
<![CDATA[
foreach(var item in Items)
{
string fileName = item.GetMetadata("FullPath");
string find = item.GetMetadata("Find");
string replaceWith = item.GetMetadata("ReplaceWith");
string optionsValue = item.GetMetadata("Options") ?? "";
var options = string.IsNullOrWhiteSpace(optionsValue) ?
RegexOptions.None : (RegexOptions)Enum.Parse(typeof(RegexOptions), optionsValue.Replace('|', ','));
if(!File.Exists(fileName))
{
Log.LogError("Could not find file: {0}", fileName);
return false;
}
string content = File.ReadAllText(fileName);
File.WriteAllText(
fileName,
Regex.Replace(
content,
find,
replaceWith,
options
),
Encoding.UTF8
);
}
]]>
</Code>
</Task>
</UsingTask>
<Target Name="CopyTemplates">
<ItemGroup>
<AssemblyInfoTemplates Include="$(ProjectDir)Properties\AssemblyInfoTemplate.cs"></AssemblyInfoTemplates>
</ItemGroup>
<Copy SourceFiles="@(AssemblyInfoTemplates)" DestinationFiles="@(AssemblyInfoTemplates->'%(RootDir)%(Directory)AssemblyInfoGenerated.cs')" ContinueOnError="false" />
<CreateItem Include="$(ProjectDir)Properties\AssemblyInfoGenerated.cs">
<Output ItemName="CopiedAssemblyInfoTemplates" TaskParameter="Include"/>
</CreateItem>
</Target>
<Target Name="PerformVersioning" DependsOnTargets="CopyTemplates">
<PropertyGroup>
<FileVersion Condition="$(FileVersion)==''">100.0.0.0</FileVersion>
<RepositoryCommitSpec Condition="$(RepositoryCommitSpec)==''">git sha checksum should be here</RepositoryCommitSpec>
</PropertyGroup>
<ItemGroup>
<RegexTransform Include="$(ProjectDir)Properties\AssemblyInfoGenerated.cs">
<Find>AssemblyVersion\(".*?"\)</Find>
<ReplaceWith>AssemblyVersion("$(FileVersion)")</ReplaceWith>
<Options>Multiline | IgnorePatternWhitespace</Options>
</RegexTransform>
<RegexTransform Include="$(ProjectDir)Properties\AssemblyInfoGenerated.cs">
<Find>AssemblyFileVersion\(".*?"\)</Find>
<ReplaceWith>AssemblyFileVersion("$(FileVersion)")</ReplaceWith>
<Options>Multiline | IgnorePatternWhitespace</Options>
</RegexTransform>
<RegexTransform Include="$(ProjectDir)Properties\AssemblyInfoGenerated.cs">
<Find>AssemblyInformationalVersion\(".*?"\)</Find>
<ReplaceWith>AssemblyInformationalVersion("$(RepositoryCommitSpec)")</ReplaceWith>
<Options>Multiline | IgnorePatternWhitespace</Options>
</RegexTransform>
</ItemGroup>
<Message Text="Finished applying version $(FileVersion)."></Message>
<RegexTransform Items="@(RegexTransform)" />
</Target>
</Project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment