Browse Source

MSBuild target

Logic Ethos Ltd 11 months ago
parent
commit
e5dc5d9c33
3 changed files with 72 additions and 7 deletions
  1. 70 0
      TFA-Bot/BuildCommon.targets
  2. 2 6
      TFA-Bot/TFA-Bot.csproj
  3. 0 1
      TFA-Bot/packages.config

+ 70 - 0
TFA-Bot/BuildCommon.targets

@@ -0,0 +1,70 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+    <PropertyGroup>
+    <CompileDependsOn>
+        CommonBuildDefineModifiedAssemblyVersion;
+        $(CompileDependsOn);
+    </CompileDependsOn>
+</PropertyGroup>
+<Target Name="CommonBuildDefineModifiedAssemblyVersion" Condition="'$(VersionAssembly)' != ''">
+    <!-- Find AssemblyInfo.cs or AssemblyInfo.vb 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) == '.vb' Or %(Extension) == '.cs')" />
+        <Compile Remove="**/AssemblyInfo.vb" />
+        <Compile Remove="**/AssemblyInfo.cs" />
+    </ItemGroup>
+    <!-- Copy the original AssemblyInfo.cs/.vb 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\(&quot;(\d+)\.(\d+)(\.(\d+)\.(\d+)|\.*)&quot;\)"
+                ReplacementText="Version(&quot;$(VersionAssembly)&quot;)"
+                />
+    <!-- Include the modified AssemblyInfo.cs/.vb 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>

+ 2 - 6
TFA-Bot/TFA-Bot.csproj

@@ -131,6 +131,7 @@
   </ItemGroup>
   <ItemGroup>
     <None Include="packages.config" />
+    <None Include="BuildCommon.targets" />
   </ItemGroup>
   <ItemGroup>
     <Folder Include="DiscordBot\" />
@@ -148,11 +149,6 @@
     </Content>
   </ItemGroup>
   <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
+  <Import Project="$(SolutionDir)\BuildCommon.targets" />
   <Import Project="..\packages\Mono.Posix.5.4.0.201\build\net45\Mono.Posix.targets" Condition="Exists('..\packages\Mono.Posix.5.4.0.201\build\net45\Mono.Posix.targets')" />
-  <Import Project="..\packages\MSBuild.Extension.Pack.1.9.1\build\net40\MSBuild.Extension.Pack.targets" Condition="Exists('..\packages\MSBuild.Extension.Pack.1.9.1\build\net40\MSBuild.Extension.Pack.targets')" />
-  <Target Name="BeforeBuild">
-    <Message Text="Updating AssemblyGitCommit to $(GitCommit)"></Message>
-    <AssemblyInfo AssemblyGitCommit="$(GitCommit)">
-    </AssemblyInfo>
-  </Target>
 </Project>

+ 0 - 1
TFA-Bot/packages.config

@@ -7,7 +7,6 @@
   <package id="LibGit2Sharp" version="0.25.2" targetFramework="net462" />
   <package id="LibGit2Sharp.NativeBinaries" version="1.0.217" targetFramework="net462" />
   <package id="Mono.Posix" version="5.4.0.201" targetFramework="net462" />
-  <package id="MSBuild.Extension.Pack" version="1.9.1" targetFramework="net462" />
   <package id="Newtonsoft.Json" version="11.0.2" targetFramework="net462" />
   <package id="RestSharp" version="106.3.1" targetFramework="net462" />
   <package id="System.ValueTuple" version="4.5.0" targetFramework="net462" />