Make Razor targets noop with no razor files

This commit is contained in:
Ryan Nowak 2018-01-10 21:48:05 -08:00
parent bb0aa9464a
commit 5f501710aa
8 changed files with 114 additions and 46 deletions

View File

@ -58,7 +58,8 @@
Outputs="@(RazorIntermediateAssembly); Outputs="@(RazorIntermediateAssembly);
@(_RazorDebugSymbolsIntermediatePath); @(_RazorDebugSymbolsIntermediatePath);
$(NonExistentFile)" $(NonExistentFile)"
DependsOnTargets="RazorCoreGenerate"> DependsOnTargets="RazorCoreGenerate"
Condition="'@(RazorCompile)'!=''">
<!-- These two compiler warnings are raised when a reference is bound to a different version <!-- These two compiler warnings are raised when a reference is bound to a different version
than specified in the assembly reference version number. MSBuild raises the same warning in this case, than specified in the assembly reference version number. MSBuild raises the same warning in this case,
so the compiler warning would be redundant. --> so the compiler warning would be redundant. -->

View File

@ -11,8 +11,8 @@
Use these properties to attach behavior to the corresponding target. Use these properties to attach behavior to the corresponding target.
--> -->
<PropertyGroup> <PropertyGroup>
<RazorGenerateDependsOn>_RazorResolveTagHelpers;RazorCoreGenerate</RazorGenerateDependsOn> <RazorGenerateDependsOn>RazorResolveGenerateInputs;RazorCoreGenerate</RazorGenerateDependsOn>
<RazorCompileDependsOn>_RazorResolveTagHelpers;RazorCoreGenerate;RazorCoreCompile</RazorCompileDependsOn> <RazorCompileDependsOn>RazorGenerate;RazorCoreCompile</RazorCompileDependsOn>
</PropertyGroup> </PropertyGroup>
<!-- <!--

View File

