Adding a UpdateContextTransform that generates items with the Update attribute set instead of Include. Also, removed the Exists condition for published content, because now that we are using Update, that check is not needed. I left the option to set a condition on the metadata though.
This commit is contained in:
parent
84682562bf
commit
a22f4be938
8 changed files with 111 additions and 50 deletions
|
@ -96,7 +96,8 @@
|
|||
"wwwroot",
|
||||
"Views",
|
||||
"appsettings.json",
|
||||
"web.config"
|
||||
"web.config",
|
||||
"nonExistingFile.config"
|
||||
]
|
||||
},
|
||||
|
||||
|
|
|
@ -49,7 +49,7 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Rules
|
|||
|
||||
foreach (var item in msbuildProject.Items)
|
||||
{
|
||||
if (string.IsNullOrEmpty(item.Include))
|
||||
if (string.IsNullOrEmpty(item.Include) && string.IsNullOrEmpty(item.Update))
|
||||
{
|
||||
item.Parent.RemoveChild(item);
|
||||
}
|
||||
|
|
|
@ -20,7 +20,7 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Rules
|
|||
var csproj = migrationRuleInputs.OutputMSBuildProject;
|
||||
var projectContext = migrationRuleInputs.DefaultProjectContext;
|
||||
|
||||
var transformResult = CopyToOutputFilesTransform.Transform(projectContext.ProjectFile.PublishOptions);
|
||||
var transformResult = CopyToPublishDirectoryTransform.Transform(projectContext.ProjectFile.PublishOptions);
|
||||
|
||||
if (transformResult != null && transformResult.Any())
|
||||
{
|
||||
|
@ -32,8 +32,8 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Rules
|
|||
}
|
||||
}
|
||||
|
||||
private IncludeContextTransform CopyToOutputFilesTransform =>
|
||||
new IncludeContextTransform("Content", transformMappings: true)
|
||||
.WithMetadata("CopyToPublishDirectory", "PreserveNewest", "Exists(%(Identity))");
|
||||
private IncludeContextTransform CopyToPublishDirectoryTransform =>
|
||||
new UpdateContextTransform("Content", transformMappings: true)
|
||||
.WithMetadata("CopyToPublishDirectory", "PreserveNewest");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -40,7 +40,7 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Transforms
|
|||
var item = element as ProjectItemElement;
|
||||
var destinationItemGroup = destinationElement as ProjectItemGroupElement;
|
||||
|
||||
MigrationTrace.Instance.WriteLine($"{nameof(ItemTransformApplicator)}: Item {{ ItemType: {item.ItemType}, Condition: {item.Condition}, Include: {item.Include}, Exclude: {item.Exclude} }}");
|
||||
MigrationTrace.Instance.WriteLine($"{nameof(ItemTransformApplicator)}: Item {{ ItemType: {item.ItemType}, Condition: {item.Condition}, Include: {item.Include}, Exclude: {item.Exclude}, Update: {item.Update} }}");
|
||||
MigrationTrace.Instance.WriteLine($"{nameof(ItemTransformApplicator)}: ItemGroup {{ Condition: {destinationItemGroup.Condition} }}");
|
||||
|
||||
if (mergeExisting)
|
||||
|
@ -88,6 +88,8 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Transforms
|
|||
var outputItem = itemGroup.ContainingProject.CreateItemElement("___TEMP___");
|
||||
outputItem.CopyFrom(item);
|
||||
|
||||
MigrationTrace.Instance.WriteLine($"{nameof(ItemTransformApplicator)}: AddItemToItemGroup {{ ItemType: {outputItem.ItemType}, Condition: {outputItem.Condition}, Include: {outputItem.Include}, Exclude: {outputItem.Exclude}, Update: {outputItem.Update} }}");
|
||||
|
||||
itemGroup.AppendChild(outputItem);
|
||||
outputItem.AddMetadata(item.Metadata);
|
||||
}
|
||||
|
|
|
@ -0,0 +1,60 @@
|
|||
// Copyright (c) .NET Foundation and contributors. All rights reserved.
|
||||
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||
|
||||
using Microsoft.DotNet.Cli.Utils;
|
||||
using Microsoft.DotNet.Internal.ProjectModel.Files;
|
||||
using System;
|
||||
using System.Linq;
|
||||
|
||||
namespace Microsoft.DotNet.ProjectJsonMigration.Transforms
|
||||
{
|
||||
internal class UpdateContextTransform : IncludeContextTransform
|
||||
{
|
||||
protected override Func<string, AddItemTransform<IncludeContext>> IncludeFilesExcludeFilesTransformGetter =>
|
||||
(itemName) =>
|
||||
new AddItemTransform<IncludeContext>(
|
||||
itemName,
|
||||
includeContext => string.Empty,
|
||||
includeContext => FormatGlobPatternsForMsbuild(includeContext.ExcludeFiles, includeContext.SourceBasePath),
|
||||
includeContext => FormatGlobPatternsForMsbuild(includeContext.IncludeFiles, includeContext.SourceBasePath),
|
||||
includeContext => includeContext != null
|
||||
&& includeContext.IncludeFiles != null
|
||||
&& includeContext.IncludeFiles.Count > 0);
|
||||
|
||||
protected override Func<string, AddItemTransform<IncludeContext>> IncludeExcludeTransformGetter =>
|
||||
(itemName) => new AddItemTransform<IncludeContext>(
|
||||
itemName,
|
||||
includeContext => string.Empty,
|
||||
includeContext =>
|
||||
{
|
||||
var fullExcludeSet = includeContext.ExcludePatterns.OrEmptyIfNull()
|
||||
.Union(includeContext.BuiltInsExclude.OrEmptyIfNull())
|
||||
.Union(includeContext.ExcludeFiles.OrEmptyIfNull());
|
||||
|
||||
return FormatGlobPatternsForMsbuild(fullExcludeSet, includeContext.SourceBasePath);
|
||||
},
|
||||
includeContext =>
|
||||
{
|
||||
var fullIncludeSet = includeContext.IncludePatterns.OrEmptyIfNull()
|
||||
.Union(includeContext.BuiltInsInclude.OrEmptyIfNull());
|
||||
|
||||
return FormatGlobPatternsForMsbuild(fullIncludeSet, includeContext.SourceBasePath);
|
||||
},
|
||||
includeContext =>
|
||||
{
|
||||
return includeContext != null &&
|
||||
(
|
||||
(includeContext.IncludePatterns != null && includeContext.IncludePatterns.Count > 0)
|
||||
||
|
||||
(includeContext.BuiltInsInclude != null && includeContext.BuiltInsInclude.Count > 0)
|
||||
);
|
||||
});
|
||||
|
||||
public UpdateContextTransform(
|
||||
string itemName,
|
||||
bool transformMappings = true,
|
||||
Func<IncludeContext, bool> condition = null) : base(itemName, transformMappings, condition)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
|
@ -18,6 +18,7 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Transforms
|
|||
|
||||
private readonly Func<T, string> _includeValueFunc;
|
||||
private readonly Func<T, string> _excludeValueFunc;
|
||||
private readonly Func<T, string> _updateValueFunc;
|
||||
|
||||
private readonly List<ItemMetadataValue<T>> _metadata = new List<ItemMetadataValue<T>>();
|
||||
|
||||
|
@ -26,7 +27,25 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Transforms
|
|||
IEnumerable<string> includeValues,
|
||||
IEnumerable<string> excludeValues,
|
||||
Func<T, bool> condition)
|
||||
: this(itemName, string.Join(";", includeValues), string.Join(";", excludeValues), condition) { }
|
||||
: this(
|
||||
itemName,
|
||||
string.Join(";", includeValues),
|
||||
string.Join(";", excludeValues),
|
||||
condition) { }
|
||||
|
||||
public AddItemTransform(
|
||||
string itemName,
|
||||
Func<T, string> includeValueFunc,
|
||||
Func<T, string> excludeValueFunc,
|
||||
Func<T, string> updateValueFunc,
|
||||
Func<T, bool> condition)
|
||||
: base(condition)
|
||||
{
|
||||
_itemName = itemName;
|
||||
_includeValueFunc = includeValueFunc;
|
||||
_excludeValueFunc = excludeValueFunc;
|
||||
_updateValueFunc = updateValueFunc;
|
||||
}
|
||||
|
||||
public AddItemTransform(
|
||||
string itemName,
|
||||
|
@ -101,9 +120,12 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Transforms
|
|||
{
|
||||
string includeValue = _includeValue ?? _includeValueFunc(source);
|
||||
string excludeValue = _excludeValue ?? _excludeValueFunc(source);
|
||||
string updateValue = _updateValueFunc != null ? _updateValueFunc(source) : null;
|
||||
|
||||
var item = _itemObjectGenerator.AddItem(_itemName, includeValue);
|
||||
var item = _itemObjectGenerator.AddItem(_itemName, "placeholder");
|
||||
item.Include = includeValue;
|
||||
item.Exclude = excludeValue;
|
||||
item.Update = updateValue;
|
||||
|
||||
foreach (var metadata in _metadata)
|
||||
{
|
||||
|
|
|
@ -15,7 +15,7 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Transforms
|
|||
{
|
||||
internal class IncludeContextTransform : ConditionalTransform<IncludeContext, IEnumerable<ProjectItemElement>>
|
||||
{
|
||||
private Func<string, AddItemTransform<IncludeContext>> IncludeFilesExcludeFilesTransformGetter =>
|
||||
protected virtual Func<string, AddItemTransform<IncludeContext>> IncludeFilesExcludeFilesTransformGetter =>
|
||||
(itemName) =>
|
||||
new AddItemTransform<IncludeContext>(
|
||||
itemName,
|
||||
|
@ -25,7 +25,7 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Transforms
|
|||
&& includeContext.IncludeFiles != null
|
||||
&& includeContext.IncludeFiles.Count > 0);
|
||||
|
||||
private Func<string, AddItemTransform<IncludeContext>> IncludeExcludeTransformGetter =>
|
||||
protected virtual Func<string, AddItemTransform<IncludeContext>> IncludeExcludeTransformGetter =>
|
||||
(itemName) => new AddItemTransform<IncludeContext>(
|
||||
itemName,
|
||||
includeContext =>
|
||||
|
@ -157,7 +157,7 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Transforms
|
|||
return transformSet.Select(t => t.Item1.Transform(t.Item2));
|
||||
}
|
||||
|
||||
private string FormatGlobPatternsForMsbuild(IEnumerable<string> patterns, string projectDirectory)
|
||||
protected string FormatGlobPatternsForMsbuild(IEnumerable<string> patterns, string projectDirectory)
|
||||
{
|
||||
if (patterns == null)
|
||||
{
|
||||
|
|
|
@ -38,14 +38,14 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Tests
|
|||
{
|
||||
item.Metadata.Count(m => m.Name == "CopyToPublishDirectory").Should().Be(1);
|
||||
|
||||
if (item.Include.Contains(@"src\file1.cs"))
|
||||
if (item.Update.Contains(@"src\file1.cs"))
|
||||
{
|
||||
item.Include.Should().Be(@"src\file1.cs;src\file2.cs");
|
||||
item.Update.Should().Be(@"src\file1.cs;src\file2.cs");
|
||||
item.Exclude.Should().Be(@"src\file2.cs");
|
||||
}
|
||||
else
|
||||
{
|
||||
item.Include.Should()
|
||||
item.Update.Should()
|
||||
.Be(@"root\**\*;src\**\*;rootfile.cs");
|
||||
|
||||
item.Exclude.Should()
|
||||
|
@ -55,36 +55,7 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Tests
|
|||
}
|
||||
|
||||
[Fact]
|
||||
public void MigratingPublishOptionsIncludeEmitsConditionalAttribute()
|
||||
{
|
||||
|
||||
var mockProj = RunPublishAndBuildOptionsRuleOnPj(@"
|
||||
{
|
||||
""publishOptions"": {
|
||||
""include"": [
|
||||
""appsettings.json"",
|
||||
""appsettings.Production.json"",
|
||||
""dist"",
|
||||
""Dockerfile"",
|
||||
""hosting.json"",
|
||||
""web.config""
|
||||
]
|
||||
}
|
||||
}");
|
||||
|
||||
mockProj.Items
|
||||
.Should()
|
||||
.ContainSingle(i => i.ItemType == "Content")
|
||||
.Which
|
||||
.Metadata
|
||||
.Should()
|
||||
.ContainSingle(m => m.Name == "CopyToPublishDirectory" &&
|
||||
m.Condition == "Exists(%(Identity))");
|
||||
|
||||
}
|
||||
|
||||
[Fact]
|
||||
private void MigratingPublishOptionsAndBuildOptionsCopyToOutputMergesContentItems()
|
||||
private void MigratingPublishOptionsAndBuildOptionsCopyToOutputDoesMergesContentItemsBecausePublishIsUpdateAndBuildIsInclude()
|
||||
{
|
||||
var testDirectory = Temp.CreateDirectory().Path;
|
||||
WriteFilesInProjectDirectory(testDirectory);
|
||||
|
@ -108,7 +79,7 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Tests
|
|||
}",
|
||||
testDirectory: testDirectory);
|
||||
|
||||
mockProj.Items.Count(i => i.ItemType.Equals("Content", StringComparison.Ordinal)).Should().Be(3);
|
||||
mockProj.Items.Count(i => i.ItemType.Equals("Content", StringComparison.Ordinal)).Should().Be(4);
|
||||
|
||||
// From ProjectReader #L725 (Both are empty)
|
||||
var defaultIncludePatterns = Enumerable.Empty<string>();
|
||||
|
@ -116,15 +87,20 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Tests
|
|||
|
||||
foreach (var item in mockProj.Items.Where(i => i.ItemType.Equals("Content", StringComparison.Ordinal)))
|
||||
{
|
||||
if (item.Include.Contains(@"root\**\*"))
|
||||
if (item.Update.Contains(@"root\**\*"))
|
||||
{
|
||||
item.Include.Should().Be(@"root\**\*");
|
||||
item.Update.Should().Be(@"root\**\*;src\**\*;rootfile.cs");
|
||||
item.Exclude.Should().Be(@"src\**\*;rootfile.cs;src\file3.cs");
|
||||
}
|
||||
else if (item.Update.Contains(@"src\file1.cs"))
|
||||
{
|
||||
item.Update.Should().Be(@"src\file1.cs;src\file2.cs");
|
||||
item.Exclude.Should().Be(@"src\file3.cs");
|
||||
}
|
||||
else if (item.Include.Contains(@"src\file1.cs"))
|
||||
{
|
||||
item.Include.Should().Be(@"src\file1.cs;src\file2.cs");
|
||||
item.Exclude.Should().Be(@"src\file2.cs;src\file3.cs");
|
||||
item.Exclude.Should().Be(@"src\file2.cs");
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -132,7 +108,7 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Tests
|
|||
.Be(@"src\**\*;rootfile.cs");
|
||||
|
||||
item.Exclude.Should()
|
||||
.Be(@"src\**\*;rootfile.cs;src\file2.cs;src\file3.cs");
|
||||
.Be(@"src\**\*;rootfile.cs;src\file2.cs");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue