Skip to content

Commit

Permalink
Make it easy to include NetCore-only and NetFx-only files (#10572)
Browse files Browse the repository at this point in the history
This migrates a change from Razor tooling and makes it available for all
the Razor repo. Essentially, this allows a C# file to be compiled for
just .NET Framework or .NET Core by its name or the name of the folder
its contained in.

1. Naming a C# file as `.NetCore.cs` or `.NetFx.cs` it will compile
_only_ for NetCore or NetFx, respectively.
2. C# files placed in a folder called `NetCore` or `NetFx` will compile
_only_ for NetCore or NetFx, respectively.
3. C# files placed in a folder whose name ends in `_NetCore` or `_NetFx`
will compile _only_ for NetCore or NetFx, respectively.
 
This makes it clear from the Solution Explorer if there are files that
only compile for one target or another without having to open them and
look at `#if..#endif` blocks.
  • Loading branch information
DustinCampbell authored Jul 2, 2024
2 parents 345ad54 + 83f0e84 commit f648105
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 15 deletions.
30 changes: 30 additions & 0 deletions Directory.Build.targets
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,36 @@
<SupportedPlatform Remove="iOS" />
</ItemGroup>

<!--
We provide a simple mechanism for including NetFx-only files.
If a C# file should only be compiled under NetFx, it can be named with a ".NetFx.cs" extension.
Or, the file can be included in a folder called "NetFx" or has a name that ends in "_NetFx".
-->
<ItemGroup Condition="'$(TargetFramework)' != '$(DefaultNetFxTargetFramework)'">
<Compile Remove="**\*.NetFx.cs" />
<Compile Remove="**\NetFx\**\*.*" />
<Compile Remove="**\*_NetFx\**\*.*" />

<None Include="**\*.NetFx.cs" />
<None Include="**\NetFx\**\*.*" />
<None Include="**\*_NetFx\**\*.*" />
</ItemGroup>

<!--
We provide a simple mechanism for including NetCore-only files.
If a C# file should only be compiled under NetCore, it can be named with a ".NetCore.cs" extension.
Or, the file can be included in a folder called "NetCore" or has a name that ends in "_NetCore".
-->
<ItemGroup Condition="'$(TargetFramework)' != '$(NetCurrent)' AND '$(TargetFramework)' != '$(NetPrevious)'">
<Compile Remove="**\*.NetCore.cs" />
<Compile Remove="**\NetCore\**\*.*" />
<Compile Remove="**\*_NetCore\**\*.*" />

<None Include="**\*.NetCore.cs" />
<None Include="**\NetCore\**\*.*" />
<None Include="**\*_NetCore\**\*.*" />
</ItemGroup>

<!--
Warn consumers until the underlying issue is fixed
https://github.com/dotnet/roslyn/issues/72657
Expand Down
15 changes: 0 additions & 15 deletions src/Razor/Directory.Build.targets

This file was deleted.

0 comments on commit f648105

Please sign in to comment.