Skip to content

Instantly share code, notes, and snippets.

@acid-chicken
Last active June 9, 2019 11:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save acid-chicken/f65def60be82ea8d0bd945c309a68fc8 to your computer and use it in GitHub Desktop.
Save acid-chicken/f65def60be82ea8d0bd945c309a68fc8 to your computer and use it in GitHub Desktop.

Usage

  1. Put Directory.Build.props in your project props directory or your project root directory

Setup

  1. Create empty file .reporoot in your project root directory
  2. Adjust <SrcRoot> nicely

Option

If you want to use 7-chars hash instead of full hash, use this diff.

-        <VersionSuffix Condition="'$(GitHeadSha)' != ''">git-$(GitHeadSha)</VersionSuffix>
+        <VersionSuffix Condition="'$(GitHeadSha)' != ''">git-$(GitHeadSha.Substring(0, 7))</VersionSuffix>
<Project>
<PropertyGroup>
<LangVersion>preview</LangVersion>
<NullableContextOptions>enable</NullableContextOptions>
<Deterministic>true</Deterministic>
<VersionPrefix>0.0.0</VersionPrefix>
<RepoRoot>$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildProjectDirectory), '.reporoot'))\</RepoRoot>
<SrcRoot>$([System.IO.Path]::GetFullPath('$(RepoRoot)src\'))</SrcRoot>
<PathMap>$(SrcRoot)=.</PathMap>
</PropertyGroup>
<Target Name="Increment Build Number" BeforeTargets="Build">
<PropertyGroup>
<BuildNumberPath>$(MSBuildProjectDirectory)\.build</BuildNumberPath>
<PreviousBuildNumber>-1</PreviousBuildNumber>
<PreviousBuildNumber Condition="Exists('$(BuildNumberPath)')">$([System.IO.File]::ReadAllText('$(BuildNumberPath)').Trim())</PreviousBuildNumber>
<BuildNumber>$([MSBuild]::Add($(PreviousBuildNumber), 1))</BuildNumber>
<Version>$(VersionPrefix).$(BuildNumber)</Version>
<Version Condition="'$(VersionSuffix)' != ''">$(VersionPrefix).$(BuildNumber)-$(VersionSuffix)</Version>
</PropertyGroup>
<Message Text="Build Number = $(BuildNumber)" Importance="high" />
<Message Text="Version = $(Version)" Importance="high" />
<WriteLinesToFile File="$(BuildNumberPath)" Lines="$(BuildNumber)" Overwrite="true" />
</Target>
<Choose>
<When Condition="'$(Configuration)' == 'Release'">
<PropertyGroup>
<DebugSymbols>false</DebugSymbols>
<DebugType>none</DebugType>
<Optimize>true</Optimize>
<DefineConstants></DefineConstants>
</PropertyGroup>
</When>
<Otherwise>
<PropertyGroup>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<DotGitRoot>$([System.IO.Path]::GetFullPath('$(RepoRoot).git\'))</DotGitRoot>
<GitHeadFileContent Condition="Exists('$(DotGitRoot)HEAD')">$([System.IO.File]::ReadAllText('$(DotGitRoot)HEAD').Trim())</GitHeadFileContent>
<GitRefPath Condition="$(GitHeadFileContent.StartsWith('ref: '))">$(DotGitRoot)$(GitHeadFileContent.Substring(5))</GitRefPath>
<GitHeadSha>0000000000000000000000000000000000000000</GitHeadSha>
<GitHeadSha Condition="'$(GitHeadFileContent)' != '' and '$(GitRefPath)' == ''">$(GitHeadFileContent)</GitHeadSha>
<GitHeadSha Condition="'$(GitRefPath)' != '' and Exists('$(GitRefPath)')">$([System.IO.File]::ReadAllText('$(GitRefPath)').Trim())</GitHeadSha>
<VersionSuffix Condition="'$(GitHeadSha)' != ''">git-$(GitHeadSha)</VersionSuffix>
</PropertyGroup>
</Otherwise>
</Choose>
</Project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment