Example #1
0
        /// <summary>
        /// Extracts up the paramters specified in <see cref="Parameters"/> from <paramref name="arguments"/>
        /// and ddds the necessary properties to the call stack's target properties.
        /// </summary>
        /// <param name="arguments">The arguments being passed in to the this target</param>
        /// <param name="callStack">The call stack where the properties will be placed</param>
        /// <exception cref="ArgumentException">If one of the non-defaulted parameters is not satisfied by an argument.</exception>
        private void PrepareArguments(IList <CallArgument> arguments, TargetCallStack callStack)
        {
            if (this.Parameters == null || !this.Parameters.Parameters.Any())
            {
                return;
            }

            var accessor = new PropertyAccessor(this.Project, callStack);

            var matchedProperties = new HashSet <String>();

            foreach (var param in this.Parameters.Parameters)
            {
                if (matchedProperties.Contains(param.PropertyName))
                {
                    throw new BuildException(String.Format(@"Paramter ""{0}"" was declared more than once", param.PropertyName));
                }

                matchedProperties.Add(param.PropertyName);

                CallArgument arg = null;

                if (arguments != null)
                {
                    arg = arguments.FirstOrDefault(a => a.PropertyName.Equals(param.PropertyName));
                }

                if (arg == null && param.DefaultValue != null)
                {
                    accessor.Set(param.PropertyName, param.DefaultValue, PropertyScope.Target, false, true);
                }
                else if (arg != null)
                {
                    if (arguments.Count(a => a.PropertyName.Equals(arg.PropertyName)) > 1)
                    {
                        throw new ArgumentException(String.Format(@"Argument ""{0}"" was specified more than once.", param.PropertyName));
                    }

                    accessor.Set(param.PropertyName, arg.PropertyValue, PropertyScope.Target, false, true);
                }
                else
                {
                    throw new ArgumentException(String.Format(@"Target ""{0}"" requires parameter ""{1}"" but it was not provided and has no default.", this.Name, param.PropertyName));
                }
            }
        }