Reputation: 436
I have a folder of generated files which mirrors my project's files.
Example: if the project has a file at Framework/HaTool.cs
then there will also be a file at Generated/Framework/HaTool.cs
.
I'm trying to use the Generated
files for building and the original files for debugging and developing. (I'm using the #line
directive)
I got almost everything to work except that I can't seem to find a way to have Visual Studio (2017 Community) both display the Generated
directory and it's files in the Solution Explorer and Not try to compile them together during design time.
My current setup won't show the files to me in the Solution Explorer but will use them to build. Here are the relevant parts of my .csproj
file:
<Project ToolsVersion="15.0">
<PropertyGroup Condition="'$(GeneratedSourcePath)'==''">
<GeneratedSourcePath>Generated</GeneratedSourcePath>
</PropertyGroup>
<ItemGroup>
<Compile Remove="$(GeneratedSourcePath)\**" />
</ItemGroup>
<Target Name="UseGeneratedTarget" BeforeTargets="BeforeBuild" KeepDuplicateOutputs="false">
<ItemGroup>
<Compile
Condition="Exists('$(GeneratedSourcePath)\%(Compile.Identity)')"
Include="$(GeneratedSourcePath)\%(Compile.Identity)" />
<Compile
Condition="Exists('$(GeneratedSourcePath)\%(Compile.Identity)')"
Remove="%(Compile.Identity)" />
</ItemGroup>
</Target>
<Target Name="CleanupTarget" AfterTargets="AfterBuild">
<ItemGroup>
<Compile>
<NonGeneratedFilename>$([System.String]::Copy('%(Compile.Identity)').Replace('$(GeneratedSourcePath)\', ''))</NonGeneratedFilename>
</Compile>
</ItemGroup>
<ItemGroup>
<Compile Include="@(Compile->'%(NonGeneratedFilename)')"/>
</ItemGroup>
</Target>
</Project>
Is there a way to simply allow VS to show the files while not building them? what am I missing?
Upvotes: 2
Views: 11163
Reputation: 63133
Copied from comment.
First, it is very important to avoid customization on MSBuild scripts if you can, as Visual Studio or any other IDE might not be able to well process your customization. That's understandable, because customization is almost without limits, but IDE code base has to follow a few rules.
Second, in your specific case, if we replace <Compile>
tags manually with <None>
tags, then VS can show the files are normal files in your project, but not source code to feed the compiler. Such a trick can be hard for others (in your team or under other circumstances) to understand why you applied it, as <None>
tags like the name indicates, are for non source code files, such as txt or other extensions.
Third, if you do need such files to have special behaviors, you might consider extending MSBuild system with your own tags (like ANTLR C# does by adding <Antlr4>
tags), https://github.com/tunnelvisionlabs/antlr4cs
Upvotes: 4