Last active
July 22, 2024 22:10
-
-
Save Tremeschin/9293dfd1a41e3c813237d105ec370464 to your computer and use it in GitHub Desktop.
smartenv - Automatically source Python virtualenvs in a smart way (plus monorepo support)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/fish | |
# | |
# (c) 2024, Tremeschin, MIT License | |
# | |
# smartenv - Automatically source Python virtualenvs in a smart way (plus monorepo support) | |
# | |
# Usage: Add this file to ~/.config/fish/conf.d/smartenv.fish and restart your shell | |
# Speed: Inside venv `hyperfine 'rye show' -i`: 10ms; Outside venv: 2.3ms (SSD + Beefy CPU) | |
# Note: This script needs [rye](https://rye.astral.sh/) to be installed and on PATH | |
# | |
function __smartenv__ --on-variable PWD --description "Automatically source Python virtualenvs in a smart way (plus monorepo support)" | |
# Safety measure to keep expected behavior | |
status --is-command-substitution; and return | |
# Warn if rye is not installed | |
if not command -v rye > /dev/null | |
echo "Warning: rye is not installed, smartenv will not work • Get it at (https://rye.astral.sh/)" | |
return | |
end | |
# Find 'venv: *' on `rye show`, silently, and its path | |
set -l venv (rye show 2> /dev/null | grep -o "venv: \S*" | cut -d' ' -f2) | |
set -l activate $venv/bin/activate.fish | |
# Always source activate.fish, deactivate if not found | |
if test -e $activate && not test -z $venv | |
source $activate | |
else if command -v deactivate > /dev/null | |
deactivate | |
end | |
end | |
# Call on start, as we may be already in a venv | |
__smartenv__ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment