diff --git a/src/Microsoft.DotNet.ProjectJsonMigration/Rules/MigrateBuildOptionsRule.cs b/src/Microsoft.DotNet.ProjectJsonMigration/Rules/MigrateBuildOptionsRule.cs index 139d13985..3193c6de2 100644 --- a/src/Microsoft.DotNet.ProjectJsonMigration/Rules/MigrateBuildOptionsRule.cs +++ b/src/Microsoft.DotNet.ProjectJsonMigration/Rules/MigrateBuildOptionsRule.cs @@ -151,7 +151,10 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Rules .WithMetadata("CopyToOutputDirectory", "PreserveNewest"); private IncludeContextTransform CopyToOutputFilesTransformForWeb => - new UpdateContextTransform("None", transformMappings: true) + new UpdateContextTransform( + "None", + transformMappings: true, + excludePatternsRule: pattern => PatternIncludedInWebSdk(pattern)) .WithMetadata("CopyToOutputDirectory", "PreserveNewest"); private AddPropertyTransform GenerateRuntimeConfigurationFilesTransform => @@ -226,6 +229,14 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Rules ConstructTransformLists(); } + private bool PatternIncludedInWebSdk(string pattern) + { + return pattern.Equals("wwwroot") || + pattern.Contains("web.config") || + pattern.Equals("**/*.cshtml") || + pattern.Contains(".json"); + } + private bool ContainsCompilerResources(string projectDirectory) { return Directory.Exists(Path.Combine(projectDirectory, "compiler", "resources")); diff --git a/src/Microsoft.DotNet.ProjectJsonMigration/Rules/MigratePublishOptionsRule.cs b/src/Microsoft.DotNet.ProjectJsonMigration/Rules/MigratePublishOptionsRule.cs index 6628a2d44..9e1429a96 100644 --- a/src/Microsoft.DotNet.ProjectJsonMigration/Rules/MigratePublishOptionsRule.cs +++ b/src/Microsoft.DotNet.ProjectJsonMigration/Rules/MigratePublishOptionsRule.cs @@ -45,7 +45,18 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Rules .WithMetadata("CopyToPublishDirectory", "PreserveNewest"); private IncludeContextTransform CopyToPublishDirectoryTransformForWeb => - new UpdateContextTransform("None", transformMappings: true) + new UpdateContextTransform( + "None", + transformMappings: true, + excludePatternsRule: pattern => PatternIncludedInWebSdk(pattern)) .WithMetadata("CopyToPublishDirectory", "PreserveNewest"); + + private bool PatternIncludedInWebSdk(string pattern) + { + return pattern.Equals("wwwroot/**") || + pattern.Equals("**/web.config") || + pattern.Equals("**/*.cshtml") || + pattern.Equals("**/*.json"); + } } } diff --git a/src/Microsoft.DotNet.ProjectJsonMigration/Transforms/UpdateContextTransform.cs b/src/Microsoft.DotNet.ProjectJsonMigration/Transforms/UpdateContextTransform.cs index 4e3080f52..68b53707e 100644 --- a/src/Microsoft.DotNet.ProjectJsonMigration/Transforms/UpdateContextTransform.cs +++ b/src/Microsoft.DotNet.ProjectJsonMigration/Transforms/UpdateContextTransform.cs @@ -16,10 +16,13 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Transforms itemName, includeContext => string.Empty, includeContext => FormatGlobPatternsForMsbuild(includeContext.ExcludeFiles, includeContext.SourceBasePath), - includeContext => FormatGlobPatternsForMsbuild(includeContext.IncludeFiles, includeContext.SourceBasePath), + includeContext => FormatGlobPatternsForMsbuild( + includeContext.IncludeFiles.OrEmptyIfNull().Where( + pattern => !ExcludePatternRule(pattern)), includeContext.SourceBasePath), includeContext => includeContext != null && includeContext.IncludeFiles != null - && includeContext.IncludeFiles.Count > 0); + && includeContext.IncludeFiles.Where( + pattern => !ExcludePatternRule(pattern)).Count() > 0); protected override Func> IncludeExcludeTransformGetter => (itemName) => new AddItemTransform( @@ -39,24 +42,27 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Transforms .Union(includeContext.BuiltInsInclude.OrEmptyIfNull()) .Union(includeContext.CustomIncludePatterns.OrEmptyIfNull()); - return FormatGlobPatternsForMsbuild(fullIncludeSet, includeContext.SourceBasePath); + return FormatGlobPatternsForMsbuild( + fullIncludeSet.Where(pattern => !ExcludePatternRule(pattern)), + includeContext.SourceBasePath); }, includeContext => { - return includeContext != null && - ( - (includeContext.IncludePatterns != null && includeContext.IncludePatterns.Count > 0) - || - (includeContext.BuiltInsInclude != null && includeContext.BuiltInsInclude.Count > 0) - || - (includeContext.CustomIncludePatterns != null && includeContext.CustomIncludePatterns.Count > 0) - ); + return includeContext != null &&includeContext.IncludePatterns.OrEmptyIfNull() + .Union(includeContext.BuiltInsInclude.OrEmptyIfNull()) + .Union(includeContext.CustomIncludePatterns.OrEmptyIfNull()) + .Where(pattern => !ExcludePatternRule(pattern)).Count() > 0; }); public UpdateContextTransform( string itemName, bool transformMappings = true, - Func condition = null) : base(itemName, transformMappings, condition) + Func condition = null, + Func excludePatternsRule = null) : base( + itemName, + transformMappings, + condition, + excludePatternsRule: excludePatternsRule) { } } diff --git a/src/Microsoft.DotNet.ProjectJsonMigration/transforms/IncludeContextTransform.cs b/src/Microsoft.DotNet.ProjectJsonMigration/transforms/IncludeContextTransform.cs index e8ff641e3..ac91119c9 100644 --- a/src/Microsoft.DotNet.ProjectJsonMigration/transforms/IncludeContextTransform.cs +++ b/src/Microsoft.DotNet.ProjectJsonMigration/transforms/IncludeContextTransform.cs @@ -20,12 +20,12 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Transforms new AddItemTransform( itemName, includeContext => FormatGlobPatternsForMsbuild(includeContext.IncludeFiles.OrEmptyIfNull() - .Where((pattern) => !_excludePatternRule(pattern)), + .Where((pattern) => !ExcludePatternRule(pattern)), includeContext.SourceBasePath), includeContext => FormatGlobPatternsForMsbuild(includeContext.ExcludeFiles, includeContext.SourceBasePath), includeContext => includeContext != null && includeContext.IncludeFiles != null - && includeContext.IncludeFiles.Where((pattern) => !_excludePatternRule(pattern)).Count() > 0); + && includeContext.IncludeFiles.Where((pattern) => !ExcludePatternRule(pattern)).Count() > 0); protected virtual Func> IncludeExcludeTransformGetter => (itemName) => new AddItemTransform( @@ -39,7 +39,7 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Transforms fullIncludeSet = fullIncludeSet.Union(includeContext.BuiltInsInclude.OrEmptyIfNull()); } - fullIncludeSet = fullIncludeSet.Where((pattern) => !_excludePatternRule(pattern)); + fullIncludeSet = fullIncludeSet.Where((pattern) => !ExcludePatternRule(pattern)); return FormatGlobPatternsForMsbuild(fullIncludeSet, includeContext.SourceBasePath); }, @@ -55,9 +55,9 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Transforms { return includeContext != null && ( - (includeContext.IncludePatterns != null && includeContext.IncludePatterns.Where((pattern) => !_excludePatternRule(pattern)).Count() > 0) + (includeContext.IncludePatterns != null && includeContext.IncludePatterns.Where((pattern) => !ExcludePatternRule(pattern)).Count() > 0) || - (includeContext.CustomIncludePatterns != null && includeContext.CustomIncludePatterns.Where((pattern) => !_excludePatternRule(pattern)).Count() > 0) + (includeContext.CustomIncludePatterns != null && includeContext.CustomIncludePatterns.Where((pattern) => !ExcludePatternRule(pattern)).Count() > 0) || (_emitBuiltInIncludes && includeContext.BuiltInsInclude != null && @@ -75,7 +75,7 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Transforms private readonly string _itemName; private bool _transformMappings; - private Func _excludePatternRule; + protected Func ExcludePatternRule { get; } private bool _emitBuiltInIncludes; private readonly List> _metadata = new List>(); @@ -89,7 +89,7 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Transforms _itemName = itemName; _transformMappings = transformMappings; _emitBuiltInIncludes = emitBuiltInIncludes; - _excludePatternRule = excludePatternsRule ?? ((pattern) => false); + ExcludePatternRule = excludePatternsRule ?? ((pattern) => false); _mappingsToTransfrom = (addItemTransform, targetPath) => { diff --git a/test/Microsoft.DotNet.ProjectJsonMigration.Tests/Rules/GivenThatIWantToMigrateBuildOptions.cs b/test/Microsoft.DotNet.ProjectJsonMigration.Tests/Rules/GivenThatIWantToMigrateBuildOptions.cs index fa206331d..679d81a54 100644 --- a/test/Microsoft.DotNet.ProjectJsonMigration.Tests/Rules/GivenThatIWantToMigrateBuildOptions.cs +++ b/test/Microsoft.DotNet.ProjectJsonMigration.Tests/Rules/GivenThatIWantToMigrateBuildOptions.cs @@ -450,11 +450,35 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Tests mockProj.Properties.First(p => p.Name == "GenerateDocumentationFile").Value.Should().Be("true"); } + [Fact] + public void ExcludedPatternsAreNotEmittedOnNoneWhenBuildingAWebProject() + { + var mockProj = RunBuildOptionsRuleOnPj(@" + { + ""buildOptions"": { + ""emitEntryPoint"": true, + ""copyToOutput"": { + ""include"": [""wwwroot"", ""**/*.cshtml"", ""appsettings.json"", ""web.config""], + } + }, + ""dependencies"": { + ""Microsoft.AspNetCore.Mvc"" : { + ""version"": ""1.0.0"" + } + }, + ""frameworks"": { + ""netcoreapp1.0"": {} + } + }"); + + mockProj.Items.Count(i => i.ItemType.Equals("None", StringComparison.Ordinal)).Should().Be(0); + } + [Theory] [InlineData("compile", "Compile", 3, "")] [InlineData("embed", "EmbeddedResource", 3, ";rootfile.cs")] [InlineData("copyToOutput", "None", 2, ";rootfile.cs")] - private void MigratingGroupIncludeExcludePopulatesAppropriateProjectItemElement( + public void MigratingGroupIncludeExcludePopulatesAppropriateProjectItemElement( string group, string itemName, int expectedNumberOfCompileItems, @@ -530,7 +554,7 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Tests [InlineData("compile", "Compile", "")] [InlineData("embed", "EmbeddedResource", ";rootfile.cs")] [InlineData("copyToOutput", "None", ";rootfile.cs")] - private void MigratingGroupIncludeOnlyPopulatesAppropriateProjectItemElement( + public void MigratingGroupIncludeOnlyPopulatesAppropriateProjectItemElement( string group, string itemName, string expectedRootFiles)