Framework/Helpers/ModuleHelper.ps1

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
<#
.Description
# Module helper function
# Provides common function to module version
#>


class ModuleHelper: EventBase {
    
    hidden ModuleHelper() {
    }

    hidden static [ModuleHelper] $Instance = $null;

    static [ModuleHelper] GetInstance() {
        if ( $null  -eq [ModuleHelper]::Instance  ) {
            [ModuleHelper]::Instance = [ModuleHelper]::new();
        }
        return [ModuleHelper]::Instance
    }
    
    #Function to get current running module
    static [string] GetModuleName()
    {
        if([ModuleHelper]::GetInstance().InvocationContext)
        {
            return [ModuleHelper]::GetInstance().InvocationContext.MyCommand.Module.Name;
        }

        throw [System.ArgumentException] "The parameter 'InvocationContext' is not set"
    }

    static [CommandDetails] GetCommandMetadata()
    {
        if([ModuleHelper]::GetInstance().InvocationContext)
        {
            $commandNoun = [ModuleHelper]::GetInstance().InvocationContext.MyCommand.Noun
            if(-not [string]::IsNullOrWhiteSpace([ModuleHelper]::GetInstance().InvocationContext.MyCommand.Module.Prefix))
            {
                # Remove the module prefix from command name
                $commandNoun = $commandNoun.TrimStart([ModuleHelper]::GetInstance().InvocationContext.MyCommand.Module.Prefix);
            }

            return [CommandHelper]::Mapping | 
                                Where-Object { $_.Noun -eq $commandNoun -and $_.Verb -eq [ModuleHelper]::GetInstance().InvocationContext.MyCommand.Verb } | 
                                Select-Object -First 1;
        }

        throw [System.ArgumentException] "The parameter 'InvocationContext' is not set"
    }

    static [bool] IsLatestVersionRequired()
    {
        if([ModuleHelper]::GetInstance().InvocationContext)
        {
            $commandNoun = [ModuleHelper]::GetInstance().InvocationContext.MyCommand.Noun
            if(-not [string]::IsNullOrWhiteSpace([ModuleHelper]::GetInstance().InvocationContext.MyCommand.Module.Prefix))
            {
                # Remove the module prefix from command name
                $commandNoun = $commandNoun.TrimStart([ModuleHelper]::GetInstance().InvocationContext.MyCommand.Module.Prefix);
            }

            $mapping = [CommandHelper]::Mapping | 
                                Where-Object { $_.Noun -eq $commandNoun -and $_.Verb -eq [ModuleHelper]::GetInstance().InvocationContext.MyCommand.Verb } | 
                                Select-Object -First 1;
            return $mapping.IsLatestRequired;
        }

        throw [System.ArgumentException] "The parameter 'InvocationContext' is not set"
    }

    static [System.Version] GetCurrentModuleVersion()
    {
        if([ModuleHelper]::GetInstance().InvocationContext)
        {
            return [System.Version] ([ModuleHelper]::GetInstance().InvocationContext.MyCommand.Version);
        }

        # Return default version which is 0.0.
        return [System.Version]::new();
    }
}