2019-12-26 23:01:54 -05:00
// Copyright Epic Games, Inc. All Rights Reserved.
2016-12-08 08:52:44 -05:00
using AutomationTool ;
2016-11-23 15:34:07 -05:00
using System ;
using System.Collections.Generic ;
using System.Linq ;
using System.Text ;
using System.Threading.Tasks ;
using System.Xml ;
2020-12-21 23:07:37 -04:00
using EpicGames.Core ;
2016-11-23 15:34:07 -05:00
using UnrealBuildTool ;
2021-06-11 18:20:44 -04:00
using UnrealBuildBase ;
2016-11-23 15:34:07 -05:00
namespace BuildGraph.Tasks
{
/// <summary>
/// Parameters for a move task
/// </summary>
public class MoveTaskParameters
{
/// <summary>
2019-10-11 16:59:16 -04:00
/// Optional filter to be applied to the list of input files.
2016-11-23 15:34:07 -05:00
/// </summary>
[TaskParameter(Optional = true, ValidationType = TaskParameterValidationType.FileSpec)]
public string Files ;
/// <summary>
2019-10-11 16:59:16 -04:00
/// The pattern(s) to copy from (for example, Engine/*.txt).
2016-11-23 15:34:07 -05:00
/// </summary>
[TaskParameter(ValidationType = TaskParameterValidationType.FileSpec)]
public string From ;
/// <summary>
2019-10-11 16:59:16 -04:00
/// The directory to copy to.
2016-11-23 15:34:07 -05:00
/// </summary>
[TaskParameter(ValidationType = TaskParameterValidationType.FileSpec)]
public string To ;
2021-02-18 18:13:28 -04:00
/// <summary>
/// Optionally if files should be overwritten, defaults to false.
/// </summary>
[TaskParameter(Optional = true)]
public bool Overwrite = false ;
2016-11-23 15:34:07 -05:00
/// <summary>
2019-10-11 16:59:16 -04:00
/// Tag to be applied to build products of this task.
2016-11-23 15:34:07 -05:00
/// </summary>
[TaskParameter(Optional = true, ValidationType = TaskParameterValidationType.TagList)]
public string Tag ;
}
/// <summary>
/// Moves files from one directory to another.
/// </summary>
[TaskElement("Move", typeof(MoveTaskParameters))]
public class MoveTask : CustomTask
{
/// <summary>
/// Parameters for this task
/// </summary>
MoveTaskParameters Parameters ;
/// <summary>
/// Constructor
/// </summary>
/// <param name="InParameters">Parameters for this task</param>
public MoveTask ( MoveTaskParameters InParameters )
{
Parameters = InParameters ;
}
/// <summary>
/// Execute the task.
/// </summary>
/// <param name="Job">Information about the current job</param>
/// <param name="BuildProducts">Set of build products produced by this node.</param>
/// <param name="TagNameToFileSet">Mapping from tag names to the set of files they include</param>
2017-07-21 12:42:36 -04:00
public override void Execute ( JobContext Job , HashSet < FileReference > BuildProducts , Dictionary < string , HashSet < FileReference > > TagNameToFileSet )
2016-11-23 15:34:07 -05:00
{
// Parse all the source patterns
2021-06-11 18:20:44 -04:00
FilePattern SourcePattern = new FilePattern ( Unreal . RootDirectory , Parameters . From ) ;
2016-11-23 15:34:07 -05:00
// Parse the target pattern
2021-06-11 18:20:44 -04:00
FilePattern TargetPattern = new FilePattern ( Unreal . RootDirectory , Parameters . To ) ;
2016-11-23 15:34:07 -05:00
// Apply the filter to the source files
HashSet < FileReference > Files = null ;
if ( ! String . IsNullOrEmpty ( Parameters . Files ) )
{
SourcePattern = SourcePattern . AsDirectoryPattern ( ) ;
Files = ResolveFilespec ( SourcePattern . BaseDirectory , Parameters . Files , TagNameToFileSet ) ;
}
// Build the file mapping
2018-04-26 14:11:04 -04:00
Dictionary < FileReference , FileReference > TargetFileToSourceFile = FilePattern . CreateMapping ( Files , ref SourcePattern , ref TargetPattern ) ;
2016-11-23 15:34:07 -05:00
// Check we got some files
if ( TargetFileToSourceFile . Count = = 0 )
{
2018-08-14 18:32:34 -04:00
CommandUtils . LogInformation ( "No files found matching '{0}'" , SourcePattern ) ;
2017-07-21 12:42:36 -04:00
return ;
2016-11-23 15:34:07 -05:00
}
// Copy them all
2018-08-14 18:32:34 -04:00
CommandUtils . LogInformation ( "Moving {0} file{1} from {2} to {3}..." , TargetFileToSourceFile . Count , ( TargetFileToSourceFile . Count = = 1 ) ? "" : "s" , SourcePattern . BaseDirectory , TargetPattern . BaseDirectory ) ;
2021-02-18 18:13:28 -04:00
CommandUtils . ParallelMoveFiles ( TargetFileToSourceFile . Select ( x = > new KeyValuePair < FileReference , FileReference > ( x . Value , x . Key ) ) , Parameters . Overwrite ) ;
2016-11-23 15:34:07 -05:00
// Update the list of build products
BuildProducts . UnionWith ( TargetFileToSourceFile . Keys ) ;
// Apply the optional output tag to them
foreach ( string TagName in FindTagNamesFromList ( Parameters . Tag ) )
{
FindOrAddTagSet ( TagNameToFileSet , TagName ) . UnionWith ( TargetFileToSourceFile . Keys ) ;
}
}
/// <summary>
/// Output this task out to an XML writer.
/// </summary>
public override void Write ( XmlWriter Writer )
{
Write ( Writer , Parameters ) ;
}
/// <summary>
/// Find all the tags which are used as inputs to this task
/// </summary>
/// <returns>The tag names which are read by this task</returns>
public override IEnumerable < string > FindConsumedTagNames ( )
{
foreach ( string TagName in FindTagNamesFromFilespec ( Parameters . Files ) )
{
yield return TagName ;
}
}
/// <summary>
/// Find all the tags which are modified by this task
/// </summary>
/// <returns>The tag names which are modified by this task</returns>
public override IEnumerable < string > FindProducedTagNames ( )
{
return FindTagNamesFromList ( Parameters . Tag ) ;
}
}
}