@@ -71,6 +71,8 @@ class Environment:
71
71
"LANGUAGE" ,
72
72
"LANG" ,
73
73
"LC_.*" ,
74
+ # Required for Zsh
75
+ "ZDOTDIR" ,
74
76
# Required for resolving relative paths:
75
77
"PWD" ,
76
78
# Required for graphical output:
@@ -383,10 +385,76 @@ def runtime_env_path(self) -> Path:
383
385
return self .runtime_dir / "launcher_env.sh"
384
386
385
387
def _prepare_runtime_dir (self ) -> None :
386
- # Clean and create runtime directory
388
+ """ Clean and create runtime directory."""
387
389
self .clean ()
388
390
logging .debug (f"Create: { self .runtime_dir } " )
389
391
self .runtime_dir .mkdir (parents = True )
392
+ self ._write_prompt_sh ()
393
+ self ._write_bashrc ()
394
+ self ._write_zshrc ()
395
+
396
+ def _write_prompt_sh (self ) -> None :
397
+ """Write prompt.sh file."""
398
+ prompt_sh_file = self .runtime_dir / "prompt.sh"
399
+ prompt_sh_data = """\
400
+ CLOE_PROMPT="\u001b [2m[cloe-shell]\u001b [0m"
401
+
402
+ prompt_cloe() {
403
+ if [[ -n $CLOE_SHELL ]]; then
404
+ PROMPT="%{%F{242}%}[cloe-shell]%f ${PROMPT}"
405
+ fi
406
+ }
407
+
408
+ CURRENT_SHELL=$(basename $0)
409
+ if [[ $CURRENT_SHELL = "zsh" ]]; then
410
+ autoload -Uz add-zsh-hook
411
+ add-zsh-hook precmd prompt_cloe
412
+ else
413
+ export PS1="$CLOE_PROMPT $PS1"
414
+ fi
415
+ """
416
+ logging .debug (f"Write: { prompt_sh_file } " )
417
+ with prompt_sh_file .open ("w" ) as file :
418
+ file .write (prompt_sh_data )
419
+
420
+ def _write_bashrc (self ) -> None :
421
+ """Write .bashrc file.
422
+
423
+ When creating a new shell, it's not possible to just "source" a file
424
+ and continue with the normal interactive session. So we need to create
425
+ a bashrc file that will be used instead, which loads the normal
426
+ configuration, and then augments it with the extra variables we need.
427
+ """
428
+ bashrc_file = self .runtime_dir / ".bashrc"
429
+ bashrc_data = """\
430
+ source /etc/bash.bashrc
431
+ if [ -f ~/.bashrc ]; then
432
+ source ~/.bashrc
433
+ fi
434
+ OLD_PS1="$PS1"
435
+ source "$(dirname "$BASH_SOURCE[0]")/launcher_env.sh"
436
+ PS1="$OLD_PS1"
437
+ source "$(dirname "$BASH_SOURCE[0]")/prompt.sh"
438
+ """
439
+ logging .debug (f"Write: { bashrc_file } " )
440
+ with bashrc_file .open ("w" ) as file :
441
+ file .write (bashrc_data )
442
+
443
+ def _write_zshrc (self ) -> None :
444
+ """Write .zshrc file."""
445
+ zshrc_file = self .runtime_dir / ".zshrc"
446
+ zshrc_data = f"""\
447
+ ZDOTDIR="${{OLD_ZDOTDIR:-$HOME}}"
448
+ source "${{ZDOTDIR}}/.zshenv"
449
+ source "${{ZDOTDIR}}/.zshrc"
450
+ OLD_PS1="$PS1"
451
+ source "{ self .runtime_dir } /launcher_env.sh"
452
+ PS1="$OLD_PS1"
453
+ source "{ self .runtime_dir } /prompt.sh"
454
+ """
455
+ logging .debug (f"Write: { zshrc_file } " )
456
+ with zshrc_file .open ("w" ) as file :
457
+ file .write (zshrc_data )
390
458
391
459
def _prepare_virtualenv (self ) -> None :
392
460
# Get conan to create a virtualenv AND virtualrunenv for us:
@@ -567,8 +635,15 @@ def shell(
567
635
# Replace this process with the SHELL now.
568
636
sys .stdout .flush ()
569
637
cmd = [shell ]
570
- if arguments is not None :
638
+ if arguments is not None and len ( arguments ) != 0 :
571
639
cmd .extend (arguments )
640
+ else :
641
+ if shell == "/bin/bash" :
642
+ cmd .extend (["--init-file" , str (self .runtime_dir / ".bashrc" )])
643
+ elif shell == "/bin/zsh" :
644
+ env .set ("OLD_ZDOTDIR" , str (env .get ("ZDOTDIR" , "" )))
645
+ env .set ("ZDOTDIR" , self .runtime_dir )
646
+ logging .debug (f"Exec: { shell } { ' ' .join (cmd )} " )
572
647
os .execvpe (shell , cmd , env .as_dict ())
573
648
574
649
def activate (
@@ -588,10 +663,14 @@ def activate(
588
663
)
589
664
sys .exit (2 )
590
665
666
+ env .set ("CLOE_SHELL" , self .runtime_env_path ())
667
+ self ._write_runtime_env (env )
668
+
591
669
print ("# Please see `cloe-launch activate --help` before activating this." )
592
670
print ()
593
671
print (f"source { self .runtime_dir / 'activate.sh' } " )
594
672
print (f"source { self .runtime_dir / 'activate_run.sh' } " )
673
+ print (f"source { self .runtime_dir / 'prompt.sh' } " )
595
674
for var in ["CLOE_PROFILE_HASH" , "CLOE_ENGINE" , "CLOE_PLUGIN_PATH" ]:
596
675
print (f'export { var } ="{ env [var ]} "' )
597
676
print (f'export CLOE_SHELL="{ self .runtime_env_path ()} "' )
0 commit comments