// 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 Microsoft.Extensions.Logging;
namespace AutomationTool.Tasks
{
///
/// Parameters for a zip task
///
public class ZipTaskParameters
{
///
/// The directory to read compressed files from.
///
[TaskParameter]
public DirectoryReference FromDir { get; set; }
///
/// List of file specifications separated by semicolons (for example, *.cpp;Engine/.../*.bat), or the name of a tag set. Relative paths are taken from FromDir.
///
[TaskParameter(Optional = true, ValidationType = TaskParameterValidationType.FileSpec)]
public string Files { get; set; }
///
/// List of files that should have an executable bit set.
///
[TaskParameter(Optional = true, ValidationType = TaskParameterValidationType.FileSpec)]
public string ExecutableFiles { get; set; }
///
/// The zip file to create.
///
[TaskParameter]
public FileReference ZipFile { get; set; }
///
/// Tag to be applied to the created zip file.
///
[TaskParameter(Optional = true, ValidationType = TaskParameterValidationType.TagList)]
public string Tag { get; set; }
}
///
/// Compresses files into a zip archive.
///
[TaskElement("Zip", typeof(ZipTaskParameters))]
public class ZipTask : BgTaskImpl
{
///
/// Parameters for this task
///
readonly ZipTaskParameters _parameters;
///
/// Constructor
///
/// Parameters for this task
public ZipTask(ZipTaskParameters 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)
{
// Find all the input files
List files;
if (_parameters.Files == null)
{
files = DirectoryReference.EnumerateFiles(_parameters.FromDir, "*", System.IO.SearchOption.AllDirectories).ToList();
}
else
{
files = ResolveFilespec(_parameters.FromDir, _parameters.Files, tagNameToFileSet).ToList();
}
// Create the zip file
Logger.LogInformation("Adding {NumFiles} files to {ZipFile}...", files.Count, _parameters.ZipFile);
HashSet executableFiles = null;
if (_parameters.ExecutableFiles != null)
{
executableFiles = ResolveFilespec(_parameters.FromDir, _parameters.ExecutableFiles, tagNameToFileSet);
foreach (FileReference executableFile in files.Intersect(executableFiles))
{
Logger.LogInformation(" Executable file: {File}", executableFile);
}
}
CommandUtils.ZipFiles(_parameters.ZipFile, _parameters.FromDir, files, executableFiles);
// Apply the optional tag to the produced archive
foreach (string tagName in FindTagNamesFromList(_parameters.Tag))
{
FindOrAddTagSet(tagNameToFileSet, tagName).Add(_parameters.ZipFile);
}
// Add the archive to the set of build products
buildProducts.Add(_parameters.ZipFile);
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.Files);
}
///
/// 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);
}
}
}