Last active
April 6, 2020 13:46
-
-
Save overing/6740dce347a8461ac9129bd22a92e9de to your computer and use it in GitHub Desktop.
Add VCS Info To AssemblyTitle for dotnet core sdk (.svn/.git only)
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
<Project> | |
<!-- MSBuildRuntimeType: cond check for omnisharp-vscode support problem --> | |
<Target Condition=" '$(OS)' == 'Windows_NT' AND '$(MSBuildRuntimeType)' == 'Core' " Name="RewriteAssemblyTitle" BeforeTargets="PrepareForBuild"> | |
<PropertyGroup> | |
<TimeText>UTC $([System.DateTime]::UtcNow.ToString('yyyy/MM/dd HH:mm:ss'))</TimeText> | |
</PropertyGroup> | |
<FindVCSInfoTarget Condition=" '$(VCSInfoTarget)' == '' OR !EXISTS( $(VCSInfoTarget) ) " CurrentDirName="$(ProjectDir)"> | |
<Output TaskParameter="DirectoryInParent" PropertyName="VCSInfoTarget" /> | |
</FindVCSInfoTarget> | |
<PropertyGroup Condition="Exists( $(VCSInfoTarget) ) AND $(VCSInfoTarget.EndsWith('.svn') )"> | |
<VCS>Svn</VCS> | |
<TSVNRevExe>C:\Program Files (x86)\TortoiseSVN\bin\subwcrev.exe</TSVNRevExe> | |
<TSVNRevExe Condition=" !Exists( $(TSVNRevExe) ) ">C:\Program Files\TortoiseSVN\bin\subwcrev.exe</TSVNRevExe> | |
<VCSCommand Condition=" Exists( $(TSVNRevExe) ) ">$(TSVNRevExe)</VCSCommand> | |
<VCSArguments>"$(ProjectDir.TrimEnd('\'))"</VCSArguments> | |
</PropertyGroup> | |
<PropertyGroup Condition="Exists( $(VCSInfoTarget) ) AND $(VCSInfoTarget.EndsWith('.git') )"> | |
<VCS>Git</VCS> | |
<GitExe>C:\Program Files (x86)\Git\bin\git.exe</GitExe> | |
<GitExe Condition=" !Exists( $(GitExe) ) ">C:\Program Files\Git\bin\git.exe</GitExe> | |
<VCSCommand Condition=" Exists( $(GitExe) ) ">$(GitExe)</VCSCommand> | |
<VCSArguments>--git-dir="$(VCSInfoTarget)" rev-parse --short HEAD</VCSArguments> | |
<CapturePattern><![CDATA[(?<revision>\\w+)]]></CapturePattern> | |
</PropertyGroup> | |
<Message Text="Read VCS Revision For VCSInfoTarget: '$(VCSInfoTarget)'" Importance="high"/> | |
<Exec Condition=" '$(VCSCommand)' != '' " ConsoleToMSBuild="true" StandardOutputImportance="Low" Command=""$(VCSCommand)" $(VCSArguments)"> | |
<Output TaskParameter="ConsoleOutput" PropertyName="RawRevision" /> | |
</Exec> | |
<Exec ConsoleToMSBuild="true" StandardOutputImportance="Low" Command="ECHO %USERNAME% on %COMPUTERNAME%"> | |
<Output TaskParameter="ConsoleOutput" PropertyName="UserName" /> | |
</Exec> | |
<ParseSubwcrev Condition=" '$(VCS)' == 'Svn' AND '$(RawRevision)' != '' " Stdout="$(RawRevision)"> | |
<Output TaskParameter="Result" PropertyName="Revision" /> | |
</ParseSubwcrev> | |
<CreateProperty Condition=" '$(VCS)' == 'Git' AND '$(RawRevision)' != '' " Value="$([System.Text.RegularExpressions.Regex]::Replace($(RawRevision), $(CapturePattern), '${revision}'))"> | |
<Output TaskParameter="Value" PropertyName="Revision" /> | |
</CreateProperty> | |
<CreateProperty Condition=" '$(Revision)' == '' " Value="$(Configuration) Build at $(TimeText) by $(UserName)"> | |
<Output TaskParameter="Value" PropertyName="AssemblyTitle" /> | |
</CreateProperty> | |
<CreateProperty Condition=" '$(Revision)' != '' " Value=" $(VCS) $(Revision) $(Configuration) build at $(TimeText) by $(UserName)"> | |
<Output TaskParameter="Value" PropertyName="AssemblyTitle" /> | |
</CreateProperty> | |
<Message Text="Rewrite AssemblyTitle: '$(AssemblyTitle)'" Importance="high"/> | |
</Target> | |
<UsingTask | |
TaskName="FindVCSInfoTarget" | |
TaskFactory="RoslynCodeTaskFactory" | |
AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll"> | |
<ParameterGroup> | |
<CurrentDirName ParameterType="System.String" Required="true" /> | |
<DirectoryInParent ParameterType="System.String" Output="true" /> | |
</ParameterGroup> | |
<Task> | |
<Using Namespace="System" /> | |
<Using Namespace="System.IO" /> | |
<Code Type="Fragment" Language="cs"><![CDATA[ | |
while (true) | |
{ | |
var gitPath = Path.Combine(CurrentDirName, ".git"); | |
if (Directory.Exists(gitPath)) | |
{ | |
DirectoryInParent = gitPath; | |
break; | |
} | |
var svnPath = Path.Combine(CurrentDirName, ".svn"); | |
if (Directory.Exists(svnPath)) | |
{ | |
DirectoryInParent = svnPath; | |
break; | |
} | |
if (CurrentDirName == Directory.GetDirectoryRoot(CurrentDirName)) | |
{ | |
DirectoryInParent = string.Empty; | |
break; | |
} | |
CurrentDirName = Directory.GetParent(CurrentDirName).FullName; | |
} | |
]]></Code> | |
</Task> | |
</UsingTask> | |
<UsingTask | |
TaskName="ParseSubwcrev" | |
TaskFactory="RoslynCodeTaskFactory" | |
AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll"> | |
<ParameterGroup> | |
<Stdout ParameterType="System.String" Required="true" /> | |
<Result ParameterType="System.String" Output="true" /> | |
</ParameterGroup> | |
<Task> | |
<Using Namespace="System" /> | |
<Using Namespace="System.Text.RegularExpressions" /> | |
<Code Type="Fragment" Language="cs"><![CDATA[ | |
var pattern = @".+Last\scommitted\sat\srevision\s(?<revision>\d+).*"; | |
var result = Regex.Replace(Stdout, pattern, "${revision}", RegexOptions.Singleline); | |
if (Stdout.Contains("Local modifications found")) result += "M"; | |
if (Stdout.Contains("Unversioned items found")) result += "U"; | |
Result = result; | |
]]></Code> | |
</Task> | |
</UsingTask> | |
</Project> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment