Functions/Private/downloadfns.ps1



function Set-EnvironmentVariablesFromTool {
    [CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'Medium')]
    param ($EnvironmentSettings)
    $EnvironmentSettings | ForEach-Object { 
        Set-item env:"$_.environmentvariable"  (join-path $ToolPath $_.path) }
}

function New-ToolPath {
    [CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'Medium')]
    param($ToolPath)

    if (-not ( Test-path $ToolPath)) {
        if ($PSCmdlet.ShouldProcess("ShouldProcess?")) {
            New-Item -ItemType Directory -Path $ToolPath | Out-Null
        }
    }
}

function Test-ShouldDownload {
    [CmdletBinding()]
    [OutputType([boolean])]
    param($MetaDataFilePath
        , $HttpResponse)
    Write-Verbose "Test-ShouldDownload - start"
  
    $downloadZip = $false
    if (Test-path $MetaDataFilePath) {
        Write-Verbose "metadata file exists getting content"
        $UrlMetadata = Get-Content  $MetaDataFilePath -Raw | Convertfrom-json
        #Check the metadata from the URL compared with the previous download
        if ($UrlMetadata.Size -ne $HttpResponse.Size -or `
        $UrlMetadata.Filename -ne $HttpResponse.Filename ) {
            Write-Verbose "metadata content different"
        }
    }
    else {
        Write-Verbose "metadata doesn't exist"
        $downloadZip = $true
    }

    return $downloadZip
} 
function Get-MetaDataFile {
    param ($ToolFolder)
    return join-path $ToolFolder "metadata.json"
}
function Save-MetaDataToFile {
    [CmdletBinding()]
    param($MetaDataFilePath
        , $HttpMetadata)
        $HttpMetadata | convertto-json | out-file $MetaDataFilePath
}