summaryrefslogtreecommitdiffstats
path: root/install.ps1
blob: d9ff77e8fa5a7a30bf24d88df676490dcea7b1f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
$version="0.23.0"

if ([Environment]::Is64BitProcess) {
  $binary_arch="amd64"
} else {
  $binary_arch="386"
}

$fzf_base=Split-Path -Parent $MyInvocation.MyCommand.Definition

function check_binary () {
  Write-Host "  - Checking fzf executable ... " -NoNewline
  $output=cmd /c $fzf_base\bin\fzf.exe --version 2>&1
  if (-not $?) {
    Write-Host "Error: $output"
    $binary_error="Invalid binary"
  } else {
    $output=(-Split $output)[0]
    if ($version -ne $output) {
      Write-Host "$output != $version"
      $binary_error="Invalid version"
    } else {
      Write-Host "$output"
      $binary_error=""
      return 1
    }
  }
  Remove-Item "$fzf_base\bin\fzf.exe"
  return 0
}

function download {
  param($file)
  Write-Host "Downloading bin/fzf ..."
  if ("$version" -ne "alpha") {
    if (Test-Path "$fzf_base\bin\fzf.exe") {
      Write-Host "  - Already exists"
      if (check_binary) {
        return
      }
    }
  }
  if (-not (Test-Path "$fzf_base\bin")) {
    md "$fzf_base\bin"
  }
  if (-not $?) {
    $binary_error="Failed to create bin directory"
    return
  }
  cd "$fzf_base\bin"
  if ("$version" -eq "alpha") {
    $url="https://github.com/junegunn/fzf-bin/releases/download/alpha/$file"
  } else {
    $url="https://github.com/junegunn/fzf-bin/releases/download/$version/$file"
  }
  $temp=$env:TMP + "\fzf.zip"
  [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
  (New-Object Net.WebClient).DownloadFile($url, $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath("$temp"))
  if ($?) {
    (Microsoft.PowerShell.Archive\Expand-Archive -Path $temp -DestinationPath .); (Remove-Item $temp)
  } else {
    $binary_error="Failed to download with powershell"
  }
  if (-not (Test-Path fzf.exe)) {
    $binary_error="Failed to download $file"
    return
  }
  check_binary >$null
}

download "fzf-$version-windows_$binary_arch.zip"

Write-Host 'For more information, see: https://github.com/junegunn/fzf'