// Copyright Epic Games, Inc. All Rights Reserved.
using AutomationTool;
using System;
using System.Collections.Generic;
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 zip task
///
public class UnzipTaskParameters
{
///
/// Path to the zip file to extract.
///
[TaskParameter(ValidationType = TaskParameterValidationType.FileSpec)]
public string ZipFile;
///
/// Output directory for the extracted files.
///
[TaskParameter]
public DirectoryReference ToDir;
///
/// Whether or not to use the legacy unzip code.
///
[TaskParameter(Optional = true)]
public bool UseLegacyUnzip = false;
///
/// Tag to be applied to the extracted files.
///
[TaskParameter(Optional = true, ValidationType = TaskParameterValidationType.TagList)]
public string Tag;
}
///
/// Extract files from a zip archive.
///
[TaskElement("Unzip", typeof(UnzipTaskParameters))]
public class UnzipTask : CustomTask
{
///
/// Parameters for this task
///
UnzipTaskParameters Parameters;
///
/// Constructor
///
/// Parameters for this task
public UnzipTask(UnzipTaskParameters 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)
{
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).Select(x => new FileReference(x)));
}
else
{
OutputFiles.UnionWith(CommandUtils.UnzipFiles(ZipFile, ToDir));
}
}
// 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);
}
///
/// 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);
}
}
}