lonix
lonix

Reputation: 20709

Conditionally include project based on path

I have this:

MyProject/
  Common/
    Common.csproj
  Main/
    Libs/
      Utils/
        Utils.csproj
      Legacy/
        Legacy.csproj
    Server/
      Server.csproj
    Directory.Build.props

I want all projects in Main/ to include the Common project.

So Directory.Build.props has this:

<ItemGroup>
  <ProjectReference Include="../../Common/Common.csproj" />
</ItemGroup>

So the Server project includes the Common project as it is two levels down. But the projects in Libs/ can't include it as it is three levels down.

I need to do something like this:

<ItemGroup>
  <ProjectReference Include="../../Common/Common.csproj" Condition="if 2 levels up is 'MyProject'"/>
  <ProjectReference Include="../../../Common/Common.csproj" Condition="if 3 levels up is 'MyProject'"/>
</ItemGroup>

What do I put in Condition? I know I need to use MSBuildThisFileDirectory but unsure how.

Upvotes: 0

Views: 46

Answers (1)

lonix
lonix

Reputation: 20709

<ItemGroup>
  <ProjectReference Include="../../Common/Common.csproj" Condition="Exists('../../Common')'"/>
  <ProjectReference Include="../../../Common/Common.csproj" Condition="Exists('../../../Common')'"/>
</ItemGroup>

Upvotes: 1

Related Questions