Lib/helper_common.psm1
<#
.SYNOPSIS Git Helper .DESCRIPTION Git Helper .NOTES Author : hillesheim@n-dimensions.de Version : 2.6 DateLastChanged : 2024-11-11 #> Write-Host "Load nested module 'helper_common' (2.6.) ... "; # ************************************************************************************************ # ************************************************************************************************ #region DECLARATION # ************************************************************************************************ #region TYPES #endregion # ************************************************************************************************ #region USER_VARIABLES #endregion # ************************************************************************************************ #region SYSTEM_VARIABLES #endregion #endregion # ************************************************************************************************ # ************************************************************************************************ #region FUNCTIONS_PUBLIC # ************************************************************************************************ # Function Show-ListSelection { Param( [String[]] $List, [Int[]] $Default = @(), [String] $Title = "PLEASE SELECT" ) Clear-Host; $rtrVal = @(); [Int[]] $selectedNumbers0 = @(); $PresentUi = { Param( [String[]] $List, [Int[]] $Default = @() ) [Int[]] $selectedNumbers1 = @(); # Compute ui [String] $ui = "`n<header_line> `n<title> `n<header_line> `n`n<list_of_choices> <explanation>"; [String] $headerLine = "*" * 70; [String] $listOfChoices = ""; [Int] $listLength = $List.Length; # [Int] $enumLength = [System.Enum]::GetValues($Enum).Length; For ( $i = 0; $i -lt $listLength; $i++ ){ $listOfChoices += "[{0}] {1}" -f ($i + 1).ToString(), $List[$i] + "`n"; } [String] $explanation = ""; $explanation += "`n- Input a single number "; $explanation += "or `n- Input a list of numbers separated by ',' "; If($Default.Length -gt 0){ $explanation += "or `n- Press ENTER for default ({0})" -f ($Default -join ","); } $ui = $ui.Replace("<header_line>", $headerLine); $ui = $ui.Replace("<title>", $Title); $ui = $ui.Replace("<list_of_choices>", $listOfChoices); $ui = $ui.Replace("<explanation>", $explanation); $userChoice = Read-Host -Prompt $ui; # If null, apply default, else user input If ( ($userChoice -eq "") -and ($Default.Length -gt 0) ){ $selectedNumbers1 = $Default; } # If invalid, apply 0 If ( ($userChoice -eq "") -and ($Default.Length -eq 0) ){ $selectedNumbers1 = @(0); } # Check input; try parse Try { If ( $userChoice.Length -gt 0){ [String[]] $stringParts = $userChoice.Split(","); } ForEach ($item in $stringParts) { $item = $item.Trim(); [Int] $number = -1; # Check type [Boolean] $success = [Int]::TryParse($item, [ref] $number); If (-not $success){ $number = 0; } # Check range If(-not ($number -ge 1 -and $number -le $listLength )) { $number = 0; } # Check duplicate If(-not ($selectedNumbers1 -contains $number )){ $selectedNumbers1 += $number; } } } Catch {} # Return Write-Output $selectedNumbers1; } # Run [Int[]] $selectedNumbers0 = $PresentUi.Invoke(@($List,$Default)); # Re-run If($selectedNumbers0 -contains 0){ $rerun = Read-Host ( "Your selection contained 0. Re-run (y|n). Press ENTER for 'n'?") If($rerun -eq "" -or $null -eq $rerun -or $rerun.ToLower() -eq "y"){ [Int[]] $selectedNumbers0 = $PresentUi.Invoke(@($List,$Default)); } } $selectedNumbers0 ` | ForEach-Object { $selection = $List[$_ - 1]; $rtrVal += $selection; } Write-Output $rtrVal; } # ************************************************************************************************ # Enter selection Function Show-EnumSelection { Param( $Enum, [Int[]] $Default = @(), [String] $Title = "PLEASE SELECT" ) $rtrVal = @(); [Int[]] $selectedNumbers0 = @(); $PresentUi = { Param( $Enum, [Int[]] $Default = @() ) [Int[]] $selectedNumbers1 = @(); # Compute ui [String] $ui = "`n<header_line> `n<title> `n<header_line> `n`n<list_of_choices> <explanation>"; [String] $headerLine = "*" * 70; # [String] $prompt = "PLEASE SELECT"; [String] $listOfChoices = ""; [Int] $enumLength = [System.Enum]::GetValues($Enum).Length; For ( $i = 1; $i -lt $enumLength; $i++ ){ $listOfChoices += "[{0}] {1}" -f $i.ToString(), $Enum.GetEnumName($i) + "`n"; } [String] $explanation = ""; $explanation += "`n- Input a single number "; $explanation += "or `n- Input a list of numbers separated by ',' "; If($Default.Length -gt 0){ $explanation += "or `n- Press ENTER for default ({0})" -f ($Default -join ","); } $ui = $ui.Replace("<header_line>", $headerLine); $ui = $ui.Replace("<title>", $Title); $ui = $ui.Replace("<list_of_choices>", $listOfChoices); $ui = $ui.Replace("<explanation>", $explanation); $userChoice = Read-Host -Prompt $ui; # If null, apply default If ( ($userChoice -eq "") -and ($Default.Length -gt 0) ){ $selectedNumbers1 = $Default; } # If invalid, apply 0 If ( ($userChoice -eq "") -and ($Default.Length -eq 0) ){ $selectedNumbers1 = @(0); } # Check input; try parse Try { If ( $userChoice.Length -gt 0){ [String[]] $stringParts = $userChoice.Split(","); } ForEach ($item in $stringParts) { $item = $item.Trim(); [Int] $number = -1; # Check type [Boolean] $success = [Int]::TryParse($item, [ref] $number); If (-not $success){ $number = 0; } # Check range If(-not ($number -ge 1 -and $number -lt $enumLength )) { $number = 0; } # Check duplicate If(-not ($selectedNumbers1 -contains $number )){ $selectedNumbers1 += $number; } } } Catch {} # Return Write-Output $selectedNumbers1; } # Run [Int[]] $selectedNumbers0 = $PresentUi.Invoke(@($Enum,$Default)); # Re-run If($selectedNumbers0 -contains 0){ $rerun = Read-Host ( "Your selection contained {0}. Re-run (y|n). Press ENTER for 'n'?" -f [Enum]::ToObject($Enum, 0)) If($rerun -eq "" -or $null -eq $rerun -or $rerun.ToLower() -eq "y"){ [Int[]] $selectedNumbers0 = $PresentUi.Invoke(@($Enum,$Default)); } } $selectedNumbers0 ` | ForEach-Object { $selection = [Enum]::ToObject($Enum, $_); $rtrVal += $Enum::$selection; } Write-Output $rtrVal; } # ************************************************************************************************ # Get artifact feed url Function Get-ArtifactFeedUrl { Param( [NugetPackage] $NugetPackage, [ArtifactSettings] $ArtifactSettings ) [String] $rtrVal = $null; [String] $feedUrl = $null; # Write-Host ("Compute artifact feed url for '{0}' ... " -f $NugetPackage.Name); # "https://pkgs.dev.azure.com/n-dimensions-dev/NAF/_packaging/NdimensionsAutomationModules/nuget/v3/index.json" [String] $templateFeedUrl = "<DEVOPS_URL>/<ORGANIZATION>/<PROJECT>/_packaging/<FEED>/nuget/v3/index.json"; $feedUrl = $templateFeedUrl.Replace("<DEVOPS_URL>", $ArtifactSettings.DevopsUrl); $feedUrl = $feedUrl.Replace("<ORGANIZATION>", $ArtifactSettings.OrganizationName); $feedUrl = $feedUrl.Replace("<PROJECT>", $ArtifactSettings.ProjectName); $feedUrl = $feedUrl.Replace("<FEED>", $NugetPackage.ArtifactFeedId); # Return $rtrVal = $feedUrl; Write-Output $rtrVal; } # ************************************************************************************************ # Open script editor Function Open-ScriptEditor { Param( [String] $Filepath ) [String] $fileName = (Get-Item -Path $Filepath).Name; Write-Host ("Edit script '{0}' ... " -f $fileName); notepad "$Filepath"; } #endregion #region EXPORT # Export-ModuleMember -Function 'Get-PathFromPattern'; Export-ModuleMember -Function 'Show-EnumSelection'; Export-ModuleMember -Function 'Show-ListSelection'; Export-ModuleMember -Function 'Get-ArtifactFeedUrl'; #endregion |