Skip to content

Commit a943234

Browse files
committed
Change CopyToPublishDirectory on Content items instead of explicitly removing this from publish items
Fixes #2295
1 parent 05c84f0 commit a943234

File tree

4 files changed

+103
-0
lines changed

4 files changed

+103
-0
lines changed

src/Microsoft.NET.Sdk.Razor/build/netstandard2.0/Sdk.Razor.CurrentVersion.targets

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ Copyright (c) .NET Foundation. All rights reserved.
6969
_RazorAddDebugSymbolsProjectOutputGroupOutput
7070
</DebugSymbolsProjectOutputGroupDependsOn>
7171

72+
<PrepareForBuildDependsOn>
73+
$(PrepareForBuildDependsOn);
74+
ResolveRazorGenerateInputs
75+
</PrepareForBuildDependsOn>
76+
7277
<PrepareForRunDependsOn>
7378
_RazorPrepareForRun;
7479
$(PrepareForRunDependsOn)
@@ -285,6 +290,8 @@ Copyright (c) .NET Foundation. All rights reserved.
285290
<!--
286291
Gathers input source files for code generation. This is a separate target so that we can avoid
287292
lots of work when there are no inputs for code generation.
293+
This target runs as part of PrepareForBuild. This gives us an opportunitity to change things like CopyToPublishDirectory
294+
for Content items before they are processed by other Build targets.
288295
289296
NOTE: This target is called as part of an incremental build scenario in VS. Do not perform any work
290297
outside of calculating RazorGenerate items in this target.
@@ -305,6 +312,20 @@ Copyright (c) .NET Foundation. All rights reserved.
305312
<ItemGroup Condition="'$(EnableDefaultRazorGenerateItems)'=='true'">
306313
<RazorGenerate Include="@(Content)" Condition="'%(Content.Extension)'=='.cshtml'" />
307314
</ItemGroup>
315+
316+
<!--
317+
Ideally we want to able to update all Content items that also appear in RazorGenerate to have
318+
CopyToPublishDirectory=Never. However, there isn't a simple way to do this (https://github.com/Microsoft/msbuild/issues/1618).
319+
Instead, we'll update all cshtml Content items when EnableDefaultRazorGenerateItems=true and Razor Sdk is used for publishing.
320+
-->
321+
<ItemGroup Condition="
322+
'$(EnableDefaultRazorGenerateItems)'=='true' and
323+
'$(CopyRazorGenerateFilesToPublishDirectory)'=='false' and
324+
'$(ResolvedRazorCompileToolset)'=='RazorSdk' and
325+
'$(RazorCompileOnPublish)'=='true'">
326+
327+
<Content Condition="'%(Content.Extension)'=='.cshtml'" CopyToPublishDirectory="Never" />
328+
</ItemGroup>
308329
</Target>
309330

310331
<Target Name="AssignRazorGenerateTargetPaths" Condition="'@(RazorGenerate)' != ''">

test/Microsoft.AspNetCore.Razor.Design.Test/IntegrationTests/PublishIntegrationTest.cs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ public async Task Publish_RazorCompileOnPublish_IsDefault()
2727
Assert.FileExists(result, PublishOutputPath, "SimpleMvc.Views.dll");
2828
Assert.FileExists(result, PublishOutputPath, "SimpleMvc.Views.pdb");
2929

30+
// Verify assets get published
31+
Assert.FileExists(result, PublishOutputPath, "wwwroot", "js", "SimpleMvc.js");
32+
Assert.FileExists(result, PublishOutputPath, "wwwroot", "css", "site.css");
33+
3034
// By default refs and .cshtml files will not be copied on publish
3135
Assert.FileCountEquals(result, 0, Path.Combine(PublishOutputPath, "refs"), "*.dll");
3236
Assert.FileCountEquals(result, 0, Path.Combine(PublishOutputPath, "Views"), "*.cshtml");
@@ -294,6 +298,11 @@ public async Task Publish_WithP2P_AndRazorCompileOnPublish_CopiesRazorAssembly()
294298
Assert.FileExists(result, PublishOutputPath, "ClassLibrary.pdb");
295299
Assert.FileExists(result, PublishOutputPath, "ClassLibrary.Views.dll");
296300
Assert.FileExists(result, PublishOutputPath, "ClassLibrary.Views.pdb");
301+
302+
// Verify fix for https://github.com/aspnet/Razor/issues/2295. No cshtml files should be published from the app
303+
// or the ClassLibrary.
304+
Assert.FileCountEquals(result, 0, Path.Combine(PublishOutputPath, "refs"), "*.dll");
305+
Assert.FileCountEquals(result, 0, Path.Combine(PublishOutputPath, "Views"), "*.cshtml");
297306
}
298307

