Private/Get-OriAzBopExternalDependenciesParamBasedOnTags.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
<#
.SYNOPSIS
    Get module into Version param structure
 
.PARAMETER ModuleName
    Name of the module
 
.PARAMETER Tags
    Array of module tags
#>

function Get-OriAzBopExternalDependenciesParamBasedOnTags {
    [CmdLetBinding()]
    [OutputType([Hashtable])]
    param (        
        [Parameter(Mandatory = $true, HelpMessage = "Name of the module")]
        [string] $ModuleName,

        [Parameter(Mandatory = $true, HelpMessage = "Array of module tags")]
        [AllowEmptyCollection()]
        [String[]] $Tags
    )
    $ErrorActionPreference = 'Stop'    
    Write-Verbose '-- begin - Get-OriAzBopExternalDependenciesParamBasedOnTags --'
    Write-Debug "ModuleName: $ModuleName"
    Write-Debug "Tags: $(ConvertTo-Json $Tags)"

    [Hashtable] $NewInstallParam = @{Name = $ModuleName }

    # Get only tags releavant to this module
    $SeachMask = 'ExternalModuleDependencies_{0}_*' -f $ModuleName
    [String[]] $relevantTags = $Tags | Where-Object { $_ -ilike $SeachMask } | Sort-Object -Unique

    # Get parameters relevant to the Module
    if ($relevantTags.Count -gt 0) {
        $moduleInstallParam = $relevantTags.Substring(("ExternalModuleDependencies_{0}_" -f $ModuleName).Length)
    }

    # Try to parse each tag to get values for next processing
    foreach ($item in $moduleInstallParam) {
        [string[]] $paramArrray = $item -split '-', 2
        if (![string]::IsNullOrEmpty($paramArrray[0])) {
            switch ($paramArrray[0]) {
                'RequiredVersion' { $NewInstallParam += @{ RequiredVersion = [Version]$paramArrray[1] }; break }
                'MaximumVersion' { $NewInstallParam += @{ MaximumVersion = [Version]$paramArrray[1] }; break }
                'MinimumVersion' { $NewInstallParam += @{ MinimumVersion = [Version]$paramArrray[1] }; break }
                'Version' { $NewInstallParam += @{ Version = [Version]$paramArrray[1] }; break }
                'AllowPrerelease' { $NewInstallParam += @{ AllowPrerelease = [boolean]::Parse($paramArrray[1]) }; break }
                'Guid' { $NewInstallParam += @{ Guid = $paramArrray[1] }; break }
                'Prerelease' { $NewInstallParam += @{ Prerelease = $paramArrray[1] }; break }
                Default { throw "Unknown tag [$item] relevant to the Module $ModuleName" }
            }
        }
    }

    Write-Debug (ConvertTo-Json -InputObject $NewInstallParam)
    Write-Verbose '-- End of Get-OriAzBopExternalDependenciesParamBasedOnTags --'
    return $NewInstallParam
}