/// <summary>
        /// Constructor with location information
        /// </summary>
        public XmlAttributeWithLocation(string prefix, string localName, string namespaceURI, XmlDocument document, int lineNumber, int columnNumber)
            : base(prefix, localName, namespaceURI, document)
        {
            XmlDocumentWithLocation documentWithLocation = (XmlDocumentWithLocation)document;

            _elementLocation = ElementLocation.Create(documentWithLocation.FullPath, lineNumber, columnNumber);
        }
 internal InvalidProjectFileException (ElementLocation start, ElementLocation end, string message,
                                     string errorSubcategory = null, string errorCode = null, string helpKeyword = null)
         : this (start != null ? start.File : null, start != null ? start.Line : 0, start != null ? start.Column : 0,
                 end != null ? end.Line : 0, end != null ? end.Column : 0,
                 message, errorSubcategory, errorCode, helpKeyword)
 {
 }
Beispiel #3
0
        public void SetUp()
        {
            LoggingServiceFactory loggingFactory = new LoggingServiceFactory(LoggerMode.Synchronous, 1);

            _loggingService = loggingFactory.CreateInstance(BuildComponentType.LoggingService) as LoggingService;

            _customLogger = new MyCustomLogger();
            _mockHost = new MockHost();
            _mockHost.LoggingService = _loggingService;

            _loggingService.RegisterLogger(_customLogger);
            _elementLocation = ElementLocation.Create("MockFile", 5, 5);

            BuildRequest buildRequest = new BuildRequest(1 /* submissionId */, 1, 1, new List<string>(), null, BuildEventContext.Invalid, null);
            BuildRequestConfiguration configuration = new BuildRequestConfiguration(1, new BuildRequestData("Nothing", new Dictionary<string, string>(), "4.0", new string[0], null), "2.0");

            configuration.Project = new ProjectInstance(ProjectRootElement.Create());

            BuildRequestEntry entry = new BuildRequestEntry(buildRequest, configuration);

            BuildResult buildResult = new BuildResult(buildRequest, false);
            buildResult.AddResultsForTarget("Build", new TargetResult(new TaskItem[] { new TaskItem("IamSuper", configuration.ProjectFullPath) }, TestUtilities.GetSkippedResult()));
            _mockRequestCallback = new MockIRequestBuilderCallback(new BuildResult[] { buildResult });
            entry.Builder = (IRequestBuilder)_mockRequestCallback;

            _taskHost = new TaskHost(_mockHost, entry, _elementLocation, null /*Dont care about the callback either unless doing a build*/);
            _taskHost.LoggingContext = new TaskLoggingContext(_loggingService, BuildEventContext.Invalid);
        }
 /// <summary>
 /// Cloning constructor
 /// </summary>
 private ProjectPropertyGroupTaskPropertyInstance(ProjectPropertyGroupTaskPropertyInstance that)
 {
     // All fields are immutable
     _name = that._name;
     _value = that._value;
     _condition = that._condition;
     _location = that._location;
     _conditionLocation = that._conditionLocation;
 }
        /// <summary>
        /// Constructor with location information
        /// </summary>
        public XmlElementWithLocation(string prefix, string localName, string namespaceURI, XmlDocumentWithLocation document, int lineNumber, int columnNumber)
            : base(prefix, localName, namespaceURI, document)
        {
            // Subtract one, just to give the same value as the old code did.
            // In the past we pointed to the column of the open angle bracket whereas the XmlTextReader points to the first character of the element name.
            // In well formed XML these are always adjacent on the same line, so it's safe to subtract one.
            // If we're loading from a stream it's zero, so don't subtract one.
            XmlDocumentWithLocation documentWithLocation = (XmlDocumentWithLocation)document;

            int adjustedColumn = (columnNumber == 0) ? columnNumber : columnNumber - 1;
            _elementLocation = ElementLocation.Create(documentWithLocation.FullPath, lineNumber, adjustedColumn);
        }
        /// <summary>
        /// Constructor called by the Evaluator.
        /// </summary>
        internal ProjectPropertyGroupTaskPropertyInstance(string name, string value, string condition, ElementLocation location, ElementLocation conditionLocation)
        {
            ErrorUtilities.VerifyThrowInternalNull(name, "name");
            ErrorUtilities.VerifyThrowInternalNull(value, "value");
            ErrorUtilities.VerifyThrowInternalNull(condition, "condition");
            ErrorUtilities.VerifyThrowInternalNull(location, "location");

            _name = name;
            _value = value;
            _condition = condition;
            _location = location;
            _conditionLocation = conditionLocation;
        }
        /// <summary>
        /// Constructor called by Evaluator
        /// </summary>
        internal ProjectTaskOutputPropertyInstance(string propertyName, string taskParameter, string condition, ElementLocation location, ElementLocation propertyNameLocation, ElementLocation taskParameterLocation, ElementLocation conditionLocation)
        {
            ErrorUtilities.VerifyThrowInternalLength(propertyName, "propertyName");
            ErrorUtilities.VerifyThrowInternalLength(taskParameter, "taskParameter");
            ErrorUtilities.VerifyThrowInternalNull(location, "location");
            ErrorUtilities.VerifyThrowInternalNull(propertyNameLocation, "propertyNameLocation");
            ErrorUtilities.VerifyThrowInternalNull(taskParameterLocation, "taskParameterLocation");

            _propertyName = propertyName;
            _taskParameter = taskParameter;
            _condition = condition;
            _location = location;
            _propertyNameLocation = propertyNameLocation;
            _taskParameterLocation = taskParameterLocation;
            _conditionLocation = conditionLocation;
        }
        /// <summary>
        /// Constructor called by evaluator
        /// </summary>
        internal ProjectTaskOutputItemInstance(string itemType, string taskParameter, string condition, ElementLocation location, ElementLocation itemTypeLocation, ElementLocation taskParameterLocation, ElementLocation conditionLocation)
        {
            ErrorUtilities.VerifyThrowInternalLength(itemType, "itemType");
            ErrorUtilities.VerifyThrowInternalLength(taskParameter, "taskParameter");
            ErrorUtilities.VerifyThrowInternalNull(location, "location");
            ErrorUtilities.VerifyThrowInternalNull(itemTypeLocation, "itemTypeLocation");
            ErrorUtilities.VerifyThrowInternalNull(taskParameterLocation, "taskParameterLocation");

            _itemType = itemType;
            _taskParameter = taskParameter;
            _condition = condition;
            _location = location;
            _itemTypeLocation = itemTypeLocation;
            _taskParameterLocation = taskParameterLocation;
            _conditionLocation = conditionLocation;
        }
        /// <summary>
        /// Constructor called by Evaluator.
        /// All parameters are in the unevaluated state.
        /// </summary>
        internal ProjectOnErrorInstance
            (
            string executeTargets,
            string condition,
            ElementLocation location,
            ElementLocation executeTargetsLocation,
            ElementLocation conditionLocation
            )
        {
            ErrorUtilities.VerifyThrowInternalLength(executeTargets, "executeTargets");
            ErrorUtilities.VerifyThrowInternalNull(condition, "condition");
            ErrorUtilities.VerifyThrowInternalNull(location, "location");

            _executeTargets = executeTargets;
            _condition = condition;
            _location = location;
            _executeTargetsLocation = executeTargetsLocation;
            _conditionLocation = conditionLocation;
        }
        /// <summary>
        /// Constructor called by the Evaluator.
        /// Assumes ProjectPropertyGroupTaskPropertyInstance is an immutable type.
        /// </summary>
        internal ProjectPropertyGroupTaskInstance
            (
            string condition,
            ElementLocation location,
            ElementLocation conditionLocation,
            IEnumerable<ProjectPropertyGroupTaskPropertyInstance> properties
            )
        {
            ErrorUtilities.VerifyThrowInternalNull(condition, "condition");
            ErrorUtilities.VerifyThrowInternalNull(location, "location");
            ErrorUtilities.VerifyThrowInternalNull(properties, "properties");

            _condition = condition;
            _location = location;
            _conditionLocation = conditionLocation;

            if (properties != null)
            {
                _properties = (properties is ICollection<ProjectPropertyGroupTaskPropertyInstance>) ?
                    ((ICollection<ProjectPropertyGroupTaskPropertyInstance>)properties) :
                    new List<ProjectPropertyGroupTaskPropertyInstance>(properties);
            }
        }
        /// <summary>
        /// Constructor called by the Evaluator.
        /// Assumes ProjectItemGroupTaskItemInstance is an immutable type.
        /// </summary>
        internal ProjectItemGroupTaskInstance
            (
            string condition,
            ElementLocation location,
            ElementLocation conditionLocation,
            IEnumerable<ProjectItemGroupTaskItemInstance> items
            )
        {
            ErrorUtilities.VerifyThrowInternalNull(condition, "condition");
            ErrorUtilities.VerifyThrowInternalNull(location, "location");
            ErrorUtilities.VerifyThrowInternalNull(items, "items");

            _condition = condition;
            _location = location;
            _conditionLocation = conditionLocation;

            if (items != null)
            {
                _items = (items is ICollection<ProjectItemGroupTaskItemInstance>) ?
                    ((ICollection<ProjectItemGroupTaskItemInstance>)items) :
                    new List<ProjectItemGroupTaskItemInstance>(items);
            }
        }
Beispiel #12
0
        /// <summary>
        /// Pop the state most recently pushed by EnterState. 
        /// The identifier (location) of a Leave must match the Enter at the top of the stack,
        /// to catch mismatched Leaves.
        /// </summary>
        internal static void LeaveState(ElementLocation location)
        {
            ErrorUtilities.VerifyThrow(s_initialized, "Not initialized");
            ErrorUtilities.VerifyThrow(DebuggingEnabled, "Debugging not enabled");
            ErrorUtilities.VerifyThrow(s_skippedEnters >= 0, "Left too many");

            // Special case: elements added by editing, such as in a solution wrapper project,
            // do not have line numbers. Such files cannot be debugged, so we special case
            // such locations by doing nothing.
            if (s_skippedEnters > 0)
            {
                s_skippedEnters--;
                return;
            }

            s_islandThread.LeaveState(location);
        }
Beispiel #13
0
            /// <summary>
            /// Constructor.
            /// State is given arbitrary provided name, which will appear in the debugger callstack: "ASSEMBLYNAME!FILENAME.STATENAME(...LOCALS...)"
            /// Early locals are any locals whose names and types available at the time the state was created. May be null.
            /// "Calling Type.GetMethod() is slow (10,000 calls can take ~1 minute). So defer that to later."
            /// CALLED ONLY FROM THE DEBUGGER MANAGER.
            /// </summary>
            internal DebuggerState(ElementLocation location, string name, ICollection<DebuggerLocalType> earlyLocalsTypes)
            {
                ErrorUtilities.VerifyThrowInternalNull(location, "location");
                ErrorUtilities.VerifyThrowInternalLength(name, "name");

                this.Location = location;
                this.Name = name;
                this.EarlyLocalsTypes = earlyLocalsTypes ?? ReadOnlyEmptyList<DebuggerLocalType>.Instance;
            }
Beispiel #14
0
            /// <summary>
            /// Posts a Leave instruction to the island thread.
            /// Called by debugger manager thread
            /// If location is provided, verifies that the state being left is the state that was entered.
            /// Stack may already be empty, in which case it is not modified.
            /// </summary>
            internal void LeaveState(ElementLocation location)
            {
                ErrorUtilities.VerifyThrow(location == null || location == _virtualStack.Peek().State.Location, "Mismatched leave was {0} expected {1}", location.LocationString, _virtualStack.Peek().State.Location.LocationString);

                _debugAction = DebugAction.Leave;

                if (_virtualStack.Count > 0) // May be falling out of the first enter
                {
                    _virtualStack.Pop();
                }

                _workToDoEvent.Set();
                _workDoneEvent.WaitOne();
            }
Beispiel #15
0
        /// <summary>
        /// Gets the outputs (as an array of ITaskItem) from the specified output parameter.
        /// </summary>
        private ITaskItem[] GetItemOutputs(TaskPropertyInfo parameter, ElementLocation parameterLocation)
        {
            object outputs = _taskFactoryWrapper.GetPropertyValue(_taskInstance, parameter);

            ITaskItem[] taskItemOutputs = outputs as ITaskItem[];
            if (null == taskItemOutputs)
            {
                taskItemOutputs = new ITaskItem[] { (ITaskItem)outputs };
            }

            return taskItemOutputs;
        }
Beispiel #16
0
        /// <summary>
        /// Gets the outputs (as an array of string values) from the specified output parameter.
        /// </summary>
        private string[] GetValueOutputs(TaskPropertyInfo parameter, ElementLocation parameterLocation)
        {
            object outputs = _taskFactoryWrapper.GetPropertyValue(_taskInstance, parameter);

            Array convertibleOutputs = parameter.PropertyType.IsArray ? (Array)outputs : new object[] { outputs };

            if (convertibleOutputs == null)
            {
                return null;
            }

            string[] stringOutputs = new string[convertibleOutputs.Length];
            for (int i = 0; i < convertibleOutputs.Length; i++)
            {
                object output = convertibleOutputs.GetValue(i);
                if (output != null)
                {
                    stringOutputs[i] = (string)Convert.ChangeType(output, typeof(string), CultureInfo.InvariantCulture);
                }
            }

            return stringOutputs;
        }
Beispiel #17
0
 /// <summary>
 /// Called on the local side.
 /// </summary>
 private bool SetValueParameter(TaskPropertyInfo parameter, Type parameterType, string expandedParameterValue, ElementLocation parameterLocation)
 {
     if (parameterType == typeof(bool))
     {
         // Convert the string to the appropriate datatype, and set the task's parameter.
         return InternalSetTaskParameter(parameter, ConversionUtilities.ConvertStringToBool(expandedParameterValue));
     }
     else if (parameterType == typeof(string))
     {
         return InternalSetTaskParameter(parameter, expandedParameterValue);
     }
     else
     {
         return InternalSetTaskParameter(parameter, Convert.ChangeType(expandedParameterValue, parameterType, CultureInfo.InvariantCulture));
     }
 }
Beispiel #18
0
        /// <summary>
        /// Given an instantiated task, this helper method sets the specified vector parameter. Vector parameters can be composed
        /// of multiple item vectors. The semicolon is the only separator allowed, and white space around the semicolon is
        /// ignored. Any item separator strings are not allowed, and embedded item vectors are not allowed.
        /// </summary>
        /// <remarks>This method is marked "internal" for unit-testing purposes only -- it should be "private" ideally.</remarks>
        /// <example>
        /// If @(CPPFiles) is a vector for the files a.cpp and b.cpp, and @(IDLFiles) is a vector for the files a.idl and b.idl:
        ///
        ///     "@(CPPFiles)"                               converts to     { a.cpp, b.cpp }
        ///
        ///     "@(CPPFiles); c.cpp; @(IDLFiles); c.idl"    converts to     { a.cpp, b.cpp, c.cpp, a.idl, b.idl, c.idl }
        ///
        ///     "@(CPPFiles,';')"                           converts to     &lt;error&gt;
        ///
        ///     "xxx@(CPPFiles)xxx"                         converts to     &lt;error&gt;
        /// </example>
        private bool InitializeTaskVectorParameter
        (
            TaskPropertyInfo parameter,
            Type parameterType,
            string parameterValue,
            ElementLocation parameterLocation,
            bool isRequired,
            out bool taskParameterSet
        )
        {
            ErrorUtilities.VerifyThrow(parameterValue != null, "Didn't expect null parameterValue in InitializeTaskVectorParameter");

            taskParameterSet = false;
            bool success = false;
            IList<TaskItem> finalTaskItems = _batchBucket.Expander.ExpandIntoTaskItemsLeaveEscaped(parameterValue, ExpanderOptions.ExpandAll, parameterLocation);

            // If there were no items, don't change the parameter's value.  EXCEPT if it's marked as a required 
            // parameter, in which case we made an explicit decision to pass in an empty array.  This is 
            // to avoid project authors having to add Conditions on all their tasks to avoid calling them
            // when a particular item list is empty.  This way, we just call the task with an empty list,
            // the task will loop over an empty list, and return quickly.
            if ((finalTaskItems.Count > 0) || isRequired)
            {
                // If the task parameter is not a ITaskItem[], then we need to convert
                // all the TaskItem's in our arraylist to the appropriate datatype.
                success = SetParameterArray(parameter, parameterType, finalTaskItems, parameterLocation);
                taskParameterSet = true;
            }
            else
            {
                success = true;
            }

            return success;
        }
Beispiel #19
0
 /// <summary>
 /// Throw an invalid project exception indicating that the child is not valid beneath the element
 /// </summary>
 internal static void ThrowProjectInvalidChildElement(string name, string parentName, ElementLocation location)
 {
     ProjectErrorUtilities.ThrowInvalidProject(location, "UnrecognizedChildElement", name, parentName);
 }
Beispiel #20
0
 /// <summary>
 /// Called on the local side.
 /// </summary>
 private bool SetTaskItemParameter(TaskPropertyInfo parameter, ITaskItem item, ElementLocation parameterLocation)
 {
     return InternalSetTaskParameter(parameter, item);
 }
Beispiel #21
0
 internal void FillLocation (XmlReader reader)
 {
         var l = reader as IXmlLineInfo;
         if (l != null && l.HasLineInfo ())
                 Location = new ProjectElementLocation (reader.BaseURI, l);
         if (reader.MoveToAttribute ("Condition") && l.HasLineInfo ())
                 ConditionLocation = new ProjectElementLocation (reader.BaseURI, l);
         if (reader.MoveToAttribute ("Label") && l.HasLineInfo ())
                 LabelLocation = new ProjectElementLocation (reader.BaseURI, l);
         reader.MoveToElement ();
 }
Beispiel #22
0
        /// <summary>
        /// Set the specified parameter based on its type.
        /// </summary>
        private bool SetTaskParameter
        (
            string parameterName,
            string parameterValue,
            ElementLocation parameterLocation,
            bool isRequired,
            out bool parameterSet
        )
        {
            bool success = false;
            parameterSet = false;

            try
            {
                // check if the task has a .NET property corresponding to the parameter
                TaskPropertyInfo parameter = _taskFactoryWrapper.GetProperty(parameterName);

                if (parameter != null)
                {
                    Type parameterType = parameter.PropertyType;

                    // try to set the parameter
                    if (TaskParameterTypeVerifier.IsValidScalarInputParameter(parameterType))
                    {
                        success = InitializeTaskScalarParameter
                            (
                            parameter,
                            parameterType,
                            parameterValue,
                            parameterLocation,
                            out parameterSet
                            );
                    }
                    else if (TaskParameterTypeVerifier.IsValidVectorInputParameter(parameterType))
                    {
                        success = InitializeTaskVectorParameter
                            (
                            parameter,
                            parameterType,
                            parameterValue,
                            parameterLocation,
                            isRequired,
                            out parameterSet
                            );
                    }
                    else
                    {
                        _taskLoggingContext.LogError
                            (
                            new BuildEventFileInfo(parameterLocation),
                            "UnsupportedTaskParameterTypeError",
                            parameterType.FullName,
                            parameter.Name,
                            _taskName
                            );
                    }

                    if (!success)
                    {
                        // flag an error if the parameter could not be set
                        _taskLoggingContext.LogError
                            (
                            new BuildEventFileInfo(parameterLocation),
                            "InvalidTaskAttributeError",
                            parameterName,
                            parameterValue,
                            _taskName
                            );
                    }
                }
                else
                {
                    // flag an error if we find a parameter that has no .NET property equivalent
                    _taskLoggingContext.LogError
                        (
                        new BuildEventFileInfo(parameterLocation),
                        "UnexpectedTaskAttribute",
                        parameterName,
                        _taskName
                        );
                }
            }
            catch (AmbiguousMatchException)
            {
                _taskLoggingContext.LogError
                    (
                    new BuildEventFileInfo(parameterLocation),
                    "AmbiguousTaskParameterError",
                    _taskName,
                    parameterName
                    );
            }
            catch (ArgumentException)
            {
                ProjectErrorUtilities.ThrowInvalidProject
                    (
                    parameterLocation,
                    "SetAccessorNotAvailableOnTaskParameter",
                    parameterName,
                    _taskName
                    );
            }

            return success;
        }
Beispiel #23
0
        /// <summary>
        /// Gets task item outputs
        /// </summary>
        private void GatherTaskItemOutputs(bool outputTargetIsItem, string outputTargetName, ITaskItem[] outputs, ElementLocation parameterLocation)
        {
            // if the task has generated outputs (if it didn't, don't do anything)
            if (outputs != null)
            {
                if (outputTargetIsItem)
                {
                    foreach (ITaskItem output in outputs)
                    {
                        // if individual items in the array are null, ignore them
                        if (output != null)
                        {
                            ProjectItemInstance newItem;

                            ProjectItemInstance.TaskItem outputAsProjectItem = output as ProjectItemInstance.TaskItem;
                            string parameterLocationEscaped = EscapingUtilities.EscapeWithCaching(parameterLocation.File);

                            if (outputAsProjectItem != null)
                            {
                                // The common case -- all items involved are Microsoft.Build.Execution.ProjectItemInstance.TaskItems.  
                                // Furthermore, because that is true, we know by definition that they also implement ITaskItem2.
                                newItem = new ProjectItemInstance(_projectInstance, outputTargetName, outputAsProjectItem.IncludeEscaped, parameterLocationEscaped);

                                newItem.SetMetadata(outputAsProjectItem.MetadataCollection); // copy-on-write!
                            }
                            else
                            {
                                ITaskItem2 outputAsITaskItem2 = output as ITaskItem2;

                                if (outputAsITaskItem2 != null)
                                {
                                    // Probably a Microsoft.Build.Utilities.TaskItem.  Not quite as good, but we can still preserve escaping. 
                                    newItem = new ProjectItemInstance(_projectInstance, outputTargetName, outputAsITaskItem2.EvaluatedIncludeEscaped, parameterLocationEscaped);

                                    // It would be nice to be copy-on-write here, but Utilities.TaskItem doesn't know about CopyOnWritePropertyDictionary. 
                                    foreach (DictionaryEntry entry in outputAsITaskItem2.CloneCustomMetadataEscaped())
                                    {
                                        newItem.SetMetadataOnTaskOutput((string)entry.Key, (string)entry.Value);
                                    }
                                }
                                else
                                {
                                    // Not a ProjectItemInstance.TaskItem or even a ITaskItem2, so we have to fake it.  
                                    // Setting an item spec expects the escaped value, as does setting metadata. 
                                    newItem = new ProjectItemInstance(_projectInstance, outputTargetName, EscapingUtilities.Escape(output.ItemSpec), parameterLocationEscaped);

                                    foreach (DictionaryEntry entry in output.CloneCustomMetadata())
                                    {
                                        newItem.SetMetadataOnTaskOutput((string)entry.Key, EscapingUtilities.Escape((string)entry.Value));
                                    }
                                }
                            }

                            _batchBucket.Lookup.AddNewItem(newItem);
                        }
                    }

                    if (_logTaskInputs && !_taskLoggingContext.LoggingService.OnlyLogCriticalEvents && outputs.Length > 0)
                    {
                        string parameterText = ItemGroupLoggingHelper.GetParameterText(
                            ResourceUtilities.FormatResourceString("OutputItemParameterMessagePrefix"),
                            outputTargetName,
                            outputs);

                        _taskLoggingContext.LogCommentFromText(MessageImportance.Low, parameterText);
                    }
                }
                else
                {
                    // to store an ITaskItem array in a property, join all the item-specs with semi-colons to make the
                    // property value, and ignore/discard the attributes on the ITaskItems.
                    //
                    // An empty ITaskItem[] should create a blank value property, for compatibility.                 
                    StringBuilder joinedOutputs = (outputs.Length == 0) ? new StringBuilder() : null;

                    foreach (ITaskItem output in outputs)
                    {
                        // if individual items in the array are null, ignore them
                        if (output != null)
                        {
                            joinedOutputs = joinedOutputs ?? new StringBuilder();

                            if (joinedOutputs.Length > 0)
                            {
                                joinedOutputs.Append(';');
                            }

                            ITaskItem2 outputAsITaskItem2 = output as ITaskItem2;

                            if (outputAsITaskItem2 != null)
                            {
                                joinedOutputs.Append(outputAsITaskItem2.EvaluatedIncludeEscaped);
                            }
                            else
                            {
                                joinedOutputs.Append(EscapingUtilities.Escape(output.ItemSpec));
                            }
                        }
                    }

                    if (joinedOutputs != null)
                    {
                        var outputString = joinedOutputs.ToString();
                        if (_logTaskInputs && !_taskLoggingContext.LoggingService.OnlyLogCriticalEvents)
                        {
                            _taskLoggingContext.LogComment(MessageImportance.Low, "OutputPropertyLogMessage", outputTargetName, outputString);
                        }

                        _batchBucket.Lookup.SetProperty(ProjectPropertyInstance.Create(outputTargetName, outputString, parameterLocation, _projectInstance.IsImmutable));
                    }
                }
            }
        }
Beispiel #24
0
        /// <summary>
        /// Enter and immediately leave a state, so that any breakpoint can be hit.
        /// </summary>
        internal static void PulseState(ElementLocation location, IDictionary<string, object> locals)
        {
            ErrorUtilities.VerifyThrow(s_initialized, "Not initialized");
            ErrorUtilities.VerifyThrow(DebuggingEnabled, "Debugging not enabled");

            EnterState(location, locals);
            LeaveState(location);
        }
