private string GetValue(bool finalValue, IList <ProjectConfig> configs, bool booleanValue) { MSBuild.Project buildProject = this.PerUser ? this.project.UserBuildProject : this.project.BuildProject; if (buildProject == null) { return(null); } string value; if (this.PerConfig) { if (configs == null || configs.Count == 0) { configs = new ProjectConfig[] { this.project.CurrentConfig }; } value = this.GetPerConfigValue(buildProject, finalValue, configs, booleanValue); } else { MSBuild.ProjectProperty buildProperty = buildProject.GetProperty(this.propertyName); value = this.GetBuildPropertyValue(buildProperty, finalValue); if (booleanValue && String.IsNullOrEmpty(value)) { value = Boolean.FalseString; } } return(value); }
/// <summary> /// Gets the value of the property. /// </summary> /// <param name="finalValue">Whether to evaluate variables in the value.</param> /// <param name="configs">Optional list of configurations to retrieve the property from; /// defaults to the project current configuration</param> /// <returns>Value (unified across configs) of the property, or null if the property is unset or /// inconsistent across configurations.</returns> public string GetValue(bool finalValue, IList <ProjectConfig> configs) { MSBuild.Project buildProject = this.PerUser ? this.project.UserBuildProject : this.project.BuildProject; if (buildProject == null) { return(null); } return(this.GetValue(finalValue, configs, false)); }
/// <summary> /// Gets the value of a boolean property. /// </summary> /// <param name="configs">Optional list of configurations to retrieve the property from; /// defaults to the project current configuration</param> /// <returns>Value (unified across configs) of the property, null if the property is /// inconsistent across configurations, or false if the property is unset.</returns> public bool?GetBooleanValue(IList <ProjectConfig> configs) { MSBuild.Project buildProject = this.PerUser ? this.project.UserBuildProject : this.project.BuildProject; if (buildProject == null) { return(null); } string value = this.GetValue(false, configs, true); if (String.Equals(value, Boolean.TrueString, StringComparison.OrdinalIgnoreCase)) { return(true); } else if (String.Equals(value, Boolean.FalseString, StringComparison.OrdinalIgnoreCase)) { return(false); } else { return(null); } }
private string GetPerConfigValue(MSBuild.Project buildProject, bool finalValue, IList <ProjectConfig> configs, bool nullIsFalse) { string unifiedValue = null; for (int i = 0; i < configs.Count; i++) { ProjectConfig config = configs[i]; bool resetCache = (i == 0); // we should be using the buildProject parameter here, but this isn't implemented in MPF MSBuild.ProjectProperty buildProperty = config.GetMsBuildProperty(this.propertyName, resetCache); string value = this.GetBuildPropertyValue(buildProperty, finalValue); if (value != null) { value = value.Trim(); } if (nullIsFalse && String.IsNullOrEmpty(value)) { value = Boolean.FalseString; } if (i == 0) { unifiedValue = value; } else if (unifiedValue != value) { unifiedValue = null; // indicates indeterminate value break; } } return(unifiedValue); }
/// <summary> /// Sets the value of the property. /// </summary> /// <param name="value">Property value to set.</param> /// <param name="configs">Optional list of configurations to set the property in; /// defaults to the project current configuration</param> /// <remarks> /// Before calling this method, the caller must ensure that the value is valid according to /// the <see cref="PropertyValidator"/> class, and that the project file is writable. /// In most cases the caller should also ensure that the new value is different from the /// existing value, to avoid dirtying the project file unnecessarily. /// </remarks> public void SetValue(string value, IList <XProjectConfig> configs) { XHelperMethods.VerifyNonNullArgument(value, "value"); var oldvalue = this.GetValue(false); value = value.Trim(); MSBuild.Project buildProject = this.project.BuildProject; if (this.PerUser) { if (this.project.UserBuildProject == null) { this.project.CreateUserBuildProject(); } buildProject = this.project.UserBuildProject; } value = this.Escape(value); if (this.PerConfig) { if (configs == null || configs.Count == 0) { configs = new XProjectConfig[] { (XProjectConfig)this.project.CurrentConfig }; } foreach (XProjectConfig config in configs) { bool set = false; // First see if there's an existing property group that matches our condition foreach (ProjectPropertyGroupElement propGroup in buildProject.Xml.PropertyGroups) { // if there is, set it within that group if (string.Equals(propGroup.Condition.Trim(), config.Condition.Trim(), StringComparison.Ordinal)) { // a property should only occur once in a group // when there is more than one property with the same name then delete all but the first // we filter on condition, because there could be 2 // same named properties with different conditions var children = propGroup.Children.Where((prop) => prop.ElementName == this.propertyName && string.IsNullOrEmpty(prop.Condition)); if (children.Count() > 1) { var first = children.First(); foreach (var child in children) { if (child != first) { propGroup.RemoveChild(child); } } } propGroup.SetProperty(this.propertyName, value); set = true; break; } } // If not, add a new property group for the condition and set the property within it if (!set) { ProjectPropertyGroupElement newPropGroup = buildProject.Xml.AddPropertyGroup(); newPropGroup.Condition = config.Condition; newPropGroup.SetProperty(this.propertyName, value); set = true; } buildProject.ReevaluateIfNecessary(); } } else { if (this.EndOfProjectFile) { List <ProjectPropertyGroupElement> propertyGroupsToDelete = new List <ProjectPropertyGroupElement>(); var groups = new List <ProjectPropertyGroupElement>(); // First see if there's an existing property group with our property foreach (ProjectPropertyGroupElement propGroup in buildProject.Xml.PropertyGroups) { List <ProjectPropertyElement> propertiesToDelete = new List <ProjectPropertyElement>(); if (!string.IsNullOrEmpty(propGroup.Condition)) { continue; } groups.Add(propGroup); foreach (ProjectPropertyElement property in propGroup.Properties) { // if there is, remove it so the new value is at the end of the file if (string.IsNullOrEmpty(property.Condition) && string.Equals(property.Name, this.propertyName, StringComparison.OrdinalIgnoreCase)) { propertiesToDelete.Add(property); } } foreach (ProjectPropertyElement property in propertiesToDelete) { propGroup.RemoveChild(property); } if (propGroup.Count == 0) { propertyGroupsToDelete.Add(propGroup); groups.Remove(propGroup); } } foreach (ProjectPropertyGroupElement propGroup in propertyGroupsToDelete) { buildProject.Xml.RemoveChild(propGroup); } ProjectPropertyGroupElement newPropGroup; if (groups.Count > 1) { newPropGroup = groups[groups.Count - 1]; } else { newPropGroup = buildProject.Xml.CreatePropertyGroupElement(); buildProject.Xml.AppendChild(newPropGroup); } newPropGroup.SetProperty(this.propertyName, value); } else { buildProject.SetProperty(this.propertyName, value); } } this.project.SetProjectFileDirty(true); this.project.RaiseProjectPropertyChanged(this.propertyName, oldvalue, value); }