|
| 1 | +#Requires -Version 5 |
| 2 | +param($install_dir) |
| 3 | + |
| 4 | +# This script can be used to download and install src-fingerprint on Windows, |
| 5 | +# from a PowerShell prompt. Refer to src-fingerprint README for more details. |
| 6 | + |
| 7 | +$DEFAULT_INSTALL_DIR = "$env:APPDATA\src-fingerprint" |
| 8 | + |
| 9 | +# Quit if anything goes wrong |
| 10 | +$old_erroractionpreference = $erroractionpreference |
| 11 | +$erroractionpreference = 'stop' |
| 12 | + |
| 13 | +if (($PSVersionTable.PSVersion.Major) -lt 5) { |
| 14 | + Write-Output "PowerShell 5 or later is required to run this installer." |
| 15 | + Write-Output "Upgrade PowerShell: https://docs.microsoft.com/en-us/powershell/scripting/setup/installing-windows-powershell" |
| 16 | + break |
| 17 | +} |
| 18 | + |
| 19 | +# Required to uncompress zip files |
| 20 | +Add-Type -Assembly "System.IO.Compression.FileSystem" |
| 21 | + |
| 22 | +function request_install_dir() { |
| 23 | + $dir = Read-Host -Prompt "Installation dir [$DEFAULT_INSTALL_DIR]" |
| 24 | + if ($dir -eq "") { |
| 25 | + $dir = $DEFAULT_INSTALL_DIR |
| 26 | + } |
| 27 | + return $dir |
| 28 | +} |
| 29 | + |
| 30 | +function recreate_dir($dir) { |
| 31 | + if (test-path $dir) { |
| 32 | + Remove-Item $dir -Recurse -Force |
| 33 | + } |
| 34 | + mkdir $dir > $null |
| 35 | +} |
| 36 | + |
| 37 | +function download($url, $dst_file) { |
| 38 | + $client = New-Object Net.Webclient |
| 39 | + $client.DownloadFile($url, $dst_file) |
| 40 | +} |
| 41 | + |
| 42 | +function extract($zip_file, $dst_dir) { |
| 43 | + [IO.Compression.ZipFile]::ExtractToDirectory($zip_file, $dst_dir) |
| 44 | +} |
| 45 | + |
| 46 | +function get_latest_version() { |
| 47 | + $response = Invoke-RestMethod -Uri "https://api.github.com/repos/GitGuardian/src-fingerprint/releases/latest" |
| 48 | + $tag = $response.tag_name |
| 49 | + if ($tag.Length -lt 2 -or $tag[0] -ne "v") { |
| 50 | + Write-Error -Category InvalidResult -Message "Invalid tag name: '$tag'" |
| 51 | + exit |
| 52 | + } |
| 53 | + # $tag is "v1.2.3", we want "1.2.3" |
| 54 | + $tag.SubString(1) |
| 55 | +} |
| 56 | + |
| 57 | +function main() { |
| 58 | + if ($install_dir -eq $null) { |
| 59 | + $install_dir = request_install_dir |
| 60 | + } |
| 61 | + |
| 62 | + $tmp_dir = "$install_dir\tmp" |
| 63 | + recreate_dir $tmp_dir |
| 64 | + |
| 65 | + Write-Output "Fetching version number of latest release" |
| 66 | + $version = get_latest_version |
| 67 | + Write-Output "Latest version is $version" |
| 68 | + |
| 69 | + $zip_url = "https://github.com/GitGuardian/src-fingerprint/releases/download/v${version}/src-fingerprint_${version}_Windows_amd64.zip" |
| 70 | + |
| 71 | + Write-Output "Downloading $zip_url" |
| 72 | + $zip_file = "$tmp_dir\install.zip" |
| 73 | + download $ZIP_URL $zip_file |
| 74 | + |
| 75 | + Write-Output 'Extracting' |
| 76 | + extract $zip_file $tmp_dir |
| 77 | + Move-Item -Path "$tmp_dir\src-fingerprint_${VERSION}_Windows_*\*" -Destination $install_dir -Force |
| 78 | + |
| 79 | + Write-Output 'Cleaning' |
| 80 | + Remove-Item $tmp_dir -Recurse -Force |
| 81 | + |
| 82 | + Write-Output "All done, src-fingerprint is now available at $install_dir\src-fingerprint.exe" |
| 83 | +} |
| 84 | + |
| 85 | +main |
| 86 | + |
| 87 | +# Reset $erroractionpreference to original value |
| 88 | +$erroractionpreference = $old_erroractionpreference |
0 commit comments