Beispiel #25
0
        /// <summary>
        /// Called on the local side.
        /// </summary>
        private bool SetParameterArray(TaskPropertyInfo parameter, Type parameterType, IList<TaskItem> taskItems, ElementLocation parameterLocation)
        {
            TaskItem currentItem = null;

            try
            {
                // Loop through all the TaskItems in our arraylist, and convert them.
                ArrayList finalTaskInputs = new ArrayList();

                if (parameterType != typeof(ITaskItem[]))
                {
                    foreach (TaskItem item in taskItems)
                    {
                        currentItem = item;
                        if (parameterType == typeof(string[]))
                        {
                            finalTaskInputs.Add(item.ItemSpec);
                        }
                        else if (parameterType == typeof(bool[]))
                        {
                            finalTaskInputs.Add(ConversionUtilities.ConvertStringToBool(item.ItemSpec));
                        }
                        else
                        {
                            finalTaskInputs.Add(Convert.ChangeType(item.ItemSpec, parameterType.GetElementType(), CultureInfo.InvariantCulture));
                        }
                    }
                }
                else
                {
                    foreach (TaskItem item in taskItems)
                    {
                        // if we've been asked to remote these items then
                        // remember them so we can disconnect them from remoting later
                        RecordItemForDisconnectIfNecessary(item);

                        finalTaskInputs.Add(item);
                    }
                }

                return InternalSetTaskParameter(parameter, finalTaskInputs.ToArray(parameterType.GetElementType()));
            }
            catch (Exception ex)
            {
                if (ex is InvalidCastException || // invalid type
                    ex is ArgumentException || // can't convert to bool
                    ex is FormatException || // bad string representation of a type
                    ex is OverflowException) // overflow when converting string representation of a numerical type
                {
                    ProjectErrorUtilities.ThrowInvalidProject
                    (
                        parameterLocation,
                        "InvalidTaskParameterValueError",
                        currentItem.ItemSpec,
                        parameter.Name,
                        parameterType.FullName,
                        _taskName
                    );
                }

                throw;
            }
        }
Beispiel #26
0
        /// <summary>
        /// Gather task outputs in array form
        /// </summary>
        private void GatherArrayStringAndValueOutputs(bool outputTargetIsItem, string outputTargetName, TaskPropertyInfo parameter, string[] outputs, ElementLocation parameterLocation)
        {
            // if the task has generated outputs (if it didn't, don't do anything)            
            if (outputs != null)
            {
                if (outputTargetIsItem)
                {
                    // to store the outputs as items, use the string representations of the outputs as item-specs
                    foreach (string output in outputs)
                    {
                        // if individual outputs in the array are null, ignore them
                        if (output != null)
                        {
                            // attempting to put an empty string into an item is a no-op.
                            if (output.Length > 0)
                            {
                                _batchBucket.Lookup.AddNewItem(new ProjectItemInstance(_projectInstance, outputTargetName, EscapingUtilities.Escape(output), EscapingUtilities.Escape(parameterLocation.File)));
                            }
                        }
                    }

                    if (_logTaskInputs && !_taskLoggingContext.LoggingService.OnlyLogCriticalEvents && outputs.Length > 0)
                    {
                        string parameterText = ItemGroupLoggingHelper.GetParameterText(ResourceUtilities.FormatResourceString("OutputItemParameterMessagePrefix"), outputTargetName, outputs);
                        _taskLoggingContext.LogCommentFromText(MessageImportance.Low, parameterText);
                    }
                }
                else
                {
                    // to store an object array in a property, join all the string representations of the objects with
                    // semi-colons to make the property value
                    //
                    // An empty ITaskItem[] should create a blank value property, for compatibility.                 
                    StringBuilder joinedOutputs = (outputs.Length == 0) ? new StringBuilder() : null;

                    foreach (string output in outputs)
                    {
                        // if individual outputs in the array are null, ignore them
                        if (output != null)
                        {
                            joinedOutputs = joinedOutputs ?? new StringBuilder();

                            if (joinedOutputs.Length > 0)
                            {
                                joinedOutputs.Append(';');
                            }

                            joinedOutputs.Append(EscapingUtilities.Escape(output));
                        }
                    }

                    if (joinedOutputs != null)
                    {
                        var outputString = joinedOutputs.ToString();
                        if (_logTaskInputs && !_taskLoggingContext.LoggingService.OnlyLogCriticalEvents)
                        {
                            _taskLoggingContext.LogComment(MessageImportance.Low, "OutputPropertyLogMessage", outputTargetName, outputString);
                        }

                        _batchBucket.Lookup.SetProperty(ProjectPropertyInstance.Create(outputTargetName, outputString, parameterLocation, _projectInstance.IsImmutable));
                    }
                }
            }
        }
