dotnet-installer/src/Microsoft.DotNet.ProjectJsonMigration/transforms/IncludeContextTransform.cs

186 lines
8.3 KiB
C#
Raw Normal View History

2016-09-22 14:35:31 -07:00
// 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.Internal.ProjectModel.Files;
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Build.Construction;
using Microsoft.DotNet.Cli.Utils;
2016-08-23 13:50:05 -07:00
using Microsoft.DotNet.ProjectJsonMigration.Models;
2016-09-08 14:40:46 -07:00
using Microsoft.DotNet.Tools.Common;
2016-08-23 13:50:05 -07:00
namespace Microsoft.DotNet.ProjectJsonMigration.Transforms
{
internal class IncludeContextTransform : ConditionalTransform<IncludeContext, IEnumerable<ProjectItemElement>>
{
protected virtual Func<string, AddItemTransform<IncludeContext>> IncludeFilesExcludeFilesTransformGetter =>
2016-09-08 14:40:46 -07:00
(itemName) =>
new AddItemTransform<IncludeContext>(
itemName,
includeContext => FormatGlobPatternsForMsbuild(includeContext.IncludeFiles.OrEmptyIfNull()
.Where((pattern) => !_excludePatternRule(pattern)),
includeContext.SourceBasePath),
2016-09-08 14:40:46 -07:00
includeContext => FormatGlobPatternsForMsbuild(includeContext.ExcludeFiles, includeContext.SourceBasePath),
includeContext => includeContext != null
&& includeContext.IncludeFiles != null
&& includeContext.IncludeFiles.Where((pattern) => !_excludePatternRule(pattern)).Count() > 0);
2017-01-20 15:59:21 -08:00
protected virtual Func<string, AddItemTransform<IncludeContext>> IncludeExcludeTransformGetter =>
2016-09-08 14:40:46 -07:00
(itemName) => new AddItemTransform<IncludeContext>(
itemName,
includeContext =>
2016-09-08 14:40:46 -07:00
{
var fullIncludeSet = includeContext.IncludePatterns.OrEmptyIfNull()
.Union(includeContext.CustomIncludePatterns.OrEmptyIfNull());
if (_emitBuiltInIncludes)
{
fullIncludeSet = fullIncludeSet.Union(includeContext.BuiltInsInclude.OrEmptyIfNull());
}
2016-09-08 14:40:46 -07:00
fullIncludeSet = fullIncludeSet.Where((pattern) => !_excludePatternRule(pattern));
2017-01-20 15:59:21 -08:00
2016-09-08 14:40:46 -07:00
return FormatGlobPatternsForMsbuild(fullIncludeSet, includeContext.SourceBasePath);
},
includeContext =>
{
var fullExcludeSet = includeContext.ExcludePatterns.OrEmptyIfNull()
.Union(includeContext.BuiltInsExclude.OrEmptyIfNull())
.Union(includeContext.ExcludeFiles.OrEmptyIfNull());
return FormatGlobPatternsForMsbuild(fullExcludeSet, includeContext.SourceBasePath);
},
includeContext =>
2016-09-08 14:40:46 -07:00
{
return includeContext != null &&
(
(includeContext.IncludePatterns != null && includeContext.IncludePatterns.Where((pattern) => !_excludePatternRule(pattern)).Count() > 0)
2016-09-08 14:40:46 -07:00
||
(includeContext.CustomIncludePatterns != null && includeContext.CustomIncludePatterns.Where((pattern) => !_excludePatternRule(pattern)).Count() > 0)
||
(_emitBuiltInIncludes &&
includeContext.BuiltInsInclude != null &&
includeContext.BuiltInsInclude.Count > 0)
2016-09-08 14:40:46 -07:00
);
});
private Func<string, string, AddItemTransform<IncludeContext>> MappingsIncludeFilesExcludeFilesTransformGetter =>
(itemName, targetPath) => AddMappingToTransform(IncludeFilesExcludeFilesTransformGetter(itemName), targetPath);
private Func<string, string, AddItemTransform<IncludeContext>> MappingsIncludeExcludeTransformGetter =>
(itemName, targetPath) => AddMappingToTransform(IncludeExcludeTransformGetter(itemName), targetPath);
2016-10-20 11:24:58 -07:00
private Func<AddItemTransform<IncludeContext>, string, AddItemTransform<IncludeContext>> _mappingsToTransfrom;
2016-08-23 13:50:05 -07:00
private readonly string _itemName;
private bool _transformMappings;
private Func<string, bool> _excludePatternRule;
private bool _emitBuiltInIncludes;
2016-08-23 13:50:05 -07:00
private readonly List<ItemMetadataValue<IncludeContext>> _metadata = new List<ItemMetadataValue<IncludeContext>>();
public IncludeContextTransform(
string itemName,
bool transformMappings = true,
Func<IncludeContext, bool> condition = null,
2017-01-20 15:59:21 -08:00
bool emitBuiltInIncludes = true,
Func<string, bool> excludePatternsRule = null) : base(condition)
{
_itemName = itemName;
_transformMappings = transformMappings;
_emitBuiltInIncludes = emitBuiltInIncludes;
_excludePatternRule = excludePatternsRule ?? ((pattern) => false);
2016-10-20 11:24:58 -07:00
_mappingsToTransfrom = (addItemTransform, targetPath) =>
{
var msbuildLinkMetadataValue = ConvertTargetPathToMsbuildMetadata(targetPath);
return addItemTransform.WithMetadata("Link", msbuildLinkMetadataValue);
};
}
public IncludeContextTransform WithMetadata(
string metadataName,
string metadataValue,
string condition = null)
{
_metadata.Add(new ItemMetadataValue<IncludeContext>(metadataName, metadataValue, condition));
return this;
}
public IncludeContextTransform WithMetadata(
string metadataName,
Func<IncludeContext, string> metadataValueFunc,
string condition = null)
{
_metadata.Add(new ItemMetadataValue<IncludeContext>(
metadataName,
metadataValueFunc,
condition: condition));
return this;
}
2016-10-20 11:24:58 -07:00
public IncludeContextTransform WithMappingsToTransform(
Func<AddItemTransform<IncludeContext>, string, AddItemTransform<IncludeContext>> mappingsToTransfrom)
{
_mappingsToTransfrom = mappingsToTransfrom;
return this;
}
2016-09-08 14:40:46 -07:00
private IEnumerable<Tuple<AddItemTransform<IncludeContext>, IncludeContext>> CreateTransformSet(IncludeContext source)
{
2016-09-08 14:40:46 -07:00
var transformSet = new List<Tuple<AddItemTransform<IncludeContext>, IncludeContext>>
{
Tuple.Create(IncludeFilesExcludeFilesTransformGetter(_itemName), source),
Tuple.Create(IncludeExcludeTransformGetter(_itemName), source)
};
2016-09-08 14:40:46 -07:00
if (source == null)
{
2016-09-08 14:40:46 -07:00
return transformSet;
}
// Mappings must be executed before the transform set to prevent a the
// non-mapped items that will merge with mapped items from being encompassed
foreach (var mappingEntry in source.Mappings.OrEmptyIfNull())
{
var targetPath = mappingEntry.Key;
var includeContext = mappingEntry.Value;
transformSet.Insert(0,
Tuple.Create(
MappingsIncludeExcludeTransformGetter(_itemName, targetPath),
includeContext));
transformSet.Insert(0,
Tuple.Create(
MappingsIncludeFilesExcludeFilesTransformGetter(_itemName, targetPath),
includeContext));
}
2016-09-08 14:40:46 -07:00
foreach (var metadataElement in _metadata)
{
2016-09-08 14:40:46 -07:00
foreach (var transform in transformSet)
{
transform.Item1.WithMetadata(metadataElement);
}
}
return transformSet;
}
2016-09-08 14:40:46 -07:00
public override IEnumerable<ProjectItemElement> ConditionallyTransform(IncludeContext source)
{
var transformSet = CreateTransformSet(source);
return transformSet.Select(t => t.Item1.Transform(t.Item2));
}
private AddItemTransform<IncludeContext> AddMappingToTransform(
AddItemTransform<IncludeContext> addItemTransform,
string targetPath)
{
2016-10-20 11:24:58 -07:00
return _mappingsToTransfrom(addItemTransform, targetPath);
2016-09-08 14:40:46 -07:00
}
}
}