aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.PMR/ExtensionMethods/VersionPackageDescriptorExtensions.cs
blob: 8c66b1c1267f53e17a75d42a097852b393e3f760 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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
{
    /// <summary>
    /// Throws exception if the tfp package is invalid.
    /// </summary>
    /// <param name="descriptor">The descriptor.</param>
    /// <exception cref="InvalidDataException">
    /// TFP package contains multiple MCU files.
    /// or
    /// TFP package MCU does specify any version.
    /// or
    /// </exception>
    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}'.");
            }
        }
    }

    /// <summary>
    /// Gets the MCU version.
    /// </summary>
    /// <param name="descriptor">The descriptor.</param>
    /// <returns></returns>
    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);
    }

    /// <summary>
    /// Determines whether this package contains an MCU file.
    /// </summary>
    /// <param name="descriptor">The descriptor.</param>
    public static bool ContainsMcu(this VersionPackageDescriptor descriptor)
    {
        return descriptor.FileDescriptors.Any(x => x.Destination == VersionFileDestination.Mcu);
    }

    /// <summary>
    /// Determines whether this package contains none MCU files.
    /// </summary>
    /// <param name="descriptor">The descriptor.</param>
    public static bool ContainsNoneMcu(this VersionPackageDescriptor descriptor)
    {
        return descriptor.FileDescriptors.Any(x => x.Destination != VersionFileDestination.Mcu);
    }
}