-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bc6967a
commit 95f57ed
Showing
2 changed files
with
79 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
<!-- | ||
Defining custom Targets to execute before project compilation starts. | ||
--> | ||
<PropertyGroup> | ||
<CompileDependsOn> | ||
CommonBuildDefineModifiedAssemblyVersion; | ||
$(CompileDependsOn); | ||
</CompileDependsOn> | ||
</PropertyGroup> | ||
|
||
|
||
<!-- | ||
Creates modified version of AssemblyInfo.cs, replaces [AssemblyVersion] attribute with the one specifying actual build version (from MSBuild properties), and includes that file instead of the original AssemblyInfo.cs in the compilation. | ||
Works with both, .cs and .vb version of the AssemblyInfo file, meaning it supports C# and VB.Net projects simultaneously. | ||
--> | ||
<Target Name="CommonBuildDefineModifiedAssemblyVersion" Condition="'$(VersionAssembly)' != ''"> | ||
<!-- Find AssemblyInfo.cs in the "Compile" Items. Remove it from "Compile" Items because we will use a modified version instead. --> | ||
<ItemGroup> | ||
<OriginalAssemblyInfo Include="@(Compile)" Condition="%(Filename) == 'AssemblyInfo' And %(Extension) == '.cs'" /> | ||
<Compile Remove="**/AssemblyInfo.cs" /> | ||
</ItemGroup> | ||
<!-- Copy the original AssemblyInfo.cs to obj\ folder, i.e. $(IntermediateOutputPath). The copied filepath is saved into @(ModifiedAssemblyInfo) Item. --> | ||
<Copy SourceFiles="@(OriginalAssemblyInfo)" | ||
DestinationFiles="@(OriginalAssemblyInfo->'$(IntermediateOutputPath)%(Identity)')"> | ||
<Output TaskParameter="DestinationFiles" ItemName="ModifiedAssemblyInfo"/> | ||
</Copy> | ||
<!-- Replace the version bit (in AssemblyVersion and AssemblyFileVersion attributes) using regular expression. Use the defined property: $(VersionAssembly). --> | ||
<Message Text="Setting AssemblyVersion to $(VersionAssembly)" /> | ||
<RegexUpdateFile Files="@(ModifiedAssemblyInfo)" | ||
Regex="Version\("(\d+)\.(\d+)(\.(\d+)\.(\d+)|\.*)"\)" | ||
ReplacementText="Version("$(VersionAssembly)")" | ||
/> | ||
<!-- Include the modified AssemblyInfo.cs file in "Compile" items (instead of the original). --> | ||
<ItemGroup> | ||
<Compile Include="@(ModifiedAssemblyInfo)" /> | ||
</ItemGroup> | ||
</Target> | ||
|
||
<UsingTask TaskName="RegexUpdateFile" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll"> | ||
<ParameterGroup> | ||
<Files ParameterType="Microsoft.Build.Framework.ITaskItem[]" Required="true" /> | ||
<Regex ParameterType="System.String" Required="true" /> | ||
<ReplacementText ParameterType="System.String" Required="true" /> | ||
</ParameterGroup> | ||
<Task> | ||
<Reference Include="System.Core" /> | ||
<Using Namespace="System" /> | ||
<Using Namespace="System.IO" /> | ||
<Using Namespace="System.Text.RegularExpressions" /> | ||
<Using Namespace="Microsoft.Build.Framework" /> | ||
<Using Namespace="Microsoft.Build.Utilities" /> | ||
<Code Type="Fragment" Language="cs"> | ||
<![CDATA[ | ||
try { | ||
var rx = new System.Text.RegularExpressions.Regex(this.Regex); | ||
for (int i = 0; i < Files.Length; ++i) | ||
{ | ||
var path = Files[i].GetMetadata("FullPath"); | ||
if (!File.Exists(path)) continue; | ||
var txt = File.ReadAllText(path); | ||
txt = rx.Replace(txt, this.ReplacementText); | ||
File.WriteAllText(path, txt); | ||
} | ||
return true; | ||
} | ||
catch (Exception ex) { | ||
Log.LogErrorFromException(ex); | ||
return false; | ||
} | ||
]]> | ||
</Code> | ||
</Task> | ||
</UsingTask> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters