// Copyright Epic Games, Inc. All Rights Reserved.
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 zip task
///
public class UnzipTaskParameters
{
///
/// Path to the zip file to extract.
///
[TaskParameter(ValidationType = TaskParameterValidationType.FileSpec)]
public string ZipFile { get; set; }
///
/// Output directory for the extracted files.
///
[TaskParameter]
public DirectoryReference ToDir { get; set; }
///
/// Whether or not to use the legacy unzip code.
///
[TaskParameter(Optional = true)]
public bool UseLegacyUnzip { get; set; } = false;
///
/// Whether or not to overwrite files during unzip.
///
[TaskParameter(Optional = true)]
public bool OverwriteFiles { get; set; } = true;
///
/// Tag to be applied to the extracted files.
///
[TaskParameter(Optional = true, ValidationType = TaskParameterValidationType.TagList)]
public string Tag { get; set; }
}
///
/// Extract files from a zip archive.
///
[TaskElement("Unzip", typeof(UnzipTaskParameters))]
public class UnzipTask : BgTaskImpl
{
readonly UnzipTaskParameters _parameters;
///
/// Constructor
///
/// Parameters for this task
public UnzipTask(UnzipTaskParameters 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)
{
DirectoryReference toDir = _parameters.ToDir;
// Find all the zip files
IEnumerable zipFiles = ResolveFilespec(Unreal.RootDirectory, _parameters.ZipFile, tagNameToFileSet);
// Extract the files
HashSet outputFiles = new HashSet();
foreach (FileReference zipFile in zipFiles)
{
if (_parameters.UseLegacyUnzip)
{
outputFiles.UnionWith(CommandUtils.LegacyUnzipFiles(zipFile.FullName, toDir.FullName, _parameters.OverwriteFiles).Select(x => new FileReference(x)));
}
else
{
outputFiles.UnionWith(CommandUtils.UnzipFiles(zipFile, toDir, _parameters.OverwriteFiles));
}
}
// Apply the optional tag to the produced archive
foreach (string tagName in FindTagNamesFromList(_parameters.Tag))
{
FindOrAddTagSet(tagNameToFileSet, tagName).UnionWith(outputFiles);
}
// Add the archive to the set of build products
buildProducts.UnionWith(outputFiles);
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.ZipFile);
}
///
/// Find all the tags which are modified by this task
///
/// The tag names which are modified by this task
public override IEnumerable FindProducedTagNames()
{
return FindTagNamesFromList(_parameters.Tag);
}
}
}