public IEnumerable <ContentItemGroup> FindItemGroups(PatternSet definition)
        {
            if (_assets.Count > 0)
            {
                var groupPatterns = definition.GroupExpressions;

                List <Tuple <ContentItem, Asset> > groupAssets = null;
                foreach (var asset in _assets)
                {
                    foreach (var groupPattern in groupPatterns)
                    {
                        var item = groupPattern.Match(asset.Path, definition.PropertyDefinitions);
                        if (item != null)
                        {
                            if (groupAssets == null)
                            {
                                groupAssets = new List <Tuple <ContentItem, Asset> >(1);
                            }

                            groupAssets.Add(Tuple.Create(item, asset));
                        }
                    }
                }

                if (groupAssets?.Count > 0)
                {
                    foreach (var grouping in groupAssets.GroupBy(key => key.Item1, GroupComparer.DefaultComparer))
                    {
                        var group = new ContentItemGroup();

                        foreach (var property in grouping.Key.Properties)
                        {
                            group.Properties.Add(property.Key, property.Value);
                        }

                        foreach (var item in FindItemsImplementation(definition, grouping.Select(match => match.Item2)))
                        {
                            group.Items.Add(item);
                        }

                        yield return(group);
                    }
                }
            }
        }
        /// <summary>
        /// Populate the provided list with ContentItemGroups based on a provided pattern set.
        /// </summary>
        /// <param name="definition">The pattern set to match</param>
        /// <param name="contentItemGroupList">The list that will be mutated and populated with the item groups</param>
        public void PopulateItemGroups(PatternSet definition, IList <ContentItemGroup> contentItemGroupList)
        {
            if (_assets.Count > 0)
            {
                var groupPatterns = definition.GroupExpressions;

                List <Tuple <ContentItem, Asset> > groupAssets = null;
                foreach (var asset in _assets)
                {
                    foreach (var groupPattern in groupPatterns)
                    {
                        var item = groupPattern.Match(asset.Path, definition.PropertyDefinitions);
                        if (item != null)
                        {
                            if (groupAssets == null)
                            {
                                groupAssets = new List <Tuple <ContentItem, Asset> >(1);
                            }

                            groupAssets.Add(Tuple.Create(item, asset));
                        }
                    }
                }

                IList <ContentItem> groupItems = new List <ContentItem>();
                if (groupAssets?.Count > 0)
                {
                    foreach (var grouping in groupAssets.GroupBy(key => key.Item1, GroupComparer.DefaultComparer))
                    {
                        var group = new ContentItemGroup();

                        foreach (var property in grouping.Key.Properties)
                        {
                            group.Properties.Add(property.Key, property.Value);
                        }

                        groupItems = FindItemsImplementation(definition, grouping.Select(match => match.Item2));
                        group.Items.AddRange(groupItems);
                        contentItemGroupList.Add(group);
                    }
                }
            }
        }
예제 #3
0
        public IEnumerable <ContentItemGroup> FindItemGroups(ContentPatternDefinition definition)
        {
            var groupPatterns = definition.GroupPatterns
                                .Select(pattern => Tuple.Create(pattern, new Infrastructure.PatternExpression(pattern.Pattern)))
                                .ToList();

            var groupAssets = new List <Tuple <ContentItem, Asset> >();

            foreach (var asset in _assets)
            {
                foreach (var groupParser in groupPatterns)
                {
                    ContentItem item = groupParser.Item2.Match(asset.Path, definition.PropertyDefinitions);
                    if (item != null)
                    {
                        foreach (var pair in groupParser.Item1.Defaults)
                        {
                            item.Properties[pair.Key] = pair.Value;
                        }
                        groupAssets.Add(Tuple.Create(item, asset));
                    }
                }
            }

            foreach (var grouping in groupAssets.GroupBy(key => key.Item1, new GroupComparer()))
            {
                var group = new ContentItemGroup();

                foreach (var property in grouping.Key.Properties)
                {
                    group.Properties.Add(property.Key, property.Value);
                }

                foreach (var item in FindItemsImplementation(definition, grouping.Select(match => match.Item2)))
                {
                    group.Items.Add(item);
                }

                yield return(group);
            }
        }