@ -37,11 +37,52 @@
<_RazorTagHelperToolAssembly>$(_RazorMSBuildRoot)tools\Microsoft.AspNetCore.Razor.TagHelperTool.dll</_RazorTagHelperToolAssembly> <_RazorTagHelperToolAssembly>$(_RazorMSBuildRoot)tools\Microsoft.AspNetCore.Razor.TagHelperTool.dll</_RazorTagHelperToolAssembly>
</PropertyGroup> </PropertyGroup>
<!--
Gathers input source files for code generation. This is a separate target so that we can avoid
lots of work when there are no inputs for code generation.
Notice there are plenty of Condition="'@(RazorGenerate)'!=''" to avoid running more expensive targets.
-->
<Target Name="RazorResolveGenerateInputs">
<ItemGroup>
<RazorGenerate Include="@(Content)" Condition="'%(Content.Extension)'=='.cshtml'" />
<_RazorGenerateOutput Include="@(RazorGenerate->'$(RazorGenerateOutputPath)%(RelativeDir)%(Filename).cs')" />
</ItemGroup>
</Target>
<Target
Name="_ResolveRazorCoreGenerateInputs"
DependsOnTargets="RazorResolveGenerateInputs"
Condition="'@(RazorGenerate)'!=''">
<PropertyGroup>
<_RazorGenerateHashFile>$(IntermediateOutputPath)$(MSBuildProjectName).RazorCoreGenerate.cache</_RazorGenerateHashFile>
</PropertyGroup>
<Hash ItemsToHash="@(RazorGenerate)" Condition="'@(RazorGenerate)'!=''">
<Output TaskParameter="HashResult" PropertyName="_RazorGenerateHash" />
</Hash>
<MakeDir
Directories="$(IntermediateOutputPath)"
Condition="!Exists('$(IntermediateOutputPath)')" />
<WriteLinesToFile
Lines="$(_RazorGenerateHash)"
File="$(_RazorGenerateHashFile)"
Overwrite="True"
WriteOnlyWhenDifferent="True" />
<ItemGroup>
<FileWrites Include="$(_RazorGenerateHashFile)" />
</ItemGroup>
</Target>
<!-- <!--
Gathers input assemblies for Tag Helper discovery and compilation. Add items to @(ReferencePath) Gathers input assemblies for Tag Helper discovery and compilation. Add items to @(ReferencePath)
--> -->
<Target <Target
Name="RazorResolveAssemblyReferences" Name="_RazorResolveReferences"
DependsOnTargets="ResolveReferences"> DependsOnTargets="ResolveReferences">
<ItemGroup> <ItemGroup>
<RazorReferencePath Include="@(ReferencePath)"/> <RazorReferencePath Include="@(ReferencePath)"/>
@ -51,9 +92,10 @@
<Target <Target
Name="_RazorResolveTagHelpers" Name="_RazorResolveTagHelpers"
DependsOnTargets="Compile;RazorResolveAssemblyReferences" DependsOnTargets="Compile;_RazorResolveReferences"
Inputs="$(MSBuildAllProjects);@(RazorReferencePath)" Inputs="$(MSBuildAllProjects);@(RazorReferencePath)"
Outputs="$(_RazorTagHelperInputCache)"> Outputs="$(_RazorTagHelperInputCache)"
Condition="'@(RazorGenerate)'!=''">
<!-- <!--
We're manipulating our output directly here because we want to separate the actual up-to-date check We're manipulating our output directly here because we want to separate the actual up-to-date check
@ -81,41 +123,12 @@
</Target> </Target>
<Target Name="_ResolveRazorCoreGenerateInputs">
<ItemGroup>
<RazorGenerate Include="@(Content)" Condition="'%(Content.Extension)'=='.cshtml'" />
<_RazorGenerateOutput Include="@(RazorGenerate->'$(RazorGenerateOutputPath)%(RelativeDir)%(Filename).cs')" />
</ItemGroup>
<PropertyGroup>
<_RazorGenerateHashFile>$(IntermediateOutputPath)$(MSBuildProjectName).RazorCoreGenerate.cache</_RazorGenerateHashFile>
</PropertyGroup>
<Hash ItemsToHash="@(RazorGenerate)">
<Output TaskParameter="HashResult" PropertyName="_RazorGenerateHash" />
</Hash>
<MakeDir
Directories="$(IntermediateOutputPath)"
Condition="!Exists('$(IntermediateOutputPath)')" />
<WriteLinesToFile
Lines="$(_RazorGenerateHash)"
File="$(_RazorGenerateHashFile)"
Overwrite="True"
WriteOnlyWhenDifferent="True" />
<ItemGroup>
<FileWrites Include="$(_RazorGenerateHashFile)" />
</ItemGroup>
</Target>
<Target <Target
Name="RazorCoreGenerate" Name="RazorCoreGenerate"
DependsOnTargets="_ResolveRazorCoreGenerateInputs;_RazorResolveTagHelpers" DependsOnTargets="_ResolveRazorCoreGenerateInputs;_RazorResolveTagHelpers"
Inputs="$(MSBuildAllProjects);$(_RazorGenerateHashFile);$(_RazorTagHelperOutputCache);@(RazorGenerate)" Inputs="$(MSBuildAllProjects);$(_RazorGenerateHashFile);$(_RazorTagHelperOutputCache);@(RazorGenerate)"
Outputs="@(_RazorGenerateOutput)"> Outputs="@(_RazorGenerateOutput)"
Condition="'@(RazorGenerate)'!= ''">
<RemoveDir <RemoveDir
Directories="$(RazorGenerateOutputPath)" Directories="$(RazorGenerateOutputPath)"
@ -142,14 +155,22 @@
</ItemGroup> </ItemGroup>
</Target> </Target>
<Target Name="_RazorAddBuiltProjectOutputGroupOutput" Condition="'$(RazorCompileOnBuild)' == 'true'" BeforeTargets="BuiltProjectOutputGroup"> <Target
<ItemGroup> Name="_RazorAddBuiltProjectOutputGroupOutput"
DependsOnTargets="RazorResolveGenerateInputs"
BeforeTargets="BuiltProjectOutputGroup">
<ItemGroup Condition="'@(RazorGenerate)'!= '' and '$(RazorCompileOnBuild)' == 'true'">
<BuiltProjectOutputGroupOutput Include="@(RazorIntermediateAssembly)" FinalOutputPath="$(Outdir)$(RazorTargetName).dll" /> <BuiltProjectOutputGroupOutput Include="@(RazorIntermediateAssembly)" FinalOutputPath="$(Outdir)$(RazorTargetName).dll" />
</ItemGroup> </ItemGroup>
</Target> </Target>
<Target Name="_RazorGetCopyToOutputDirectoryItems"> <Target
<ItemGroup> Name="_RazorGetCopyToOutputDirectoryItems"
DependsOnTargets="RazorResolveGenerateInputs">
<ItemGroup Condition="'@(RazorGenerate)'!= '' and '$(RazorCompileOnBuild)' == 'true'">
<AllItemsFullPathWithTargetPath Include="@(RazorIntermediateAssembly->'%(FullPath)')"> <AllItemsFullPathWithTargetPath Include="@(RazorIntermediateAssembly->'%(FullPath)')">
<TargetPath>%(Filename)%(Extension)</TargetPath> <TargetPath>%(Filename)%(Extension)</TargetPath>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
@ -159,6 +180,7 @@
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</AllItemsFullPathWithTargetPath> </AllItemsFullPathWithTargetPath>
</ItemGroup> </ItemGroup>
</Target> </Target>
<Target Name="_RazorCopyFilesToOutputDirectory"> <Target Name="_RazorCopyFilesToOutputDirectory">

