Пример #1
0
        /// <summary>
        /// Pushes the list of targets specified onto the target stack in reverse order specified, so that
        /// they will be built in the order specified.
        /// </summary>
        /// <param name="targets">List of targets to build.</param>
        /// <param name="parentTargetEntry">The target which should be considered the parent of these targets.</param>
        /// <param name="baseLookup">The lookup to be used to build these targets.</param>
        /// <param name="addAsErrorTarget">True if this should be considered an error target.</param>
        /// <param name="stopProcessingOnCompletion">True if target stack processing should terminate when the last target in the list is processed.</param>
        /// <param name="buildReason">The reason the target is being built by the parent.</param>
        /// <returns>True if we actually pushed any targets, false otherwise.</returns>
        private async Task <bool> PushTargets(IList <TargetSpecification> targets, TargetEntry parentTargetEntry, Lookup baseLookup, bool addAsErrorTarget, bool stopProcessingOnCompletion, TargetBuiltReason buildReason)
        {
            List <TargetEntry> targetsToPush = new List <TargetEntry>(targets.Count);

            // Iterate the list in reverse order so that the first target in the list is the last pushed, and thus the first to be executed.
            for (int i = targets.Count - 1; i >= 0; i--)
            {
                TargetSpecification targetSpecification = targets[i];

                if (buildReason == TargetBuiltReason.BeforeTargets || buildReason == TargetBuiltReason.AfterTargets)
                {
                    // Don't build any Before or After targets for which we already have results.  Unlike other targets,
                    // we don't explicitly log a skipped-with-results message because it is not interesting.
                    if (_buildResult.HasResultsForTarget(targetSpecification.TargetName))
                    {
                        if (_buildResult[targetSpecification.TargetName].ResultCode != TargetResultCode.Skipped)
                        {
                            continue;
                        }
                    }
                }

                ElementLocation targetLocation = targetSpecification.ReferenceLocation;

                // See if this target is already building under a different build request.  If so, we need to wait.
                int idOfAlreadyBuildingRequest = BuildRequest.InvalidGlobalRequestId;
                if (_requestEntry.RequestConfiguration.ActivelyBuildingTargets.TryGetValue(targetSpecification.TargetName, out idOfAlreadyBuildingRequest))
                {
                    if (idOfAlreadyBuildingRequest != _requestEntry.Request.GlobalRequestId)
                    {
                        // Another request elsewhere is building it.  We need to wait.
                        await _requestBuilderCallback.BlockOnTargetInProgress(idOfAlreadyBuildingRequest, targetSpecification.TargetName, null);

                        // If we come out of here and the target is *still* active, it means the scheduler detected a circular dependency and told us to
                        // continue so we could throw the exception.
                        if (_requestEntry.RequestConfiguration.ActivelyBuildingTargets.ContainsKey(targetSpecification.TargetName))
                        {
                            ProjectErrorUtilities.ThrowInvalidProject(targetLocation, "CircularDependency", targetSpecification.TargetName);
                        }
                    }
                    else
                    {
                        if (buildReason == TargetBuiltReason.AfterTargets)
                        {
                            // If the target we are pushing is supposed to run after the current target and it is already set to run after us then skip adding it now.
                            continue;
                        }

                        // We are already building this target on this request. That's a circular dependency.
                        ProjectErrorUtilities.ThrowInvalidProject(targetLocation, "CircularDependency", targetSpecification.TargetName);
                    }
                }
                else
                {
                    // Does this target exist in our direct parent chain, if it is a before target (since these can cause circular dependency issues)
                    if (buildReason == TargetBuiltReason.BeforeTargets || buildReason == TargetBuiltReason.DependsOn || buildReason == TargetBuiltReason.None)
                    {
                        if (HasCircularDependenceInTargets(parentTargetEntry, targetSpecification, out List <string> targetDependenceChain))
                        {
                            ProjectErrorUtilities.ThrowInvalidProject(targetLocation, "CircularDependencyInTargetGraph", targetSpecification.TargetName, parentTargetEntry.Name, buildReason, targetSpecification.TargetName, string.Join("<-", targetDependenceChain));
                        }
                    }
                    else
                    {
                        // For an after target, if it is already ANYWHERE on the stack, we don't need to push it because it is already going to run
                        // after whatever target is causing it to be pushed now.
                        bool alreadyPushed = false;
                        foreach (TargetEntry entry in _targetsToBuild)
                        {
                            if (String.Equals(entry.Name, targetSpecification.TargetName, StringComparison.OrdinalIgnoreCase))
                            {
                                alreadyPushed = true;
                                break;
                            }
                        }

                        if (alreadyPushed)
                        {
                            continue;
                        }
                    }
                }

                // Add to the list of targets to push.  We don't actually put it on the stack here because we could run into a circular dependency
                // during this loop, in which case the target stack would be out of whack.
                TargetEntry newEntry = new TargetEntry(_requestEntry, this as ITargetBuilderCallback, targetSpecification, baseLookup, parentTargetEntry, buildReason, _componentHost, stopProcessingOnCompletion);
                newEntry.ErrorTarget = addAsErrorTarget;
                targetsToPush.Add(newEntry);
                stopProcessingOnCompletion = false; // The first target on the stack (the last one to be run) always inherits the stopProcessing flag.
            }

            // Now push the targets since this operation cannot fail.
            foreach (TargetEntry targetToPush in targetsToPush)
            {
                _targetsToBuild.Push(targetToPush);
            }

            bool pushedTargets = (targetsToPush.Count > 0);

            return(pushedTargets);
        }
