Ejemplo n.º 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));
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Gets a value indicating whether the target should be executed.
        /// </summary>
        /// <value>
        /// <see langword="true" /> if the target should be executed; otherwise,
        /// <see langword="false" />.
        /// </value>
        public bool IfDefined(PropertyAccessor propertyAccesor)
        {
            // expand properties in condition
            string expandedCondition = propertyAccesor.ExpandProperties(IfCondition, Location);

            // if a condition is supplied, it should evaluate to a bool
            if (!String.IsNullOrEmpty(expandedCondition))
            {
                try
                {
                    return(Convert.ToBoolean(expandedCondition, CultureInfo.InvariantCulture));
                }
                catch (FormatException)
                {
                    throw new BuildException(string.Format(CultureInfo.InvariantCulture,
                                                           ResourceUtils.GetString("NA1070"), expandedCondition), Location);
                }
            }

            // no condition is supplied
            return(true);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Executes this target
        /// </summary>
        /// <param name="callStack">The current call stack on which this target will be pused</param>
        /// <param name="logger">The logger this target and its stasks will use for logging messages</param>
        /// <param name="arguments">Optionally, the arguments to provide to the target.  Should match those required by <see cref="Parameters"/></param>
        /// <exception cref="ArgumentException">If one of the non-defaulted parameters is not satisfied by an argument.</exception>
        private void DoExecute(TargetCallStack callStack, ITargetLogger logger, IList <CallArgument> arguments = null)
        {
            var propertyAccessor = new PropertyAccessor(this.Project, callStack);

            var sw = Stopwatch.StartNew();

            if (IfDefined(propertyAccessor) && !UnlessDefined(propertyAccessor))
            {
                try
                {
                    using (callStack.Push(this, logger))
                    {
                        this.PrepareArguments(arguments, callStack);

                        Project.OnTargetStarted(this, new TargetBuildEventArgs(this, sw));
                        logger.OnTargetLoggingStarted(this, new TargetBuildEventArgs(this, sw));

                        var paramtersDone = false;

                        // select all the task nodes and execute them
                        foreach (XmlNode childNode in XmlNode)
                        {
                            if (!(childNode.NodeType == XmlNodeType.Element) || !childNode.NamespaceURI.Equals(NamespaceManager.LookupNamespace("nant")))
                            {
                                continue;
                            }

                            if (childNode.Name.Equals("parameters"))
                            {
                                if (paramtersDone)
                                {
                                    throw new BuildException("parameters must appear before all tasks", this.Location);
                                }

                                continue;
                            }
                            else
                            {
                                paramtersDone = true;
                                if (TypeFactory.TaskBuilders.Contains(childNode.Name))
                                {
                                    Task task = Project.CreateTask(childNode, this, callStack);
                                    if (task != null)
                                    {
                                        task.Execute();
                                    }
                                }
                                else if (TypeFactory.DataTypeBuilders.Contains(childNode.Name))
                                {
                                    DataTypeBase dataType = Project.CreateDataTypeBase(childNode, callStack);
                                    logger.Log(Level.Verbose, "Adding a {0} reference with id '{1}'.",
                                               childNode.Name, dataType.ID);
                                    if (!Project.DataTypeReferences.Contains(dataType.ID))
                                    {
                                        Project.DataTypeReferences.Add(dataType.ID, dataType);
                                    }
                                    else
                                    {
                                        Project.DataTypeReferences[dataType.ID] = dataType; // overwrite with the new reference.
                                    }
                                }
                                else
                                {
                                    throw new BuildException(string.Format(CultureInfo.InvariantCulture,
                                                                           ResourceUtils.GetString("NA1071"),
                                                                           childNode.Name), Project.LocationMap.GetLocation(childNode));
                                }
                            }
                        }
                    }
                }
                finally
                {
                    _executed = true;
                    sw.Stop();
                    Project.OnTargetFinished(this, new TargetBuildEventArgs(this, sw));
                    logger.OnTargetLoggingFinished(this, new TargetBuildEventArgs(this, sw));
                }
            }
        }
Ejemplo n.º 4
0
 /// <summary>
 /// Initializes a new instance of the <see cref="FunctionSetBase"/> class.
 /// </summary>
 /// <param name="project">The current project.</param>
 /// <param name="properties">The projects properties.</param>
 /// <param name="callStack">The target call stack</param>
 protected FunctionSetBase(Project project, PropertyAccessor properties, TargetCallStack callStack)
 {
     _project             = project;
     this.PropertyAccesor = properties;
     this.CallStack       = callStack;
 }
Ejemplo n.º 5
0
 public BackwardsCompatiblePropertyAccessor(Project project, PropertyAccessor accessor, TargetCallStack callstack)
     : base(project, PropertyScope.Global)
 {
     this.PropertyAccessor = accessor;
     this.TargetCallStack  = callstack;
 }