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.Collections.Generic;
using System.IO;
using System.Linq;
using Microsoft.Build.Construction;
using Microsoft.DotNet.ProjectJsonMigration;
using Microsoft.DotNet.ProjectJsonMigration.Transforms;
using Microsoft.DotNet.ProjectModel;
using Microsoft.DotNet.ProjectModel.Files;
using Microsoft.DotNet.Tools.Common;
namespace Microsoft.DotNet.ProjectJsonMigration.Rules
{
@ -52,6 +56,19 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Rules
packOptions => 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 List<AddPropertyTransform<PackOptions>> _propertyTransforms;
@ -80,21 +97,52 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Rules
public void Apply(MigrationSettings migrationSettings, MigrationRuleInputs migrationRuleInputs)
{
var propertyGroup = migrationRuleInputs.CommonPropertyGroup;
var projectContext = migrationRuleInputs.DefaultProjectContext;
var packOptions = projectContext.ProjectFile.PackOptions;
if(packOptions.PackInclude != null)
{
MigrationErrorCodes
.MIGRATE20018("Migrating projects with Files specified in PackOptions is not supported.").Throw();
}
TransformProperties(packOptions, migrationRuleInputs.CommonPropertyGroup);
TransformPackFiles(packOptions, migrationRuleInputs.CommonItemGroup);
}
private void TransformProperties(PackOptions packOptions, ProjectPropertyGroupElement propertyGroup)
{
foreach (var propertyTransfrom in _propertyTransforms)
{
_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 =>
(itemName, targetPath) => AddMappingToTransform(IncludeExcludeTransformGetter(itemName), targetPath);
private Func<AddItemTransform<IncludeContext>, string, AddItemTransform<IncludeContext>> _mappingsToTransfrom;
private readonly string _itemName;
private bool _transformMappings;
private readonly List<ItemMetadataValue<IncludeContext>> _metadata = new List<ItemMetadataValue<IncludeContext>>();
@ -70,6 +72,13 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Transforms
{
_itemName = itemName;
_transformMappings = transformMappings;
_mappingsToTransfrom = (addItemTransform, targetPath) =>
{
var msbuildLinkMetadataValue = ConvertTargetPathToMsbuildMetadata(targetPath);
return addItemTransform.WithMetadata("Link", msbuildLinkMetadataValue);
};
}
public IncludeContextTransform WithMetadata(string metadataName, string metadataValue)
@ -84,6 +93,13 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Transforms
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)
{
var transformSet = new List<Tuple<AddItemTransform<IncludeContext>, IncludeContext>>
@ -172,10 +188,7 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Transforms
AddItemTransform<IncludeContext> addItemTransform,
string targetPath)
{
var targetIsFile = MappingsTargetPathIsFile(targetPath);
var msbuildLinkMetadataValue = ConvertTargetPathToMsbuildMetadata(targetPath, targetIsFile);
return addItemTransform.WithMetadata("Link", msbuildLinkMetadataValue);
return _mappingsToTransfrom(addItemTransform, targetPath);
}
private bool PatternIsDirectory(string pattern, string projectDirectory)
@ -192,8 +205,10 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Transforms
return Directory.Exists(path);
}
private string ConvertTargetPathToMsbuildMetadata(string targetPath, bool targetIsFile)
private string ConvertTargetPathToMsbuildMetadata(string targetPath)
{
var targetIsFile = MappingsTargetPathIsFile(targetPath);
if (targetIsFile)
{
return targetPath;

View file

@ -187,21 +187,53 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Tests
}
[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"": {
""files"": {
""include"": [""somefile.cs""]
""include"": [""path/to/some/file.cs"", ""path/to/some/other/file.cs""]
}
}
}");
action.ShouldThrow<Exception>()
.Where(e => e.Message.Contains("Migrating projects with Files specified in PackOptions is not supported."));
var contentItems = mockProj.Items
.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)
{
testDirectory = testDirectory ?? Temp.CreateDirectory().Path;