Пример #2
0
Файл: Target.cs Проект: 3F/IeXod
        /// <summary>
        /// Builds this target if it has not already been built as part of its parent project. Before we actually execute the
        /// tasks for this target, though, we first call on all the dependent targets to build themselves.
        /// This function may throw InvalidProjectFileException
        /// </summary>
        internal void Build
        (
            ProjectBuildState buildContext
        )
        {
            // Depending on the build state, we may do different things.
            switch (buildState)
            {
            case BuildState.InProgress:
                // In single proc mode if the build state was already "in progress"
                // and somebody just told us to build ourselves, it means that there is
                // a loop (circular dependency) in the target dependency graph. In multi
                // proc mode we need to analyze the dependency graph before we can
                // tell if there a circular dependency or if two independent chains
                // of targets happen to need the result of this target.
                if (parentEngine.Router.SingleThreadedMode || buildContext.ContainsCycle(this.Name))
                {
                    ProjectErrorUtilities.VerifyThrowInvalidProject(false, TargetElement, "CircularDependency", targetName);
                }
                else
                {
                    buildContext.CurrentBuildContextState = ProjectBuildState.BuildContextState.WaitingForTarget;
                    this.executionState.AddWaitingBuildContext(buildContext);
                }
                break;

            case BuildState.CompletedSuccessfully:
            case BuildState.CompletedUnsuccessfully:
                // If this target has already been built as part of this project,
                // we're not going to build it again.  Just return the result
                // from when it was built previously.  Note:  This condition
                // could really only ever hold true if the user specifically
                // requested us to build multiple targets and there existed
                // a direct or indirect dependency relationship between two or
                // more of those top-level targets.
                // Note: we aren't really entering the target in question here, so don't use the target
                // event context. Using the target ID for skipped messages would force us to
                // cache the individual target IDs for unloaded projects and it's not really worth the trouble.
                // Just use the parent event context.
                parentEngine.LoggingServices.LogComment(buildContext.ProjectBuildEventContext,
                                                        ((buildState == BuildState.CompletedSuccessfully) ? "TargetAlreadyCompleteSuccess" : "TargetAlreadyCompleteFailure"),
                                                        this.targetName);

                // Only contexts which are generated from an MSBuild task could need
                // the outputs of this target, such contexts have a non-null evaluation
                // request
                if ((buildState == BuildState.CompletedSuccessfully) &&
                    (buildContext.BuildRequest.OutputsByTarget != null &&
                     buildContext.NameOfBlockingTarget == null))
                {
                    error.VerifyThrow(
                        String.Compare(EscapingUtilities.UnescapeAll(buildContext.NameOfTargetInProgress), this.Name, StringComparison.OrdinalIgnoreCase) == 0,
                        "The name of the target in progress is inconsistent with the target being built");

                    error.VerifyThrow(targetOutputItems != null,
                                      "If the target built successfully, we must have its outputs.");

                    buildContext.BuildRequest.OutputsByTarget[Name] = targetOutputItems.ToArray();
                }

                if (buildContext.NameOfBlockingTarget == null)
                {
                    buildContext.BuildRequest.ResultByTarget[Name] = buildState;
                }
                break;

            case BuildState.NotStarted:
            case BuildState.Skipped:
            {
                // Always have to create a new context in build as other projects or targets may try and build this target
                BuildEventContext targetBuildEventContext = new BuildEventContext
                                                            (
                    buildContext.ProjectBuildEventContext.NodeId,
                    this.id,
                    buildContext.ProjectBuildEventContext.ProjectContextId,
                    buildContext.ProjectBuildEventContext.TaskId
                                                            );

                Expander expander = new Expander(this.parentProject.evaluatedProperties, this.parentProject.evaluatedItemsByName);

                // We first make sure no batching was attempted with the target's condition.
                if (!conditionCheckedForInvalidMetadataReferences)
                {
                    if (ExpressionShredder.ContainsMetadataExpressionOutsideTransform(this.Condition))
                    {
                        ProjectErrorUtilities.ThrowInvalidProject(this.conditionAttribute, "TargetConditionHasInvalidMetadataReference", targetName, this.Condition);
                    }
                    conditionCheckedForInvalidMetadataReferences = true;
                }

                // If condition is false (based on propertyBag), set this target's state to
                // "Skipped" since we won't actually build it.
                if (!Utilities.EvaluateCondition(this.Condition, this.conditionAttribute,
                                                 expander, null, ParserOptions.AllowProperties | ParserOptions.AllowItemLists,
                                                 parentEngine.LoggingServices, targetBuildEventContext))
                {
                    buildState = BuildState.Skipped;

                    if (buildContext.NameOfBlockingTarget == null)
                    {
                        buildContext.BuildRequest.ResultByTarget[Name] = buildState;
                    }

                    if (!parentEngine.LoggingServices.OnlyLogCriticalEvents)
                    {
                        // Expand the expression for the Log.
                        string expanded = expander.ExpandAllIntoString(this.Condition, this.conditionAttribute);
                        // By design: Not building dependencies. This is what NAnt does too.
                        parentEngine.LoggingServices.LogComment(targetBuildEventContext, "TargetSkippedFalseCondition",
                                                                this.targetName, this.Condition, expanded);
                    }
                }
                else
                {
                    // This target has not been built yet.  So build it!
                    // Change our state to "in progress". TargetParameters will need to be re-calculated if Inputs and Outputs attribute has changed.
                    buildState = BuildState.InProgress;
                    List <string> batchableTargetParameters = GetBatchableTargetParameters();
                    executionState = new TargetExecutionWrapper(this, taskElementList, batchableTargetParameters, targetElement, expander, targetBuildEventContext);
                    ContinueBuild(buildContext, null);
                }
            }
            break;

            default:
                error.VerifyThrow(false, "Build state {0} not handled in Target.Build method", buildState);
                break;
            }
        }
Пример #3
0
        /// <summary>
        /// Retrieves the list of dependencies this target needs to have built and moves the target to the next state.
        /// Never returns null.
        /// </summary>
        /// <returns>A collection of targets on which this target depends.</returns>
        internal List <TargetSpecification> GetDependencies(ProjectLoggingContext projectLoggingContext)
        {
            VerifyState(_state, TargetEntryState.Dependencies);

            // Resolve the target now, since from this point on we are going to be doing work with the actual instance.
            GetTargetInstance();

            // We first make sure no batching was attempted with the target's condition.
            // UNDONE: (Improvement) We want to allow this actually.  In order to do this we need to determine what the
            // batching buckets are, and if there are any which aren't empty, return our list of dependencies.
            // Only in the case where all bucket conditions fail do we want to skip the target entirely (and
            // this skip building the dependencies.)
            if (ExpressionShredder.ContainsMetadataExpressionOutsideTransform(_target.Condition))
            {
                ProjectErrorUtilities.ThrowInvalidProject(_target.ConditionLocation, "TargetConditionHasInvalidMetadataReference", _target.Name, _target.Condition);
            }

            // If condition is false (based on propertyBag), set this target's state to
            // "Skipped" since we won't actually build it.
            bool condition = ConditionEvaluator.EvaluateCondition
                             (
                _target.Condition,
                ParserOptions.AllowPropertiesAndItemLists,
                _expander,
                ExpanderOptions.ExpandPropertiesAndItems,
                _requestEntry.ProjectRootDirectory,
                _target.ConditionLocation,
                projectLoggingContext.LoggingService,
                projectLoggingContext.BuildEventContext,
                FileSystems.Default);

            if (!condition)
            {
                _targetResult = new TargetResult(Array.Empty <TaskItem>(), new WorkUnitResult(WorkUnitResultCode.Skipped, WorkUnitActionCode.Continue, null));
                _state        = TargetEntryState.Completed;

                if (!projectLoggingContext.LoggingService.OnlyLogCriticalEvents)
                {
                    // Expand the expression for the Log.  Since we know the condition evaluated to false, leave unexpandable properties in the condition so as not to cause an error
                    string expanded = _expander.ExpandIntoStringAndUnescape(_target.Condition, ExpanderOptions.ExpandPropertiesAndItems | ExpanderOptions.LeavePropertiesUnexpandedOnError, _target.ConditionLocation);

                    // By design: Not building dependencies. This is what NAnt does too.
                    // NOTE: In the original code, this was logged from the target logging context.  However, the target
                    // hadn't been "started" by then, so you'd get a target message outside the context of a started
                    // target.  In the Task builder (and original Task Engine), a Task Skipped message would be logged in
                    // the context of the target, not the task.  This should be the same, especially given that we
                    // wish to allow batching on the condition of a target.
                    var skippedTargetEventArgs = new TargetSkippedEventArgs(
                        ResourceUtilities.GetResourceString("TargetSkippedFalseCondition"),
                        _target.Name,
                        _target.Condition,
                        expanded)
                    {
                        BuildEventContext = projectLoggingContext.BuildEventContext,
                        TargetName        = _target.Name,
                        TargetFile        = _target.Location.File,
                        ParentTarget      = ParentEntry?.Target?.Name,
                        BuildReason       = BuildReason
                    };

                    projectLoggingContext.LogBuildEvent(skippedTargetEventArgs);
                }

                return(new List <TargetSpecification>());
            }

            var dependencies = _expander.ExpandIntoStringListLeaveEscaped(_target.DependsOnTargets, ExpanderOptions.ExpandPropertiesAndItems, _target.DependsOnTargetsLocation);
            List <TargetSpecification> dependencyTargets = new List <TargetSpecification>();

            foreach (string escapedDependency in dependencies)
            {
                string dependencyTargetName = EscapingUtilities.UnescapeAll(escapedDependency);
                dependencyTargets.Add(new TargetSpecification(dependencyTargetName, _target.DependsOnTargetsLocation));
            }

            _state = TargetEntryState.Execution;

            return(dependencyTargets);
        }
