Common/Initialization-Module.psm1

Import-Module "$PSScriptRoot\Utils-Module.psm1" -Force
$ErrorActionPreference = "Stop"
function ConfigureChoco {
    Write-Output "SAF loves the latest Chocolatey and is getting it..."
    try {
        choco | Out-Null
        Write-Output "Chocolatey is installed. Checking for updates..."
        choco upgrade chocolatey --limitoutput --confirm
    }
    catch {
        Write-Output "Chocolatey is not installed. Installing..."
        Invoke-WebRequest https://chocolatey.org/install.ps1 -UseBasicParsing | Invoke-Expression
    }
    choco feature enable -n allowGlobalConfirmation | Out-Null
    
    Write-Output "SAF is happy with having the latest Choco!"
}

function ConfigurePSGallery {

    Write-Output "SAF is ensuring latest NuGet package provider..."
    Install-PackageProvider NuGet -Force | Out-Null
    $latestNuget = Get-PackageProvider Nuget -ListAvailable | Select-Object -First 1
    Import-PackageProvider Nuget -RequiredVersion $latestNuget.Version -Force | Out-Null

    # Ensure Trusted, so that users are not prompted before installing modules from that source.
    Write-Output "SAF is trusting PSGallery..."
    Set-PSRepository -Name PSGallery -InstallationPolicy Trusted

    Write-Output "SAF is ensuring latest PowerShellGet package provider..."
    $latestInstalledPSGet = Get-PackageProvider PowerShellGet -ListAvailable | Select-Object -First 1
    $latestOnlinePSGet = Find-PackageProvider PowerShellGet
    if ($latestOnlinePSGet.version -gt $latestInstalledPSGet.version) {
        Install-PackageProvider PowerShellGet -Force | Out-Null
        RestartPowerShell
    }

    Import-PackageProvider PowerShellGet -RequiredVersion $latestInstalledPSGet.Version -Force | Out-Null
}

Export-ModuleMember -Function "ConfigureChoco"
Export-ModuleMember -Function "ConfigurePSGallery"