11import os
2- import shutil
3- import subprocess
4- import zipfile
52from pathlib import Path
63
74from prompt_toolkit import print_formatted_text
@@ -18,59 +15,36 @@ def print_error(error: str) -> None:
1815 print_formatted_text (FormattedText ([("ansired" , error )]))
1916
2017
21- def pip_output (text : str ) -> None :
22- print_formatted_text (FormattedText ([("ansigray" , text )]), end = "" )
23-
24-
2518def replace_home_with_tilde (path : Path ) -> Path :
2619 relative_path = path .relative_to (Path .home ())
2720 return Path ("~" ) / relative_path
2821
2922
30- def ensure_zsh ():
23+ def ensure_zsh () -> None :
3124 shell = os .environ .get ("SHELL" )
25+ if shell is None :
26+ print_error ("Unable to determine shell environment. If you are confident "
27+ "that you are running in zsh, run again with `SHELL=zsh python3 -m shelloracle --init`" )
28+ exit (1 )
3229 if "zsh" not in shell :
3330 print_error (f"'{ shell } ' is currently unsupported. "
3431 f"Please open an issue https://github.com/djcopley/ShellOracle/issues." )
3532 exit (1 )
3633
3734
38- def install_shelloracle ():
39- print_info ("Installing shelloracle" )
40- python = shutil .which ("python3" )
41- pip = subprocess .Popen ([python , "-m" , "pip" , "install" , "--upgrade" , "shelloracle" ],
42- stdout = subprocess .PIPE , text = True )
43- for line in pip .stdout .readlines ():
44- pip_output (line )
45- if (ret := pip .wait ()) == 0 :
46- print_info ("Successfully installed shelloracle" )
47- else :
48- print_error (f"Unable to install shelloracle" )
49- exit (ret )
50-
51-
5235zshrc_path = Path .home () / ".zshrc"
5336shelloracle_zsh_dest = Path .home () / ".shelloracle.zsh"
5437
5538
56- def read_shelloracle_zsh ():
57- working_dir = Path (__file__ ).parent .absolute ()
58- zsh_path = "shelloracle.zsh"
59- if working_dir .suffix == ".pyz" :
60- with zipfile .ZipFile (working_dir , "r" ) as zip_app :
61- shelloracle_zsh = zip_app .read (zsh_path )
62- else :
63- shelloracle_zsh = (working_dir / zsh_path ).read_bytes ()
64- return shelloracle_zsh
65-
66-
67- def write_shelloracle_zsh ():
68- shelloracle_zsh = read_shelloracle_zsh ()
39+ def write_shelloracle_zsh () -> None :
40+ zsh_path = Path (__file__ ).parent .absolute () / "shelloracle.zsh"
41+ shelloracle_zsh = zsh_path .read_bytes ()
6942 shelloracle_zsh_dest .write_bytes (shelloracle_zsh )
7043 print_info (f"Successfully wrote key bindings to { replace_home_with_tilde (shelloracle_zsh_dest )} " )
7144
7245
73- def update_zshrc ():
46+ def update_zshrc () -> None :
47+ zshrc_path .touch (exist_ok = True )
7448 with zshrc_path .open ("r" ) as file :
7549 zshrc = file .read ()
7650 line = f"[ -f { shelloracle_zsh_dest } ] && source { shelloracle_zsh_dest } "
@@ -81,17 +55,16 @@ def update_zshrc():
8155 print_info (f"Successfully updated { replace_home_with_tilde (zshrc_path )} " )
8256
8357
84- def install () :
58+ def bootstrap () -> None :
8559 with create_app_session_from_tty ():
8660 ensure_zsh ()
87- install_shelloracle ()
8861
8962 if confirm ("Enable terminal keybindings?" , suffix = " ([y]/n) " ) is False :
90- exit ( 0 )
63+ return
9164
9265 write_shelloracle_zsh ()
9366 update_zshrc ()
9467
9568
9669if __name__ == '__main__' :
97- install ()
70+ bootstrap ()
0 commit comments