Пример #4
0
        /// <summary>
        /// Parse a ProjectTargetElement
        /// </summary>
        private ProjectTargetElement ParseProjectTargetElement(XmlElementWithLocation element)
        {
            ProjectXmlUtilities.VerifyThrowProjectAttributes(element, s_validAttributesOnTarget);
            ProjectXmlUtilities.VerifyThrowProjectRequiredAttribute(element, XMakeAttributes.name);

            string targetName = ProjectXmlUtilities.GetAttributeValue(element, XMakeAttributes.name);

            // Orcas compat: all target names are automatically unescaped
            targetName = EscapingUtilities.UnescapeAll(targetName);

            int indexOfSpecialCharacter = targetName.IndexOfAny(XMakeElements.illegalTargetNameCharacters);

            if (indexOfSpecialCharacter >= 0)
            {
                ProjectErrorUtilities.ThrowInvalidProject(element.GetAttributeLocation(XMakeAttributes.name), "NameInvalid", targetName, targetName[indexOfSpecialCharacter]);
            }

            ProjectTargetElement  target  = new ProjectTargetElement(element, _project, _project);
            ProjectOnErrorElement onError = null;

            foreach (XmlElementWithLocation childElement in ProjectXmlUtilities.GetVerifyThrowProjectChildElements(element))
            {
                ProjectElement child = null;

                switch (childElement.Name)
                {
                case XMakeElements.propertyGroup:
                    if (onError != null)
                    {
                        ProjectErrorUtilities.ThrowInvalidProject(onError.Location, "NodeMustBeLastUnderElement", XMakeElements.onError, XMakeElements.target, childElement.Name);
                    }

                    child = ParseProjectPropertyGroupElement(childElement, target);
                    break;

                case XMakeElements.itemGroup:
                    if (onError != null)
                    {
                        ProjectErrorUtilities.ThrowInvalidProject(onError.Location, "NodeMustBeLastUnderElement", XMakeElements.onError, XMakeElements.target, childElement.Name);
                    }

                    child = ParseProjectItemGroupElement(childElement, target);
                    break;

                case XMakeElements.onError:
                    onError = ParseProjectOnErrorElement(childElement, target);
                    child   = onError;
                    break;

                case XMakeElements.itemDefinitionGroup:
                    ProjectErrorUtilities.ThrowInvalidProject(childElement.Location, "ItemDefinitionGroupNotLegalInsideTarget", childElement.Name);
                    break;

                default:
                    if (onError != null)
                    {
                        ProjectErrorUtilities.ThrowInvalidProject(onError.Location, "NodeMustBeLastUnderElement", XMakeElements.onError, XMakeElements.target, childElement.Name);
                    }

                    child = ParseProjectTaskElement(childElement, target);
                    break;
                }

                target.AppendParentedChildNoChecks(child);
            }

            return(target);
        }
Пример #5
0
        /// <summary>
        /// Parse a ProjectItemElement
        /// </summary>
        private ProjectItemElement ParseProjectItemElement(XmlElementWithLocation element, ProjectItemGroupElement parent)
        {
            bool belowTarget = parent.Parent is ProjectTargetElement;

            string itemType = element.Name;
            string include  = element.GetAttribute(XMakeAttributes.include);
            string exclude  = element.GetAttribute(XMakeAttributes.exclude);
            string remove   = element.GetAttribute(XMakeAttributes.remove);
            string update   = element.GetAttribute(XMakeAttributes.update);

            var exclusiveItemOperation  = "";
            int exclusiveAttributeCount = 0;

            if (element.HasAttribute(XMakeAttributes.include))
            {
                exclusiveAttributeCount++;
                exclusiveItemOperation = XMakeAttributes.include;
            }
            if (element.HasAttribute(XMakeAttributes.remove))
            {
                exclusiveAttributeCount++;
                exclusiveItemOperation = XMakeAttributes.remove;
            }
            if (element.HasAttribute(XMakeAttributes.update))
            {
                exclusiveAttributeCount++;
                exclusiveItemOperation = XMakeAttributes.update;
            }

            //  At most one of the include, remove, or update attributes may be specified
            if (exclusiveAttributeCount > 1)
            {
                XmlAttributeWithLocation errorAttribute = remove.Length > 0 ? (XmlAttributeWithLocation)element.Attributes[XMakeAttributes.remove] : (XmlAttributeWithLocation)element.Attributes[XMakeAttributes.update];
                ProjectErrorUtilities.ThrowInvalidProject(errorAttribute.Location, "InvalidAttributeExclusive");
            }

            // Include, remove, or update must be present unless inside a target
            ProjectErrorUtilities.VerifyThrowInvalidProject(exclusiveAttributeCount == 1 || belowTarget, element.Location, "IncludeRemoveOrUpdate", exclusiveItemOperation, itemType);

            // Exclude must be missing, unless Include exists
            ProjectXmlUtilities.VerifyThrowProjectInvalidAttribute(exclude.Length == 0 || include.Length > 0, (XmlAttributeWithLocation)element.Attributes[XMakeAttributes.exclude]);

            // If we have an Include attribute at all, it must have non-zero length
            ProjectErrorUtilities.VerifyThrowInvalidProject(include.Length > 0 || element.Attributes[XMakeAttributes.include] == null, element.Location, "MissingRequiredAttribute", XMakeAttributes.include, itemType);

            // If we have a Remove attribute at all, it must have non-zero length
            ProjectErrorUtilities.VerifyThrowInvalidProject(remove.Length > 0 || element.Attributes[XMakeAttributes.remove] == null, element.Location, "MissingRequiredAttribute", XMakeAttributes.remove, itemType);

            // If we have an Update attribute at all, it must have non-zero length
            ProjectErrorUtilities.VerifyThrowInvalidProject(update.Length > 0 || element.Attributes[XMakeAttributes.update] == null, element.Location, "MissingRequiredAttribute", XMakeAttributes.update, itemType);

            XmlUtilities.VerifyThrowProjectValidElementName(element);
            ProjectErrorUtilities.VerifyThrowInvalidProject(XMakeElements.IllegalItemPropertyNames[itemType] == null, element.Location, "CannotModifyReservedItem", itemType);

            ProjectItemElement item = new ProjectItemElement(element, parent, _project);

            foreach (XmlAttributeWithLocation attribute in element.Attributes)
            {
                bool isKnownAttribute;
                bool isValidMetadataNameInAttribute;

                CheckMetadataAsAttributeName(attribute.Name, out isKnownAttribute, out isValidMetadataNameInAttribute);

                if (!isKnownAttribute && !isValidMetadataNameInAttribute)
                {
                    ProjectXmlUtilities.ThrowProjectInvalidAttribute(attribute);
                }
                else if (isValidMetadataNameInAttribute)
                {
                    ProjectMetadataElement metadatum = _project.CreateMetadataElement(attribute.Name, attribute.Value);
                    metadatum.ExpressedAsAttribute = true;
                    metadatum.Parent = item;

                    item.AppendParentedChildNoChecks(metadatum);
                }
            }

            foreach (XmlElementWithLocation childElement in ProjectXmlUtilities.GetVerifyThrowProjectChildElements(element))
            {
                ProjectMetadataElement metadatum = ParseProjectMetadataElement(childElement, item);

                item.AppendParentedChildNoChecks(metadatum);
            }

            return(item);
        }
