/// <summary> /// Gets the effective global properties for an item that will get passed to <see cref="MSBuild.Projects"/>. /// </summary> /// <remarks> /// The behavior of this method matches the hardcoded behaviour of the msbuild task /// and the <paramref name="globalPropertyModifiers"/> parameter can contain other mutations done at build time in targets / tasks /// </remarks> private static PropertyDictionary <ProjectPropertyInstance> GetGlobalPropertiesForItem( ProjectItemInstance projectReference, PropertyDictionary <ProjectPropertyInstance> requesterGlobalProperties, IEnumerable <GlobalPropertiesModifier> globalPropertyModifiers = null) { ErrorUtilities.VerifyThrowInternalNull(projectReference, nameof(projectReference)); ErrorUtilities.VerifyThrowArgumentNull(requesterGlobalProperties, nameof(requesterGlobalProperties)); var properties = SplitPropertyNameValuePairs(ItemMetadataNames.PropertiesMetadataName, projectReference.GetMetadataValue(ItemMetadataNames.PropertiesMetadataName)); var additionalProperties = SplitPropertyNameValuePairs(ItemMetadataNames.AdditionalPropertiesMetadataName, projectReference.GetMetadataValue(ItemMetadataNames.AdditionalPropertiesMetadataName)); var undefineProperties = SplitPropertyNames(projectReference.GetMetadataValue(ItemMetadataNames.UndefinePropertiesMetadataName)); var defaultParts = new GlobalPropertyPartsForMSBuildTask(properties.ToImmutableDictionary(), additionalProperties.ToImmutableDictionary(), undefineProperties.ToImmutableList()); var globalPropertyParts = globalPropertyModifiers?.Aggregate(defaultParts, (currentProperties, modifier) => modifier(currentProperties, projectReference)) ?? defaultParts; if (globalPropertyParts.AllEmpty()) { return(requesterGlobalProperties); } // Make a copy to avoid mutating the requester var globalProperties = new PropertyDictionary <ProjectPropertyInstance>(requesterGlobalProperties); // Append and remove properties as specified by the various metadata MergeIntoPropertyDictionary(globalProperties, globalPropertyParts.Properties); MergeIntoPropertyDictionary(globalProperties, globalPropertyParts.AdditionalProperties); RemoveFromPropertyDictionary(globalProperties, globalPropertyParts.UndefineProperties); return(globalProperties); }
/// <summary> /// Gets the effective global properties for a project reference item. /// </summary> /// <remarks> /// The behavior of this method should match the logic in the SDK /// </remarks> private static GlobalPropertyPartsForMSBuildTask ProjectReferenceGlobalPropertiesModifier( GlobalPropertyPartsForMSBuildTask defaultParts, ProjectItemInstance projectReference ) { // ProjectReference defines yet another metadata name containing properties to undefine. Merge it in if non empty. var globalPropertiesToRemove = SplitPropertyNames(projectReference.GetMetadataValue(GlobalPropertiesToRemoveMetadataName)); var newUndefineProperties = defaultParts.UndefineProperties; newUndefineProperties = newUndefineProperties.AddRange(defaultParts.UndefineProperties); newUndefineProperties = newUndefineProperties.AddRange(globalPropertiesToRemove); newUndefineProperties.Add("InnerBuildProperty"); var newProperties = defaultParts.Properties; // The properties on the project reference supersede the ones from the MSBuild task instead of appending. if (newProperties.Count == 0) { // TODO: Mimic AssignProjectConfiguration's behavior for determining the values for these. var setConfigurationString = projectReference.GetMetadataValue(SetConfigurationMetadataName); var setPlatformString = projectReference.GetMetadataValue(SetPlatformMetadataName); var setTargetFrameworkString = projectReference.GetMetadataValue(SetTargetFrameworkMetadataName); if (!String.IsNullOrEmpty(setConfigurationString) || !String.IsNullOrEmpty(setPlatformString) || !String.IsNullOrEmpty(setTargetFrameworkString)) { newProperties = SplitPropertyNameValuePairs( ItemMetadataNames.PropertiesMetadataName, $"{setConfigurationString};{setPlatformString};{setTargetFrameworkString}").ToImmutableDictionary(); } } return(new GlobalPropertyPartsForMSBuildTask(newProperties, defaultParts.AdditionalProperties, newUndefineProperties)); }