299308
[Fact]
@@ -310,5 +319,40 @@ public async Task Publish_SimpleMvcFSharp_NoopsWithoutFailing()
310319
Assert.FileDoesNotExist(result, OutputPath, "SimpleMvcFSharp.Views.dll");
311320
Assert.FileDoesNotExist(result, OutputPath, "SimpleMvcFSharp.Views.pdb");
312321
}
322+
323+
[Fact]
324+
[InitializeTestProject("SimpleMvc")]
325+
public async Task Publish_DoesNotPublishCustomRazorGenerateItems()
326+
{
327+
var additionalProjectContent = @"
328+
<PropertyGroup>
329+
<EnableDefaultRazorGenerateItems>false</EnableDefaultRazorGenerateItems>
330+
</PropertyGroup>
331+
<ItemGroup>
332+
<RazorGenerate Include=""Views\_ViewImports.cshtml"" />
333+
<RazorGenerate Include=""Views\Home\Index.cshtml"" />
334+
</ItemGroup>
335+
";
336+
AddProjectFileContent(additionalProjectContent);
337+
var result = await DotnetMSBuild("Publish");
338+
339+
Assert.BuildPassed(result);
340+
341+
Assert.FileExists(result, PublishOutputPath, "SimpleMvc.dll");
342+
Assert.FileExists(result, PublishOutputPath, "SimpleMvc.pdb");
343+
Assert.FileExists(result, PublishOutputPath, "SimpleMvc.Views.dll");
344+
Assert.FileExists(result, PublishOutputPath, "SimpleMvc.Views.pdb");
345+
346+
// Verify assets get published
347+
Assert.FileExists(result, PublishOutputPath, "wwwroot", "js", "SimpleMvc.js");
348+
Assert.FileExists(result, PublishOutputPath, "wwwroot", "css", "site.css");
349+
350+
// By default refs and .cshtml files will not be copied on publish
351+
Assert.FileCountEquals(result, 0, Path.Combine(PublishOutputPath, "refs"), "*.dll");
352+
// Custom RazorGenerate item does not get published
353+
Assert.FileDoesNotExist(result, PublishOutputPath, "Views", "Home", "Home.cshtml");
354+
// cshtml Content item that's not part of RazorGenerate gets published.
355+
Assert.FileExists(result, PublishOutputPath, "Views", "Home", "About.cshtml");
356+
}
313357
}
314358
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/* Please see documentation at https://docs.microsoft.com/aspnet/core/client-side/bundling-and-minification\
2+
for details on configuring this project to bundle and minify static web assets. */
3+
body {
4+
padding-top: 50px;
5+
padding-bottom: 20px;
6+
}
7+
8+
/* Wrapping element */
9+
/* Set some basic padding to keep content from hitting the edges */
10+
.body-content {
11+
padding-left: 15px;
12+
padding-right: 15px;
13+
}
14+
15+
/* Carousel */
16+
.carousel-caption p {
17+
font-size: 20px;
18+
line-height: 1.4;
19+
}
20+
21+
/* Make .svg files in the carousel display properly in older browsers */
22+
.carousel-inner .item img[src$=".svg"] {
23+
width: 100%;
24+
}
25+
26+
/* QR code generator */
27+
#qrCode {
28+
margin: 15px;
29+
}
30+
31+
/* Hide/rearrange for smaller screens */
32+
@media screen and (max-width: 767px) {
33+
/* Hide captions */
34+
.carousel-caption {
35+
display: none;
36+
}
37+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// This is a test file

0 commit comments

Comments
 (0)