|
| 1 | +# Enable installing from a powershell command |
| 2 | +# |
| 3 | +# For example |
| 4 | +# To install the latest version: |
| 5 | +# $ProgressPreference = "silentlyContinue"; |
| 6 | +# iex ((New-Object System.Net.WebClient).DownloadString('https://tools.veracode.com/veracode-cli/install.ps1')) |
| 7 | +# |
| 8 | +# To install the specific version: |
| 9 | +# $scriptPath = ((new-object net.webclient).DownloadString('https://tools.veracode.com/veracode-cli/install.ps1')); |
| 10 | +# Invoke-Command -ScriptBlock ([scriptblock]::Create($scriptPath)) -ArgumentList "1.9.0" |
| 11 | +# |
| 12 | + |
| 13 | +param ( |
| 14 | + [string]$version, |
| 15 | + [string]$proxyUrl |
| 16 | +) |
| 17 | + |
| 18 | + |
| 19 | +function Get-Downloader { |
| 20 | + $webClient = New-Object System.Net.WebClient |
| 21 | + if ($proxyUrl) { |
| 22 | + if ($proxyUrl -notmatch '^http://') { |
| 23 | + throw "Invalid proxy URL. Only 'http://' is supported. Received: $proxyUrl" |
| 24 | + } |
| 25 | + try { |
| 26 | + $uri = [System.Uri]::new($proxyUrl) |
| 27 | + } catch { |
| 28 | + throw "Invalid proxy URL format: $proxyUrl. Ensure it is a valid URL." |
| 29 | + } |
| 30 | + $proxy = New-Object System.Net.WebProxy($proxyUrl, $true) |
| 31 | + $webClient.Proxy = $proxy |
| 32 | + } |
| 33 | + return $webClient |
| 34 | +} |
| 35 | + |
| 36 | +function Remove-Old-Version { |
| 37 | + param([string]$destinationDir) |
| 38 | + Write-Debug "Removing from destination directory $destinationDir" |
| 39 | + # Check if the destination directory exists, if not, create it |
| 40 | + if (Test-Path -Path $destinationDir\veracode -PathType Container) { |
| 41 | + # Delete existing application |
| 42 | + Remove-Item -Path $destinationDir\veracode -Recurse -Force |
| 43 | + } |
| 44 | + # Get the current PATH environment variable |
| 45 | + $currentPath = [Environment]::GetEnvironmentVariable("PATH", "Machine") |
| 46 | + |
| 47 | + # Split the PATH variable into individual entries |
| 48 | + $pathEntries = $currentPath -split ";" |
| 49 | + |
| 50 | + # Remove the unwanted entry |
| 51 | + $newPathEntries = $pathEntries | Where-Object { $_ -ne "$destinationDir\veracode" } |
| 52 | + |
| 53 | + # Join the remaining entries back into a semicolon-separated string |
| 54 | + $newPath = $newPathEntries -join ";" |
| 55 | + |
| 56 | + # Set the modified PATH as the new environment variable |
| 57 | + [Environment]::SetEnvironmentVariable("PATH", $newPath, "Machine") |
| 58 | + |
| 59 | + # Optional: Display the modified PATH for verification |
| 60 | + Write-Debug "Modified PATH: $newPath" |
| 61 | +} |
| 62 | + |
| 63 | +function Get-Version { |
| 64 | + param([string] $url) |
| 65 | + Write-Debug "Argument received: $version" |
| 66 | + # URL of the executable to download |
| 67 | + if (-not $version) { |
| 68 | + $version = Download-String("$url/LATEST_VERSION") |
| 69 | + Write-Host "Downloading the version: $version" |
| 70 | + } |
| 71 | + $version = $version.trim() |
| 72 | + return $version |
| 73 | +} |
| 74 | + |
| 75 | +function Download-String { |
| 76 | + param ( |
| 77 | + [string]$url |
| 78 | + ) |
| 79 | + Write-Debug "Downloading string from $url" |
| 80 | + $downloader = Get-Downloader |
| 81 | + |
| 82 | + return $downloader.DownloadString($url) |
| 83 | +} |
| 84 | + |
| 85 | +function Check-Arch { |
| 86 | + param ([string]$version, [string]$destinationDir) |
| 87 | + if([System.Environment]::Is32BitOperatingSystem) { |
| 88 | + Write-Debug "Check-Arch: architecture 36 bit is supported" |
| 89 | + $fileName = "veracode-cli_${version}_windows_386.zip" |
| 90 | + Write-Debug "Destination path $fileName." |
| 91 | + return $fileName |
| 92 | + } elseif([System.Environment]::Is64BitOperatingSystem) { |
| 93 | + Write-Debug "Check-Arch: architecture 64 bit is supported" |
| 94 | + $fileName = "veracode-cli_${version}_windows_x86.zip" |
| 95 | + Write-Debug "Destination path $fileName." |
| 96 | + return $fileName |
| 97 | + } else { |
| 98 | + Write-Debug "Check-Arch: architecture is not x86_32 or x86_64" |
| 99 | + throw "Error: Veracode Interactive only supports x86_32 and x86_64, but your uname -m reported $arch" |
| 100 | + } |
| 101 | + |
| 102 | +} |
| 103 | + |
| 104 | +function Get-Url { |
| 105 | + param([string]$url, [string]$filename) |
| 106 | + return "$url/$fileName" |
| 107 | +} |
| 108 | + |
| 109 | +function Get-Destination { |
| 110 | + param([string]$destinationDir, [string]$fileName) |
| 111 | + return Join-Path -Path $destinationDir -ChildPath $fileName |
| 112 | +} |
| 113 | + |
| 114 | +function Download-Compress-File { |
| 115 | + param([string] $url, [string] $destination) |
| 116 | + # Create a WebClient instance |
| 117 | + $webClient = Get-Downloader |
| 118 | + |
| 119 | + try { |
| 120 | + # Check if the file exists at the URL |
| 121 | + $response = $null |
| 122 | + if($proxyUrl) { |
| 123 | + $response = Invoke-WebRequest -Uri $url -Proxy $proxyUrl -UseBasicParsing -Method Head -ErrorAction Stop |
| 124 | + } else { |
| 125 | + $response = Invoke-WebRequest -Uri $url -UseBasicParsing -Method Head -ErrorAction Stop |
| 126 | + } |
| 127 | + |
| 128 | + if ($response.StatusCode -eq 200) { |
| 129 | + # File exists, proceed with download asynchronously |
| 130 | + $downloadTask = $webClient.DownloadFileTaskAsync($url, $destination) |
| 131 | + |
| 132 | + # Display a simple progress bar while downloading |
| 133 | + Check-Download-Progress |
| 134 | + Write-Debug "Download complete. File saved to: $destination" |
| 135 | + } else { |
| 136 | + Write-Host "File not found at the specified URL: $url" |
| 137 | + } |
| 138 | + } catch { |
| 139 | + Write-Host "Download failed. URL: $url. Error: $_.Exception.Message" |
| 140 | + } finally { |
| 141 | + # Dispose WebClient object after download completes or fails |
| 142 | + $webClient.Dispose() |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 146 | +# Code taken from chocolatey.org/install.ps1 |
| 147 | +function Extract-Compress-File { |
| 148 | + param ([string] $destination, [string] $destinationDir) |
| 149 | + # Extract the contents of the downloaded ZIP file to a folder |
| 150 | + Write-Debug "Destination path $destination." |
| 151 | + $extractedFolder = Join-Path -Path $destinationDir -ChildPath "veracode-temp" |
| 152 | + Write-Debug "Extraction path to $extractedFolder." |
| 153 | + Expand-Archive -Path $destination -DestinationPath $extractedFolder -Force |
| 154 | + Write-Debug "Extraction completed to $extractedFolder." |
| 155 | +} |
| 156 | + |
| 157 | +function Move-To-Veracode-Dir { |
| 158 | + param ([string] $fileName, [string] $destinationDir) |
| 159 | + $folderName =$fileName.Replace(".zip", "") |
| 160 | + $tempDir = "$destinationDir/veracode-temp/$folderName" |
| 161 | + $veracodeDir = "$destinationDir/veracode" |
| 162 | + Write-Debug "Temp dir $tempDir moving to $veracodeDir ." |
| 163 | + Move-Item -Path $tempDir -Destination "$veracodeDir" -Force |
| 164 | + Remove-Item -Path "$destinationDir/veracode-temp" -Force -Recurse |
| 165 | +} |
| 166 | + |
| 167 | +function Remove-Compress-File { |
| 168 | + Get-ChildItem -Path $destinationDir\veracode-cli_* -Recurse | Remove-Item -Force -Recurse |
| 169 | +} |
| 170 | + |
| 171 | +function Check-Download-Progress { |
| 172 | + while (-not $downloadTask.IsCompleted) { |
| 173 | + $bytesReceived = $webClient.DownloadProgress.BytesReceived |
| 174 | + $totalBytes = $webClient.DownloadProgress.TotalBytesToReceive |
| 175 | + |
| 176 | + if ($totalBytes -gt 0) { |
| 177 | + $progress = [math]::Round(($bytesReceived / $totalBytes) * 100) |
| 178 | + } else { |
| 179 | + $progress = 0 |
| 180 | + } |
| 181 | + |
| 182 | + Write-Progress -Activity "Downloading File" -Status "Progress: $progress%" -PercentComplete $progress |
| 183 | + Start-Sleep -Seconds 1 |
| 184 | + } |
| 185 | +} |
| 186 | + |
| 187 | +function Install-Veracode { |
| 188 | + # Add the installation directory to the PATH environment variable |
| 189 | + $installationPath = "$destinationDir\veracode" |
| 190 | + $env:Path += ";$installationPath" |
| 191 | + |
| 192 | + $pathVariable = [System.Environment]::GetEnvironmentVariable("Path", "Machine") |
| 193 | + $newPath = "$pathVariable;$installationPath" |
| 194 | + [System.Environment]::SetEnvironmentVariable("Path", $newPath, "Machine") |
| 195 | + |
| 196 | + Write-Host "Installation completed." |
| 197 | +} |
| 198 | + |
| 199 | +function Main { |
| 200 | + $url = "https://tools.veracode.com/veracode-cli" |
| 201 | + $destinationDir = "$env:APPDATA" |
| 202 | + Remove-Old-Version $destinationDir |
| 203 | + $version = Get-Version $url |
| 204 | + $fileName = Check-Arch $version $destinationDir |
| 205 | + $downloadUrl = Get-Url $url $fileName |
| 206 | + $destination = Get-Destination $destinationDir $fileName |
| 207 | + Download-Compress-File $downloadUrl $destination |
| 208 | + Extract-Compress-File $destination $destinationDir |
| 209 | + Move-To-Veracode-Dir $fileName $destinationDir |
| 210 | + Remove-Compress-File |
| 211 | + Install-Veracode |
| 212 | +} |
| 213 | + |
| 214 | + |
| 215 | +try{ |
| 216 | + Write-Debug "Executing installer.ps1!!" |
| 217 | + Main |
| 218 | +} catch { |
| 219 | + # Output an error message if download or installation fails |
| 220 | + Write-Host "Installation failed. Please contact support at veracode@support.com for assistance: $_.Exception.Message" |
| 221 | + |
| 222 | +} |
0 commit comments