Core/ThirdPartyDependencies-Module.psm1

$ErrorActionPreference = "Stop"
function ConfigureChoco {
    try {
        choco | Out-Null
        choco upgrade chocolatey --limitoutput --confirm | Out-Null
    }
    catch {
        Invoke-WebRequest https://chocolatey.org/install.ps1 -UseBasicParsing | Invoke-Expression | Out-Null
    }
    choco feature enable -n allowGlobalConfirmation | Out-Null
    RefreshEnvironment
}

function ConfigurePSGallery {
    $latestNuget = Find-PackageProvider -Name "NuGet" -Force
    $installedNuget = Get-PackageProvider -Name "Nuget" -ListAvailable | Select-Object -First 1
    if ($latestNuget.version -gt $installedNuget.version) {
        Install-PackageProvider -Name "NuGet" -Source "PSGallery" -RequiredVersion $latestNuget.version -Force | Out-Null
    }
   
    Import-PackageProvider -Name "Nuget" -RequiredVersion $latestNuget.version -Force | Out-Null

    # Ensure Trusted, so that users are not prompted before installing modules from that source.
    Set-PSRepository -Name PSGallery -InstallationPolicy Trusted

    $latestPSGet = Find-PackageProvider -Name "PowerShellGet"
    $installedPSGet = Get-PackageProvider -Name "PowerShellGet" -ListAvailable | Select-Object -First 1
    if ($latestPSGet.version -gt $installedPSGet.version) {
        Install-PackageProvider -Name "PowerShellGet" -Source "PSGallery" -RequiredVersion $latestPSGet.version -Force | Out-Null
    }
    Import-PackageProvider -Name "PowerShellGet" -RequiredVersion $latestPSGet.version -Force | Out-Null
}

function GetRequiredSIFVersion {
    if ($null -ne $SAFConfiguration.sitecore.version) {
        if ($SAFConfiguration.sitecore.version.Contains("9.0")) {
            return $SAFSitecore90SIFVersion
        }
        if ($SAFConfiguration.sitecore.version.Contains("9.1")) {
            return $SAFSitecore91SIFVersion
        }
    }
   
    return $SAFSitecore90SIFVersion
}

function ImportSIF {
    EnableIISAdministration
    $repositoryURL = "https://sitecore.myget.org/F/sc-powershell/api/v2"
    $repositoryName = "SitecoreGallery"

    $existing = Get-PSRepository -Name $repositoryName -ErrorAction Ignore
    if ($null -eq $existing) {
        Register-PSRepository -Name $repositoryName -SourceLocation $repositoryURL -InstallationPolicy Trusted
    }
    
    # Ensure Trusted, so that users are not prompted before installing modules from that source.
    Set-PSRepository -Name $repositoryName -InstallationPolicy Trusted

    $requiredSIFVersion = GetRequiredSIFVersion
    $sifModules = Get-Module -Name SitecoreInstallFramework -ListAvailable
    $installRequiredSIFVersion = $true

    if ($null -ne $sifModules) {
        foreach ($module in $sifModules) {
            if ($module.Version -eq $requiredSIFVersion) {
                $installRequiredSIFVersion = $false
            }
        }
    }
    
    if ($installRequiredSIFVersion) {
        Install-Module -Name SitecoreInstallFramework -RequiredVersion $requiredSIFVersion -Repository $repositoryName -AllowClobber -Force
    }
   
    Get-Module -Name SitecoreInstallFramework | Remove-Module -Force
    Import-Module -Name SitecoreInstallFramework -RequiredVersion $requiredSIFVersion -Force -Global
}

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