public void TestAttributeMethods() { Assert.IsFalse(XMakeAttributes.IsSpecialTaskAttribute("NotAnAttribute")); Assert.IsTrue(XMakeAttributes.IsSpecialTaskAttribute(XMakeAttributes.xmlns)); Assert.IsTrue(XMakeAttributes.IsSpecialTaskAttribute(XMakeAttributes.continueOnError)); Assert.IsTrue(XMakeAttributes.IsSpecialTaskAttribute(XMakeAttributes.condition)); Assert.IsTrue(XMakeAttributes.IsSpecialTaskAttribute(XMakeAttributes.msbuildArchitecture)); Assert.IsTrue(XMakeAttributes.IsSpecialTaskAttribute(XMakeAttributes.msbuildRuntime)); Assert.IsFalse(XMakeAttributes.IsBadlyCasedSpecialTaskAttribute("NotAnAttribute")); Assert.IsFalse(XMakeAttributes.IsBadlyCasedSpecialTaskAttribute(XMakeAttributes.include)); Assert.IsFalse(XMakeAttributes.IsBadlyCasedSpecialTaskAttribute(XMakeAttributes.continueOnError)); Assert.IsFalse(XMakeAttributes.IsBadlyCasedSpecialTaskAttribute(XMakeAttributes.condition)); Assert.IsFalse(XMakeAttributes.IsBadlyCasedSpecialTaskAttribute(XMakeAttributes.msbuildArchitecture)); Assert.IsFalse(XMakeAttributes.IsBadlyCasedSpecialTaskAttribute(XMakeAttributes.msbuildRuntime)); Assert.IsTrue(XMakeAttributes.IsBadlyCasedSpecialTaskAttribute("continueOnError")); Assert.IsTrue(XMakeAttributes.IsBadlyCasedSpecialTaskAttribute("condition")); Assert.IsTrue(XMakeAttributes.IsBadlyCasedSpecialTaskAttribute("MsbuildRuntime")); Assert.IsTrue(XMakeAttributes.IsBadlyCasedSpecialTaskAttribute("msbuildarchitecture")); Assert.IsFalse(XMakeAttributes.IsNonBatchingTargetAttribute("NotAnAttribute")); Assert.IsTrue(XMakeAttributes.IsNonBatchingTargetAttribute(XMakeAttributes.dependsOnTargets)); Assert.IsTrue(XMakeAttributes.IsNonBatchingTargetAttribute(XMakeAttributes.name)); Assert.IsTrue(XMakeAttributes.IsNonBatchingTargetAttribute(XMakeAttributes.condition)); }
/// <summary> /// Removes all parameters from the task. /// Does not remove any "special" parameters: ContinueOnError, Condition, etc. /// </summary> public void RemoveAllParameters() { if (Link != null) { TaskLink.RemoveAllParameters(); return; } lock (_locker) { _parameters = null; List <XmlAttribute> toRemove = null; // note this was a long standing bug in here (which would make this only work if there is no attributes to remove). // calling XmlElement.RemoveAttributeNode will cause foreach to throw ArgumentException (collection modified) foreach (XmlAttribute attribute in XmlElement.Attributes) { if (!XMakeAttributes.IsSpecialTaskAttribute(attribute.Name)) { toRemove = toRemove ?? new List <XmlAttribute>(); toRemove.Add(attribute); } } if (toRemove != null) { foreach (var attribute in toRemove) { XmlElement.RemoveAttributeNode(attribute); } MarkDirty("Remove all task parameters on {0}", Name); } } }
/// <summary> /// This constructor allows all output data to be initialized. /// </summary> /// <owner>SumedhK</owner> /// <param name="node">The XML element for the Output tag.</param> internal TaskOutput(XmlElement node) { ErrorUtilities.VerifyThrow(node != null, "Need the XML for the <Output> tag."); ProjectXmlUtilities.VerifyThrowProjectNoChildElements(node); int requiredData = 0; string taskName = node.ParentNode.Name; foreach (XmlAttribute outputAttribute in node.Attributes) { switch (outputAttribute.Name) { case XMakeAttributes.taskParameter: ProjectErrorUtilities.VerifyThrowInvalidProject(outputAttribute.Value.Length > 0, outputAttribute, "InvalidAttributeValue", outputAttribute.Value, outputAttribute.Name, XMakeElements.output); ProjectErrorUtilities.VerifyThrowInvalidProject(!XMakeAttributes.IsSpecialTaskAttribute(outputAttribute.Value) && !XMakeAttributes.IsBadlyCasedSpecialTaskAttribute(outputAttribute.Value), outputAttribute, "BadlyCasedSpecialTaskAttribute", outputAttribute.Value, taskName, taskName); this.taskParameterAttribute = outputAttribute; break; case XMakeAttributes.itemName: ProjectErrorUtilities.VerifyThrowInvalidProject(outputAttribute.Value.Length > 0, outputAttribute, "InvalidAttributeValue", outputAttribute.Value, outputAttribute.Name, XMakeElements.output); this.itemNameAttribute = outputAttribute; requiredData++; break; case XMakeAttributes.propertyName: ProjectErrorUtilities.VerifyThrowInvalidProject(outputAttribute.Value.Length > 0, outputAttribute, "InvalidAttributeValue", outputAttribute.Value, outputAttribute.Name, XMakeElements.output); ProjectErrorUtilities.VerifyThrowInvalidProject(!ReservedPropertyNames.IsReservedProperty(outputAttribute.Value), node, "CannotModifyReservedProperty", outputAttribute.Value); this.propertyNameAttribute = outputAttribute; requiredData++; break; case XMakeAttributes.condition: this.conditionAttribute = outputAttribute; break; default: ProjectXmlUtilities.ThrowProjectInvalidAttribute(outputAttribute); break; } } /* NOTE: * TaskParameter must be specified * either ItemName or PropertyName must be specified * if ItemName is specified, then PropertyName cannot be specified * if PropertyName is specified, then ItemName cannot be specified * only Condition is truly optional */ ProjectErrorUtilities.VerifyThrowInvalidProject((this.taskParameterAttribute != null) && (requiredData == 1), node, "InvalidTaskOutputSpecification", taskName); }
public void TestIsSpecialTaskAttribute() { Assert.False(XMakeAttributes.IsSpecialTaskAttribute("NotAnAttribute")); Assert.True(XMakeAttributes.IsSpecialTaskAttribute(XMakeAttributes.xmlns)); Assert.True(XMakeAttributes.IsSpecialTaskAttribute(XMakeAttributes.continueOnError)); Assert.True(XMakeAttributes.IsSpecialTaskAttribute(XMakeAttributes.condition)); Assert.True(XMakeAttributes.IsSpecialTaskAttribute(XMakeAttributes.msbuildArchitecture)); Assert.True(XMakeAttributes.IsSpecialTaskAttribute(XMakeAttributes.msbuildRuntime)); }
/// <summary> /// Adds (or modifies the value of) a parameter on this task /// </summary> public void SetParameter(string name, string unevaluatedValue) { lock (_locker) { ErrorUtilities.VerifyThrowArgumentLength(name, "name"); ErrorUtilities.VerifyThrowArgumentNull(unevaluatedValue, "unevaluatedValue"); ErrorUtilities.VerifyThrowArgument(!XMakeAttributes.IsSpecialTaskAttribute(name), "CannotAccessKnownAttributes", name); _parameters = null; XmlElement.SetAttribute(name, unevaluatedValue); MarkDirty("Set task parameter {0}", name); } }
/// <summary> /// Removes all parameters from the task. /// Does not remove any "special" parameters: ContinueOnError, Condition, etc. /// </summary> public void RemoveAllParameters() { lock (_locker) { _parameters = null; foreach (XmlAttribute attribute in XmlElement.Attributes) { if (!XMakeAttributes.IsSpecialTaskAttribute(attribute.Name)) { XmlElement.RemoveAttributeNode(attribute); } } MarkDirty("Remove all task parameters on {0}", Name); } }
/// <summary> /// This retrieves an arbitrary attribute from the task element. These /// are attributes that the project author has placed on the task element /// that have no meaning to MSBuild other than that they get passed to the /// task itself as arguments. /// </summary> /// <owner>RGoel</owner> public string GetParameterValue ( string attributeName ) { // You can only request the value of user-defined attributes. The well-known // ones, like "ContinueOnError" for example, are accessed through other means. error.VerifyThrowArgument(!XMakeAttributes.IsSpecialTaskAttribute(attributeName), "CannotAccessKnownAttributes", attributeName); error.VerifyThrowInvalidOperation(this.taskElement != null, "CannotUseParameters"); // If this is a persisted Task, grab the attribute directly from the // task element. return(taskElement.GetAttribute(attributeName) ?? string.Empty); }
/// <summary> /// Initialize parameters cache. /// Must be called within the lock. /// </summary> private void EnsureParametersInitialized() { if (_parameters == null) { _parameters = new CopyOnWriteDictionary <string, Tuple <string, ElementLocation> >(XmlElement.Attributes.Count, StringComparer.OrdinalIgnoreCase); foreach (XmlAttributeWithLocation attribute in XmlElement.Attributes) { if (!XMakeAttributes.IsSpecialTaskAttribute(attribute.Name)) { // By pulling off and caching the Location early here, it becomes frozen for the life of this object. // That means that if the name of the file is changed after first load (possibly from null) it will // remain the old value here. Correctly, this should cache the attribute not the location. Fixing // that will need profiling, though, as this cache was added for performance. _parameters[attribute.Name] = new Tuple <string, ElementLocation>(attribute.Value, attribute.Location); } } } }
/// <summary> /// This retrieves the list of all parameter names from the element /// node of this task. Note that it excludes anything that a specific /// property is exposed for or that isn't valid here (Name, Condition, /// ContinueOnError). /// /// Note that if there are none, it returns string[0], rather than null, /// as it makes writing foreach statements over the return value so /// much simpler. /// </summary> /// <returns></returns> /// <owner>rgoel</owner> public string[] GetParameterNames() { if (this.taskElement == null) { return(new string[0]); } ArrayList list = new ArrayList(); foreach (XmlAttribute attrib in this.taskElement.Attributes) { string attributeValue = attrib.Name; if (!XMakeAttributes.IsSpecialTaskAttribute(attributeValue)) { list.Add(attributeValue); } } return((string[])list.ToArray(typeof(string))); }
/// <summary> /// This sets an arbitrary attribute on the task element. These /// are attributes that the project author has placed on the task element /// that get passed in to the task. /// </summary> /// <owner>RGoel</owner> public void SetParameterValue ( string parameterName, string parameterValue ) { // You can only set the value of user-defined attributes. The well-known // ones, like "ContinueOnError" for example, are accessed through other means. error.VerifyThrowArgument(!XMakeAttributes.IsSpecialTaskAttribute(parameterName), "CannotAccessKnownAttributes", parameterName); // If this task was imported from another project, we don't allow modifying it. error.VerifyThrowInvalidOperation(!this.importedFromAnotherProject, "CannotModifyImportedProjects"); error.VerifyThrowInvalidOperation(this.taskElement != null, "CannotUseParameters"); // If this is a persisted Task, set the attribute directly on the // task element. taskElement.SetAttribute(parameterName, parameterValue); this.MarkTaskAsDirty(); }