View File

@ -133,10 +133,18 @@ namespace Microsoft.AspNetCore.Razor.Design.IntegrationTests
directoryPath = Path.Combine(result.Project.DirectoryPath, directoryPath); directoryPath = Path.Combine(result.Project.DirectoryPath, directoryPath);
var files = Directory.GetFiles(directoryPath, searchPattern, SearchOption.AllDirectories); if (Directory.Exists(directoryPath))
if (files.Length != expected)
{ {
throw new FileCountException(result, expected, directoryPath, searchPattern, files); var files = Directory.GetFiles(directoryPath, searchPattern, SearchOption.AllDirectories);
if (files.Length != expected)
{
throw new FileCountException(result, expected, directoryPath, searchPattern, files);
}
}
else if (expected > 0)
{
// directory doesn't exist, that's OK if we expected to find nothing.
throw new FileCountException(result, expected, directoryPath, searchPattern, Array.Empty<string>());
} }
} }

View File

@ -41,6 +41,21 @@ namespace Microsoft.AspNetCore.Razor.Design.IntegrationTests
} }
} }
[Fact]
[InitializeTestProject("SimpleMvc")]
public async Task Build_SimpleMvc_NoopsWithNoFiles()
{
Directory.Delete(Path.Combine(Project.DirectoryPath, "Views"), recursive: true);
var result = await DotnetMSBuild("Build", "/p:RazorCompileOnBuild=true");
Assert.BuildPassed(result);
Assert.FileExists(result, OutputPath, "SimpleMvc.dll");
Assert.FileExists(result, OutputPath, "SimpleMvc.pdb");
Assert.FileDoesNotExist(result, OutputPath, "SimpleMvc.PrecompiledViews.dll");
Assert.FileDoesNotExist(result, OutputPath, "SimpleMvc.PrecompiledViews.pdb");
}
[Fact] [Fact]
[InitializeTestProject("SimpleMvc")] [InitializeTestProject("SimpleMvc")]
public async Task Build_ErrorInGeneratedCode_ReportsMSBuildError() public async Task Build_ErrorInGeneratedCode_ReportsMSBuildError()

View File

@ -11,7 +11,7 @@ namespace Microsoft.AspNetCore.Razor.Design.IntegrationTests
{ {
[Fact] [Fact]
[InitializeTestProject("ClassLibrary")] [InitializeTestProject("ClassLibrary")]
public async Task Pack_Wortks_IncludesRazorAssembly() public async Task Pack_Works_IncludesRazorAssembly()
{ {
var result = await DotnetMSBuild("Pack", "/p:RazorCompileOnBuild=true"); var result = await DotnetMSBuild("Pack", "/p:RazorCompileOnBuild=true");

View File

@ -178,5 +178,25 @@ namespace Microsoft.AspNetCore.Razor.Design.IntegrationTests
Assert.BuildPassed(result); Assert.BuildPassed(result);
Assert.FileDoesNotExist(result, generatedFile); Assert.FileDoesNotExist(result, generatedFile);
} }
[Fact]
[InitializeTestProject("SimpleMvc")]
public async Task RazorGenerate_Noops_WithNoFiles()
{
Directory.Delete(Path.Combine(Project.DirectoryPath, "Views"), recursive: true);
var result = await DotnetMSBuild(RazorGenerateTarget);
Assert.BuildPassed(result);
// We shouldn't need to look for tag helpers
Assert.FileDoesNotExist(result, Path.Combine(IntermediateOutputPath, "SimpleMvc.TagHelpers.input.cache"));
Assert.FileDoesNotExist(result, Path.Combine(IntermediateOutputPath, "SimpleMvc.TagHelpers.output.cache"));
// We shouldn't need to hash the files
Assert.FileDoesNotExist(result, Path.Combine(IntermediateOutputPath, "SimpleMvc.RazorCoreGenerate.cache"));
Assert.FileCountEquals(result, 0, RazorIntermediateOutputPath, "*.cs");
}
} }
} }

View File

@ -10,7 +10,9 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Content Include="**\*.cshtml"/> <Content Include="**\*.cshtml">
<Pack>false</Pack>
</Content>
</ItemGroup> </ItemGroup>
<ItemGroup Condition="'$(BinariesRoot)'!=''"> <ItemGroup Condition="'$(BinariesRoot)'!=''">