Beispiel #27
0
        /// <summary>
        /// Given an instantiated task, this helper method sets the specified scalar parameter based on its type.
        /// </summary>
        private bool InitializeTaskScalarParameter
        (
            TaskPropertyInfo parameter,
            Type parameterType,
            string parameterValue,
            ElementLocation parameterLocation,
            out bool taskParameterSet
        )
        {
            taskParameterSet = false;

            bool success = false;

            try
            {
                if (parameterType == typeof(ITaskItem))
                {
                    // We don't know how many items we're going to end up with, but we'll
                    // keep adding them to this arraylist as we find them.
                    IList<TaskItem> finalTaskItems = _batchBucket.Expander.ExpandIntoTaskItemsLeaveEscaped(parameterValue, ExpanderOptions.ExpandAll, parameterLocation);

                    if (finalTaskItems.Count == 0)
                    {
                        success = true;
                    }
                    else
                    {
                        if (finalTaskItems.Count != 1)
                        {
                            // We only allow a single item to be passed into a parameter of ITaskItem.

                            // Some of the computation (expansion) here is expensive, so don't make the above
                            // "if" statement directly part of the first param to VerifyThrowInvalidProject.
                            ProjectErrorUtilities.VerifyThrowInvalidProject
                                (
                                false,
                                parameterLocation,
                                "CannotPassMultipleItemsIntoScalarParameter",
                                _batchBucket.Expander.ExpandIntoStringAndUnescape(parameterValue, ExpanderOptions.ExpandAll, parameterLocation),
                                parameter.Name,
                                parameterType.FullName,
                                _taskName
                                );
                        }

                        RecordItemForDisconnectIfNecessary(finalTaskItems[0]);

                        success = SetTaskItemParameter(parameter, finalTaskItems[0], parameterLocation);

                        taskParameterSet = true;
                    }
                }
                else
                {
                    // Expand out all the metadata, properties, and item vectors in the string.
                    string expandedParameterValue = _batchBucket.Expander.ExpandIntoStringAndUnescape(parameterValue, ExpanderOptions.ExpandAll, parameterLocation);

                    if (expandedParameterValue.Length == 0)
                    {
                        success = true;
                    }
                    else
                    {
                        success = SetValueParameter(parameter, parameterType, expandedParameterValue, parameterLocation);
                        taskParameterSet = true;
                    }
                }
            }
            catch (Exception ex)
            {
                if (ex is InvalidCastException || // invalid type
                    ex is ArgumentException || // can't convert to bool
                    ex is FormatException || // bad string representation of a type
                    ex is OverflowException) // overflow when converting string representation of a numerical type
                {
                    ProjectErrorUtilities.ThrowInvalidProject
                    (
                        parameterLocation,
                        "InvalidTaskParameterValueError",
                        _batchBucket.Expander.ExpandIntoStringAndUnescape(parameterValue, ExpanderOptions.ExpandAll, parameterLocation),
                        parameter.Name,
                        parameterType.FullName,
                        _taskName
                    );
                }

                throw;
            }

            return success;
        }
Beispiel #28
0
 public void TearDown()
 {
     _customLogger = null;
     _mockHost = null;
     _elementLocation = null;
     _taskHost = null;
 }
Beispiel #29
0
 /// <summary>
 /// Initialize to run a specific task.
 /// </summary>
 void ITaskExecutionHost.InitializeForTask(IBuildEngine2 buildEngine, TargetLoggingContext loggingContext, ProjectInstance projectInstance, string taskName, ElementLocation taskLocation, ITaskHost taskHost, bool continueOnError, AppDomainSetup appDomainSetup, bool isOutOfProc, CancellationToken cancellationToken)
 {
     _buildEngine = buildEngine;
     _projectInstance = projectInstance;
     _targetLoggingContext = loggingContext;
     _taskName = taskName;
     _taskLocation = taskLocation;
     _cancellationTokenRegistration = cancellationToken.Register(this.Cancel);
     _taskHost = taskHost;
     _continueOnError = continueOnError;
     _taskExecutionIdle.Set();
     this.AppDomainSetup = appDomainSetup;
     this.IsOutOfProc = isOutOfProc;
 }
Beispiel #30
0
        /// <summary>
        /// Retrieve the outputs from the task.
        /// </summary>
        /// <returns>True of the outputs were gathered successfully, false otherwise.</returns>
        bool ITaskExecutionHost.GatherTaskOutputs(string parameterName, ElementLocation parameterLocation, bool outputTargetIsItem, string outputTargetName)
        {
            ErrorUtilities.VerifyThrow(_taskFactoryWrapper != null, "Need a taskFactoryWrapper to retrieve outputs from.");

            bool gatheredGeneratedOutputsSuccessfully = true;

            try
            {
                TaskPropertyInfo parameter = _taskFactoryWrapper.GetProperty(parameterName);

                // flag an error if we find a parameter that has no .NET property equivalent
                ProjectErrorUtilities.VerifyThrowInvalidProject
                (
                    parameter != null,
                    parameterLocation,
                    "UnexpectedTaskOutputAttribute",
                    parameterName,
                    _taskName
                );

                // output parameters must have their corresponding .NET properties marked with the Output attribute
                ProjectErrorUtilities.VerifyThrowInvalidProject
                (
                    _taskFactoryWrapper.GetNamesOfPropertiesWithOutputAttribute.ContainsKey(parameterName),
                    parameterLocation,
                    "UnmarkedOutputTaskParameter",
                    parameter.Name,
                    _taskName
                );

                // grab the outputs from the task's designated output parameter (which is a .NET property)
                Type type = parameter.PropertyType;

                if (TaskParameterTypeVerifier.IsAssignableToITask(type))
                {
                    ITaskItem[] outputs = GetItemOutputs(parameter, parameterLocation);
                    GatherTaskItemOutputs(outputTargetIsItem, outputTargetName, outputs, parameterLocation);
                }
                else if (TaskParameterTypeVerifier.IsValueTypeOutputParameter(type))
                {
                    string[] outputs = GetValueOutputs(parameter, parameterLocation);
                    GatherArrayStringAndValueOutputs(outputTargetIsItem, outputTargetName, parameter, outputs, parameterLocation);
                }
                else
                {
                    ProjectErrorUtilities.VerifyThrowInvalidProject
                    (
                        false,
                        parameterLocation,
                        "UnsupportedTaskParameterTypeError",
                        parameter.PropertyType.FullName,
                        parameter.Name,
                        _taskName
                    );
                }
            }
            catch (InvalidOperationException e)
            {
                // handle invalid TaskItems in task outputs
                _targetLoggingContext.LogError
                (
                    new BuildEventFileInfo(parameterLocation),
                    "InvalidTaskItemsInTaskOutputs",
                    _taskName,
                    parameterName,
                    e.Message
                );

                gatheredGeneratedOutputsSuccessfully = false;
            }
            catch (TargetInvocationException e)
            {
                // handle any exception thrown by the task's getter
                // Exception thrown by the called code itself
                // Log the stack, so the task vendor can fix their code
                // Log the task line number, whatever the value of ContinueOnError;
                // because this will be a hard error anyway.
                _targetLoggingContext.LogFatalTaskError
                (
                    new BuildEventFileInfo(parameterLocation),
                    e.InnerException,
                    _taskName
                );

                // We do not recover from a task exception while getting outputs,
                // so do not merely set gatheredGeneratedOutputsSuccessfully = false; here
                ProjectErrorUtilities.VerifyThrowInvalidProject
                (
                    false,
                    parameterLocation,
                    "FailedToRetrieveTaskOutputs",
                    _taskName,
                    parameterName,
                    e.InnerException.Message
                );
            }
            catch (Exception e)
            {
                // Catching Exception, but rethrowing unless it's a well-known exception.
                if (ExceptionHandling.NotExpectedReflectionException(e))
                {
                    throw;
                }

                ProjectErrorUtilities.VerifyThrowInvalidProject
                (
                    false,
                    parameterLocation,
                    "FailedToRetrieveTaskOutputs",
                    _taskName,
                    parameterName,
                    e.Message
                );
            }

            return gatheredGeneratedOutputsSuccessfully;
        }
