// Copyright 1998-2019 Epic Games, Inc. All Rights Reserved.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using Tools.DotNETCommon;
using UnrealBuildTool;
namespace AutomationTool.Tasks
{
///
/// Parameters for a task which runs a UE4 commandlet
///
public class CommandletTaskParameters
{
///
/// The commandlet name to execute
///
[TaskParameter]
public string Name;
///
/// The project to run the editor with
///
[TaskParameter(Optional = true, ValidationType = TaskParameterValidationType.FileSpec)]
public string Project;
///
/// Arguments to be passed to the commandlet
///
[TaskParameter(Optional = true)]
public string Arguments;
///
/// The editor executable to use. Defaults to the development UE4Editor executable for the current platform.
///
[TaskParameter(Optional = true)]
public FileReference EditorExe;
}
///
/// Spawns the editor to run a commandlet.
///
[TaskElement("Commandlet", typeof(CommandletTaskParameters))]
public class CommandletTask : CustomTask
{
///
/// Parameters for this task
///
CommandletTaskParameters Parameters;
///
/// Construct a new CommandletTask.
///
/// Parameters for this task
public CommandletTask(CommandletTaskParameters InParameters)
{
Parameters = InParameters;
}
///
/// Execute 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 void Execute(JobContext Job, HashSet BuildProducts, Dictionary> TagNameToFileSet)
{
// Get the full path to the project file
FileReference ProjectFile = null;
if(!String.IsNullOrEmpty(Parameters.Project))
{
if(Parameters.Project.EndsWith(".uproject", StringComparison.OrdinalIgnoreCase))
{
ProjectFile = ResolveFile(Parameters.Project);
}
else
{
ProjectFile = NativeProjects.EnumerateProjectFiles().FirstOrDefault(x => x.GetFileNameWithoutExtension().Equals(Parameters.Project, StringComparison.OrdinalIgnoreCase));
}
if(ProjectFile == null || !FileReference.Exists(ProjectFile))
{
throw new BuildException("Unable to resolve project '{0}'", Parameters.Project);
}
}
// Get the path to the editor, and check it exists
FileReference EditorExe;
if(Parameters.EditorExe == null)
{
EditorExe = new FileReference(HostPlatform.Current.GetUE4ExePath("UE4Editor-Cmd.exe"));
}
else
{
EditorExe = Parameters.EditorExe;
}
// Make sure the editor exists
if(!FileReference.Exists(EditorExe))
{
throw new AutomationException("{0} does not exist", EditorExe.FullName);
}
// Run the commandlet
CommandUtils.RunCommandlet(ProjectFile, EditorExe.FullName, Parameters.Name, Parameters.Arguments);
}
///
/// 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()
{
yield break;
}
///
/// Find all the tags which are modified by this task
///
/// The tag names which are modified by this task
public override IEnumerable FindProducedTagNames()
{
yield break;
}
}
}