Functions/Private/downloadfns.ps1
function Get-UrlMetadata { param ( [string] $url ) try { Write-Verbose "Fetching file from $url ..." $HttpResponse = [System.Net.WebRequest]::Create($url).GetResponse(); Convert-UrlResponseToMetaData $HttpResponse Write-Verbose "Got filename: $($HttpResponse.ResponseUri.Segments[-1]), Size: $($HttpResponse.ContentLength)" } finally { Write-Verbose "Disposing of webrequest response." $HttpResponse.Close() $HttpResponse.Dispose() } } 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) $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 } |