Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 18 additions & 21 deletions src/Build.UnitTests/BackEnd/AssemblyTaskFactory_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,7 @@ public class AssemblyTaskFactory_Tests
/// </summary>
private AssemblyLoadInfo _loadInfo;

/// <summary>
/// The loaded type from the initialized task factory.
/// </summary>
private LoadedType _loadedType;
private Dictionary<string, string> _taskFactoryIdentityParameters = null;

/// <summary>
/// Initialize a task factory
Expand All @@ -55,8 +52,8 @@ public void NullLoadInfo()
{
Assert.Throws<ArgumentNullException>(() =>
{
AssemblyTaskFactory taskFactory = new AssemblyTaskFactory();
taskFactory.InitializeFactory(null, "TaskToTestFactories", new Dictionary<string, TaskPropertyInfo>(), string.Empty, null, false, null, ElementLocation.Create("NONE"), String.Empty);
AssemblyTaskFactory taskFactory = new();
taskFactory.InitializeFactory(null, "TaskToTestFactories", new Dictionary<string, TaskPropertyInfo>(), string.Empty, ref _taskFactoryIdentityParameters, false, null, ElementLocation.Create("NONE"), String.Empty);
}
);
}
Expand All @@ -69,7 +66,7 @@ public void NullTaskName()
Assert.Throws<InvalidProjectFileException>(() =>
{
AssemblyTaskFactory taskFactory = new AssemblyTaskFactory();
taskFactory.InitializeFactory(_loadInfo, null, new Dictionary<string, TaskPropertyInfo>(), string.Empty, null, false, null, ElementLocation.Create("NONE"), String.Empty);
taskFactory.InitializeFactory(_loadInfo, null, new Dictionary<string, TaskPropertyInfo>(), string.Empty, ref _taskFactoryIdentityParameters, false, null, ElementLocation.Create("NONE"), String.Empty);
}
);
}
Expand All @@ -82,7 +79,7 @@ public void EmptyTaskName()
Assert.Throws<InvalidProjectFileException>(() =>
{
AssemblyTaskFactory taskFactory = new AssemblyTaskFactory();
taskFactory.InitializeFactory(_loadInfo, String.Empty, new Dictionary<string, TaskPropertyInfo>(), string.Empty, null, false, null, ElementLocation.Create("NONE"), String.Empty);
taskFactory.InitializeFactory(_loadInfo, String.Empty, new Dictionary<string, TaskPropertyInfo>(), string.Empty, ref _taskFactoryIdentityParameters, false, null, ElementLocation.Create("NONE"), String.Empty);
}
);
}
Expand All @@ -95,7 +92,7 @@ public void GoodTaskNameButNotInInfo()
Assert.Throws<InvalidProjectFileException>(() =>
{
AssemblyTaskFactory taskFactory = new AssemblyTaskFactory();
taskFactory.InitializeFactory(_loadInfo, "RandomTask", new Dictionary<string, TaskPropertyInfo>(), string.Empty, null, false, null, ElementLocation.Create("NONE"), String.Empty);
taskFactory.InitializeFactory(_loadInfo, "RandomTask", new Dictionary<string, TaskPropertyInfo>(), string.Empty, ref _taskFactoryIdentityParameters, false, null, ElementLocation.Create("NONE"), String.Empty);
}
);
}
Expand Down Expand Up @@ -180,7 +177,7 @@ public void CreatableByTaskFactoryNullTaskName()
[Fact]
public void CreatableByTaskFactoryMatchingIdentity()
{
IDictionary<string, string> factoryIdentityParameters = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
Dictionary<string, string> factoryIdentityParameters = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
factoryIdentityParameters.Add(XMakeAttributes.runtime, XMakeAttributes.MSBuildRuntimeValues.currentRuntime);
factoryIdentityParameters.Add(XMakeAttributes.architecture, XMakeAttributes.MSBuildArchitectureValues.currentArchitecture);

Expand All @@ -200,7 +197,7 @@ public void CreatableByTaskFactoryMatchingIdentity()
[Fact]
public void CreatableByTaskFactoryMismatchedIdentity()
{
IDictionary<string, string> factoryIdentityParameters = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
Dictionary<string, string> factoryIdentityParameters = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
factoryIdentityParameters.Add(XMakeAttributes.runtime, XMakeAttributes.MSBuildRuntimeValues.clr2);
factoryIdentityParameters.Add(XMakeAttributes.architecture, XMakeAttributes.MSBuildArchitectureValues.currentArchitecture);

Expand Down Expand Up @@ -342,7 +339,7 @@ public void VerifyMatchingUsingTaskParametersDontLaunchTaskHost1()
ITask createdTask = null;
try
{
IDictionary<string, string> taskParameters = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
Dictionary<string, string> taskParameters = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
taskParameters.Add(XMakeAttributes.runtime, XMakeAttributes.MSBuildRuntimeValues.any);
taskParameters.Add(XMakeAttributes.architecture, XMakeAttributes.MSBuildArchitectureValues.any);

Expand Down Expand Up @@ -375,7 +372,7 @@ public void VerifyMatchingUsingTaskParametersDontLaunchTaskHost2()
ITask createdTask = null;
try
{
IDictionary<string, string> taskParameters = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
Dictionary<string, string> taskParameters = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
taskParameters.Add(XMakeAttributes.runtime, XMakeAttributes.MSBuildRuntimeValues.any);
taskParameters.Add(XMakeAttributes.architecture, XMakeAttributes.GetCurrentMSBuildArchitecture());

Expand Down Expand Up @@ -408,7 +405,7 @@ public void VerifyMatchingParametersDontLaunchTaskHost()
ITask createdTask = null;
try
{
IDictionary<string, string> factoryParameters = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
Dictionary<string, string> factoryParameters = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
factoryParameters.Add(XMakeAttributes.runtime, XMakeAttributes.GetCurrentMSBuildRuntime());

SetupTaskFactory(factoryParameters, false /* don't want task host */);
Expand Down Expand Up @@ -444,7 +441,7 @@ public void VerifyNonmatchingUsingTaskParametersLaunchTaskHost()
ITask createdTask = null;
try
{
IDictionary<string, string> taskParameters = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
Dictionary<string, string> taskParameters = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
taskParameters.Add(XMakeAttributes.runtime, XMakeAttributes.MSBuildRuntimeValues.clr2);
taskParameters.Add(XMakeAttributes.architecture, XMakeAttributes.MSBuildArchitectureValues.any);

Expand Down Expand Up @@ -510,7 +507,7 @@ public void VerifyNonmatchingParametersLaunchTaskHost()
ITask createdTask = null;
try
{
IDictionary<string, string> factoryParameters = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
Dictionary<string, string> factoryParameters = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
factoryParameters.Add(XMakeAttributes.runtime, XMakeAttributes.MSBuildRuntimeValues.clr2);

SetupTaskFactory(factoryParameters, false /* don't want task host */);
Expand Down Expand Up @@ -575,7 +572,7 @@ public void VerifyExplicitlyLaunchTaskHostEvenIfParametersMatch1()
ITask createdTask = null;
try
{
IDictionary<string, string> taskParameters = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
Dictionary<string, string> taskParameters = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
taskParameters.Add(XMakeAttributes.runtime, XMakeAttributes.MSBuildRuntimeValues.any);
taskParameters.Add(XMakeAttributes.architecture, XMakeAttributes.MSBuildArchitectureValues.any);

Expand Down Expand Up @@ -641,7 +638,7 @@ public void VerifyExplicitlyLaunchTaskHostEvenIfParametersMatch2()
public void VerifySameFactoryCanGenerateDifferentTaskInstances()
{
ITask createdTask = null;
IDictionary<string, string> factoryParameters = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
Dictionary<string, string> factoryParameters = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
factoryParameters.Add(XMakeAttributes.runtime, XMakeAttributes.MSBuildRuntimeValues.any);
factoryParameters.Add(XMakeAttributes.architecture, XMakeAttributes.MSBuildArchitectureValues.any);

Expand Down Expand Up @@ -694,16 +691,16 @@ public void VerifySameFactoryCanGenerateDifferentTaskInstances()
/// Abstract out the creation of the new AssemblyTaskFactory with default task, and
/// with some basic validation.
/// </summary>
private void SetupTaskFactory(IDictionary<string, string> factoryParameters, bool explicitlyLaunchTaskHost)
private void SetupTaskFactory(Dictionary<string, string> factoryParameters, bool explicitlyLaunchTaskHost)
{
_taskFactory = new AssemblyTaskFactory();
#if FEATURE_ASSEMBLY_LOCATION
_loadInfo = AssemblyLoadInfo.Create(null, Assembly.GetAssembly(typeof(TaskToTestFactories)).Location);
#else
_loadInfo = AssemblyLoadInfo.Create(typeof(TaskToTestFactories).GetTypeInfo().Assembly.FullName, null);
#endif
_loadedType = _taskFactory.InitializeFactory(_loadInfo, "TaskToTestFactories", new Dictionary<string, TaskPropertyInfo>(), string.Empty, factoryParameters, explicitlyLaunchTaskHost, null, ElementLocation.Create("NONE"), String.Empty);
Assert.True(_loadedType.Assembly.Equals(_loadInfo)); // "Expected the AssemblyLoadInfo to be equal"
TypeInformation typeInfo = _taskFactory.InitializeFactory(_loadInfo, "TaskToTestFactories", new Dictionary<string, TaskPropertyInfo>(), string.Empty, ref factoryParameters, explicitlyLaunchTaskHost, null, ElementLocation.Create("NONE"), String.Empty);
typeInfo.LoadInfo.ShouldBe(_loadInfo, "Expected the AssemblyLoadInfo to be equal");
}

#endregion
Expand Down
2 changes: 1 addition & 1 deletion src/Build.UnitTests/BackEnd/TaskExecutionHost_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1170,7 +1170,7 @@ private void InitializeHost(bool throwOnExecute)
TaskBuilderTestTask.TaskBuilderTestTaskFactory taskFactory = new TaskBuilderTestTask.TaskBuilderTestTaskFactory();
taskFactory.ThrowOnExecute = throwOnExecute;
string taskName = "TaskBuilderTestTask";
(_host as TaskExecutionHost)._UNITTESTONLY_TaskFactoryWrapper = new TaskFactoryWrapper(taskFactory, loadedType, taskName, null);
(_host as TaskExecutionHost)._UNITTESTONLY_TaskFactoryWrapper = new TaskFactoryWrapper(taskFactory, new TypeInformation(loadedType), taskName, null);
_host.InitializeForTask
(
this,
Expand Down
50 changes: 19 additions & 31 deletions src/Build/BackEnd/TaskExecutionHost/TaskExecutionHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -257,12 +257,12 @@ void ITaskExecutionHost.InitializeForTask(IBuildEngine2 buildEngine, TargetLoggi

TaskRequirements requirements = TaskRequirements.None;

if (_taskFactoryWrapper.TaskFactoryLoadedType.HasSTAThreadAttribute())
if (_taskFactoryWrapper.TaskFactoryTypeInformation.HasSTAThreadAttribute)
{
requirements |= TaskRequirements.RequireSTAThread;
}

if (_taskFactoryWrapper.TaskFactoryLoadedType.HasLoadInSeparateAppDomainAttribute())
if (_taskFactoryWrapper.TaskFactoryTypeInformation.HasLoadInSeparateAppDomainAttribute)
{
requirements |= TaskRequirements.RequireSeparateAppDomain;

Expand Down Expand Up @@ -299,7 +299,7 @@ bool ITaskExecutionHost.InitializeForBatch(TaskLoggingContext loggingContext, It
if (_resolver == null)
{
_resolver = new TaskEngineAssemblyResolver();
_resolver.Initialize(_taskFactoryWrapper.TaskFactoryLoadedType.Assembly.AssemblyFile);
_resolver.Initialize(_taskFactoryWrapper.TaskFactoryTypeInformation.LoadInfo.AssemblyFile);
_resolver.InstallHandler();
}
#endif
Expand Down Expand Up @@ -899,15 +899,18 @@ private TaskFactoryWrapper FindTaskInRegistry(IDictionary<string, string> taskId
}
}

string taskFactoryFullName = returnClass.TaskFactory is AssemblyTaskFactory atf ? atf.TaskName : returnClass.TaskFactory.TaskType.FullName;
// Map to an intrinsic task, if necessary.
if (String.Equals(returnClass.TaskFactory.TaskType.FullName, "Microsoft.Build.Tasks.MSBuild", StringComparison.OrdinalIgnoreCase))
if (String.Equals(taskFactoryFullName, "Microsoft.Build.Tasks.MSBuild", StringComparison.OrdinalIgnoreCase))
{
returnClass = new TaskFactoryWrapper(new IntrinsicTaskFactory(typeof(MSBuild)), new LoadedType(typeof(MSBuild), AssemblyLoadInfo.Create(typeof(TaskExecutionHost).GetTypeInfo().Assembly.FullName, null)), _taskName, null);
AssemblyLoadInfo loadInfo = AssemblyLoadInfo.Create(typeof(TaskExecutionHost).GetTypeInfo().Assembly.FullName, null);
returnClass = new TaskFactoryWrapper(new IntrinsicTaskFactory(typeof(MSBuild)), new TypeInformation(new LoadedType(typeof(MSBuild), loadInfo)), _taskName, null);
_intrinsicTasks[_taskName] = returnClass;
}
else if (String.Equals(returnClass.TaskFactory.TaskType.FullName, "Microsoft.Build.Tasks.CallTarget", StringComparison.OrdinalIgnoreCase))
else if (String.Equals(taskFactoryFullName, "Microsoft.Build.Tasks.CallTarget", StringComparison.OrdinalIgnoreCase))
{
returnClass = new TaskFactoryWrapper(new IntrinsicTaskFactory(typeof(CallTarget)), new LoadedType(typeof(CallTarget), AssemblyLoadInfo.Create(typeof(TaskExecutionHost).GetTypeInfo().Assembly.FullName, null)), _taskName, null);
AssemblyLoadInfo loadInfo = AssemblyLoadInfo.Create(typeof(TaskExecutionHost).GetTypeInfo().Assembly.FullName, null);
returnClass = new TaskFactoryWrapper(new IntrinsicTaskFactory(typeof(CallTarget)), new TypeInformation(new LoadedType(typeof(CallTarget), loadInfo)), _taskName, null);
_intrinsicTasks[_taskName] = returnClass;
}
}
Expand Down Expand Up @@ -1073,30 +1076,15 @@ out parameterSet
else
{
// flag an error if we find a parameter that has no .NET property equivalent
if (_taskFactoryWrapper.TaskFactoryLoadedType.LoadedAssembly is null)
{
_taskLoggingContext.LogError
(
new BuildEventFileInfo( parameterLocation ),
"UnexpectedTaskAttribute",
parameterName,
_taskName,
_taskFactoryWrapper.TaskFactoryLoadedType.Type.Assembly.FullName,
_taskFactoryWrapper.TaskFactoryLoadedType.Type.Assembly.Location
);
}
else
{
_taskLoggingContext.LogError
(
new BuildEventFileInfo( parameterLocation ),
"UnexpectedTaskAttribute",
parameterName,
_taskName,
_taskFactoryWrapper.TaskFactoryLoadedType.LoadedAssembly.FullName,
_taskFactoryWrapper.TaskFactoryLoadedType.LoadedAssembly.Location
);
}
_taskLoggingContext.LogError
(
new BuildEventFileInfo( parameterLocation ),
"UnexpectedTaskAttribute",
parameterName,
_taskName,
_taskFactoryWrapper.TaskFactoryTypeInformation.LoadInfo.AssemblyName,
_taskFactoryWrapper.TaskFactoryTypeInformation.LoadInfo.AssemblyLocation
);
}
}
catch (AmbiguousMatchException)
Expand Down
37 changes: 30 additions & 7 deletions src/Build/Instance/ReflectableTaskPropertyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,19 @@ namespace Microsoft.Build.Execution
internal class ReflectableTaskPropertyInfo : TaskPropertyInfo
{
/// <summary>
/// The reflection-produced PropertyInfo.
/// The name of the generated tasks.
/// </summary>
private PropertyInfo _propertyInfo;
private readonly string taskName;

/// <summary>
/// The type of the generated tasks.
/// Function for accessing information about a property on a task via its name.
/// </summary>
private Type _taskType;
private readonly Func<string, BindingFlags, PropertyInfo> getProperty;

/// <summary>
/// The reflection-produced PropertyInfo.
/// </summary>
private PropertyInfo _propertyInfo;

/// <summary>
/// Initializes a new instance of the <see cref="ReflectableTaskPropertyInfo"/> class.
Expand All @@ -35,7 +40,16 @@ internal ReflectableTaskPropertyInfo(TaskPropertyInfo taskPropertyInfo, Type tas
: base(taskPropertyInfo.Name, taskPropertyInfo.PropertyType, taskPropertyInfo.Output, taskPropertyInfo.Required)
{
ErrorUtilities.VerifyThrowArgumentNull(taskType, nameof(taskType));
_taskType = taskType;
getProperty = taskType.GetProperty;
taskName = taskType.FullName;
}

internal ReflectableTaskPropertyInfo(TaskPropertyInfo taskPropertyInfo, TypeInformation typeInformation)
: base(taskPropertyInfo.Name, taskPropertyInfo.PropertyType, taskPropertyInfo.Output, taskPropertyInfo.Required)
{
ErrorUtilities.VerifyThrowArgumentNull(typeInformation, nameof(typeInformation));
getProperty = typeInformation.GetProperty;
taskName = typeInformation.TypeName;
}

/// <summary>
Expand All @@ -52,6 +66,15 @@ internal ReflectableTaskPropertyInfo(PropertyInfo propertyInfo)
_propertyInfo = propertyInfo;
}

internal ReflectableTaskPropertyInfo(TypeInformation.PropertyInfo propertyInfo) :
base(
propertyInfo.Name,
propertyInfo.PropertyType,
propertyInfo.OutputAttribute,
propertyInfo.RequiredAttribute)
{
}

/// <summary>
/// Gets or sets the reflection-produced PropertyInfo.
/// </summary>
Expand All @@ -61,8 +84,8 @@ internal PropertyInfo Reflection
{
if (_propertyInfo == null)
{
_propertyInfo = _taskType.GetProperty(Name, BindingFlags.Instance | BindingFlags.Public | BindingFlags.IgnoreCase);
ErrorUtilities.VerifyThrow(_propertyInfo != null, "Could not find property {0} on type {1} that the task factory indicated should exist.", Name, _taskType.FullName);
_propertyInfo = getProperty(Name, BindingFlags.Instance | BindingFlags.Public | BindingFlags.IgnoreCase);
ErrorUtilities.VerifyThrow(_propertyInfo != null, "Could not find property {0} on type {1} that the task factory indicated should exist.", Name, taskName);
}

return _propertyInfo;
Expand Down
Loading