// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//-----------------------------------------------------------------------
//
// A fake task builder used for testing other components.
//-----------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Linq;
using Microsoft.Build.Framework;
using Microsoft.Build.BackEnd;
using Microsoft.Build.Shared;
using Microsoft.Build.Collections;
using Microsoft.Build.Evaluation;
using Microsoft.Build.Execution;
using TargetLoggingContext = Microsoft.Build.BackEnd.Logging.TargetLoggingContext;
using System.Threading.Tasks;
namespace Microsoft.Build.UnitTests.BackEnd
{
///
/// The mock task builder.
///
internal class MockTaskBuilder : ITaskBuilder, IBuildComponent
{
///
/// The component host.
///
private IBuildComponentHost _host;
///
/// The current task number.
///
private int _taskNumber;
///
/// Constructor.
///
public MockTaskBuilder()
{
Reset();
}
///
/// The list of tasks executed.
///
public List ExecutedTasks
{
get;
set;
}
///
/// The list of OnError tasks.
///
public List ErrorTasks
{
get;
set;
}
///
/// The task ordinal to fail on.
///
public int FailTaskNumber
{
get;
set;
}
///
/// Resets the mock task builder to its clean state.
///
public void Reset()
{
ErrorTasks = new List();
ExecutedTasks = new List();
FailTaskNumber = -1;
_taskNumber = 0;
}
#region ITaskBuilder Members
///
/// Executes the task.
///
public Task ExecuteTask(TargetLoggingContext targetLoggingContext, BuildRequestEntry requestEntry, ITargetBuilderCallback targetBuilderCallback, ProjectTargetInstanceChild task, TaskExecutionMode mode, Lookup lookupForInference, Lookup lookupForExecution, CancellationToken cancellationToken)
{
if (cancellationToken.IsCancellationRequested)
{
return Task.FromResult(new WorkUnitResult(WorkUnitResultCode.Canceled, WorkUnitActionCode.Stop, null));
}
ProjectOnErrorInstance errorTask = task as ProjectOnErrorInstance;
if (null != errorTask)
{
ErrorTasks.Add(errorTask);
}
else
{
ProjectTaskInstance taskInstance = task as ProjectTaskInstance;
ExecutedTasks.Add(taskInstance);
if ((mode & TaskExecutionMode.InferOutputsOnly) == TaskExecutionMode.InferOutputsOnly)
{
lookupForInference.AddNewItem(new ProjectItemInstance(requestEntry.RequestConfiguration.Project, taskInstance.Name + "_Item", "Item", task.Location.File));
}
else if ((mode & TaskExecutionMode.ExecuteTaskAndGatherOutputs) == TaskExecutionMode.ExecuteTaskAndGatherOutputs)
{
lookupForExecution.AddNewItem(new ProjectItemInstance(requestEntry.RequestConfiguration.Project, taskInstance.Name + "_Item", "Item", task.Location.File));
}
if (String.Equals(taskInstance.Name, "CallTarget", StringComparison.OrdinalIgnoreCase))
{
taskInstance.GetParameter("Targets");
char[] splitter = new char[] { ';' };
targetBuilderCallback.LegacyCallTarget(taskInstance.GetParameter("Targets").Split(splitter), false, taskInstance.Location);
}
_taskNumber++;
if (FailTaskNumber == _taskNumber)
{
if (taskInstance.ContinueOnError == "True")
{
return Task.FromResult(new WorkUnitResult(WorkUnitResultCode.Failed, WorkUnitActionCode.Continue, null));
}
return Task.FromResult(new WorkUnitResult(WorkUnitResultCode.Failed, WorkUnitActionCode.Stop, null));
}
}
return Task.FromResult(new WorkUnitResult(WorkUnitResultCode.Success, WorkUnitActionCode.Continue, null));
}
#endregion
#region IBuildComponent Members
///
/// Sets the component host.
///
/// The component host
public void InitializeComponent(IBuildComponentHost host)
{
_host = host;
}
///
/// Shuts down the component.
///
public void ShutdownComponent()
{
_host = null;
}
#endregion
}
}