Exemplo n.º 1
0
        /// <summary>
        /// Provides an enumerator over property definitions for a specified tools version
        /// </summary>
        /// <param name="toolsVersion"></param>
        /// <returns></returns>
        protected override IEnumerable <PropertyDefinition> GetPropertyDefinitions(string toolsVersion)
        {
            ToolsetElement toolsetElement = ConfigurationSection.Toolsets.GetElement(toolsVersion);

            if (toolsetElement == null)
            {
                yield break;
            }

            foreach (ToolsetElement.PropertyElement propertyElement in toolsetElement.PropertyElements)
            {
                string location = ResourceUtilities.FormatResourceString
                                  (
                    "ConfigFileLocation",
                    propertyElement.ElementInformation.Source,
                    propertyElement.ElementInformation.LineNumber
                                  );

                if (propertyElement.Name != null && propertyElement.Name.Length == 0)
                {
                    InvalidToolsetDefinitionException.Throw("InvalidToolsetValueInConfigFileValue", location);
                }

                yield return(new PropertyDefinition(propertyElement.Name, propertyElement.Value, location));
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Provides an enumerator over property definitions for a specified tools version
        /// </summary>
        /// <param name="toolsVersion"></param>
        /// <returns></returns>
        protected override IEnumerable <PropertyDefinition> GetPropertyDefinitions(string toolsVersion)
        {
            RegistryKeyWrapper toolsVersionWrapper = null;

            try
            {
                toolsVersionWrapper = msbuildRegistryWrapper.OpenSubKey("ToolsVersions\\" + toolsVersion);
            }
            catch (RegistryException ex)
            {
                InvalidToolsetDefinitionException.Throw(ex, "RegistryReadError", ex.Source, ex.Message);
            }

            foreach (string propertyName in toolsVersionWrapper.GetValueNames())
            {
                string propertyValue = null;

                if (propertyName?.Length == 0)
                {
                    InvalidToolsetDefinitionException.Throw("PropertyNameInRegistryHasZeroLength", toolsVersionWrapper.Name);
                }

                try
                {
                    propertyValue = GetValue(toolsVersionWrapper, propertyName);
                }
                catch (RegistryException ex)
                {
                    InvalidToolsetDefinitionException.Throw(ex, "RegistryReadError", ex.Source, ex.Message);
                }

                yield return(new PropertyDefinition(propertyName, propertyValue, toolsVersionWrapper.Name + "@" + propertyName));
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Reads a string value from the specified registry key
        /// </summary>
        /// <param name="baseKeyWrapper">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>
        /// Expands the given unexpanded property expression using the properties in the
        /// given BuildPropertyGroup.
        /// </summary>
        /// <param name="unexpandedProperty"></param>
        /// <param name="properties"></param>
        /// <returns></returns>
        private string ExpandProperty(PropertyDefinition property, Expander expander)
        {
            try
            {
                return(expander.ExpandAllIntoStringLeaveEscaped(property.Value, null));
            }
            catch (InvalidProjectFileException ex)
            {
                InvalidToolsetDefinitionException.Throw(ex, "ErrorEvaluatingToolsetPropertyExpression", property.Value, property.Source, ex.Message);
            }

            return(string.Empty);
        }
 /// <summary>
 /// Sets the given property in the given property group.
 /// </summary>
 /// <param name="property"></param>
 /// <param name="propertyGroup"></param>
 /// <param name="globalProperties"></param>
 private void SetProperty(PropertyDefinition property, BuildPropertyGroup propertyGroup, BuildPropertyGroup globalProperties)
 {
     try
     {
         // Global properties cannot be overwritten
         if (globalProperties[property.Name] == null)
         {
             propertyGroup.SetProperty(property.Name, property.Value);
         }
     }
     catch (ArgumentException ex)
     {
         InvalidToolsetDefinitionException.Throw(ex, "InvalidPropertyNameInToolset", property.Name, property.Source, ex.Message);
     }
 }
 public void CtorArity3()
 {
     InvalidToolsetDefinitionException toolSetException = new InvalidToolsetDefinitionException("Message", "Error", new Exception("Inner"));
     Assertion.AssertEquals("Message", toolSetException.Message);
     Assertion.AssertEquals("Error", toolSetException.ErrorCode);
     Assertion.AssertEquals("Inner", toolSetException.InnerException.Message);
 }
        /// <summary>
        /// Reads the settings for a specified tools version
        /// </summary>
        /// <param name="toolsVersion"></param>
        /// <param name="globalProperties"></param>
        /// <param name="initialProperties"></param>
        /// <param name="accumulateProperties"></param>
        /// <returns></returns>
        private Toolset ReadToolset(PropertyDefinition toolsVersion,
                                    BuildPropertyGroup globalProperties,
                                    BuildPropertyGroup 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;
            BuildPropertyGroup properties = new BuildPropertyGroup();

            IEnumerable <PropertyDefinition> rawProperties = GetPropertyDefinitions(toolsVersion.Name);
            Expander expander = new Expander(initialProperties);

            foreach (PropertyDefinition property in rawProperties)
            {
                if (String.Equals(property.Name, ReservedPropertyNames.toolsPath, StringComparison.OrdinalIgnoreCase))
                {
                    toolsPath = ExpandProperty(property, expander);
                    toolsPath = ExpandRelativePathsRelativeToExeLocation(toolsPath);

                    if (accumulateProperties)
                    {
                        SetProperty
                        (
                            new PropertyDefinition(ReservedPropertyNames.toolsPath, toolsPath, property.Source),
                            initialProperties,
                            globalProperties
                        );
                    }
                }
                else if (String.Equals(property.Name, ReservedPropertyNames.binPath, StringComparison.OrdinalIgnoreCase))
                {
                    binPath = ExpandProperty(property, expander);
                    binPath = ExpandRelativePathsRelativeToExeLocation(binPath);

                    if (accumulateProperties)
                    {
                        SetProperty
                        (
                            new PropertyDefinition(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.FormatResourceString("CannotModifyReservedProperty", property.Name);
                    InvalidToolsetDefinitionException.Throw("InvalidPropertyNameInToolset", property.Name, property.Source, baseMessage);
                }
                else
                {
                    // It's an arbitrary property
                    string             propertyValue    = ExpandProperty(property, expander);
                    PropertyDefinition expandedProperty = new PropertyDefinition(property.Name, propertyValue, property.Source);

                    SetProperty(expandedProperty, properties, globalProperties);

                    if (accumulateProperties)
                    {
                        SetProperty(expandedProperty, initialProperties, globalProperties);
                    }
                }

                if (accumulateProperties)
                {
                    expander = new Expander(initialProperties);
                }
            }

            // All tools versions must specify a value for MSBuildToolsPath (or MSBuildBinPath)
            if (String.IsNullOrEmpty(toolsPath) && String.IsNullOrEmpty(binPath))
            {
                InvalidToolsetDefinitionException.Throw("MSBuildToolsPathIsNotSpecified", toolsVersion.Name, toolsVersion.Source);
            }

            // If both MSBuildBinPath and MSBuildToolsPath are present, they must be the same
            if (toolsPath != null && binPath != null && !toolsPath.Equals(binPath, StringComparison.OrdinalIgnoreCase))
            {
                return(null);
            }

            Toolset toolset = null;

            try
            {
                toolset = new Toolset(toolsVersion.Name, toolsPath ?? binPath, properties);
            }
            catch (ArgumentException e)
            {
                InvalidToolsetDefinitionException.Throw("ErrorCreatingToolset", toolsVersion.Name, e.Message);
            }

            return(toolset);
        }
 public void CtorMessage_Empty()
 {
     InvalidToolsetDefinitionException toolSetException = new InvalidToolsetDefinitionException(String.Empty);
     Assertion.AssertEquals(String.Empty, toolSetException.Message);
 }
 public void CtorMessage_Null()
 {
     InvalidToolsetDefinitionException toolSetException = new InvalidToolsetDefinitionException(null); 
 }
 public void CtorMessage()
 {
     InvalidToolsetDefinitionException toolSetException = new InvalidToolsetDefinitionException("Message");
     Assertion.AssertEquals("Message", toolSetException.Message);
 }
 public void SerializationBinary()
 {
     MemoryStream memoryStream = null;
     InvalidToolsetDefinitionException toolSetException = new InvalidToolsetDefinitionException("Message", "errorCode");
     try
     {
         memoryStream = new MemoryStream();
         IFormatter binaryForamtter = new BinaryFormatter();
         binaryForamtter.Serialize(memoryStream, toolSetException);
         memoryStream.Position = 0; // reset pointer into stream for read
         Object returnObj = binaryForamtter.Deserialize(memoryStream);
         Assertion.Assert(returnObj is InvalidToolsetDefinitionException);
         Assertion.AssertEquals("Message", ((InvalidToolsetDefinitionException)returnObj).Message);
     }
     finally
     {
         memoryStream.Close();
     }
 }
 public void SerializationXML()
 {
     InvalidToolsetDefinitionException toolSetException = new InvalidToolsetDefinitionException("Message", "errorCode");
     MemoryStream memoryStream = null;
     try
     {
         memoryStream = new MemoryStream();
         XmlSerializer xs = new XmlSerializer(typeof(InvalidToolsetDefinitionException));
         XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);
         xs.Serialize(xmlTextWriter, toolSetException);
         memoryStream = (MemoryStream)xmlTextWriter.BaseStream;
     }
     finally
     {
         memoryStream.Close(); 
     }
 }