// Copyright Epic Games, Inc. All Rights Reserved.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Xml;
using EpicGames.Core;
using UnrealBuildBase;
namespace AutomationTool.Tasks
{
///
/// Parameters for a task that executes MSBuild
///
public class MsBuildTaskParameters
{
///
/// The C# project file to compile. Using semicolons, more than one project file can be specified.
///
[TaskParameter]
public string Project { get; set; }
///
/// The configuration to compile.
///
[TaskParameter(Optional = true)]
public string Configuration { get; set; }
///
/// The platform to compile.
///
[TaskParameter(Optional = true)]
public string Platform { get; set; }
///
/// Additional options to pass to the compiler.
///
[TaskParameter(Optional = true)]
public string Arguments { get; set; }
///
/// The MSBuild output verbosity.
///
[TaskParameter(Optional = true)]
public string Verbosity { get; set; } = "minimal";
}
///
/// Executes MsBuild
///
[TaskElement("MsBuild", typeof(MsBuildTaskParameters))]
public class MsBuildTask : BgTaskImpl
{
///
/// Parameters for the task
///
readonly MsBuildTaskParameters _parameters;
///
/// Constructor.
///
/// Parameters for this task
public MsBuildTask(MsBuildTaskParameters parameters)
{
_parameters = parameters;
}
///
/// ExecuteAsync the task.
///
/// Information about the current job
/// Set of build products produced by this node.
/// Mapping from tag names to the set of files they include
public override Task ExecuteAsync(JobContext job, HashSet buildProducts, Dictionary> tagNameToFileSet)
{
// Get the project file
HashSet projectFiles = ResolveFilespec(Unreal.RootDirectory, _parameters.Project, tagNameToFileSet);
foreach (FileReference projectFile in projectFiles)
{
if (!FileReference.Exists(projectFile))
{
throw new AutomationException("Couldn't find project file '{0}'", projectFile.FullName);
}
}
// Build the argument list
List arguments = new List();
if (!String.IsNullOrEmpty(_parameters.Platform))
{
arguments.Add(String.Format("/p:Platform={0}", CommandUtils.MakePathSafeToUseWithCommandLine(_parameters.Platform)));
}
if (!String.IsNullOrEmpty(_parameters.Configuration))
{
arguments.Add(String.Format("/p:Configuration={0}", CommandUtils.MakePathSafeToUseWithCommandLine(_parameters.Configuration)));
}
if (!String.IsNullOrEmpty(_parameters.Arguments))
{
arguments.Add(_parameters.Arguments);
}
if (!String.IsNullOrEmpty(_parameters.Verbosity))
{
arguments.Add(String.Format("/verbosity:{0}", _parameters.Verbosity));
}
arguments.Add("/nologo");
// Build all the projects
foreach (FileReference projectFile in projectFiles)
{
CommandUtils.MsBuild(CommandUtils.CmdEnv, projectFile.FullName, String.Join(" ", arguments), null);
}
return Task.CompletedTask;
}
///
/// Output this task out to an XML writer.
///
public override void Write(XmlWriter writer)
{
Write(writer, _parameters);
}
///
/// Find all the tags which are used as inputs to this task
///
/// The tag names which are read by this task
public override IEnumerable FindConsumedTagNames()
{
return FindTagNamesFromFilespec(_parameters.Project);
}
///
/// Find all the tags which are modified by this task
///
/// The tag names which are modified by this task
public override IEnumerable FindProducedTagNames()
{
return Enumerable.Empty();
}
}
}