using Castle.Core.Internal;
using Hast.Samples.Consumer.Attributes;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace Hast.Samples.Consumer.Models;
///
/// This class contains the configuration options you can set through the command line arguments and the terminal GUI.
/// The arguments are all case-insensitive and start with a single dash. The next argument after it is the value. The
/// lists additional aliases. For example -devicename "Nexys A7" or -device
/// "Nexys A7".
///
public class ConsumerConfiguration
{
private const string StorageFileName = nameof(ConsumerConfiguration) + ".json";
[Argument("device")]
[Hint(
"Which supported hardware device to use? If you leave this empty the first one will be used. If you're",
"testing Hastlayer locally then most possibly you'll need to use the \"Nexys A7\" or \"Nexys4 DDR\" devices;" +
"for high-performance local or cloud FPGAs, or embedded devices, see the docs.")]
public string DeviceName { get; set; }
[Argument("sample")]
[Hint(
"Which sample algorithm to transform and run? Choose one. Currently the GenomeMatcher sample is not",
"up-to-date enough and shouldn't be really taken as good examples (check out the other ones). If you want",
"to learn more about what each sample does, check out matching *SampleRunner.cs file in the project's",
"SampleRunners directory.")]
public Sample SampleToRun { get; set; }
///
/// Gets or sets the value of .
///
[Argument("name", "label")]
[Hint("This label helps you identify individual builds when you run this program in a batch with different configurations")]
public string BuildLabel { get; set; }
[Argument("build")]
[Hint(
"Do you want to skip the sample execution? This flag is useful if you are cross-compiling a sample with",
"this program for a different machine where you can use the",
nameof(SingleBinaryPath),
"option or the corresponding \"-bin\" command line switch.")]
public bool GenerateHardwareOnly { get; set; }
[Argument("verify")]
[Hint(
"Do you want to check the FPGA results against a CPU run? If there are any discrepancies the program will",
"throw an exception with details.")]
public bool VerifyResults { get; set; }
[Argument]
[Hint(
"Specify a path here where the hardware framework is located. The file describing the hardware to be",
"generated will be saved there as well as anything else necessary. If the path is relative (like the",
"default) then the file will be saved along this project's executable in the bin output directory.\nYou",
"should always run this program from its own directory. In other words the working directory should be",
"where the Hast.Samples.Consumer.dll is located. Otherwise you will see unexpected issues with this and",
"other relative paths.")]
public string HardwareFrameworkPath { get; set; } = "HardwareFramework";
[Argument("bin", "binary")]
[Hint(
"Do you already have a cross-compiled binary? Enter the path here and the driver will use it instead of",
"going through the normal hardware generation build process. ")]
public string SingleBinaryPath { get; set; }
///
/// Gets or sets the name of this configuration, if you want to save it for later reuse with the -load
/// argument.
///
///
///
/// This is because it's only used by to immediately save
/// the parsed object and doesn't need to be made available outside.
///
///
[Argument("save")]
private string SaveName { get; set; }
///
/// Gets a lazy string dictionary that maps the property names that have in this class
/// to their hint texts.
///
public static Lazy> HintDictionary { get; } = new(() =>
GetPropertyAttributes().ToDictionary(pair => pair.Property.Name, pair => pair.Attribute.Text));
private static IEnumerable<(PropertyInfo Property, T Attribute)> GetPropertyAttributes()
where T : Attribute =>
typeof(ConsumerConfiguration)
.GetProperties(BindingFlags.Public | BindingFlags.Instance)
.Select(property => (Property: property, Attribute: property.GetCustomAttributes().FirstOrDefault()))
.Where(pair => pair.Attribute != null);
///
/// Returns new instance of with values set according to the arguments in
/// .
///
/// The command line arguments received from the shell.
///
/// The saved configs loaded from the ConsumerConfiguration.json file.
///
public static ConsumerConfiguration FromCommandLine(
IList args,
IDictionary consumerConfigurations)
{
var loadIndex = args
.Select((argument, index) => (Argument: argument.ToUpperInvariant(), Index: index))
.Aggregate(-1, (current, item) => current == -1 && item.Argument == "-LOAD" ? item.Index : current);
if (loadIndex >= 0 && loadIndex + 1 < args.Count)
{
return consumerConfigurations[args[loadIndex + 1]];
}
var configuration = new ConsumerConfiguration();
var properties = GetPropertyAttributes()
.ToDictionary(pair => pair.Property, pair => pair.Attribute.Aliases);
foreach (var (property, aliases) in properties)
{
var names = aliases
.Concat(new[] { property.Name.ToUpperInvariant() })
.Select(name => "-" + name)
.ToList();
for (int i = 0; i < args.Count; i++)
{
if (!names.Contains(args[i].ToUpperInvariant())) continue;
var value = property.PropertyType == typeof(bool) ? (object)true : args[i + 1];
if (property.PropertyType.IsEnum) value = Enum.Parse(property.PropertyType, (string)value);
property.SetValue(configuration, value);
}
}
if (!configuration.SaveName.IsNullOrEmpty())
{
consumerConfigurations[configuration.SaveName] = configuration;
SaveConfigurations(consumerConfigurations);
}
return configuration;
}
public static async Task> LoadConfigurationsAsync()
{
var configurations = File.Exists(StorageFileName)
? JsonConvert.DeserializeObject>(
await File.ReadAllTextAsync(StorageFileName))
: new Dictionary();
// Make the name case-insensitive.
configurations = new Dictionary(
configurations,
StringComparer.InvariantCultureIgnoreCase);
return configurations;
}
public static void SaveConfigurations(IDictionary configurations)
{
using var writer = new StreamWriter(StorageFileName, append: false, Encoding.UTF8);
var serializer = new JsonSerializer { Formatting = Formatting.Indented };
serializer.Serialize(writer, configurations);
}
}