// Copyright 1998-2019 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 Tools.DotNETCommon; using UnrealBuildTool; namespace BuildGraph.Tasks { /// /// Parameters for a copy task /// public class DeleteTaskParameters { /// /// List of file specifications separated by semicolons (eg. *.cpp;Engine/.../*.bat), or the name of a tag set /// [TaskParameter(ValidationType = TaskParameterValidationType.FileSpec)] public string Files; /// /// Whether to delete empty directories after deleting the files. Defaults to true. /// [TaskParameter(Optional = true)] public bool DeleteEmptyDirectories = true; } /// /// 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) { // Find all the referenced files and delete them HashSet Files = ResolveFilespec(CommandUtils.RootDirectory, Parameters.Files, TagNameToFileSet); foreach(FileReference File in Files) { 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 != CommandUtils.RootDirectory; CurrentDirectory = CurrentDirectory.ParentDirectory) { if(!TryDeleteEmptyDirectory(CurrentDirectory)) { break; } } } } } /// /// 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; } } }