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);
@(_RazorDebugSymbolsIntermediatePath);
$(NonExistentFile)"
DependsOnTargets="RazorCoreGenerate">
DependsOnTargets="RazorCoreGenerate"
Condition="'@(RazorCompile)'!=''">
<!-- 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,
so the compiler warning would be redundant. -->

View File

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

View File

@ -37,11 +37,52 @@
<_RazorTagHelperToolAssembly>$(_RazorMSBuildRoot)tools\Microsoft.AspNetCore.Razor.TagHelperTool.dll</_RazorTagHelperToolAssembly>
</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)
-->
<Target
Name="RazorResolveAssemblyReferences"
Name="_RazorResolveReferences"
DependsOnTargets="ResolveReferences">
<ItemGroup>
<RazorReferencePath Include="@(ReferencePath)"/>
@ -51,9 +92,10 @@
<Target
Name="_RazorResolveTagHelpers"
DependsOnTargets="Compile;RazorResolveAssemblyReferences"
DependsOnTargets="Compile;_RazorResolveReferences"
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
@ -81,41 +123,12 @@
</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
Name="RazorCoreGenerate"
DependsOnTargets="_ResolveRazorCoreGenerateInputs;_RazorResolveTagHelpers"
Inputs="$(MSBuildAllProjects);$(_RazorGenerateHashFile);$(_RazorTagHelperOutputCache);@(RazorGenerate)"
Outputs="@(_RazorGenerateOutput)">
Outputs="@(_RazorGenerateOutput)"
Condition="'@(RazorGenerate)'!= ''">
<RemoveDir
Directories="$(RazorGenerateOutputPath)"
@ -142,14 +155,22 @@
</ItemGroup>
</Target>
<Target Name="_RazorAddBuiltProjectOutputGroupOutput" Condition="'$(RazorCompileOnBuild)' == 'true'" BeforeTargets="BuiltProjectOutputGroup">
<ItemGroup>
<Target
Name="_RazorAddBuiltProjectOutputGroupOutput"
DependsOnTargets="RazorResolveGenerateInputs"
BeforeTargets="BuiltProjectOutputGroup">
<ItemGroup Condition="'@(RazorGenerate)'!= '' and '$(RazorCompileOnBuild)' == 'true'">
<BuiltProjectOutputGroupOutput Include="@(RazorIntermediateAssembly)" FinalOutputPath="$(Outdir)$(RazorTargetName).dll" />
</ItemGroup>
</Target>
<Target Name="_RazorGetCopyToOutputDirectoryItems">
<ItemGroup>
<Target
Name="_RazorGetCopyToOutputDirectoryItems"
DependsOnTargets="RazorResolveGenerateInputs">
<ItemGroup Condition="'@(RazorGenerate)'!= '' and '$(RazorCompileOnBuild)' == 'true'">
<AllItemsFullPathWithTargetPath Include="@(RazorIntermediateAssembly->'%(FullPath)')">
<TargetPath>%(Filename)%(Extension)</TargetPath>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
@ -159,6 +180,7 @@
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</AllItemsFullPathWithTargetPath>
</ItemGroup>
</Target>
<Target Name="_RazorCopyFilesToOutputDirectory">

View File

@ -133,10 +133,18 @@ namespace Microsoft.AspNetCore.Razor.Design.IntegrationTests
directoryPath = Path.Combine(result.Project.DirectoryPath, directoryPath);
var files = Directory.GetFiles(directoryPath, searchPattern, SearchOption.AllDirectories);
if (files.Length != expected)
if (Directory.Exists(directoryPath))
{
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]
[InitializeTestProject("SimpleMvc")]
public async Task Build_ErrorInGeneratedCode_ReportsMSBuildError()

View File

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

View File

@ -178,5 +178,25 @@ namespace Microsoft.AspNetCore.Razor.Design.IntegrationTests
Assert.BuildPassed(result);
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>
<Content Include="**\*.cshtml"/>
<Content Include="**\*.cshtml">
<Pack>false</Pack>
</Content>
</ItemGroup>
<ItemGroup Condition="'$(BinariesRoot)'!=''">