devshells: improve select-shell
This commit is contained in:
@@ -38,7 +38,7 @@
|
|||||||
];
|
];
|
||||||
shellHook = ''
|
shellHook = ''
|
||||||
echo -e "${ansiEscapes.green}switch to another dev-shell using: select-shell${ansiEscapes.reset}"
|
echo -e "${ansiEscapes.green}switch to another dev-shell using: select-shell${ansiEscapes.reset}"
|
||||||
export PROJECT_ROOT=$(git rev-parse --show-toplevel)
|
export PRJ_ROOT=$(git rev-parse --show-toplevel)
|
||||||
'';
|
'';
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,39 +1,79 @@
|
|||||||
import argparse
|
import argparse
|
||||||
import json
|
import json
|
||||||
|
import os
|
||||||
import pathlib
|
import pathlib
|
||||||
import subprocess
|
import subprocess
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
|
selected_shell_file = pathlib.Path(".direnv/selected-shell")
|
||||||
|
|
||||||
|
|
||||||
|
def parse_args() -> argparse.Namespace:
|
||||||
parser = argparse.ArgumentParser(description="Select a devshell")
|
parser = argparse.ArgumentParser(description="Select a devshell")
|
||||||
parser.add_argument("shell", help="the name of the devshell to select", nargs="?")
|
parser.add_argument("shell", help="the name of the devshell to select", nargs="?")
|
||||||
parser.add_argument("--list", action="store_true", help="list available devshells")
|
parser.add_argument("--list", action="store_true", help="list available devshells")
|
||||||
args = parser.parse_args()
|
parser.add_argument(
|
||||||
|
"--show", action="store_true", help="show the currently selected devshell"
|
||||||
selected_shell_file = pathlib.Path(".direnv/selected-shell")
|
|
||||||
|
|
||||||
if not args.list and not args.shell:
|
|
||||||
parser.print_help()
|
|
||||||
exit(0)
|
|
||||||
if args.list:
|
|
||||||
flake_show = subprocess.run(
|
|
||||||
["nix", "flake", "show", "--json", "--no-write-lock-file"],
|
|
||||||
stdout=subprocess.PIPE,
|
|
||||||
)
|
)
|
||||||
data = json.loads(flake_show.stdout.decode())
|
return parser.parse_args()
|
||||||
print("Available devshells:")
|
|
||||||
print("\n".join(data["devShells"]["x86_64-linux"].keys()))
|
|
||||||
exit(0)
|
def get_current_shell() -> str | None:
|
||||||
if selected_shell_file.exists():
|
if selected_shell_file.exists():
|
||||||
with open(selected_shell_file) as f:
|
with open(selected_shell_file) as f:
|
||||||
current_shell = f.read().strip()
|
return f.read().strip()
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def print_current_shell() -> None:
|
||||||
|
current_shell = get_current_shell()
|
||||||
|
if current_shell:
|
||||||
|
print(f"Currently selected devshell: {current_shell}")
|
||||||
else:
|
else:
|
||||||
current_shell = ""
|
print("No devshell selected")
|
||||||
|
|
||||||
if current_shell == args.shell:
|
|
||||||
print(f"{args.shell} devshell already selected. No changes made.")
|
def print_devshells() -> None:
|
||||||
|
project_root = os.environ.get("PRJ_ROOT")
|
||||||
|
flake_show = subprocess.run(
|
||||||
|
[
|
||||||
|
"nix",
|
||||||
|
"eval",
|
||||||
|
"--json",
|
||||||
|
"--no-write-lock-file",
|
||||||
|
"--apply",
|
||||||
|
"shells: builtins.mapAttrs (name: _shell: name) shells",
|
||||||
|
f"{project_root}#devShells.x86_64-linux",
|
||||||
|
],
|
||||||
|
stdout=subprocess.PIPE,
|
||||||
|
)
|
||||||
|
names = json.loads(flake_show.stdout.decode())
|
||||||
|
print("Available devshells:\n")
|
||||||
|
print("\n".join(names))
|
||||||
|
|
||||||
|
|
||||||
|
def select_shell(shell: str) -> None:
|
||||||
|
if shell == get_current_shell():
|
||||||
|
print(f"{shell} devshell already selected. No changes made.")
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
|
else:
|
||||||
with open(selected_shell_file, "w") as f:
|
with open(selected_shell_file, "w") as f:
|
||||||
f.write(args.shell)
|
f.write(shell)
|
||||||
|
print(f"{shell} devshell selected")
|
||||||
|
|
||||||
print(f"{args.shell} devshell selected")
|
|
||||||
|
def main() -> None:
|
||||||
|
args = parse_args()
|
||||||
|
if args.show:
|
||||||
|
print_current_shell()
|
||||||
|
elif args.list:
|
||||||
|
print_devshells()
|
||||||
|
elif args.shell:
|
||||||
|
select_shell(args.shell)
|
||||||
|
else:
|
||||||
|
print_current_shell()
|
||||||
|
print("Use --help for more information")
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
|
|||||||
Reference in New Issue
Block a user