Adding pack files migration.

This commit is contained in:
Livar Cunha 2016-10-20 11:24:58 -07:00
parent ecbc45098d
commit ddef1fc424
3 changed files with 112 additions and 17 deletions

View file

@ -3,10 +3,14 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
using System.Linq; using System.Linq;
using Microsoft.Build.Construction;
using Microsoft.DotNet.ProjectJsonMigration; using Microsoft.DotNet.ProjectJsonMigration;
using Microsoft.DotNet.ProjectJsonMigration.Transforms; using Microsoft.DotNet.ProjectJsonMigration.Transforms;
using Microsoft.DotNet.ProjectModel; using Microsoft.DotNet.ProjectModel;
using Microsoft.DotNet.ProjectModel.Files;
using Microsoft.DotNet.Tools.Common;
namespace Microsoft.DotNet.ProjectJsonMigration.Rules namespace Microsoft.DotNet.ProjectJsonMigration.Rules
{ {
@ -52,6 +56,19 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Rules
packOptions => packOptions.RepositoryUrl, packOptions => packOptions.RepositoryUrl,
packOptions => !string.IsNullOrEmpty(packOptions.RepositoryUrl)); packOptions => !string.IsNullOrEmpty(packOptions.RepositoryUrl));
private IncludeContextTransform PackFilesTransform =>
new IncludeContextTransform("Content", transformMappings: true)
.WithMetadata("Pack", "True")
.WithMappingsToTransform(_mappingsToTransfrom);
private Func<AddItemTransform<IncludeContext>, string, AddItemTransform<IncludeContext>> _mappingsToTransfrom =>
(addItemTransform, targetPath) =>
{
var msbuildLinkMetadataValue = ConvertTargetPathToMsbuildMetadata(targetPath);
return addItemTransform.WithMetadata("PackagePath", msbuildLinkMetadataValue);
};
private readonly ITransformApplicator _transformApplicator; private readonly ITransformApplicator _transformApplicator;
private List<AddPropertyTransform<PackOptions>> _propertyTransforms; private List<AddPropertyTransform<PackOptions>> _propertyTransforms;
@ -80,21 +97,52 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Rules
public void Apply(MigrationSettings migrationSettings, MigrationRuleInputs migrationRuleInputs) public void Apply(MigrationSettings migrationSettings, MigrationRuleInputs migrationRuleInputs)
{ {
var propertyGroup = migrationRuleInputs.CommonPropertyGroup;
var projectContext = migrationRuleInputs.DefaultProjectContext; var projectContext = migrationRuleInputs.DefaultProjectContext;
var packOptions = projectContext.ProjectFile.PackOptions; var packOptions = projectContext.ProjectFile.PackOptions;
if(packOptions.PackInclude != null) TransformProperties(packOptions, migrationRuleInputs.CommonPropertyGroup);
{
MigrationErrorCodes
.MIGRATE20018("Migrating projects with Files specified in PackOptions is not supported.").Throw();
}
TransformPackFiles(packOptions, migrationRuleInputs.CommonItemGroup);
}
private void TransformProperties(PackOptions packOptions, ProjectPropertyGroupElement propertyGroup)
{
foreach (var propertyTransfrom in _propertyTransforms) foreach (var propertyTransfrom in _propertyTransforms)
{ {
_transformApplicator.Execute(propertyTransfrom.Transform(packOptions), propertyGroup, true); _transformApplicator.Execute(propertyTransfrom.Transform(packOptions), propertyGroup, true);
} }
} }
private void TransformPackFiles(PackOptions packOptions, ProjectItemGroupElement itemGroup)
{
var transformResult = PackFilesTransform.Transform(packOptions.PackInclude);
if (transformResult != null && transformResult.Any())
{
_transformApplicator.Execute(
transformResult,
itemGroup,
mergeExisting: true);
}
}
private string ConvertTargetPathToMsbuildMetadata(string targetPath)
{
var targetIsDirectory = PathIsDirectory(targetPath);
if (targetIsDirectory)
{
return targetPath;
}
return Path.GetDirectoryName(targetPath);
}
private bool PathIsDirectory(string targetPath)
{
var normalizedTargetPath = PathUtility.GetPathWithDirectorySeparator(targetPath);
return normalizedTargetPath[normalizedTargetPath.Length - 1] == Path.DirectorySeparatorChar;
}
} }
} }

View file

