using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using Tango.PMR.FirmwareUpgrade; public static class VersionPackageDescriptorExtensions { /// /// Throws exception if the tfp package is invalid. /// /// The descriptor. /// /// TFP package contains multiple MCU files. /// or /// TFP package MCU does specify any version. /// or /// public static void Validate(this VersionPackageDescriptor descriptor) { if (descriptor.FileDescriptors.Where(x => x.Destination == VersionFileDestination.Mcu).Count() > 1) { throw new InvalidDataException("TFP package contains multiple MCU files."); } if (descriptor.FileDescriptors.GroupBy(x => x.FileName).Any(x => x.Count() > 1)) { throw new InvalidDataException("The TPF package cannot contain duplicate file names."); } foreach (var item in descriptor.FileDescriptors) { try { Version.Parse(item.Version); } catch { throw new InvalidDataException($"Invalid version ({item.Version}) for file '{item.FileName}'."); } } } /// /// Gets the MCU version. /// /// The descriptor. /// public static Version GetMcuVersion(this VersionPackageDescriptor descriptor) { descriptor.Validate(); var mcuFile = descriptor.FileDescriptors.SingleOrDefault(x => x.Destination == VersionFileDestination.Mcu); if (mcuFile == null) { throw new FileNotFoundException("Could not retrieve TFP package version. MCU file not found."); } return Version.Parse(mcuFile.Version); } /// /// Determines whether this package contains an MCU file. /// /// The descriptor. public static bool ContainsMcu(this VersionPackageDescriptor descriptor) { return descriptor.FileDescriptors.Any(x => x.Destination == VersionFileDestination.Mcu); } /// /// Determines whether this package contains none MCU files. /// /// The descriptor. public static bool ContainsNoneMcu(this VersionPackageDescriptor descriptor) { return descriptor.FileDescriptors.Any(x => x.Destination != VersionFileDestination.Mcu); } }