aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.PMR/ExtensionMethods/VersionPackageDescriptorExtensions.cs
blob: 78b1797365af907de836aff84b765bee49dcf165 (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
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>
    /// Validates the specified TFP package.
    /// </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.");
        }

        var mcuFile = descriptor.FileDescriptors.SingleOrDefault(x => x.Destination == VersionFileDestination.Mcu);

        if (String.IsNullOrWhiteSpace(mcuFile.Version))
        {
            throw new InvalidDataException("TFP package MCU does specify any version.");
        }

        try
        {
            Version.Parse(mcuFile.Version);
        }
        catch
        {
            throw new InvalidDataException($"TFP package MCU version '{mcuFile.Version}' is invalid.");
        }
    }

    /// <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);

        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);
    }
}