@ -59,6 +59,8 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Transforms
private Func<string, string, AddItemTransform<IncludeContext>> MappingsIncludeExcludeTransformGetter => private Func<string, string, AddItemTransform<IncludeContext>> MappingsIncludeExcludeTransformGetter =>
(itemName, targetPath) => AddMappingToTransform(IncludeExcludeTransformGetter(itemName), targetPath); (itemName, targetPath) => AddMappingToTransform(IncludeExcludeTransformGetter(itemName), targetPath);
private Func<AddItemTransform<IncludeContext>, string, AddItemTransform<IncludeContext>> _mappingsToTransfrom;
private readonly string _itemName; private readonly string _itemName;
private bool _transformMappings; private bool _transformMappings;
private readonly List<ItemMetadataValue<IncludeContext>> _metadata = new List<ItemMetadataValue<IncludeContext>>(); private readonly List<ItemMetadataValue<IncludeContext>> _metadata = new List<ItemMetadataValue<IncludeContext>>();
@ -70,6 +72,13 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Transforms
{ {
_itemName = itemName; _itemName = itemName;
_transformMappings = transformMappings; _transformMappings = transformMappings;
_mappingsToTransfrom = (addItemTransform, targetPath) =>
{
var msbuildLinkMetadataValue = ConvertTargetPathToMsbuildMetadata(targetPath);
return addItemTransform.WithMetadata("Link", msbuildLinkMetadataValue);
};
} }
public IncludeContextTransform WithMetadata(string metadataName, string metadataValue) public IncludeContextTransform WithMetadata(string metadataName, string metadataValue)
@ -84,6 +93,13 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Transforms
return this; return this;
} }
public IncludeContextTransform WithMappingsToTransform(
Func<AddItemTransform<IncludeContext>, string, AddItemTransform<IncludeContext>> mappingsToTransfrom)
{
_mappingsToTransfrom = mappingsToTransfrom;
return this;
}
private IEnumerable<Tuple<AddItemTransform<IncludeContext>, IncludeContext>> CreateTransformSet(IncludeContext source) private IEnumerable<Tuple<AddItemTransform<IncludeContext>, IncludeContext>> CreateTransformSet(IncludeContext source)
{ {
var transformSet = new List<Tuple<AddItemTransform<IncludeContext>, IncludeContext>> var transformSet = new List<Tuple<AddItemTransform<IncludeContext>, IncludeContext>>
@ -172,10 +188,7 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Transforms
AddItemTransform<IncludeContext> addItemTransform, AddItemTransform<IncludeContext> addItemTransform,
string targetPath) string targetPath)
{ {
var targetIsFile = MappingsTargetPathIsFile(targetPath); return _mappingsToTransfrom(addItemTransform, targetPath);
var msbuildLinkMetadataValue = ConvertTargetPathToMsbuildMetadata(targetPath, targetIsFile);
return addItemTransform.WithMetadata("Link", msbuildLinkMetadataValue);
} }
private bool PatternIsDirectory(string pattern, string projectDirectory) private bool PatternIsDirectory(string pattern, string projectDirectory)
@ -192,8 +205,10 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Transforms
return Directory.Exists(path); return Directory.Exists(path);
} }
private string ConvertTargetPathToMsbuildMetadata(string targetPath, bool targetIsFile) private string ConvertTargetPathToMsbuildMetadata(string targetPath)
{ {
var targetIsFile = MappingsTargetPathIsFile(targetPath);
if (targetIsFile) if (targetIsFile)
{ {
return targetPath; return targetPath;

View file

@ -187,21 +187,53 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Tests
} }
[Fact] [Fact]
public void Migrating_Files_throws_an_exception_for_now() public void Migrating_Files_without_mappings_populates_content_with_same_path_as_include_and_pack_true()
{ {
Action action = () => RunPackOptionsRuleOnPj(@" var mockProj = RunPackOptionsRuleOnPj(@"
{ {
""packOptions"": { ""packOptions"": {
""files"": { ""files"": {
""include"": [""somefile.cs""] ""include"": [""path/to/some/file.cs"", ""path/to/some/other/file.cs""]
} }
} }
}"); }");
action.ShouldThrow<Exception>() var contentItems = mockProj.Items
.Where(e => e.Message.Contains("Migrating projects with Files specified in PackOptions is not supported.")); .Where(item => item.ItemType.Equals("Content", StringComparison.Ordinal))
.Where(item => item.GetMetadataWithName("Pack").Value == "True");
contentItems.Count().Should().Be(1);
contentItems.First().Include.Should().Be(@"path\to\some\file.cs;path\to\some\other\file.cs");
} }
[Fact]
public void Migrating_Files_with_mappings_populates_content_PackagePath_metadata()
{
var mockProj = RunPackOptionsRuleOnPj(@"
{
""packOptions"": {
""files"": {
""include"": [""path/to/some/file.cs""],
""mappings"": {
""some/other/path/file.cs"": ""path/to/some/file.cs""
}
}
}
}");
var contentItems = mockProj.Items
.Where(item => item.ItemType.Equals("Content", StringComparison.Ordinal))
.Where(item =>
item.GetMetadataWithName("Pack").Value == "True" &&
item.GetMetadataWithName("PackagePath") != null);
contentItems.Count().Should().Be(1);
contentItems.First().Include.Should().Be(@"path\to\some\file.cs");
contentItems.First().GetMetadataWithName("PackagePath").Value.Should().Be(@"some/other/path");
}
// add a test where we map the file to the empty folder (package root)
private ProjectRootElement RunPackOptionsRuleOnPj(string packOptions, string testDirectory = null) private ProjectRootElement RunPackOptionsRuleOnPj(string packOptions, string testDirectory = null)
{ {
testDirectory = testDirectory ?? Temp.CreateDirectory().Path; testDirectory = testDirectory ?? Temp.CreateDirectory().Path;