Пример #6
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;
                }
            }
        }
Пример #7
0
        internal BuildItemGroupChildXml(XmlElement element, ChildType childTypeExpected)
        {
            ErrorUtilities.VerifyThrow(element != null, "Need an XML node.");
            ErrorUtilities.VerifyThrowNoAssert(childTypeExpected != ChildType.Invalid, "Can't expect invalid childtype");
            ProjectXmlUtilities.VerifyThrowProjectValidNameAndNamespace(element);

            this.element = element;

            // Loop through each of the attributes on the item element.
            foreach (XmlAttribute attribute in element.Attributes)
            {
                switch (attribute.Name)
                {
                case XMakeAttributes.include:
                    this.includeAttribute = attribute;
                    break;

                case XMakeAttributes.exclude:
                    this.excludeAttribute = attribute;
                    break;

                case XMakeAttributes.condition:
                    this.conditionAttribute = attribute;
                    break;

                case XMakeAttributes.xmlns:
                    // We already verified that the namespace is correct
                    break;

                case XMakeAttributes.remove:
                    this.removeAttribute = attribute;
                    break;

                case XMakeAttributes.keepMetadata:
                case XMakeAttributes.removeMetadata:
                case XMakeAttributes.keepDuplicates:
                    // Ignore these - they are part of the new OM.
                    break;

                default:
                    ProjectXmlUtilities.ThrowProjectInvalidAttribute(attribute);
                    break;
                }
            }

            this.childType = ChildType.Invalid;

            // Default to modify, if that's one of the child types we are told to expect.
            if ((childTypeExpected & ChildType.BuildItemModify) == ChildType.BuildItemModify)
            {
                this.childType = ChildType.BuildItemModify;
            }

            if (this.includeAttribute != null)
            {
                ProjectXmlUtilities.VerifyThrowProjectInvalidAttribute((childTypeExpected & ChildType.BuildItemAdd) == ChildType.BuildItemAdd, includeAttribute);
                ProjectErrorUtilities.VerifyThrowInvalidProject(Include.Length > 0, element, "MissingRequiredAttribute", XMakeAttributes.include, element.Name);
                ProjectXmlUtilities.VerifyThrowProjectInvalidAttribute(removeAttribute == null, removeAttribute);
                this.childType = ChildType.BuildItemAdd;
            }

            if (this.excludeAttribute != null)
            {
                ProjectXmlUtilities.VerifyThrowProjectInvalidAttribute((childTypeExpected & ChildType.BuildItemAdd) == ChildType.BuildItemAdd, excludeAttribute);
                ProjectErrorUtilities.VerifyThrowInvalidProject(Include.Length > 0, element, "MissingRequiredAttribute", XMakeAttributes.include, element.Name);
                ProjectXmlUtilities.VerifyThrowProjectInvalidAttribute(removeAttribute == null, removeAttribute);
                this.childType = ChildType.BuildItemAdd;
            }

            if (this.removeAttribute != null)
            {
                ProjectXmlUtilities.VerifyThrowProjectInvalidAttribute((childTypeExpected & ChildType.BuildItemRemove) == ChildType.BuildItemRemove, removeAttribute);
                ProjectErrorUtilities.VerifyThrowInvalidProject(Remove.Length > 0, element, "MissingRequiredAttribute", XMakeAttributes.remove, element.Name);
                ProjectXmlUtilities.VerifyThrowProjectInvalidAttribute(includeAttribute == null, includeAttribute);
                ProjectXmlUtilities.VerifyThrowProjectInvalidAttribute(excludeAttribute == null, excludeAttribute);
                this.childType = ChildType.BuildItemRemove;
            }

            if (this.childType == ChildType.Invalid)
            {
                // So the xml wasn't consistent with any of the child types that we were told to expect.
                // Figure out the most reasonable message to produce.
                if ((childTypeExpected & ChildType.BuildItemAdd) == ChildType.BuildItemAdd)
                {
                    ProjectErrorUtilities.VerifyThrowInvalidProject(Include.Length > 0, element, "MissingRequiredAttribute", XMakeAttributes.include, element.Name);
                }
                else if ((childTypeExpected & ChildType.BuildItemRemove) == ChildType.BuildItemRemove)
                {
                    ProjectErrorUtilities.VerifyThrowInvalidProject(Remove.Length > 0, element, "MissingRequiredAttribute", XMakeAttributes.remove, element.Name);
                }
                else
                {
                    ErrorUtilities.ThrowInternalError("Unexpected child type");
                }
            }

            // Validate each of the child nodes beneath the item.
            List <XmlElement> children = ProjectXmlUtilities.GetValidChildElements(element);

            if (this.childType == ChildType.BuildItemRemove && children.Count != 0)
            {
                ProjectErrorUtilities.ThrowInvalidProject(element, "ChildElementsBelowRemoveNotAllowed", children[0].Name);
            }

            foreach (XmlElement child in children)
            {
                ProjectXmlUtilities.VerifyThrowProjectValidNameAndNamespace(child);

                ProjectErrorUtilities.VerifyThrowInvalidProject(!FileUtilities.IsItemSpecModifier(child.Name), child, "ItemSpecModifierCannotBeCustomMetadata", child.Name);
                ProjectErrorUtilities.VerifyThrowInvalidProject(XMakeElements.IllegalItemPropertyNames[child.Name] == null, child, "CannotModifyReservedItemMetadata", child.Name);
            }
        }
