Пример #1
0
        /// <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);
        }
Пример #2
0
        /// <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;
        }
Пример #3
0
 /// <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);
     }
 }
Пример #4
0
        /// <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 (0 == String.Compare(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 (0 == String.Compare(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 == null ? binPath : toolsPath, properties);
            }
            catch (ArgumentException e)
            {
                InvalidToolsetDefinitionException.Throw("ErrorCreatingToolset", toolsVersion.Name, e.Message);
            }

            return toolset;
        }