Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MSBuild: How to include a bunch of files in the parent directory as linked?

I'm writing a C# project where I have the following directory structure:

LibFoo
|
---- LibFoo.Shared
|    |
|    ---- [a bunch of .cs files]
|
---- LibFoo.Uwp
|    |
|    ---- LibFoo.Uwp.csproj
|
---- LibFoo.Wpf
     |
     ---- LibFoo.Wpf.csproj

I'm curious to know, is it possible to include the C# files from the shared directory, so that they show up in the Solution Explorer in Visual Studio? I know you can do this via setting the Link property for a <Compile> tag, but I'm not quite sure how to do this when there are a variable number of .cs files in the project.

To clarify, here is the relevant portion of my csproj file:

<PropertyGroup>
    <!-- Compile everything in this dir -->
    <CompileRoot>..\LibFoo.Shared</CompileRoot>
</PropertyGroup>

<ItemGroup>
    <Compile Include="$(CompileRoot)\**\*.cs">
        <Link> <!--What goes here?--> </Link>
    </Compile>
</ItemGroup>

Thanks for helping!

edit: Forgot to mention, the fact that they're contained within the parent directory is relevant since that's why they aren't showing up in Visual Studio. If they were showing up in VS, I wouldn't have to do any of this linking stuff.

edit 2: Following shamp00's suggestion, I've tried this:

<ItemGroup>
    <Compile Include="$(CompileRoot)\**\*.cs">
        <Link>$([MSBuild]::MakeRelative('$(CompileRoot)', '%(FullPath)'))</Link>
    </Compile>
</ItemGroup>

Unfortunately, although it seems to be outputting fine when I run a Message task from the project, the links seem to be simply getting ignored in Visual Studio.

edit 3: For those interested in repro-ing this issue, you can clone the sources from the GitHub repository:

git clone [email protected]:jamesqo/typed-xaml
cd typed-xaml

After that, you can open up the solution in Visual Studio and see the effects yourself. The relevant code is in this file.

like image 437
James Ko Avatar asked Sep 25 '22 08:09

James Ko


1 Answers

Something like this should work:

<Target Name="Default">
    <ItemGroup>
        <Parent Include="..\LibFoo.Shared\**\*.cs"/>
        <Compile Include="@(Parent)">
            <Link>..\LibFoo.Shared\%(Parent.Filename).cs</Link>
        </Compile>
    </ItemGroup>
    <Message Text="%(Compile.Identity) is linked to %(Compile.Link)"/>
</Target>

Edit

According to this answer, the following works...

<Compile Include="..\LibFoo.Shared\**\*.cs">
  <Link>.\thisDummyFolderNameDoesNotMatter</Link>
</Content>

Edit 2

I'm not sure how to get it to work with your external common.props file, but it works if you add the following directly to Typed.Xaml.Wpf.csproj.

<ItemGroup>
  <Compile Include="..\Typed.Xaml\**\*.cs">
    <Link>notimportant</Link>
  </Compile>
</ItemGroup>
like image 134
shamp00 Avatar answered Sep 28 '22 06:09

shamp00