/// <summary> /// Given the set of parameters that are set to the factory, and the set of parameters coming from the task invocation that we're searching for /// a matching record to, determine whether the parameters match this record. /// </summary> private static bool TaskIdentityParametersMatchFactory(IDictionary <string, string> factoryIdentityParameters, IDictionary <string, string> taskIdentityParameters) { if (taskIdentityParameters == null || taskIdentityParameters.Count == 0 || factoryIdentityParameters == null || factoryIdentityParameters.Count == 0) { // either the task or the using task doesn't care about anything, in which case we match by default. return(true); } string taskRuntime = null; string taskArchitecture = null; string usingTaskRuntime = null; string usingTaskArchitecture = null; taskIdentityParameters.TryGetValue(XMakeAttributes.runtime, out taskRuntime); factoryIdentityParameters.TryGetValue(XMakeAttributes.runtime, out usingTaskRuntime); if (XMakeAttributes.RuntimeValuesMatch(taskRuntime, usingTaskRuntime)) { taskIdentityParameters.TryGetValue(XMakeAttributes.architecture, out taskArchitecture); factoryIdentityParameters.TryGetValue(XMakeAttributes.architecture, out usingTaskArchitecture); if (XMakeAttributes.ArchitectureValuesMatch(taskArchitecture, usingTaskArchitecture)) { // both match return(true); } } // one or more does not match, so we don't match. return(false); }
/// <summary> /// Parse a ProjectTaskElement /// </summary> private ProjectTaskElement ParseProjectTaskElement(XmlElementWithLocation element, ProjectTargetElement parent) { foreach (XmlAttributeWithLocation attribute in element.Attributes) { ProjectErrorUtilities.VerifyThrowInvalidProject ( !XMakeAttributes.IsBadlyCasedSpecialTaskAttribute(attribute.Name), attribute.Location, "BadlyCasedSpecialTaskAttribute", attribute.Name, element.Name, element.Name ); } ProjectTaskElement task = new ProjectTaskElement(element, parent, _project); foreach (XmlElementWithLocation childElement in ProjectXmlUtilities.GetVerifyThrowProjectChildElements(element)) { ProjectErrorUtilities.VerifyThrowInvalidProject(childElement.Name == XMakeElements.output, childElement.Location, "UnrecognizedChildElement", childElement.Name, task.Name); ProjectOutputElement output = ParseProjectOutputElement(childElement, task); task.AppendParentedChildNoChecks(output); } return(task); }
public void TestIsNonBatchingTargetAttribute() { Assert.False(XMakeAttributes.IsNonBatchingTargetAttribute("NotAnAttribute")); Assert.True(XMakeAttributes.IsNonBatchingTargetAttribute(XMakeAttributes.dependsOnTargets)); Assert.True(XMakeAttributes.IsNonBatchingTargetAttribute(XMakeAttributes.name)); Assert.True(XMakeAttributes.IsNonBatchingTargetAttribute(XMakeAttributes.condition)); }
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)); }
public void TestMergeRuntimeValues(string left, string right, bool success, string expected) { XMakeAttributes.TryMergeRuntimeValues(left, right, out string mergedRuntime) .ShouldBe(success); mergedRuntime.ShouldBe(expected); }
public void VerifyMatchingUsingTaskParametersDontLaunchTaskHost2() { ITask createdTask = null; try { IDictionary <string, string> taskParameters = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase); taskParameters.Add(XMakeAttributes.runtime, XMakeAttributes.MSBuildRuntimeValues.any); taskParameters.Add(XMakeAttributes.architecture, XMakeAttributes.GetCurrentMSBuildArchitecture()); SetupTaskFactory(taskParameters, false /* don't want task host */); createdTask = _taskFactory.CreateTaskInstance(ElementLocation.Create("MSBUILD"), null, new MockHost(), null, #if FEATURE_APPDOMAIN new AppDomainSetup(), #endif false); Assert.NotNull(createdTask); Assert.False(createdTask is TaskHostTask); } finally { if (createdTask != null) { _taskFactory.CleanupTask(createdTask); } } }
/// <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> /// Given a set of task parameters from the UsingTask and from the task invocation, generate a dictionary that combines the two, or throws if the merge /// is impossible (we shouldn't ever get to this point if it is ...) /// </summary> private static IDictionary <string, string> MergeTaskFactoryParameterSets(IDictionary <string, string> factoryIdentityParameters, IDictionary <string, string> taskIdentityParameters) { IDictionary <string, string> mergedParameters = null; string mergedRuntime = null; string mergedArchitecture = null; if (factoryIdentityParameters == null || factoryIdentityParameters.Count == 0) { mergedParameters = new Dictionary <string, string>(taskIdentityParameters, StringComparer.OrdinalIgnoreCase); } else if (taskIdentityParameters == null || taskIdentityParameters.Count == 0) { mergedParameters = new Dictionary <string, string>(factoryIdentityParameters, StringComparer.OrdinalIgnoreCase); } if (mergedParameters != null) { mergedParameters.TryGetValue(XMakeAttributes.runtime, out mergedRuntime); mergedParameters.TryGetValue(XMakeAttributes.architecture, out mergedArchitecture); mergedParameters[XMakeAttributes.runtime] = XMakeAttributes.GetExplicitMSBuildRuntime(mergedRuntime); mergedParameters[XMakeAttributes.architecture] = XMakeAttributes.GetExplicitMSBuildArchitecture(mergedArchitecture); } else { string taskRuntime = null; string taskArchitecture = null; string usingTaskRuntime = null; string usingTaskArchitecture = null; mergedParameters = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase); taskIdentityParameters.TryGetValue(XMakeAttributes.runtime, out taskRuntime); factoryIdentityParameters.TryGetValue(XMakeAttributes.runtime, out usingTaskRuntime); if (!XMakeAttributes.TryMergeRuntimeValues(taskRuntime, usingTaskRuntime, out mergedRuntime)) { ErrorUtilities.ThrowInternalError("How did we get two runtime values that were unmergeable?"); } else { mergedParameters.Add(XMakeAttributes.runtime, mergedRuntime); } taskIdentityParameters.TryGetValue(XMakeAttributes.architecture, out taskArchitecture); factoryIdentityParameters.TryGetValue(XMakeAttributes.architecture, out usingTaskArchitecture); if (!XMakeAttributes.TryMergeArchitectureValues(taskArchitecture, usingTaskArchitecture, out mergedArchitecture)) { ErrorUtilities.ThrowInternalError("How did we get two runtime values that were unmergeable?"); } else { mergedParameters.Add(XMakeAttributes.architecture, mergedArchitecture); } } return(mergedParameters); }
/// <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 TestMergeRuntimeValuesAnyAcceptsCurrent() { XMakeAttributes.TryMergeRuntimeValues(XMakeAttributes.MSBuildRuntimeValues.any, XMakeAttributes.MSBuildRuntimeValues.currentRuntime, out string mergedRuntime) .ShouldBeTrue(); mergedRuntime.ShouldBe(XMakeAttributes.GetCurrentMSBuildRuntime()); }
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)); }
public void TestRuntimeValuesMatch() { Assert.IsTrue(XMakeAttributes.RuntimeValuesMatch(XMakeAttributes.MSBuildRuntimeValues.any, XMakeAttributes.MSBuildRuntimeValues.currentRuntime)); Assert.IsTrue(XMakeAttributes.RuntimeValuesMatch(XMakeAttributes.MSBuildRuntimeValues.any, XMakeAttributes.MSBuildRuntimeValues.clr4)); Assert.IsTrue(XMakeAttributes.RuntimeValuesMatch(XMakeAttributes.MSBuildRuntimeValues.clr2, XMakeAttributes.MSBuildRuntimeValues.any)); Assert.IsTrue(XMakeAttributes.RuntimeValuesMatch(XMakeAttributes.MSBuildRuntimeValues.currentRuntime, XMakeAttributes.MSBuildRuntimeValues.clr4)); Assert.IsFalse(XMakeAttributes.RuntimeValuesMatch(XMakeAttributes.MSBuildRuntimeValues.currentRuntime, XMakeAttributes.MSBuildRuntimeValues.clr2)); Assert.IsFalse(XMakeAttributes.RuntimeValuesMatch(XMakeAttributes.MSBuildRuntimeValues.clr4, XMakeAttributes.MSBuildRuntimeValues.clr2)); }
/// <summary> /// Returns the TaskHostContext corresponding to this process /// </summary> internal static TaskHostContext GetCurrentTaskHostContext() { // We know that whichever assembly is executing this code -- whether it's MSBuildTaskHost.exe or // Microsoft.Build.dll -- is of the version of the CLR that this process is running. So grab // the version of mscorlib currently in use and call that good enough. Version mscorlibVersion = typeof(bool).GetTypeInfo().Assembly.GetName().Version; string currentMSBuildArchitecture = XMakeAttributes.GetCurrentMSBuildArchitecture(); TaskHostContext hostContext = GetTaskHostContext(currentMSBuildArchitecture.Equals(XMakeAttributes.MSBuildArchitectureValues.x64), mscorlibVersion.Major); return(hostContext); }
public void TestArchitectureValuesMatch() { string currentArchitecture = EnvironmentUtilities.Is64BitProcess ? XMakeAttributes.MSBuildArchitectureValues.x64 : XMakeAttributes.MSBuildArchitectureValues.x86; string notCurrentArchitecture = EnvironmentUtilities.Is64BitProcess ? XMakeAttributes.MSBuildArchitectureValues.x86 : XMakeAttributes.MSBuildArchitectureValues.x64; Assert.True(XMakeAttributes.ArchitectureValuesMatch(XMakeAttributes.MSBuildArchitectureValues.any, XMakeAttributes.MSBuildArchitectureValues.currentArchitecture)); Assert.True(XMakeAttributes.ArchitectureValuesMatch(XMakeAttributes.MSBuildArchitectureValues.any, XMakeAttributes.MSBuildArchitectureValues.x64)); Assert.True(XMakeAttributes.ArchitectureValuesMatch(XMakeAttributes.MSBuildArchitectureValues.x86, XMakeAttributes.MSBuildArchitectureValues.any)); Assert.True(XMakeAttributes.ArchitectureValuesMatch(XMakeAttributes.MSBuildArchitectureValues.currentArchitecture, currentArchitecture)); Assert.False(XMakeAttributes.ArchitectureValuesMatch(XMakeAttributes.MSBuildArchitectureValues.currentArchitecture, notCurrentArchitecture)); Assert.False(XMakeAttributes.ArchitectureValuesMatch(XMakeAttributes.MSBuildArchitectureValues.x64, XMakeAttributes.MSBuildArchitectureValues.x86)); }
public void TestIsBadlyCasedSpecialTaskAttribute() { Assert.False(XMakeAttributes.IsBadlyCasedSpecialTaskAttribute("NotAnAttribute")); Assert.False(XMakeAttributes.IsBadlyCasedSpecialTaskAttribute(XMakeAttributes.include)); Assert.False(XMakeAttributes.IsBadlyCasedSpecialTaskAttribute(XMakeAttributes.continueOnError)); Assert.False(XMakeAttributes.IsBadlyCasedSpecialTaskAttribute(XMakeAttributes.condition)); Assert.False(XMakeAttributes.IsBadlyCasedSpecialTaskAttribute(XMakeAttributes.msbuildArchitecture)); Assert.False(XMakeAttributes.IsBadlyCasedSpecialTaskAttribute(XMakeAttributes.msbuildRuntime)); Assert.True(XMakeAttributes.IsBadlyCasedSpecialTaskAttribute("continueOnError")); Assert.True(XMakeAttributes.IsBadlyCasedSpecialTaskAttribute("condition")); Assert.True(XMakeAttributes.IsBadlyCasedSpecialTaskAttribute("MsbuildRuntime")); Assert.True(XMakeAttributes.IsBadlyCasedSpecialTaskAttribute("msbuildarchitecture")); }
/// <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); } }
public void TestMergeRuntimeValuesCurrentToClr4() { XMakeAttributes.TryMergeRuntimeValues( XMakeAttributes.MSBuildRuntimeValues.currentRuntime, XMakeAttributes.MSBuildRuntimeValues.clr4, out string mergedRuntime).ShouldBeTrue(); mergedRuntime.ShouldBe(XMakeAttributes.MSBuildRuntimeValues.clr4); XMakeAttributes.TryMergeRuntimeValues( XMakeAttributes.MSBuildRuntimeValues.currentRuntime, XMakeAttributes.MSBuildRuntimeValues.net, out mergedRuntime).ShouldBeFalse(); mergedRuntime.ShouldBeNull(); }
/// <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); } }
public void CreatableByTaskFactoryMatchingIdentity() { IDictionary <string, string> factoryIdentityParameters = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase); factoryIdentityParameters.Add(XMakeAttributes.runtime, XMakeAttributes.MSBuildRuntimeValues.currentRuntime); factoryIdentityParameters.Add(XMakeAttributes.architecture, XMakeAttributes.MSBuildArchitectureValues.currentArchitecture); SetupTaskFactory(factoryIdentityParameters, false /* don't want task host */); IDictionary <string, string> taskIdentityParameters = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase); taskIdentityParameters.Add(XMakeAttributes.runtime, XMakeAttributes.GetCurrentMSBuildRuntime()); taskIdentityParameters.Add(XMakeAttributes.architecture, XMakeAttributes.GetCurrentMSBuildArchitecture()); Assert.True(_taskFactory.TaskNameCreatableByFactory("TaskToTestFactories", taskIdentityParameters, String.Empty, null, ElementLocation.Create(".", 1, 1))); }
/// <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> /// Validates the given set of parameters, logging the appropriate errors as necessary. /// </summary> private static void VerifyThrowIdentityParametersValid(IDictionary <string, string> identityParameters, IElementLocation errorLocation, string taskName, string runtimeName, string architectureName) { // validate the task factory parameters if (identityParameters != null && identityParameters.Count > 0) { string runtime = null; if (identityParameters.TryGetValue(XMakeAttributes.runtime, out runtime)) { if (!XMakeAttributes.IsValidMSBuildRuntimeValue(runtime)) { ProjectErrorUtilities.ThrowInvalidProject ( errorLocation, "TaskLoadFailureInvalidTaskHostFactoryParameter", taskName, runtime, runtimeName, XMakeAttributes.MSBuildRuntimeValues.clr2, XMakeAttributes.MSBuildRuntimeValues.clr4, XMakeAttributes.MSBuildRuntimeValues.currentRuntime, XMakeAttributes.MSBuildRuntimeValues.any ); } } string architecture = null; if (identityParameters.TryGetValue(XMakeAttributes.architecture, out architecture)) { if (!XMakeAttributes.IsValidMSBuildArchitectureValue(architecture)) { ProjectErrorUtilities.ThrowInvalidProject ( errorLocation, "TaskLoadFailureInvalidTaskHostFactoryParameter", taskName, architecture, architectureName, XMakeAttributes.MSBuildArchitectureValues.x86, XMakeAttributes.MSBuildArchitectureValues.x64, XMakeAttributes.MSBuildArchitectureValues.currentArchitecture, XMakeAttributes.MSBuildArchitectureValues.any ); } } } }
/// <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); } } } }
public void TestRuntimeValuesMatch() { Assert.True(XMakeAttributes.RuntimeValuesMatch(XMakeAttributes.MSBuildRuntimeValues.any, XMakeAttributes.MSBuildRuntimeValues.currentRuntime)); Assert.True(XMakeAttributes.RuntimeValuesMatch(XMakeAttributes.MSBuildRuntimeValues.any, XMakeAttributes.MSBuildRuntimeValues.net)); Assert.True(XMakeAttributes.RuntimeValuesMatch(XMakeAttributes.MSBuildRuntimeValues.any, XMakeAttributes.MSBuildRuntimeValues.clr4)); Assert.True(XMakeAttributes.RuntimeValuesMatch(XMakeAttributes.MSBuildRuntimeValues.clr2, XMakeAttributes.MSBuildRuntimeValues.any)); #if NET5_0_OR_GREATER Assert.True(XMakeAttributes.RuntimeValuesMatch(XMakeAttributes.MSBuildRuntimeValues.currentRuntime, XMakeAttributes.MSBuildRuntimeValues.net)); #else Assert.True(XMakeAttributes.RuntimeValuesMatch(XMakeAttributes.MSBuildRuntimeValues.currentRuntime, XMakeAttributes.MSBuildRuntimeValues.clr4)); #endif // Never true Assert.False(XMakeAttributes.RuntimeValuesMatch(XMakeAttributes.MSBuildRuntimeValues.currentRuntime, XMakeAttributes.MSBuildRuntimeValues.clr2)); Assert.False(XMakeAttributes.RuntimeValuesMatch(XMakeAttributes.MSBuildRuntimeValues.clr4, XMakeAttributes.MSBuildRuntimeValues.clr2)); Assert.False(XMakeAttributes.RuntimeValuesMatch(XMakeAttributes.MSBuildRuntimeValues.clr4, XMakeAttributes.MSBuildRuntimeValues.net)); Assert.False(XMakeAttributes.RuntimeValuesMatch(XMakeAttributes.MSBuildRuntimeValues.clr2, XMakeAttributes.MSBuildRuntimeValues.net)); }
/// <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> /// Returns true if a task host exists that can service the requested runtime and architecture /// values, and false otherwise. /// </summary> internal static bool DoesTaskHostExist(string runtime, string architecture) { if (runtime != null) { runtime = runtime.Trim(); } if (architecture != null) { architecture = architecture.Trim(); } if (!XMakeAttributes.IsValidMSBuildRuntimeValue(runtime)) { ErrorUtilities.ThrowArgument("InvalidTaskHostFactoryParameter", runtime, "Runtime", XMakeAttributes.MSBuildRuntimeValues.clr2, XMakeAttributes.MSBuildRuntimeValues.clr4, XMakeAttributes.MSBuildRuntimeValues.currentRuntime, XMakeAttributes.MSBuildRuntimeValues.any); } if (!XMakeAttributes.IsValidMSBuildArchitectureValue(architecture)) { ErrorUtilities.ThrowArgument("InvalidTaskHostFactoryParameter", architecture, "Architecture", XMakeAttributes.MSBuildArchitectureValues.x86, XMakeAttributes.MSBuildArchitectureValues.x64, XMakeAttributes.MSBuildArchitectureValues.currentArchitecture, XMakeAttributes.MSBuildArchitectureValues.any); } runtime = XMakeAttributes.GetExplicitMSBuildRuntime(runtime); architecture = XMakeAttributes.GetExplicitMSBuildArchitecture(architecture); IDictionary <string, string> parameters = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase); parameters.Add(XMakeAttributes.runtime, runtime); parameters.Add(XMakeAttributes.architecture, architecture); TaskHostContext desiredContext = CommunicationsUtilities.GetTaskHostContext(parameters); string taskHostLocation = NodeProviderOutOfProcTaskHost.GetMSBuildLocationFromHostContext(desiredContext); if (taskHostLocation != null && FileUtilities.FileExistsNoThrow(taskHostLocation)) { return(true); } return(false); }
/// <summary> /// Given the appropriate information, return the equivalent HandshakeOptions. /// </summary> internal static HandshakeOptions GetHandshakeOptions(bool taskHost, bool is64Bit = false, int clrVersion = 0, bool nodeReuse = false, bool lowPriority = false, IDictionary <string, string> taskHostParameters = null) { HandshakeOptions context = taskHost ? HandshakeOptions.TaskHost : HandshakeOptions.None; // We don't know about the TaskHost. Figure it out. if (taskHost && clrVersion == 0) { // Take the current TaskHost context if (taskHostParameters == null) { clrVersion = typeof(bool).GetTypeInfo().Assembly.GetName().Version.Major; is64Bit = XMakeAttributes.GetCurrentMSBuildArchitecture().Equals(XMakeAttributes.MSBuildArchitectureValues.x64); } else { ErrorUtilities.VerifyThrow(taskHostParameters.ContainsKey(XMakeAttributes.runtime), "Should always have an explicit runtime when we call this method."); ErrorUtilities.VerifyThrow(taskHostParameters.ContainsKey(XMakeAttributes.architecture), "Should always have an explicit architecture when we call this method."); clrVersion = taskHostParameters[XMakeAttributes.runtime].Equals(XMakeAttributes.MSBuildRuntimeValues.clr4, StringComparison.OrdinalIgnoreCase) ? 4 : 2; is64Bit = taskHostParameters[XMakeAttributes.architecture].Equals(XMakeAttributes.MSBuildArchitectureValues.x64); } } if (is64Bit) { context |= HandshakeOptions.X64; } if (clrVersion == 2) { context |= HandshakeOptions.CLR2; } if (nodeReuse) { context |= HandshakeOptions.NodeReuse; } if (lowPriority) { context |= HandshakeOptions.LowPriority; } return(context); }
public void TestMergeRuntimeValues() { string mergedRuntime = null; Assert.IsTrue(XMakeAttributes.TryMergeRuntimeValues(XMakeAttributes.MSBuildRuntimeValues.any, XMakeAttributes.MSBuildRuntimeValues.currentRuntime, out mergedRuntime)); Assert.AreEqual(XMakeAttributes.MSBuildRuntimeValues.clr4, mergedRuntime); Assert.IsTrue(XMakeAttributes.TryMergeRuntimeValues(XMakeAttributes.MSBuildRuntimeValues.any, XMakeAttributes.MSBuildRuntimeValues.clr4, out mergedRuntime)); Assert.AreEqual(XMakeAttributes.MSBuildRuntimeValues.clr4, mergedRuntime); Assert.IsTrue(XMakeAttributes.TryMergeRuntimeValues(XMakeAttributes.MSBuildRuntimeValues.clr2, XMakeAttributes.MSBuildRuntimeValues.any, out mergedRuntime)); Assert.AreEqual(XMakeAttributes.MSBuildRuntimeValues.clr2, mergedRuntime); Assert.IsTrue(XMakeAttributes.TryMergeRuntimeValues(XMakeAttributes.MSBuildRuntimeValues.currentRuntime, XMakeAttributes.MSBuildRuntimeValues.clr4, out mergedRuntime)); Assert.AreEqual(XMakeAttributes.MSBuildRuntimeValues.clr4, mergedRuntime); Assert.IsFalse(XMakeAttributes.TryMergeRuntimeValues(XMakeAttributes.MSBuildRuntimeValues.currentRuntime, XMakeAttributes.MSBuildRuntimeValues.clr2, out mergedRuntime)); Assert.IsNull(mergedRuntime); Assert.IsFalse(XMakeAttributes.TryMergeRuntimeValues(XMakeAttributes.MSBuildRuntimeValues.clr4, XMakeAttributes.MSBuildRuntimeValues.clr2, out mergedRuntime)); Assert.IsNull(mergedRuntime); }
// Ignore: Test requires installed toolset. public void VerifyMatchingTaskParametersDontLaunchTaskHost2() { ITask createdTask = null; try { IDictionary <string, string> taskParameters = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase); taskParameters.Add(XMakeAttributes.runtime, XMakeAttributes.MSBuildRuntimeValues.clr4); taskParameters.Add(XMakeAttributes.architecture, XMakeAttributes.GetCurrentMSBuildArchitecture()); createdTask = _taskFactory.CreateTaskInstance(ElementLocation.Create("MSBUILD"), null, new MockHost(), taskParameters, new AppDomainSetup(), false); Assert.IsNotNull(createdTask); Assert.IsNotInstanceOfType(createdTask, typeof(TaskHostTask)); } finally { if (createdTask != null) { _taskFactory.CleanupTask(createdTask); } } }
public void TestMergeArchitectureValues() { string currentArchitecture = EnvironmentUtilities.Is64BitProcess ? XMakeAttributes.MSBuildArchitectureValues.x64 : XMakeAttributes.MSBuildArchitectureValues.x86; string notCurrentArchitecture = EnvironmentUtilities.Is64BitProcess ? XMakeAttributes.MSBuildArchitectureValues.x86 : XMakeAttributes.MSBuildArchitectureValues.x64; string mergedArchitecture; Assert.True(XMakeAttributes.TryMergeArchitectureValues(XMakeAttributes.MSBuildArchitectureValues.any, XMakeAttributes.MSBuildArchitectureValues.currentArchitecture, out mergedArchitecture)); Assert.Equal(currentArchitecture, mergedArchitecture); Assert.True(XMakeAttributes.TryMergeArchitectureValues(XMakeAttributes.MSBuildArchitectureValues.any, XMakeAttributes.MSBuildArchitectureValues.x64, out mergedArchitecture)); Assert.Equal(XMakeAttributes.MSBuildArchitectureValues.x64, mergedArchitecture); Assert.True(XMakeAttributes.TryMergeArchitectureValues(XMakeAttributes.MSBuildArchitectureValues.x86, XMakeAttributes.MSBuildArchitectureValues.any, out mergedArchitecture)); Assert.Equal(XMakeAttributes.MSBuildArchitectureValues.x86, mergedArchitecture); Assert.True(XMakeAttributes.TryMergeArchitectureValues(XMakeAttributes.MSBuildArchitectureValues.currentArchitecture, currentArchitecture, out mergedArchitecture)); Assert.Equal(currentArchitecture, mergedArchitecture); Assert.False(XMakeAttributes.TryMergeArchitectureValues(XMakeAttributes.MSBuildArchitectureValues.currentArchitecture, notCurrentArchitecture, out mergedArchitecture)); Assert.False(XMakeAttributes.TryMergeArchitectureValues(XMakeAttributes.MSBuildArchitectureValues.x64, XMakeAttributes.MSBuildArchitectureValues.x86, out mergedArchitecture)); }
/// <summary> /// Returns true if the provided set of task host parameters matches the current process, /// and false otherwise. /// </summary> private static bool TaskHostParametersMatchCurrentProcess(IDictionary <string, string> mergedParameters) { if (mergedParameters == null || mergedParameters.Count == 0) { // We don't care, so they match by default. return(true); } string runtime; if (mergedParameters.TryGetValue(XMakeAttributes.runtime, out runtime)) { string currentRuntime = XMakeAttributes.GetExplicitMSBuildRuntime(XMakeAttributes.MSBuildRuntimeValues.currentRuntime); if (!currentRuntime.Equals(XMakeAttributes.GetExplicitMSBuildRuntime(runtime), StringComparison.OrdinalIgnoreCase)) { // runtime doesn't match return(false); } } string architecture; if (mergedParameters.TryGetValue(XMakeAttributes.architecture, out architecture)) { string currentArchitecture = XMakeAttributes.GetCurrentMSBuildArchitecture(); if (!currentArchitecture.Equals(XMakeAttributes.GetExplicitMSBuildArchitecture(architecture), StringComparison.OrdinalIgnoreCase)) { // architecture doesn't match return(false); } } // if it doesn't not match, then it matches return(true); }