// // Set Ice Home and force projects to re evaluate changes in the imported project // public static void SetIceHome(List <IVsProject> projects, string iceHome, string iceVersion, string iceIntVersion, string iceVersionMM) { foreach (IVsProject p in projects) { if (DTEUtil.IsIceBuilderEnabled(p) != IceBuilderProjectType.None) { Microsoft.Build.Evaluation.Project project = LoadedProject(ProjectUtil.GetProjectFullPath(p), DTEUtil.IsCppProject(p), true); ResolvedImport import = project.Imports.FirstOrDefault(i => i.ImportedProject.FullPath.EndsWith("IceBuilder.Common.props")); if (import.ImportedProject != null) { ProjectPropertyGroupElement group = import.ImportedProject.PropertyGroups.FirstOrDefault( g => g.Label.Equals("IceHome", StringComparison.CurrentCultureIgnoreCase)); if (group != null) { group.SetProperty(Package.IceHomeValue, iceHome); group.SetProperty(Package.IceVersionValue, iceVersion); group.SetProperty(Package.IceIntVersionValue, iceIntVersion); group.SetProperty(Package.IceVersionMMValue, iceVersionMM); project.ReevaluateIfNecessary(); } } } } }
public void Generate(string outputPath, string propertyVersionNamePrefix, string propertyPathNamePrefix, PackageRestoreData restoreData) { // Delete an existing file in case there are no properties generated and we don't end up saving the file // if (File.Exists(outputPath)) { Retry(() => File.Delete(outputPath), TimeSpan.FromMilliseconds(500)); } ProjectRootElement project = ProjectRootElement.Create(); ProjectPropertyGroupElement propertyGroup = project.AddPropertyGroup(); propertyGroup.SetProperty("MSBuildAllProjects", "$(MSBuildAllProjects);$(MSBuildThisFileFullPath)"); ProjectItemGroupElement itemGroup = project.AddItemGroup(); bool anyPropertiesCreated = false; foreach (string packageConfigPath in _packageConfigPaths) { _logger.LogMessage(MessageImportance.Low, $"Parsing '{packageConfigPath}'"); IEnumerable <PackageIdentityWithPath> parsedPackages = null; INuGetPackageConfigParser configParser = null; // A bug in nuget sometimes causes "NuGet.Configuration.NuGetConfigurationException: Unexpected failure reading NuGet.Config." when multiple instances are running in parrallel such as in the quickbuild scenario. Retry(() => configParser = _configParsersLazy.Value.FirstOrDefault(i => i.TryGetPackages(packageConfigPath, restoreData, out parsedPackages)), TimeSpan.FromMilliseconds(1000)); if (configParser != null && parsedPackages != null) { anyPropertiesCreated = true; foreach (PackageIdentityWithPath packageInfo in parsedPackages) { propertyGroup.SetProperty($"{propertyPathNamePrefix}{packageInfo.Id.Replace(".", "_")}", $"{packageInfo.FullPath}"); propertyGroup.SetProperty($"{propertyVersionNamePrefix}{packageInfo.Id.Replace(".", "_")}", $"{packageInfo.Version.ToString()}"); // Consider adding item metadata of packageid and version for ease of consumption of this property. itemGroup.AddItem("CBTNuGetPackageDir", packageInfo.FullPath); } } } // Don't save the file if no properties were created. In Visual Studio design time builds, this can be called multiple times until there are finally // properties that can be created. If we generate an empty file, it won't get regenerated once there are properties to create. // if (anyPropertiesCreated) { Retry(() => project.Save(outputPath), TimeSpan.FromMilliseconds(500)); } }
internal void CreatePropsFile(IDictionary <string, string> propertyPairs, string propsFile) { ProjectRootElement project = ProjectRootElement.Create(); ProjectPropertyGroupElement propertyGroup = project.AddPropertyGroup(); propertyGroup.SetProperty("MSBuildAllProjects", "$(MSBuildAllProjects);$(MSBuildThisFileFullPath)"); foreach (var kvp in propertyPairs) { propertyGroup.SetProperty(kvp.Key, kvp.Value); } project.Save(propsFile); }
// // Set Ice Home and force projects to re evaluate changes in the imported project // public static void SetIceHome(List <EnvDTE.Project> projects, String iceHome, String iceVersion, String iceIntVersion, String iceVersionMM) { foreach (EnvDTE.Project p in projects) { Microsoft.Build.Evaluation.Project project = MSBuildUtils.LoadedProject(p.FullName); ResolvedImport import = project.Imports.FirstOrDefault(i => i.ImportedProject.FullPath.EndsWith("IceBuilder.Common.props")); if (import.ImportedProject != null) { ProjectPropertyGroupElement group = import.ImportedProject.PropertyGroups.FirstOrDefault(g => g.Label.Equals("IceHome")); if (group != null) { group.SetProperty(Package.IceHomeValue, iceHome); group.SetProperty(Package.IceVersionValue, iceVersion); group.SetProperty(Package.IceIntVersionValue, iceIntVersion); group.SetProperty(Package.IceVersionMMValue, iceVersionMM); project.ReevaluateIfNecessary(); } } } }
/// <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 <ProjectConfig> configs) { WixHelperMethods.VerifyNonNullArgument(value, "value"); 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 ProjectConfig[] { this.project.CurrentConfig }; } foreach (ProjectConfig 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, config.Condition, StringComparison.Ordinal)) { 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>(); // 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; } 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); } } foreach (ProjectPropertyGroupElement propGroup in propertyGroupsToDelete) { buildProject.Xml.RemoveChild(propGroup); } ProjectPropertyGroupElement newPropGroup = buildProject.Xml.CreatePropertyGroupElement(); buildProject.Xml.AppendChild(newPropGroup); newPropGroup.SetProperty(this.propertyName, value); } else { buildProject.SetProperty(this.propertyName, value); } } this.project.InvalidatePropertyCache(); this.project.SetProjectFileDirty(true); }
/// <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); }