// Copyright Epic Games, Inc. All Rights Reserved.
using AutomationTool;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using EpicGames.Core;
using UnrealBuildTool;
using UnrealBuildBase;
namespace BuildGraph.Tasks
{
///
/// Parameters for a copy task
///
public class DeleteTaskParameters
{
///
/// List of file specifications separated by semicolons (for example, *.cpp;Engine/.../*.bat), or the name of a tag set
///
[TaskParameter(Optional = true, ValidationType = TaskParameterValidationType.FileSpec)]
public string Files;
///
/// List of directory names
///
[TaskParameter(Optional = true)]
public string Directories;
///
/// Whether to delete empty directories after deleting the files. Defaults to true.
///
[TaskParameter(Optional = true)]
public bool DeleteEmptyDirectories = true;
///
/// Whether or not to use verbose logging.
///
[TaskParameter(Optional = true)]
public bool Verbose = false;
}
///
/// Delete a set of files.
///
[TaskElement("Delete", typeof(DeleteTaskParameters))]
public class DeleteTask : CustomTask
{
///
/// Parameters for this task
///
DeleteTaskParameters Parameters;
///
/// Constructor
///
/// Parameters for this task
public DeleteTask(DeleteTaskParameters 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)
{
if (Parameters.Files != null)
{
// Find all the referenced files and delete them
HashSet Files = ResolveFilespec(Unreal.RootDirectory, Parameters.Files, TagNameToFileSet);
foreach (FileReference File in Files)
{
if (Parameters.Verbose)
{
Log.TraceInformation("Deleting {0}", File.FullName);
}
if (!InternalUtils.SafeDeleteFile(File.FullName))
{
CommandUtils.LogWarning("Couldn't delete file {0}", File.FullName);
}
}
// Try to delete all the parent directories. Keep track of the directories we've already deleted to avoid hitting the disk.
if (Parameters.DeleteEmptyDirectories)
{
// Find all the directories that we're touching
HashSet ParentDirectories = new HashSet();
foreach (FileReference File in Files)
{
ParentDirectories.Add(File.Directory);
}
// Recurse back up from each of those directories to the root folder
foreach (DirectoryReference ParentDirectory in ParentDirectories)
{
for (DirectoryReference CurrentDirectory = ParentDirectory; CurrentDirectory != Unreal.RootDirectory; CurrentDirectory = CurrentDirectory.ParentDirectory)
{
if (!TryDeleteEmptyDirectory(CurrentDirectory))
{
break;
}
}
}
}
}
if (Parameters.Directories != null)
{
foreach (string Directory in Parameters.Directories.Split(';'))
{
if (!String.IsNullOrEmpty(Directory))
{
if (Parameters.Verbose)
{
Log.TraceInformation("Deleting {0}", Directory);
}
DirectoryReference FullDir = new DirectoryReference(Directory);
if (DirectoryReference.Exists(FullDir))
{
FileUtils.ForceDeleteDirectory(FullDir);
}
}
}
}
}
///
/// Deletes a directory, if it's empty
///
/// The directory to check
/// True if the directory was deleted, false if not
static bool TryDeleteEmptyDirectory(DirectoryReference CandidateDirectory)
{
// Make sure the directory exists
if(!DirectoryReference.Exists(CandidateDirectory))
{
return false;
}
// Check if there are any files in it. If there are, don't bother trying to delete it.
if(Directory.EnumerateFiles(CandidateDirectory.FullName).Any() || Directory.EnumerateDirectories(CandidateDirectory.FullName).Any())
{
return false;
}
// Try to delete the directory.
try
{
Directory.Delete(CandidateDirectory.FullName);
return true;
}
catch(Exception Ex)
{
CommandUtils.LogWarning("Couldn't delete directory {0} ({1})", CandidateDirectory.FullName, Ex.Message);
return false;
}
}
///
/// 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.Files);
}
///
/// 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;
}
}
}