Beispiel #31
0
        private void Parse()
        {
            // XML guarantees exactly one root element
            XmlElementWithLocation element = _document.DocumentElement as XmlElementWithLocation;

            if (element is null)
            {
                ProjectErrorUtilities.ThrowInvalidProject(ElementLocation.Create(_document.FullPath), "NoRootProjectElement", XMakeElements.project);
            }
            ProjectErrorUtilities.VerifyThrowInvalidProject(element.Name != XMakeElements.visualStudioProject, element.Location, "ProjectUpgradeNeeded", _project.FullPath);
            ProjectErrorUtilities.VerifyThrowInvalidProject(element.LocalName == XMakeElements.project, element.Location, "UnrecognizedElement", element.Name);

            // If a namespace was specified it must be the default MSBuild namespace.
            if (!ProjectXmlUtilities.VerifyValidProjectNamespace(element))
            {
                ProjectErrorUtilities.ThrowInvalidProject(element.Location, "ProjectMustBeInMSBuildXmlNamespace",
                                                          XMakeAttributes.defaultXmlNamespace);
            }
            else
            {
                _project.XmlNamespace = element.NamespaceURI;
            }

            // Historically, we allow any attribute on the Project element

            // The element wasn't available to the ProjectRootElement constructor so we have to set it now
            _project.SetProjectRootElementFromParser(element, _project);

            foreach (XmlElementWithLocation childElement in ProjectXmlUtilities.GetVerifyThrowProjectChildElements(element))
            {
                switch (childElement.Name)
                {
                case XMakeElements.propertyGroup:
                    _project.AppendParentedChildNoChecks(ParseProjectPropertyGroupElement(childElement, _project));
                    break;

                case XMakeElements.itemGroup:
                    _project.AppendParentedChildNoChecks(ParseProjectItemGroupElement(childElement, _project));
                    break;

                case XMakeElements.importGroup:
                    _project.AppendParentedChildNoChecks(ParseProjectImportGroupElement(childElement, _project));
                    break;

                case XMakeElements.import:
                    _project.AppendParentedChildNoChecks(ParseProjectImportElement(childElement, _project));
                    break;

                case XMakeElements.usingTask:
                    _project.AppendParentedChildNoChecks(ParseProjectUsingTaskElement(childElement));
                    break;

                case XMakeElements.target:
                    _project.AppendParentedChildNoChecks(ParseProjectTargetElement(childElement));
                    break;

                case XMakeElements.itemDefinitionGroup:
                    _project.AppendParentedChildNoChecks(ParseProjectItemDefinitionGroupElement(childElement));
                    break;

                case XMakeElements.choose:
                    _project.AppendParentedChildNoChecks(ParseProjectChooseElement(childElement, _project, 0 /* nesting depth */));
                    break;

                case XMakeElements.projectExtensions:
                    _project.AppendParentedChildNoChecks(ParseProjectExtensionsElement(childElement));
                    break;

                case XMakeElements.sdk:
                    _project.AppendParentedChildNoChecks(ParseProjectSdkElement(childElement));
                    break;

                // Obsolete
                case XMakeElements.error:
                case XMakeElements.warning:
                case XMakeElements.message:
                    ProjectErrorUtilities.ThrowInvalidProject(childElement.Location, "ErrorWarningMessageNotSupported", childElement.Name);
                    break;

                default:
                    ProjectXmlUtilities.ThrowProjectInvalidChildElement(childElement.Name, childElement.ParentNode.Name, childElement.Location);
                    break;
                }
            }
        }