// Copyright Epic Games, Inc. All Rights Reserved.
using System;
using System.Collections.Generic;
using System.Linq;
using EpicGames.Core;
#pragma warning disable CA2227 // Change x to be read-only by removing the property setter
namespace EpicGames.Horde.Jobs.Templates
{
///
/// Response describing a template
///
public class GetTemplateResponseBase
{
///
/// Name of the template
///
public string Name { get; set; }
///
/// Description for the template
///
public string? Description { get; set; }
///
/// Default priority for this job
///
public Priority? Priority { get; set; }
///
/// Whether to allow preflights of this template
///
public bool AllowPreflights { get; set; }
///
/// Whether to always update issues on jobs using this template
///
public bool UpdateIssues { get; set; }
///
/// The initial agent type to parse the BuildGraph script on
///
public string? InitialAgentType { get; set; }
///
/// Path to a file within the stream to submit to generate a new changelist for jobs
///
public string? SubmitNewChange { get; }
///
/// Parameters for the job.
///
public List Arguments { get; set; }
///
/// List of parameters for this template
///
public List Parameters { get; set; }
///
/// Parameterless constructor for serialization
///
protected GetTemplateResponseBase()
{
Name = null!;
AllowPreflights = true;
Arguments = new List();
Parameters = new List();
}
///
/// Constructor
///
/// The template to construct from
public GetTemplateResponseBase(ITemplate template)
{
Name = template.Name;
Description = template.Description;
Priority = template.Priority;
AllowPreflights = template.AllowPreflights;
UpdateIssues = template.UpdateIssues;
InitialAgentType = template.InitialAgentType;
SubmitNewChange = template.SubmitNewChange;
Arguments = new List(template.Arguments);
Parameters = template.Parameters.ConvertAll(x => CreateParameterResponse(x));
}
static GetTemplateParameterResponse CreateParameterResponse(ITemplateParameter parameter)
{
return parameter switch
{
ITemplateBoolParameter boolParameter
=> new GetTemplateBoolParameterResponse(boolParameter),
ITemplateTextParameter textParameter
=> new GetTemplateTextParameterResponse(textParameter),
ITemplateListParameter listParameter
=> new GetTemplateListParameterResponse(listParameter),
_ => throw new NotImplementedException()
};
}
}
///
/// Response describing a template
///
public class GetTemplateResponse : GetTemplateResponseBase
{
///
/// Unique id of the template
///
public string Id { get; set; }
///
/// Parameterless constructor for serialization
///
protected GetTemplateResponse()
: base()
{
Id = null!;
}
///
/// Constructor
///
/// The template to construct from
public GetTemplateResponse(ITemplate template)
: base(template)
{
Id = template.Hash.ToString();
}
}
///
/// Base class for template parameters
///
[JsonKnownTypes(typeof(GetTemplateBoolParameterResponse), typeof(GetTemplateTextParameterResponse), typeof(GetTemplateListParameterResponse))]
public abstract class GetTemplateParameterResponse : ITemplateParameter
{
void ITemplateParameter.GetArguments(IReadOnlyDictionary parameters, bool scheduledBuild, List arguments)
=> throw new NotSupportedException();
void ITemplateParameter.GetDefaultParameters(Dictionary parameters, bool scheduledBuild)
=> throw new NotSupportedException();
}
///
/// Allows the user to toggle an option on or off
///
[JsonDiscriminator("Bool")]
public class GetTemplateBoolParameterResponse : GetTemplateParameterResponse, ITemplateBoolParameter
{
///
public ParameterId Id { get; set; }
///
public string Label { get; set; }
///
public string? ArgumentIfEnabled { get; set; }
///
public List? ArgumentsIfEnabled { get; set; }
///
public string? ArgumentIfDisabled { get; set; }
///
public List? ArgumentsIfDisabled { get; set; }
///
public bool Default { get; set; }
///
public bool? ScheduleOverride { get; set; }
///
public string? ToolTip { get; set; }
IReadOnlyList? ITemplateBoolParameter.ArgumentsIfEnabled => ArgumentsIfEnabled;
IReadOnlyList? ITemplateBoolParameter.ArgumentsIfDisabled => ArgumentsIfDisabled;
///
/// Default constructor
///
public GetTemplateBoolParameterResponse()
{
Label = String.Empty;
}
///
/// Constructor
///
public GetTemplateBoolParameterResponse(ITemplateBoolParameter parameter)
{
Id = parameter.Id;
Label = parameter.Label;
ArgumentIfEnabled = parameter.ArgumentIfEnabled;
ArgumentsIfEnabled = parameter.ArgumentsIfEnabled?.ToList();
ArgumentIfDisabled = parameter.ArgumentIfDisabled;
ArgumentsIfDisabled = parameter.ArgumentsIfDisabled?.ToList();
Default = parameter.Default;
ScheduleOverride = parameter.ScheduleOverride;
ToolTip = parameter.ToolTip;
}
}
///
/// Free-form text entry parameter
///
[JsonDiscriminator("Text")]
public class GetTemplateTextParameterResponse : GetTemplateParameterResponse, ITemplateTextParameter
{
///
public ParameterId Id { get; set; }
///
public string Label { get; set; }
///
public string Argument { get; set; }
///
public string Default { get; set; }
///
public string? ScheduleOverride { get; set; }
///
public string? Hint { get; set; }
///
public string? Validation { get; set; }
///
public string? ValidationError { get; set; }
///
public string? ToolTip { get; set; }
///
/// Default constructor
///
public GetTemplateTextParameterResponse()
{
Label = String.Empty;
Argument = String.Empty;
Default = String.Empty;
}
///
/// Constructor
///
public GetTemplateTextParameterResponse(ITemplateTextParameter parameter)
{
Id = parameter.Id;
Label = parameter.Label;
Argument = parameter.Argument;
Default = parameter.Default;
ScheduleOverride = parameter.ScheduleOverride;
Hint = parameter.Hint;
Validation = parameter.Validation;
ValidationError = parameter.ValidationError;
ToolTip = parameter.ToolTip;
}
}
///
/// Allows the user to select a value from a constrained list of choices
///
[JsonDiscriminator("List")]
public class GetTemplateListParameterResponse : GetTemplateParameterResponse, ITemplateListParameter
{
///
public string Label { get; }
///
public TemplateListParameterStyle Style { get; }
///
public List Items { get; set; }
///
public string? ToolTip { get; }
IReadOnlyList ITemplateListParameter.Items => Items;
///
/// Default constructor
///
public GetTemplateListParameterResponse()
{
Label = String.Empty;
Items = new List();
}
///
/// Constructor
///
public GetTemplateListParameterResponse(ITemplateListParameter parameter)
{
Label = parameter.Label;
Style = parameter.Style;
Items = parameter.Items.ConvertAll(x => new GetTemplateListParameterItemResponse(x));
ToolTip = parameter.ToolTip;
}
}
///
/// Possible option for a list parameter
///
public class GetTemplateListParameterItemResponse : ITemplateListParameterItem
{
///
public ParameterId Id { get; set; }
///
public string? Group { get; set; }
///
public string Text { get; set; }
///
public string? ArgumentIfEnabled { get; set; }
///
public List? ArgumentsIfEnabled { get; set; }
///
public string? ArgumentIfDisabled { get; set; }
///
public List? ArgumentsIfDisabled { get; set; }
///
public bool Default { get; set; }
///
public bool? ScheduleOverride { get; set; }
IReadOnlyList? ITemplateListParameterItem.ArgumentsIfEnabled => ArgumentsIfEnabled;
IReadOnlyList? ITemplateListParameterItem.ArgumentsIfDisabled => ArgumentsIfDisabled;
///
/// Default constructor
///
public GetTemplateListParameterItemResponse()
{
Text = String.Empty;
}
///
/// Constructor
///
public GetTemplateListParameterItemResponse(ITemplateListParameterItem item)
{
Id = item.Id;
Group = item.Group;
Text = item.Text;
ArgumentIfEnabled = item.ArgumentIfEnabled;
ArgumentsIfEnabled = item.ArgumentsIfEnabled?.ToList();
ArgumentIfDisabled = item.ArgumentIfDisabled;
ArgumentsIfDisabled = item.ArgumentsIfDisabled?.ToList();
Default = item.Default;
ScheduleOverride = item.ScheduleOverride;
}
}
}