GitInfo icon indicating copy to clipboard operation
GitInfo copied to clipboard

$(GitBranch) prop value is empty in .csproj file

Open sanjaykulkarni04 opened this issue 4 years ago • 1 comments
trafficstars

Discussed in https://github.com/devlooped/GitInfo/discussions/177

Originally posted by sanjaykulkarni04 September 23, 2021 Hello,

While building csproj locally/CI server on master branch, I wanted to add RC NuGet package reference otherwise PRE release NuGet package reference. Please find the below code added in .csproj file. What's wrong with the below code? Can someone assist me?

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net5.0</TargetFramework>
    <GitBranch>$(GitBranch.Trim())</GitBranch>
  </PropertyGroup>

  <ItemGroup>
	  <PackageReference Include="AutoMapper" Version="10.1.1" />
	  <PackageReference Include="GitInfo" Version="2.2.0" />
    <PackageReference Include="Data.Foundation.Messaging" Version="4.1.5" />    
  </ItemGroup>

	<Choose>
		<When Condition="$(GitBranch) == 'master'">
			<ItemGroup>
				<PackageReference Include="Data.Account.Domain.Messaging" Version="1.0.0-rc*" IncludePrerelease="true" />
			</ItemGroup>
		</When>
		<Otherwise>
			<ItemGroup>
				<PackageReference Include="Data.Account.Domain.Messaging" Version="1.0.0-pre*" IncludePrerelease="true" />
			</ItemGroup>
		</Otherwise>
	</Choose>
  <ItemGroup>
    <ProjectReference Include="..\Data.Account.GraphQl.Schema\Data.Account.GraphQl.Schema.csproj" />
  </ItemGroup>
  
</Project>
```</div>

sanjaykulkarni04 avatar Sep 24 '21 13:09 sanjaykulkarni04

Hello,

I have the latest 2.2.0 version installed. Can someone please help me with this? In VS .csproj file I can see a warning message - Target GitInfo is not defined.

I wanted to control the NuGet package reference based on $(GitBranch) prop value. If the value is master then refer RC pkg otherwise PRE releases pkg.

I have updated the .csproj file as suggested in https://github.com/devlooped/GitInfo/discussions/143

<Target Name="PopulateInfo" DependsOnTargets="GitInfo" BeforeTargets="PrepareForBuild">
		<PropertyGroup>
			<PackageId>Crlib</PackageId>
			<Version>$(GitBaseVersion)</Version>
			<Authors>Rcmcpe</Authors>
			<PackageLicenseExpression>MIT</PackageLicenseExpression>
			<RepositoryUrl>$(GitRepositoryUrl)</RepositoryUrl>
			<RepositoryType>git</RepositoryType>
			<RepositoryBranch>$(GitBranch)</RepositoryBranch>
			<RepositoryCommit>$(GitCommit)</RepositoryCommit>
		</PropertyGroup>
	</Target>
	<ItemGroup>
		<PackageReference Include="GitInfo" Version="2.2.0">
			<PrivateAssets>all</PrivateAssets>
			<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
		</PackageReference>    
       </ItemGroup>
       <Choose>
		<When Condition="$(RepositoryBranch) == 'master'">
			<ItemGroup>
				<PackageReference Include="Linedata.Account.Domain.Messaging" Version="1.0.0-rc*" IncludePrerelease="true" />
			</ItemGroup>
		</When>
		<Otherwise>
			<ItemGroup>
				<PackageReference Include="Linedata.Account.Domain.Messaging" Version="1.0.0-pre*" IncludePrerelease="true" />
			</ItemGroup>
		</Otherwise>
	</Choose>

sanjaykulkarni04 avatar Sep 24 '21 13:09 sanjaykulkarni04

Target won't be defined until after a successful restore. You can condition that target to $(GitInfoImported) == 'true' to ensure GitInfo has been properly installed and imported.

Also, note that the properties that GitInfo populates are called Git*. So, your RepositoryBranch isn't actually one of the populated props. It should be GitBranch. See https://github.com/devlooped/GitInfo#details

kzu avatar Feb 06 '23 22:02 kzu