packaging/chocolatey/tools/chocolateyInstall.ps1 (6090 bytes)
1 $ErrorActionPreference = 'Stop' 2 3 $packageName = 'sbm' 4 $version = '0.3' 5 $toolsDir = Split-Path -Parent $MyInvocation.MyCommand.Definition 6 $stage = Join-Path $toolsDir 'staging' 7 8 # What Makefile's TOOLS installs. 9 $scripts = @('bm', 'bm-migrate', 'bm-import', 'bm-check', 'bm-html', 'bm-title', 'bm-commit', 'bm-watch', 'bm-sync') 10 11 # --------------------------------------------------------------------------- 12 # Fetch and unpack. 13 # 14 # GitHub serves a tag as .tar.gz. 7zip needs two passes for that: .tar.gz to 15 # .tar, then .tar to the tree. Nothing is embedded in this package, because no 16 # licence has been chosen upstream yet. 17 # --------------------------------------------------------------------------- 18 19 $packageArgs = @{ 20 packageName = $packageName 21 unzipLocation = $stage 22 url = "https://github.com/equwal/sbm/archive/refs/tags/v$version.tar.gz" 23 # No release tag exists yet, so there is nothing to hash. Replace 24 # SBM_SHA256_TBD with the sha256 of the tarball once v0.3 is tagged: 25 # Get-FileHash .\v0.3.tar.gz -Algorithm SHA256 26 checksum = 'SBM_SHA256_TBD' 27 checksumType = 'sha256' 28 } 29 30 Install-ChocolateyZipPackage @packageArgs 31 32 $tar = Get-ChildItem -Path $stage -Filter '*.tar' -File | Select-Object -First 1 33 if (-not $tar) { 34 throw "sbm: no .tar found in $stage after unpacking the release archive." 35 } 36 Get-ChocolateyUnzip -FileFullPath $tar.FullName -Destination $stage -PackageName $packageName 37 Remove-Item -LiteralPath $tar.FullName -Force 38 39 $src = Join-Path $stage "$packageName-$version" 40 if (-not (Test-Path -LiteralPath $src)) { 41 # A -git style archive, or a renamed top directory: take the only one there. 42 $only = Get-ChildItem -Path $stage -Directory | Select-Object -First 1 43 if (-not $only) { throw "sbm: the release archive did not unpack into a directory." } 44 $src = $only.FullName 45 } 46 47 foreach ($s in $scripts) { 48 $from = Join-Path $src $s 49 if (-not (Test-Path -LiteralPath $from)) { throw "sbm: $s is missing from the release archive." } 50 Copy-Item -LiteralPath $from -Destination (Join-Path $toolsDir $s) -Force 51 } 52 foreach ($extra in @('README', 'usertags', 'engines')) { 53 $from = Join-Path $src $extra 54 if (Test-Path -LiteralPath $from) { 55 Copy-Item -LiteralPath $from -Destination (Join-Path $toolsDir $extra) -Force 56 } 57 } 58 Remove-Item -LiteralPath $stage -Recurse -Force -ErrorAction SilentlyContinue 59 60 # --------------------------------------------------------------------------- 61 # Find a POSIX sh. Git for Windows ships one; the package depends on git for 62 # exactly that reason. Look in several places rather than one hardcoded path, 63 # because Git installs per machine, per user, 32 bit, and through Chocolatey. 64 # --------------------------------------------------------------------------- 65 66 function Get-PosixSh { 67 $candidates = New-Object 'System.Collections.Generic.List[string]' 68 69 $gitCmd = Get-Command 'git.exe' -ErrorAction SilentlyContinue 70 if ($gitCmd) { 71 # ...\Git\cmd\git.exe and ...\Git\bin\git.exe both give ...\Git here. 72 $gitRoot = Split-Path -Parent (Split-Path -Parent $gitCmd.Source) 73 $candidates.Add((Join-Path $gitRoot 'bin\sh.exe')) 74 $candidates.Add((Join-Path $gitRoot 'usr\bin\sh.exe')) 75 } 76 77 $keys = @( 78 'HKLM:\SOFTWARE\GitForWindows', 79 'HKLM:\SOFTWARE\WOW6432Node\GitForWindows', 80 'HKCU:\SOFTWARE\GitForWindows' 81 ) 82 foreach ($key in $keys) { 83 try { 84 $installPath = (Get-ItemProperty -Path $key -Name 'InstallPath' -ErrorAction Stop).InstallPath 85 if ($installPath) { 86 $candidates.Add((Join-Path $installPath 'bin\sh.exe')) 87 $candidates.Add((Join-Path $installPath 'usr\bin\sh.exe')) 88 } 89 } catch { 90 # No such key on this machine. 91 } 92 } 93 94 $roots = @( 95 $env:ProgramFiles, 96 ${env:ProgramFiles(x86)}, 97 $env:ProgramW6432, 98 $(if ($env:LOCALAPPDATA) { Join-Path $env:LOCALAPPDATA 'Programs' }), 99 $(if ($env:ChocolateyInstall) { Join-Path $env:ChocolateyInstall 'lib\git.install\tools' }) 100 ) 101 foreach ($root in $roots) { 102 if ($root) { 103 $candidates.Add((Join-Path $root 'Git\bin\sh.exe')) 104 $candidates.Add((Join-Path $root 'Git\usr\bin\sh.exe')) 105 } 106 } 107 108 $shCmd = Get-Command 'sh.exe' -ErrorAction SilentlyContinue 109 if ($shCmd) { $candidates.Add($shCmd.Source) } 110 111 foreach ($candidate in $candidates) { 112 if ($candidate -and (Test-Path -LiteralPath $candidate -PathType Leaf)) { return $candidate } 113 } 114 return $null 115 } 116 117 $sh = Get-PosixSh 118 if (-not $sh) { 119 throw @' 120 sbm: no POSIX sh found. 121 122 sbm is POSIX shell and needs one to run. Git for Windows ships sh.exe and is 123 a dependency of this package: 124 125 choco install git 126 127 If Git is installed somewhere unusual, sbm still works: put that sh.exe on 128 PATH and reinstall this package. 129 '@ 130 } 131 Write-Host "sbm: using $sh" 132 133 # --------------------------------------------------------------------------- 134 # Generate a .cmd per script and register it on PATH. 135 # 136 # The sh path found above is baked in, with a fall back to whatever sh.exe is 137 # on PATH, so moving or upgrading Git does not break the shims outright. 138 # --------------------------------------------------------------------------- 139 140 foreach ($s in $scripts) { 141 $cmdPath = Join-Path $toolsDir "$s.cmd" 142 $body = @" 143 @echo off 144 rem Generated by the sbm Chocolatey package. Do not edit: reinstalling 145 rem the package rewrites this file. 146 setlocal 147 if not defined SBM_COPY set "SBM_COPY=clip" 148 if not defined SBM_PASTE set "SBM_PASTE=powershell -NoProfile -Command Get-Clipboard" 149 set "SBM_SH=$sh" 150 if not exist "%SBM_SH%" set "SBM_SH=sh.exe" 151 "%SBM_SH%" "%~dp0$s" %* 152 "@ 153 Set-Content -LiteralPath $cmdPath -Value $body -Encoding ASCII 154 Install-BinFile -Name $s -Path $cmdPath 155 } 156 157 Write-Host @' 158 sbm is installed. On Windows: 159 160 * The menu is fzf. dmenu is an X11 program and is not available here. 161 * SBM_COPY and SBM_PASTE default to clip and Get-Clipboard. 162 * Set an opener yourself, for example: 163 setx SBM_OPEN "powershell -NoProfile -Command Start-Process" 164 * Example tag and engine files are in this package's tools directory. 165 Copy them to ~/.local/share/sbm/ (the HOME that Git Bash uses). 166 '@