Пример #8
0
        /// <summary>
        /// Initialize the factory from the task registry
        /// </summary>
        internal LoadedType InitializeFactory
        (
            AssemblyLoadInfo loadInfo,
            string taskName,
            IDictionary <string, TaskPropertyInfo> taskParameters,
            string taskElementContents,
            IDictionary <string, string> taskFactoryIdentityParameters,
            bool taskHostFactoryExplicitlyRequested,
            TargetLoggingContext targetLoggingContext,
            ElementLocation elementLocation,
            string taskProjectFile
        )
        {
            ErrorUtilities.VerifyThrowArgumentNull(loadInfo, nameof(loadInfo));
            VerifyThrowIdentityParametersValid(taskFactoryIdentityParameters, elementLocation, taskName, "Runtime", "Architecture");

            if (taskFactoryIdentityParameters != null)
            {
                _factoryIdentityParameters = new Dictionary <string, string>(taskFactoryIdentityParameters, StringComparer.OrdinalIgnoreCase);
            }

            _taskHostFactoryExplicitlyRequested = taskHostFactoryExplicitlyRequested;

            try
            {
                ErrorUtilities.VerifyThrowArgumentLength(taskName, nameof(taskName));
                _taskName   = taskName;
                _loadedType = _typeLoader.Load(taskName, loadInfo);
                ProjectErrorUtilities.VerifyThrowInvalidProject(_loadedType != null, elementLocation, "TaskLoadFailure", taskName, loadInfo.AssemblyLocation, String.Empty);
            }
            catch (TargetInvocationException e)
            {
                // Exception thrown by the called code itself
                // Log the stack, so the task vendor can fix their code
                ProjectErrorUtilities.ThrowInvalidProject(elementLocation, "TaskLoadFailure", taskName, loadInfo.AssemblyLocation, Environment.NewLine + e.InnerException.ToString());
            }
            catch (ReflectionTypeLoadException e)
            {
                // ReflectionTypeLoadException.LoaderExceptions may contain nulls
                foreach (Exception exception in e.LoaderExceptions)
                {
                    if (exception != null)
                    {
                        targetLoggingContext.LogError(new BuildEventFileInfo(taskProjectFile), "TaskLoadFailure", taskName, loadInfo.AssemblyLocation, exception.Message);
                    }
                }

                ProjectErrorUtilities.ThrowInvalidProject(elementLocation, "TaskLoadFailure", taskName, loadInfo.AssemblyLocation, e.Message);
            }
            catch (ArgumentNullException e)
            {
                // taskName may be null
                ProjectErrorUtilities.ThrowInvalidProject(elementLocation, "TaskLoadFailure", taskName, loadInfo.AssemblyLocation, e.Message);
            }
            catch (Exception e) // Catching Exception, but rethrowing unless it's a well-known exception.
            {
                if (ExceptionHandling.NotExpectedReflectionException(e))
                {
                    throw;
                }

                ProjectErrorUtilities.ThrowInvalidProject(elementLocation, "TaskLoadFailure", taskName, loadInfo.AssemblyLocation, e.Message);
            }

            return(_loadedType);
        }