install_extras.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. #!/usr/bin/env python3
  2. # /// script
  3. # dependencies = [
  4. # "httpx",
  5. # ]
  6. # ///
  7. from __future__ import annotations
  8. import os
  9. import pathlib
  10. import platform
  11. import shutil
  12. import subprocess
  13. import tarfile
  14. import zipfile
  15. import httpx
  16. CURRENT_DIR = pathlib.Path(__file__).parent
  17. def main():
  18. bat()
  19. choose()
  20. curlie()
  21. delta()
  22. dua()
  23. dust()
  24. eza()
  25. fx()
  26. git_whence()
  27. jjui()
  28. jujutsu()
  29. lazygit()
  30. oh_my_posh()
  31. procs()
  32. #starship()
  33. uv()
  34. yazi()
  35. def bat():
  36. if platform.system() == 'Darwin':
  37. print('$ brew install git-delta')
  38. subprocess.run(['brew', 'install', 'bat'], check=True)
  39. return
  40. else:
  41. assert platform.system() == 'Linux'
  42. if subprocess.run(['dpkg', '-l', 'bat'], stdout=subprocess.DEVNULL).returncode == 0:
  43. print_gray('bat package already installed')
  44. return
  45. subprocess.run(['sudo', 'apt', 'install', 'bat', '--yes'], check=True)
  46. def choose():
  47. if platform.system() == 'Darwin':
  48. print('$ brew install choose-rust')
  49. subprocess.run(['brew', 'install', 'choose-rust'], check=True)
  50. return
  51. else:
  52. assert platform.system() == 'Linux'
  53. if (CURRENT_DIR / 'choose').exists():
  54. print_gray('choose already downloaded')
  55. return
  56. client = httpx.Client()
  57. arch = platform.machine()
  58. if arch == 'arm64':
  59. arch = 'aarch64'
  60. url = f'https://github.com/theryangeary/choose/releases/latest/download/choose-{arch}-unknown-linux-gnu'
  61. download(client, url, CURRENT_DIR / 'choose')
  62. os.chmod(CURRENT_DIR / 'choose', 0o755)
  63. def curlie():
  64. if platform.system() == 'Darwin':
  65. print('$ brew install curlie')
  66. subprocess.run(['brew', 'install', 'curlie'], check=True)
  67. return
  68. if subprocess.run(['dpkg', '-l', 'curlie'], stdout=subprocess.DEVNULL).returncode == 0:
  69. print_gray('curlie package already installed')
  70. return
  71. client = httpx.Client()
  72. latest = gh_latest_version(client, 'rs', 'curlie')
  73. version = latest['tag_name'].removeprefix('v')
  74. system = platform.system().lower()
  75. arch = get_output(['dpkg', '--print-architecture'])
  76. filename = f'curlie_{version}_{system}_{arch}.deb'
  77. (asset,) = (asset for asset in latest['assets'] if asset['name'] == filename)
  78. deb_path = CURRENT_DIR / 'curlie.deb'
  79. download(client, asset['browser_download_url'], deb_path)
  80. try:
  81. subprocess.run(['sudo', 'dpkg', '-i', deb_path], check=True)
  82. finally:
  83. os.unlink(deb_path)
  84. def delta():
  85. if platform.system() == 'Darwin':
  86. print('$ brew install git-delta')
  87. subprocess.run(['brew', 'install', 'git-delta'], check=True)
  88. return
  89. else:
  90. assert platform.system() == 'Linux'
  91. if subprocess.run(['dpkg', '-l', 'git-delta'], stdout=subprocess.DEVNULL).returncode == 0:
  92. print_gray('git-delta package already installed')
  93. return
  94. client = httpx.Client()
  95. latest = gh_latest_version(client, 'dandavison', 'delta')
  96. version = latest['name']
  97. arch = get_output(['dpkg', '--print-architecture'])
  98. filename = f'git-delta_{version}_{arch}.deb'
  99. (asset,) = (asset for asset in latest['assets'] if asset['name'] == filename)
  100. deb_path = CURRENT_DIR / 'git-delta.deb'
  101. download(client, asset['browser_download_url'], deb_path)
  102. try:
  103. subprocess.run(['sudo', 'dpkg', '-i', deb_path], check=True)
  104. finally:
  105. os.unlink(deb_path)
  106. def dua():
  107. if platform.system() == 'Darwin':
  108. print('$ brew install dua-cli')
  109. subprocess.run(['brew', 'install', 'dua-cli'], check=True)
  110. return
  111. else:
  112. assert platform.system() == 'Linux'
  113. dua_path = CURRENT_DIR / 'dua'
  114. if dua_path.exists():
  115. print_gray('dua already downloaded')
  116. return
  117. client = httpx.Client()
  118. latest = gh_latest_version(client, 'Byron', 'dua-cli')
  119. version = latest['name']
  120. dirname = f'dua-{version}-{platform.machine()}-unknown-linux-musl'
  121. filename = dirname + '.tar.gz'
  122. (asset,) = (asset for asset in latest['assets'] if asset['name'] == filename)
  123. tarball_path = CURRENT_DIR / 'dua.tar.gz'
  124. download(client, asset['browser_download_url'], tarball_path)
  125. with tarfile.open(tarball_path) as tar:
  126. with tar.extractfile(dirname + '/dua') as binary, dua_path.open('wb') as f: # pyright: ignore[reportOptionalContextManager]
  127. shutil.copyfileobj(binary, f)
  128. os.unlink(tarball_path)
  129. dua_path.chmod(0o755)
  130. def dust():
  131. if platform.system() == 'Darwin':
  132. print('$ brew install dust')
  133. subprocess.run(['brew', 'install', 'dust'], check=True)
  134. return
  135. else:
  136. assert platform.system() == 'Linux'
  137. dust_path = CURRENT_DIR / 'dust'
  138. if dust_path.exists():
  139. print_gray('dust already downloaded')
  140. return
  141. client = httpx.Client()
  142. latest = gh_latest_version(client, 'bootandy', 'dust')
  143. version = latest['name']
  144. dirname = f'dust-{version}-{platform.machine()}-unknown-linux-musl'
  145. filename = dirname + '.tar.gz'
  146. (asset,) = (asset for asset in latest['assets'] if asset['name'] == filename)
  147. tarball_path = CURRENT_DIR / 'dust.tar.gz'
  148. download(client, asset['browser_download_url'], tarball_path)
  149. with tarfile.open(tarball_path) as tar:
  150. with tar.extractfile(dirname + '/dust') as binary, dust_path.open('wb') as f: # pyright: ignore[reportOptionalContextManager]
  151. shutil.copyfileobj(binary, f)
  152. os.unlink(tarball_path)
  153. dust_path.chmod(0o755)
  154. def eza():
  155. if platform.system() == 'Darwin':
  156. print('$ brew install eza')
  157. subprocess.run(['brew', 'install', 'eza'], check=True)
  158. return
  159. else:
  160. assert platform.system() == 'Linux'
  161. if (CURRENT_DIR / 'eza').exists():
  162. print_gray('eza already downloaded')
  163. return
  164. client = httpx.Client()
  165. url = f'https://github.com/eza-community/eza/releases/latest/download/eza_{platform.machine()}-unknown-linux-gnu.tar.gz'
  166. tarball_path = CURRENT_DIR / 'eza.tar.gz'
  167. download(client, url, tarball_path)
  168. with tarfile.open(tarball_path) as tar:
  169. tar.extract('./eza', CURRENT_DIR)
  170. tarball_path.unlink()
  171. def fx():
  172. if platform.system() == 'Darwin':
  173. print('$ brew install fx')
  174. subprocess.run(['brew', 'install', 'fx'], check=True)
  175. return
  176. else:
  177. assert platform.system() == 'Linux'
  178. if (CURRENT_DIR / 'fx').exists():
  179. print_gray('fx already downloaded')
  180. return
  181. client = httpx.Client()
  182. arch = platform.machine()
  183. if arch == 'aarch64':
  184. arch = 'arm64'
  185. elif arch == 'x86_64':
  186. arch = 'amd64'
  187. url = f'https://github.com/antonmedv/fx/releases/latest/download/fx_linux_{arch}'
  188. download(client, url, CURRENT_DIR / 'fx')
  189. os.chmod(CURRENT_DIR / 'fx', 0o755)
  190. def git_whence():
  191. if platform.system() == 'Darwin':
  192. print('$ brew install raylu/formulae/git-whence')
  193. subprocess.run(['brew', 'install', 'raylu/formulae/git-whence'], check=True)
  194. return
  195. else:
  196. assert platform.system() == 'Linux'
  197. if (CURRENT_DIR / 'git-whence').exists():
  198. print_gray('git-whence already downloaded')
  199. return
  200. client = httpx.Client()
  201. url = f'https://github.com/raylu/git-whence/releases/latest/download/git-whence-{platform.machine()}-unknown-linux-gnu'
  202. download(client, url, CURRENT_DIR / 'git-whence')
  203. os.chmod(CURRENT_DIR / 'git-whence', 0o755)
  204. def jjui():
  205. if (CURRENT_DIR / 'jjui').exists():
  206. print_gray('jjui already downloaded')
  207. return
  208. if platform.system() == 'Darwin':
  209. print('$ brew install jjui')
  210. subprocess.run(['brew', 'install', 'jjui'], check=True)
  211. return
  212. else:
  213. assert platform.system() == 'Linux'
  214. client = httpx.Client()
  215. latest = gh_latest_version(client, 'idursun', 'jjui')
  216. version = latest['name']
  217. system = platform.system().lower()
  218. arch = platform.machine()
  219. name = f'jjui-{version}-{arch}-{system}'
  220. url = f'https://github.com/idursun/jjui/releases/download/{version}/{name}.zip'
  221. zip_path = CURRENT_DIR / 'jjui.zip'
  222. download(client, url, zip_path)
  223. with zipfile.ZipFile(zip_path, 'r') as zipf:
  224. extracted = (CURRENT_DIR / 'jjui')
  225. with zipf.open(name, 'r') as binary, extracted.open('wb') as f:
  226. shutil.copyfileobj(binary, f)
  227. extracted.chmod(0o755)
  228. zip_path.unlink()
  229. def jujutsu():
  230. if (CURRENT_DIR / 'jj').exists():
  231. print_gray('jj already downloaded')
  232. return
  233. if platform.system() == 'Darwin':
  234. print('$ brew install jj')
  235. subprocess.run(['brew', 'install', 'jj'], check=True)
  236. return
  237. else:
  238. assert platform.system() == 'Linux'
  239. client = httpx.Client()
  240. latest = gh_latest_version(client, 'jj-vcs', 'jj')
  241. version = latest['name']
  242. arch = platform.machine()
  243. url = f'https://github.com/jj-vcs/jj/releases/download/{version}/jj-{version}-{arch}-unknown-linux-musl.tar.gz'
  244. tarball_path = CURRENT_DIR / 'jj.tar.gz'
  245. download(client, url, tarball_path)
  246. with tarfile.open(tarball_path, 'r:gz') as tar:
  247. tar.extract('./jj', CURRENT_DIR, filter='data')
  248. tarball_path.unlink()
  249. def lazygit():
  250. if (CURRENT_DIR / 'lazygit').exists():
  251. print_gray('lazygit package already installed')
  252. return
  253. client = httpx.Client()
  254. latest = gh_latest_version(client, 'jesseduffield', 'lazygit')
  255. version = latest['name']
  256. arch = platform.machine()
  257. if arch == 'aarch64':
  258. arch = 'arm64'
  259. url = f'https://github.com/jesseduffield/lazygit/releases/download/{version}/lazygit_{version.lstrip("v")}_{platform.system()}_{arch}.tar.gz'
  260. download(client, url, CURRENT_DIR / 'lazygit.tgz')
  261. with tarfile.open(CURRENT_DIR / 'lazygit.tgz', 'r:gz') as tar:
  262. tar.extract('lazygit', CURRENT_DIR)
  263. os.unlink(CURRENT_DIR / 'lazygit.tgz')
  264. def oh_my_posh():
  265. if platform.system() == 'Darwin':
  266. print('$ brew install oh-my-posh')
  267. subprocess.run(['brew', 'install', 'oh-my-posh'], check=True)
  268. return
  269. else:
  270. assert platform.system() == 'Linux'
  271. if (CURRENT_DIR / 'oh-my-posh').exists():
  272. print_gray('oh-my-posh already downloaded')
  273. return
  274. client = httpx.Client()
  275. arch = platform.machine()
  276. if arch == 'aarch64':
  277. arch = 'arm64'
  278. elif arch == 'x86_64':
  279. arch = 'amd64'
  280. url = f'https://github.com/JanDeDobbeleer/oh-my-posh/releases/latest/download/posh-linux-{arch}'
  281. download(client, url, CURRENT_DIR / 'oh-my-posh')
  282. os.chmod(CURRENT_DIR / 'oh-my-posh', 0o755)
  283. def procs():
  284. if (CURRENT_DIR / 'procs').exists():
  285. print_gray('procs already downloaded')
  286. return
  287. client = httpx.Client()
  288. latest = gh_latest_version(client, 'dalance', 'procs')
  289. version = latest['name']
  290. system = platform.system().lower()
  291. arch = platform.machine()
  292. if arch == 'arm64':
  293. arch = 'aarch64'
  294. if system == 'darwin':
  295. system = 'mac'
  296. name = f'procs-{version}-{arch}-{system}.zip'
  297. url = f'https://github.com/dalance/procs/releases/download/{version}/{name}'
  298. zip_path = CURRENT_DIR / 'procs.zip'
  299. download(client, url, zip_path)
  300. with zipfile.ZipFile(zip_path, 'r') as zipf:
  301. extracted = (CURRENT_DIR / 'procs')
  302. with zipf.open('procs', 'r') as binary, extracted.open('wb') as f:
  303. shutil.copyfileobj(binary, f)
  304. extracted.chmod(0o755)
  305. zip_path.unlink()
  306. def starship():
  307. if platform.system() == 'Darwin':
  308. print('$ brew install starship')
  309. subprocess.run(['brew', 'install', 'starship'], check=True)
  310. return
  311. else:
  312. assert platform.system() == 'Linux'
  313. if (CURRENT_DIR / 'starship').exists():
  314. print_gray('starship already downloaded')
  315. return
  316. client = httpx.Client()
  317. arch = platform.machine()
  318. if arch == 'x86_64':
  319. libc = 'gnu'
  320. else:
  321. libc = 'musl'
  322. url = f'https://github.com/starship/starship/releases/latest/download/starship-{arch}-unknown-linux-{libc}.tar.gz'
  323. tarball_path = CURRENT_DIR / 'starship.tar.gz'
  324. download(client, url, tarball_path)
  325. with tarfile.open(tarball_path, 'r:gz') as tar:
  326. tar.extract('starship', CURRENT_DIR)
  327. tarball_path.unlink()
  328. def uv():
  329. if (CURRENT_DIR / 'uv').exists():
  330. print_gray('uv already downloaded')
  331. return
  332. if which := shutil.which('uv'):
  333. print_gray('uv already at ' + which)
  334. return
  335. client = httpx.Client()
  336. arch = platform.machine()
  337. if arch == 'arm64':
  338. arch = 'aarch64'
  339. os = platform.system().lower()
  340. if os == 'darwin':
  341. prefix = f'uv-{arch}-apple-darwin'
  342. else:
  343. prefix = f'uv-{arch}-unknown-linux-gnu'
  344. url = f'https://github.com/astral-sh/uv/releases/latest/download/{prefix}.tar.gz'
  345. tarball_path = CURRENT_DIR / 'uv.tar.gz'
  346. download(client, url, tarball_path)
  347. with tarfile.open(tarball_path, 'r:gz') as tar:
  348. for filename in ['uv', 'uvx']:
  349. target_path = CURRENT_DIR / filename
  350. with tar.extractfile(f'{prefix}/{filename}') as binary, target_path.open('wb') as f: # pyright: ignore[reportOptionalContextManager]
  351. shutil.copyfileobj(binary, f)
  352. target_path.chmod(0o755)
  353. tarball_path.unlink()
  354. def yazi():
  355. if platform.system() == 'Darwin':
  356. print('$ brew install yazi')
  357. subprocess.run(['brew', 'install', 'yazi'], check=True)
  358. return
  359. else:
  360. assert platform.system() == 'Linux'
  361. if (CURRENT_DIR / 'yazi').exists():
  362. print_gray('yazi already downloaded')
  363. return
  364. client = httpx.Client()
  365. name = f'yazi-{platform.machine()}-unknown-linux-musl'
  366. url = f'https://github.com/sxyazi/yazi/releases/latest/download/{name}.zip'
  367. zip_path = CURRENT_DIR / 'yazi.zip'
  368. download(client, url, zip_path)
  369. with zipfile.ZipFile(zip_path, 'r') as zipf:
  370. for filename in ['ya', 'yazi']:
  371. extracted = (CURRENT_DIR / filename)
  372. with zipf.open(f'{name}/{filename}', 'r') as binary, extracted.open('wb') as f:
  373. shutil.copyfileobj(binary, f)
  374. extracted.chmod(0o755)
  375. zip_path.unlink()
  376. def get_output(argv: list[str]) -> str:
  377. proc = subprocess.run(argv, check=True, capture_output=True, encoding='ascii')
  378. return proc.stdout.rstrip('\n')
  379. def gh_latest_version(client: httpx.Client, org: str, repo: str) -> dict:
  380. r = client.get(f'https://api.github.com/repos/{org}/{repo}/releases/latest',
  381. headers={'Accept': 'application/vnd.github+json', 'X-GitHub-Api-Version': '2022-11-28'})
  382. r.raise_for_status()
  383. return r.json()
  384. def download(client: httpx.Client, url: str, path: pathlib.Path) -> None:
  385. print('downloading', url, 'to', path)
  386. with client.stream('GET', url, follow_redirects=True) as r:
  387. r.raise_for_status()
  388. with path.open('wb') as f:
  389. for chunk in r.iter_bytes():
  390. f.write(chunk)
  391. def print_gray(*args: object):
  392. print('\x1B[90;m', end='')
  393. print(*args, end='')
  394. print('\x1B[0m')
  395. if __name__ == '__main__':
  396. main()