/// <summary> /// Group all items by the primary key, then select the nearest TxM /// within each group. /// Items that do not contain the primaryKey will be filtered out. /// </summary> private static List <ContentItemGroup> GetContentGroupsForFramework( NuGetFramework framework, List <ContentItemGroup> contentGroups, string primaryKey, MaccatalystFallback maccatalystFallback) { var groups = new List <ContentItemGroup>(); // Group by primary key and find the nearest TxM under each. var primaryGroups = new Dictionary <string, List <ContentItemGroup> >(StringComparer.Ordinal); foreach (var group in contentGroups) { object keyObj; if (group.Properties.TryGetValue(primaryKey, out keyObj)) { var key = (string)keyObj; List <ContentItemGroup> index; if (!primaryGroups.TryGetValue(key, out index)) { index = new List <ContentItemGroup>(1); primaryGroups.Add(key, index); } index.Add(group); } } // Find the nearest TxM within each primary key group. foreach (var primaryGroup in primaryGroups) { var groupedItems = primaryGroup.Value; var nearestGroup = NuGetFrameworkUtility.GetNearest <ContentItemGroup>(groupedItems, framework, group => { // In the case of /native there is no TxM, here any should be used. object frameworkObj; if (group.Properties.TryGetValue( ManagedCodeConventions.PropertyNames.TargetFrameworkMoniker, out frameworkObj)) { return((NuGetFramework)frameworkObj); } return(NuGetFramework.AnyFramework); }); // If a compatible group exists within the secondary key add it to the results if (nearestGroup != null) { MaccatalystFallback.CheckFallback(maccatalystFallback, nearestGroup.Properties); groups.Add(nearestGroup); } } return(groups); }
/// <summary> /// Get all content groups that have the nearest TxM /// </summary> internal static List <ContentItemGroup> GetContentGroupsForFramework( LockFileTargetLibrary lockFileLib, NuGetFramework framework, IEnumerable <ContentItemGroup> contentGroups, MaccatalystFallback maccatalystFallback) { var groups = new List <ContentItemGroup>(); // Group by content by code language and find the nearest TxM under each language. var groupsByLanguage = new Dictionary <string, List <ContentItemGroup> >(StringComparer.OrdinalIgnoreCase); foreach (var group in contentGroups) { var codeLanguage = (string)group.Properties[ManagedCodeConventions.PropertyNames.CodeLanguage]; List <ContentItemGroup> index; if (!groupsByLanguage.TryGetValue(codeLanguage, out index)) { index = new List <ContentItemGroup>(1); groupsByLanguage.Add(codeLanguage, index); } index.Add(group); } // Find the nearest TxM within each language foreach (var codeLanguagePair in groupsByLanguage) { var languageGroups = codeLanguagePair.Value; var nearestGroup = NuGetFrameworkUtility.GetNearest <ContentItemGroup>(languageGroups, framework, group => (NuGetFramework)group.Properties[ManagedCodeConventions.PropertyNames.TargetFrameworkMoniker]); // If a compatible group exists within the code language add it to the results if (nearestGroup != null) { MaccatalystFallback.CheckFallback(maccatalystFallback, nearestGroup.Properties); groups.Add(nearestGroup); } } return(groups); }
/// <summary> /// Create lock file items for the best matching group. /// </summary> /// <remarks>Enumerate this once after calling.</remarks> private static IEnumerable <LockFileItem> GetLockFileItems( IReadOnlyList <SelectionCriteria> criteria, ContentItemCollection items, Action <LockFileItem> additionalAction, MaccatalystFallback maccatalystFallback, params PatternSet[] patterns) { // Loop through each criteria taking the first one that matches one or more items. foreach (var managedCriteria in criteria) { var group = items.FindBestItemGroup( managedCriteria, patterns); if (group != null) { MaccatalystFallback.CheckFallback(maccatalystFallback, group.Properties); foreach (var item in group.Items) { var newItem = new LockFileItem(item.Path); object locale; if (item.Properties.TryGetValue("locale", out locale)) { newItem.Properties["locale"] = (string)locale; } additionalAction?.Invoke(newItem); yield return(newItem); } // Take only the first group that has items break; } } yield break; }