예제 #4
0
        public ContentItemGroup FindBestItemGroup(SelectionCriteria criteria, params ContentPatternDefinition[] definitions)
        {
            foreach (var definition in definitions)
            {
                var itemGroups = FindItemGroups(definition).ToList();
                foreach (var criteriaEntry in criteria.Entries)
                {
                    ContentItemGroup bestGroup = null;
                    var bestAmbiguity          = false;

                    foreach (var itemGroup in itemGroups)
                    {
                        var groupIsValid = true;
                        foreach (var criteriaProperty in criteriaEntry.Properties)
                        {
                            if (criteriaProperty.Value == null)
                            {
                                if (itemGroup.Properties.ContainsKey(criteriaProperty.Key))
                                {
                                    groupIsValid = false;
                                    break;
                                }
                            }
                            else
                            {
                                object itemProperty;
                                if (!itemGroup.Properties.TryGetValue(criteriaProperty.Key, out itemProperty))
                                {
                                    groupIsValid = false;
                                    break;
                                }
                                ContentPropertyDefinition propertyDefinition;
                                if (!definition.PropertyDefinitions.TryGetValue(criteriaProperty.Key, out propertyDefinition))
                                {
                                    groupIsValid = false;
                                    break;
                                }
                                if (!propertyDefinition.IsCriteriaSatisfied(criteriaProperty.Value, itemProperty))
                                {
                                    groupIsValid = false;
                                    break;
                                }
                            }
                        }
                        if (groupIsValid)
                        {
                            if (bestGroup == null)
                            {
                                bestGroup = itemGroup;
                            }
                            else
                            {
                                var groupComparison = 0;
                                foreach (var criteriaProperty in criteriaEntry.Properties)
                                {
                                    if (criteriaProperty.Value == null)
                                    {
                                        continue;
                                    }
                                    else
                                    {
                                        var bestGroupValue     = bestGroup.Properties[criteriaProperty.Key];
                                        var itemGroupValue     = itemGroup.Properties[criteriaProperty.Key];
                                        var propertyDefinition = definition.PropertyDefinitions[criteriaProperty.Key];
                                        groupComparison = propertyDefinition.Compare(criteriaProperty.Value, bestGroupValue, itemGroupValue);
                                        if (groupComparison != 0)
                                        {
                                            break;
                                        }
                                    }
                                }
                                if (groupComparison > 0)
                                {
                                    bestGroup     = itemGroup;
                                    bestAmbiguity = false;
                                }
                                else if (groupComparison == 0)
                                {
                                    bestAmbiguity = true;
                                }
                            }
                        }
                    }
                    if (bestGroup != null)
                    {
                        if (bestAmbiguity)
                        {
                            // TODO: Something
                        }
                        return(bestGroup);
                    }
                }
            }
            return(null);
        }
예제 #5
0
        private IEnumerable<string> GetRuntimeAssetFoldersForPromotion(ContentItemGroup runtimeAssets, NuGetFramework targetFramework, string targetFrameworkName)
        {
            if (runtimeAssets == null || runtimeAssets.Items.Count == 0)
            {
                return Enumerable.Empty<string>();
            }

            if (runtimeAssets.Items.All(ci => NuGetAssetResolver.IsPlaceholder(ci.Path)))
            {
                return Enumerable.Empty<string>();
            }

            if (targetFrameworkName == null)
            {
                targetFrameworkName = targetFramework.GetShortFolderName();
            }

            var resolvedFramework = runtimeAssets.Properties["tfm"] as NuGetFramework;
            if (targetFramework.Equals(resolvedFramework))
            {
                Log.LogMessage(LogImportance.Low, $"Not promoting explicit implementation for {targetFrameworkName}");
                return Enumerable.Empty<string>();
            }

            return NuGetAssetResolver.GetPackageTargetDirectories(runtimeAssets);
        }
예제 #6
0
        private IEnumerable<string> GetObscuredAssetFolders(ContentItemGroup assets, ContentItemGroup obscuredAssets, NuGetFramework targetFramework, string targetFrameworkName, string expectedAssetFolder, string ignoredAssetFolder = null)
        {
            if (assets == null || assets.Items.Count == 0)
            {
                return Enumerable.Empty<string>();
            }

            if (assets.Items.Any(ci => !NuGetAssetResolver.IsPlaceholder(ci.Path)))
            {
                return Enumerable.Empty<string>();
            }

            if (targetFrameworkName == null)
            {
                targetFrameworkName = targetFramework.GetShortFolderName();
            }

            var resolvedFramework = assets.Properties["tfm"] as NuGetFramework;
            if (targetFramework.Equals(resolvedFramework))
            {
                Log.LogMessage(LogImportance.Low, $"Not overriding explicit placeholder for {targetFrameworkName}");
                return Enumerable.Empty<string>();
            }

            var obscuredAssetPaths = NuGetAssetResolver.GetPackageTargetDirectories(obscuredAssets);

            if (ignoredAssetFolder != null)
            {
                string ignoredFolder = ignoredAssetFolder + '/';
                obscuredAssetPaths = obscuredAssetPaths.Where(i => -1 == i.IndexOf(ignoredFolder, StringComparison.OrdinalIgnoreCase));
            }

            if (expectedAssetFolder != null)
            {
                var unexpectedAssetPaths = obscuredAssetPaths.Where(ri => !ri.StartsWith(expectedAssetFolder, StringComparison.OrdinalIgnoreCase));
                foreach (var unexpectedAssetPath in unexpectedAssetPaths)
                {
                    Log.LogWarning($"Unexpected targetPath {unexpectedAssetPath}.  Expected only {expectedAssetFolder}.");
                }

                // filter after we've warned
                obscuredAssetPaths = obscuredAssetPaths.Except(unexpectedAssetPaths);
            }

            if (!obscuredAssetPaths.Any())
            {
                // it's acceptable to have no override, this is the case for packages which 
                // carry implementation in a runtime-specific package
                Log.LogMessage(LogImportance.Low, $"No {expectedAssetFolder} assets could be found to override inbox placeholder for {targetFrameworkName}.");
            }

            return obscuredAssetPaths;
        }
예제 #7
0
        public static IEnumerable<string> GetPackageTargetDirectories(ContentItemGroup contentGroup)
        {
            if (contentGroup == null)
            {
                yield break;
            }

            foreach (var contentItem in contentGroup.Items)
            {
                // package paths are standardized to '/'
                int dirLength = contentItem.Path.LastIndexOf(Path.AltDirectorySeparatorChar);

                if (dirLength == -1)
                {
                    yield return "";
                }
                else
                {
                    yield return contentItem.Path.Substring(0, dirLength);
                }
            }
        }