Created
August 9, 2023 22:03
-
-
Save assarbad/6d92afcf8c8fc6f2f2d9c596b07fc5bc to your computer and use it in GitHub Desktop.
Just playing around with the retrieval of the Git revision from within MSBuild and caching it ...
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
<?xml version="1.0" encoding="utf-8"?> | |
<Project InitialTargets="GetGitRevision" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | |
<PropertyGroup> | |
<GitWorkTreeRootDir>$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), '.git\index'))</GitWorkTreeRootDir> | |
<GitWorkTreeRootDir Condition="'$(GitWorkTreeRootDir)' != '' and !HasTrailingSlash('$(GitWorkTreeRootDir)')">$(GitWorkTreeRootDir)\</GitWorkTreeRootDir> | |
<GitDirSentinelFile Condition="Exists('$(GitWorkTreeRootDir).git\index')">$(GitWorkTreeRootDir).git\index</GitDirSentinelFile> | |
<GitRevPropsFile>$(MSBuildThisFileDirectory)__GitRev.props</GitRevPropsFile> | |
</PropertyGroup> | |
<Target Name="FindGitExecutablePath" Condition="'$(GitDirSentinelFile)' != ''"> | |
<PropertyGroup> | |
<_GitExecutableToFind>git.exe</_GitExecutableToFind> | |
</PropertyGroup> | |
<ItemGroup> | |
<_GitExecPaths Include="$([System.Environment]::GetEnvironmentVariable('PATH').Split(';'))" /> | |
<_GitFilteredExecPaths Include="@(_GitExecPaths)" Condition="Exists('%(FullPath)\$(_GitExecutableToFind)')" /> | |
<_GitFilteredExecPaths Include="@(_GitFilteredExecPaths->Reverse())" Condition="Exists('%(FullPath)\$(_GitExecutableToFind)')" /> | |
</ItemGroup> | |
<PropertyGroup> | |
<GitExecutableFullPath Condition="'%(_GitFilteredExecPaths.Identity)' != ''">%(_GitFilteredExecPaths.Identity)\$(_GitExecutableToFind)</GitExecutableFullPath> | |
<GitExecutableFullPath Condition="'%(_GitFilteredExecPaths.Identity)' == ''"></GitExecutableFullPath> | |
</PropertyGroup> | |
</Target> | |
<Target Name="ActualGetGitRevision" DependsOnTargets="FindGitExecutablePath" Condition="'$(GitDirSentinelFile)' != ''"> | |
<Message Importance="low" Text="Retrieved %24(GitWorkTreeRootDir) = '$(GitWorkTreeRootDir)'" /> | |
<Message Importance="low" Text="Detecting Git revision, since '$(GitDirSentinelFile)' was found. Using %24(GitExecutableFullPath) = '$(GitExecutableFullPath)'." Condition="'$(GitExecutableFullPath)' != '' and '$(GitWorkTreeRootDir)' != ''" /> | |
<Exec Command=""$(GitExecutableFullPath)" -C "$(GitWorkTreeRootDir)\" rev-parse HEAD" EchoOff="true" Condition="'$(GitExecutableFullPath)' != '' and '$(GitWorkTreeRootDir)' != ''" ConsoleToMsBuild="true"> | |
<Output TaskParameter="ConsoleOutput" PropertyName="RetrievedGitRevision" /> | |
</Exec> | |
<Message Importance="low" Text="Retrieved for HEAD %24(RetrievedGitRevision) = '$(RetrievedGitRevision)'." Condition="'$(RetrievedGitRevision)' != ''" /> | |
</Target> | |
<Target Name="GetGitRevision" DependsOnTargets="ActualGetGitRevision"> | |
<Message Importance="low" Text="%24(Exists('$(GitRevPropsFile)')) = $([System.IO.File]::Exists('$(GitRevPropsFile)'))" /> | |
<Message Importance="low" Text="%24([System.IO.File]::GetLastWriteTime('$(GitDirSentinelFile)').Ticks) = $([System.IO.File]::GetLastWriteTime('$(GitDirSentinelFile)').Ticks) [$([System.IO.File]::GetLastWriteTime('$(GitDirSentinelFile)'))]" /> | |
<Message Importance="low" Text="%24([System.IO.File]::GetLastWriteTime('$(GitRevPropsFile)').Ticks) = $([System.IO.File]::GetLastWriteTime('$(GitRevPropsFile)').Ticks) [$([System.IO.File]::GetLastWriteTime('$(GitRevPropsFile)'))]" /> | |
<CallTarget Targets="WriteNewGitRevsPropsFile" /> | |
</Target> | |
<Target Name="WriteNewGitRevsPropsFile" Condition="'$(RetrievedGitRevision)' != '' AND '$(RetrievedGitRevision)' != '$(GitRevision)'"> | |
<PropertyGroup> | |
<GitRevision>$(RetrievedGitRevision)</GitRevision> | |
</PropertyGroup> | |
<Message Importance="high" Text="%24(GitRevPropsFile) = '$(GitRevPropsFile)'" /> | |
<Message Importance="low" Text="Old revision HEAD %24(GitRevision) = '$(GitRevision)'." Condition="'$(GitRevision)' != ''" /> | |
<ItemGroup Condition="'$(GitRevision)' != ''"> | |
<MSBuildSnippetLine Include='<%3Fxml version="1.0" encoding="utf-8"%3F>' /> | |
<MSBuildSnippetLine Include='<!-- File was auto-generated by $(MSBuildThisFile) -->' /> | |
<MSBuildSnippetLine Include='<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">' /> | |
<MSBuildSnippetLine Include='%09<PropertyGroup>' /> | |
<MSBuildSnippetLine Include='%09%09<GitRevision>$(GitRevision)</GitRevision>' /> | |
<MSBuildSnippetLine Include='%09</PropertyGroup>' /> | |
<MSBuildSnippetLine Include='%09<Target Name="SetClCompileGitRev" BeforeTargets="ClCompile">' /> | |
<MSBuildSnippetLine Include='%09%09<ItemGroup>' /> | |
<MSBuildSnippetLine Include='%09%09%09<ClCompile>' /> | |
<MSBuildSnippetLine Include='%09%09%09%09<PreprocessorDefinitions>GIT_COMMIT="%24(GitRevision)"%3B%25(PreprocessorDefinitions)</PreprocessorDefinitions>' /> | |
<MSBuildSnippetLine Include='%09%09%09</ClCompile>' /> | |
<MSBuildSnippetLine Include='%09%09</ItemGroup>' /> | |
<MSBuildSnippetLine Include='%09</Target>' /> | |
<MSBuildSnippetLine Include='%09<Target Name="SetResourceCompileGitRev" BeforeTargets="ResourceCompile">' /> | |
<MSBuildSnippetLine Include='%09%09<ItemGroup>' /> | |
<MSBuildSnippetLine Include='%09%09%09<ResourceCompile>' /> | |
<MSBuildSnippetLine Include='%09%09%09%09<PreprocessorDefinitions>GIT_COMMIT="%24(GitRevision)"%3B%25(PreprocessorDefinitions)</PreprocessorDefinitions>' /> | |
<MSBuildSnippetLine Include='%09%09%09</ResourceCompile>' /> | |
<MSBuildSnippetLine Include='%09%09</ItemGroup>' /> | |
<MSBuildSnippetLine Include='%09</Target>' /> | |
<MSBuildSnippetLine Include='</Project>' /> | |
</ItemGroup> | |
<WriteLinesToFile Condition="'$(GitRevision)' != ''" File="$(GitRevPropsFile)" Lines="@(MSBuildSnippetLine)" Overwrite="true" WriteOnlyWhenDifferent="true" /> | |
</Target> | |
<ImportGroup> | |
<Import Project="$(GitRevPropsFile)" Condition="Exists('$(GitRevPropsFile)')" /> | |
</ImportGroup> | |
</Project> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This was an abandoned approach I tried.