PackageProvider.Utils.ps1

#Requires -RunAsAdministrator

function Use-ChocoPackageProvider {
    <#
    .SYNOPSIS
    Installs ChocolateyGet PackageProvider if one is not installed.
    #>


    Set-StrictMode -Version Latest
    $ErrorActionPreference = "Stop"
    $PSDefaultParameterValues['*:ErrorAction'] = 'Stop'

    $Provider = 'ChocolateyGet'

    Import-Module PackageManagement

    $providers = Get-PackageProvider -Verbose -ListAvailable
    if ($providers.Name -notcontains $Provider) {
        $a = Find-PackageProvider -Name $Provider -Verbose -ForceBootstrap

        if ($a.Name -eq $Provider) {
            Write-Host "Installing $Provider PackageProvider ..."

            Install-PackageProvider $Provider -Verbose -Force | Out-Null

            Write-Host "$Provider PackageProvider has been installed"
        }
        else {
            throw "$Provider PackageProvider has not been found"
        }
    }
    else {
        Write-Host "$Provider PackageProvider is installed already"
    }
}

function Use-ChocoPackage
{
    <#
    .SYNOPSIS
    Installs a package using ChocolateyGet PackageProvider if one is not installed.
    Returns $true if package provider has been installed.
    Returns $false if package provider is installed already.
    Throws an error if installation fails
    #>


    [CmdletBinding()]
    param
    (
      [parameter(Mandatory = $true, Position = 0)] [String]$Name,
      [parameter(Mandatory = $false, Position = 1)] [System.Version] $MinimumVersion = $null,
      [parameter(Mandatory = $false, Position = 2)] [String] $Params = ''
    )

    Set-StrictMode -Version Latest
    $ErrorActionPreference = "Stop"
    $PSDefaultParameterValues['*:ErrorAction'] = 'Stop'

    $Provider = 'ChocolateyGet'

    $versionText = ''
    if($MinimumVersion) {
        $versionText = " v$MinimumVersion"
    }

    Write-Host "Checking whether $Name$versionText package is installed ..."

    [System.Version]$installedPackageVersion = $null

    if($MinimumVersion) {
        $installedPackageVersion = Get-Package -Name $Name -MinimumVersion $MinimumVersion -ProviderName $Provider -ErrorAction SilentlyContinue -ForceBootstrap -Force -Verbose | % { $_.Version } | Select-Object -First 1
    } else {
        $installedPackageVersion = Get-Package -Name $Name -ProviderName $Provider -ErrorAction SilentlyContinue -ForceBootstrap -Force -Verbose  | % { $_.Version } | Select-Object -First 1
    }

    if (!$installedPackageVersion) {
        Import-PackageProvider -Name $Provider -Force | Out-Null

        Write-Host "Installing $Name$versionText package ..."

        if($MinimumVersion) {
            Install-Package -Name $Name -ProviderName $Provider -MinimumVersion $MinimumVersion -Verbose -AdditionalArguments $Params -Force | Out-Null
        } else {
            Install-Package -Name $Name -ProviderName $Provider -Verbose -AdditionalArguments $Params -Force | Out-Null
        }

        Write-Host "$Name$versionText package has been installed"

        . $PSScriptRoot/Update-SessionEnvironment.ps1
        Update-SessionEnvironment

        return $true
    }
    else {
        Write-Host "$Name v$installedPackageVersion package is installed already"
    }

    return $false
}