/// <summary> /// Reads a string value from the specified registry key /// </summary> /// <param name="wrapper">wrapper around key</param> /// <param name="valueName">name of the value</param> /// <returns>string data in the value</returns> private static string GetValue(RegistryKeyWrapper wrapper, string valueName) { if (wrapper.Exists()) { object result = wrapper.GetValue(valueName); // RegistryKey.GetValue returns null if the value is not present // and String.Empty if the value is present and no data is defined. // We preserve this distinction, because a string property in the registry with // no value really has an empty string for a value (which is a valid property value) // rather than null for a value (which is an invalid property value) if (result != null) { // Must be a value of string type if (!(result is string)) { InvalidToolsetDefinitionException.Throw("NonStringDataInRegistry", wrapper.Name + "@" + valueName); } return(result.ToString()); } } return(null); }
/// <summary> /// Provides an enumerator over the set of sub-toolset names available to a particular /// toolsversion /// </summary> /// <param name="toolsVersion">The tools version.</param> /// <returns>An enumeration of the sub-toolsets that belong to that toolsversion.</returns> protected override IEnumerable <string> GetSubToolsetVersions(string toolsVersion) { RegistryKeyWrapper toolsVersionWrapper = null; try { try { toolsVersionWrapper = _msbuildRegistryWrapper.OpenSubKey("ToolsVersions\\" + toolsVersion); } catch (RegistryException ex) { InvalidToolsetDefinitionException.Throw(ex, "RegistryReadError", ex.Source, ex.Message); } return(toolsVersionWrapper.GetSubKeyNames()); } finally { if (toolsVersionWrapper != null) { toolsVersionWrapper.Dispose(); } } }
/// <summary> /// Provides an enumerator over property definitions for a specified tools version /// </summary> /// <param name="toolsVersion">The tools version</param> /// <returns>An enumeration of property definitions</returns> protected override IEnumerable <ToolsetPropertyDefinition> GetPropertyDefinitions(string toolsVersion) { RegistryKeyWrapper toolsVersionWrapper = null; try { try { toolsVersionWrapper = _msbuildRegistryWrapper.OpenSubKey("ToolsVersions\\" + toolsVersion); } catch (RegistryException ex) { InvalidToolsetDefinitionException.Throw(ex, "RegistryReadError", ex.Source, ex.Message); } foreach (string propertyName in toolsVersionWrapper.GetValueNames()) { yield return(CreatePropertyFromRegistry(toolsVersionWrapper, propertyName)); } } finally { if (toolsVersionWrapper != null) { toolsVersionWrapper.Dispose(); } } }
/// <summary> /// Processes a particular ToolsetPropertyDefinition into the correct value and location in the initial and/or final property set. /// </summary> /// <param name="property">The ToolsetPropertyDefinition being analyzed.</param> /// <param name="properties">The final set of properties that we wish this toolset property to be added to. </param> /// <param name="globalProperties">The global properties, used for expansion and to make sure none are overridden.</param> /// <param name="initialProperties">The initial properties, used for expansion and added to if "accumulateProperties" is true.</param> /// <param name="accumulateProperties">If "true", we add this property to the initialProperties dictionary, as well, so that properties later in the toolset can use this value.</param> /// <param name="toolsPath">If this toolset property is the "MSBuildToolsPath" property, we will return the value in this parameter.</param> /// <param name="binPath">If this toolset property is the "MSBuildBinPath" property, we will return the value in this parameter.</param> /// <param name="expander">The expander used to expand the value of the properties. Ref because if we are accumulating the properties, we need to re-create the expander to account for the new property value.</param> private void EvaluateAndSetProperty(ToolsetPropertyDefinition property, PropertyDictionary <ProjectPropertyInstance> properties, PropertyDictionary <ProjectPropertyInstance> globalProperties, PropertyDictionary <ProjectPropertyInstance> initialProperties, bool accumulateProperties, ref string toolsPath, ref string binPath, ref Expander <ProjectPropertyInstance, ProjectItemInstance> expander) { if (0 == String.Compare(property.Name, ReservedPropertyNames.toolsPath, StringComparison.OrdinalIgnoreCase)) { toolsPath = ExpandPropertyUnescaped(property, expander); toolsPath = ExpandRelativePathsRelativeToExeLocation(toolsPath); if (accumulateProperties) { SetProperty ( new ToolsetPropertyDefinition(ReservedPropertyNames.toolsPath, toolsPath, property.Source), initialProperties, globalProperties ); } } else if (0 == String.Compare(property.Name, ReservedPropertyNames.binPath, StringComparison.OrdinalIgnoreCase)) { binPath = ExpandPropertyUnescaped(property, expander); binPath = ExpandRelativePathsRelativeToExeLocation(binPath); if (accumulateProperties) { SetProperty ( new ToolsetPropertyDefinition(ReservedPropertyNames.binPath, binPath, property.Source), initialProperties, globalProperties ); } } else if (ReservedPropertyNames.IsReservedProperty(property.Name)) { // We don't allow toolsets to define reserved properties string baseMessage = ResourceUtilities.FormatResourceStringStripCodeAndKeyword("CannotModifyReservedProperty", property.Name); InvalidToolsetDefinitionException.Throw("InvalidPropertyNameInToolset", property.Name, property.Source.LocationString, baseMessage); } else { // It's an arbitrary property property.Value = ExpandPropertyUnescaped(property, expander); SetProperty(property, properties, globalProperties); if (accumulateProperties) { SetProperty(property, initialProperties, globalProperties); } } if (accumulateProperties) { expander = new Expander <ProjectPropertyInstance, ProjectItemInstance>(initialProperties, FileSystems.Default); } }
/// <summary> /// Expands the given unexpanded property expression using the properties in the /// given expander. /// </summary> private string ExpandPropertyUnescaped(ToolsetPropertyDefinition property, Expander <ProjectPropertyInstance, ProjectItemInstance> expander) { try { return(expander.ExpandIntoStringAndUnescape(property.Value, ExpanderOptions.ExpandProperties, property.Source)); } catch (InvalidProjectFileException ex) { InvalidToolsetDefinitionException.Throw(ex, "ErrorEvaluatingToolsetPropertyExpression", property.Value, property.Source.LocationString, ex.BaseMessage); } return(string.Empty); }
/// <summary> /// Sets the given property in the given property group. /// </summary> private void SetProperty(ToolsetPropertyDefinition property, PropertyDictionary <ProjectPropertyInstance> propertyGroup, PropertyDictionary <ProjectPropertyInstance> globalProperties) { try { // Global properties cannot be overwritten if (globalProperties[property.Name] == null) { propertyGroup.Set(ProjectPropertyInstance.Create(property.Name, EscapingUtilities.UnescapeAll(property.Value), true /* may be reserved */, false /* not immutable */)); } } catch (ArgumentException ex) { InvalidToolsetDefinitionException.Throw(ex, "InvalidPropertyNameInToolset", property.Name, property.Source.LocationString, ex.Message); } }
/// <summary> /// Provides an enumerator over property definitions for a specified tools version /// </summary> protected override IEnumerable <ToolsetPropertyDefinition> GetPropertyDefinitions(string toolsVersion) { ToolsetElement toolsetElement = ConfigurationSection.Toolsets.GetElement(toolsVersion); if (toolsetElement == null) { yield break; } foreach (ToolsetElement.PropertyElement propertyElement in toolsetElement.PropertyElements) { ElementLocation location = ElementLocation.Create(propertyElement.ElementInformation.Source, propertyElement.ElementInformation.LineNumber, 0); if (propertyElement.Name != null && propertyElement.Name.Length == 0) { InvalidToolsetDefinitionException.Throw("InvalidToolsetValueInConfigFileValue", location.LocationString); } yield return(new ToolsetPropertyDefinition(propertyElement.Name, propertyElement.Value, location)); } }
/// <summary> /// Given a registry location containing a property name and value, create the ToolsetPropertyDefinition that maps to it /// </summary> /// <param name="toolsetWrapper">Wrapper for the key that we're getting values from</param> /// <param name="propertyName">The name of the property whose value we wish to generate a ToolsetPropertyDefinition for.</param> /// <returns>A ToolsetPropertyDefinition instance corresponding to the property name requested.</returns> private static ToolsetPropertyDefinition CreatePropertyFromRegistry(RegistryKeyWrapper toolsetWrapper, string propertyName) { string propertyValue = null; if (propertyName != null && propertyName.Length == 0) { InvalidToolsetDefinitionException.Throw("PropertyNameInRegistryHasZeroLength", toolsetWrapper.Name); } try { propertyValue = GetValue(toolsetWrapper, propertyName); } catch (RegistryException ex) { InvalidToolsetDefinitionException.Throw(ex, "RegistryReadError", ex.Source, ex.Message); } // For the purposes of error location, use the registry path instead of a file name IElementLocation location = new RegistryLocation(toolsetWrapper.Name + "@" + propertyName); return(new ToolsetPropertyDefinition(propertyName, propertyValue, location)); }
/// <summary> /// Reads the settings for a specified tools version /// </summary> private Toolset ReadToolset ( ToolsetPropertyDefinition toolsVersion, PropertyDictionary <ProjectPropertyInstance> globalProperties, PropertyDictionary <ProjectPropertyInstance> initialProperties, bool accumulateProperties ) { // Initial properties is the set of properties we're going to use to expand property expressions like $(foo) // in the values we read out of the registry or config file. We'll add to it as we pick up properties (including binpath) // from the registry or config file, so that properties there can be referenced in values below them. // After processing all the properties, we don't need initialProperties anymore. string toolsPath = null; string binPath = null; PropertyDictionary <ProjectPropertyInstance> properties = new PropertyDictionary <ProjectPropertyInstance>(); IEnumerable <ToolsetPropertyDefinition> rawProperties = GetPropertyDefinitions(toolsVersion.Name); Expander <ProjectPropertyInstance, ProjectItemInstance> expander = new Expander <ProjectPropertyInstance, ProjectItemInstance>(initialProperties, FileSystems.Default); foreach (ToolsetPropertyDefinition property in rawProperties) { EvaluateAndSetProperty(property, properties, globalProperties, initialProperties, accumulateProperties, ref toolsPath, ref binPath, ref expander); } Dictionary <string, SubToolset> subToolsets = new Dictionary <string, SubToolset>(StringComparer.OrdinalIgnoreCase); IEnumerable <string> subToolsetVersions = GetSubToolsetVersions(toolsVersion.Name); foreach (string subToolsetVersion in subToolsetVersions) { string subToolsetToolsPath = null; string subToolsetBinPath = null; IEnumerable <ToolsetPropertyDefinition> rawSubToolsetProperties = GetSubToolsetPropertyDefinitions(toolsVersion.Name, subToolsetVersion); PropertyDictionary <ProjectPropertyInstance> subToolsetProperties = new PropertyDictionary <ProjectPropertyInstance>(); // If we have a sub-toolset, any values defined here will override the toolset properties. foreach (ToolsetPropertyDefinition property in rawSubToolsetProperties) { EvaluateAndSetProperty(property, subToolsetProperties, globalProperties, initialProperties, false /* do not ever accumulate sub-toolset properties */, ref subToolsetToolsPath, ref subToolsetBinPath, ref expander); } if (subToolsetToolsPath != null || subToolsetBinPath != null) { InvalidToolsetDefinitionException.Throw("MSBuildToolsPathNotSupportedInSubToolsets", toolsVersion.Name, toolsVersion.Source.LocationString, subToolsetVersion); } subToolsets[subToolsetVersion] = new SubToolset(subToolsetVersion, subToolsetProperties); } // All tools versions must specify a value for MSBuildToolsPath (or MSBuildBinPath) if (String.IsNullOrEmpty(toolsPath) && String.IsNullOrEmpty(binPath)) { return(null); } // If both MSBuildBinPath and MSBuildToolsPath are present, they must be the same if (toolsPath != null && binPath != null && !toolsPath.Equals(binPath, StringComparison.OrdinalIgnoreCase)) { InvalidToolsetDefinitionException.Throw("ConflictingValuesOfMSBuildToolsPath", toolsVersion.Name, toolsVersion.Source.LocationString); } AppendStandardProperties(properties, globalProperties, toolsVersion.Name, null, toolsPath); Toolset toolset = null; try { var importSearchPathsTable = GetProjectImportSearchPathsTable(toolsVersion.Name, NativeMethodsShared.GetOSNameForExtensionsPath()); toolset = new Toolset(toolsVersion.Name, toolsPath == null ? binPath : toolsPath, properties, _environmentProperties, globalProperties, subToolsets, MSBuildOverrideTasksPath, DefaultOverrideToolsVersion, importSearchPathsTable); } catch (ArgumentException e) { InvalidToolsetDefinitionException.Throw("ErrorCreatingToolset", toolsVersion.Name, e.Message); } return(toolset); }