From f95afb160dcad08b3a080e4a145ee2680de22e9e Mon Sep 17 00:00:00 2001 From: kubouch Date: Sat, 17 Sep 2022 22:18:25 +0200 Subject: [PATCH 1/7] Clean up Nushell activation and make it a module --- src/virtualenv/activation/nushell/__init__.py | 7 ---- src/virtualenv/activation/nushell/activate.nu | 33 ++++++++++--------- tests/unit/activation/test_nushell.py | 7 ++++ 3 files changed, 24 insertions(+), 23 deletions(-) diff --git a/src/virtualenv/activation/nushell/__init__.py b/src/virtualenv/activation/nushell/__init__.py index 839c19c26..cc991b67d 100644 --- a/src/virtualenv/activation/nushell/__init__.py +++ b/src/virtualenv/activation/nushell/__init__.py @@ -7,20 +7,13 @@ class NushellActivator(ViaTemplateActivator): def templates(self): yield Path("activate.nu") - yield Path("deactivate.nu") def replacements(self, creator, dest_folder): - # Due to nushell scoping, it isn't easy to create a function that will - # deactivate the environment. For that reason a __DEACTIVATE_PATH__ - # replacement pointing to the deactivate.nu file is created - return { "__VIRTUAL_PROMPT__": "" if self.flag_prompt is None else self.flag_prompt, "__VIRTUAL_ENV__": str(creator.dest), "__VIRTUAL_NAME__": creator.env_name, "__BIN_NAME__": str(creator.bin_dir.relative_to(creator.dest)), - "__PATH_SEP__": os.pathsep, - "__DEACTIVATE_PATH__": str(Path(dest_folder) / "deactivate.nu"), } diff --git a/src/virtualenv/activation/nushell/activate.nu b/src/virtualenv/activation/nushell/activate.nu index 48c85b488..c3e5c9608 100644 --- a/src/virtualenv/activation/nushell/activate.nu +++ b/src/virtualenv/activation/nushell/activate.nu @@ -1,5 +1,12 @@ -# This command prepares the required environment variables -def-env activate-virtualenv [] { +# virtualenv activation module +# Activate with `overlay use activate.nu` +# Deactivate with `deactivate`, as usual +# +# To customize the overlay name, you can call `overlay use activate.nu as foo`, +# but then simply `deactivate` won't work because it is just an alias to hide +# the "activate" overlay. You'd need to call `overlay hide foo` manually. + +export-env { def is-string [x] { ($x | describe) == 'string' } @@ -8,10 +15,10 @@ def-env activate-virtualenv [] { $name in (env).name } - let is_windows = ((sys).host.name | str downcase) == 'windows' + let is_windows = ($nu.os-info.name | str downcase) == 'windows' let virtual_env = '__VIRTUAL_ENV__' let bin = '__BIN_NAME__' - let path_sep = '__PATH_SEP__' + let path_sep = (char esep) let path_name = if $is_windows { if (has-env 'Path') { 'Path' @@ -40,7 +47,7 @@ def-env activate-virtualenv [] { ) let venv_path = ([$virtual_env $bin] | path join) - let new_path = ($old_path | prepend $venv_path | str collect $path_sep) + let new_path = ($old_path | prepend $venv_path | str join $path_sep) # Creating the new prompt for the session let virtual_prompt = if ('__VIRTUAL_PROMPT__' == '') { @@ -71,22 +78,16 @@ def-env activate-virtualenv [] { { $'($virtual_prompt)' } } - # Environment variables that will be batched loaded to the virtual env - let new_env = { + # Environment variables that will be loaded as the virtual env + load-env { $path_name : $new_path VIRTUAL_ENV : $virtual_env - _OLD_VIRTUAL_PATH : ($old_path | str collect $path_sep) + _OLD_VIRTUAL_PATH : ($old_path | str join $path_sep) _OLD_PROMPT_COMMAND : $old_prompt_command PROMPT_COMMAND : $new_prompt VIRTUAL_PROMPT : $virtual_prompt } - - # Activate the environment variables - load-env $new_env } -# Activate the virtualenv -activate-virtualenv - -alias pydoc = python -m pydoc -alias deactivate = source '__DEACTIVATE_PATH__' +export alias pydoc = python -m pydoc +export alias deactivate = overlay hide activate diff --git a/tests/unit/activation/test_nushell.py b/tests/unit/activation/test_nushell.py index a778d4975..cf5886a0c 100644 --- a/tests/unit/activation/test_nushell.py +++ b/tests/unit/activation/test_nushell.py @@ -13,9 +13,16 @@ def __init__(self, session): super().__init__(NushellActivator, session, cmd, "activate.nu", "nu") + self.activate_cmd = "overlay use" self.unix_line_ending = not IS_WIN def print_prompt(self): return r"$env.VIRTUAL_PROMPT" + def activate_call(self, script): + # Commands are called without quotes in Nushell + cmd = self.activate_cmd + scr = self.quote(str(script)) + return f"{cmd} {scr}".strip() + activation_tester(Nushell) From adf238ff405945e616b6c915e7cc00dd7f7c0a23 Mon Sep 17 00:00:00 2001 From: kubouch Date: Sat, 17 Sep 2022 22:19:54 +0200 Subject: [PATCH 2/7] Remove unused Nushell deactivate script --- .../activation/nushell/deactivate.nu | 32 ------------------- 1 file changed, 32 deletions(-) delete mode 100644 src/virtualenv/activation/nushell/deactivate.nu diff --git a/src/virtualenv/activation/nushell/deactivate.nu b/src/virtualenv/activation/nushell/deactivate.nu deleted file mode 100644 index 4dd132c34..000000000 --- a/src/virtualenv/activation/nushell/deactivate.nu +++ /dev/null @@ -1,32 +0,0 @@ -def-env deactivate-virtualenv [] { - def has-env [name: string] { - $name in (env).name - } - - let is_windows = ((sys).host.name | str downcase) == 'windows' - - let path_name = if $is_windows { - if (has-env 'Path') { - 'Path' - } else { - 'PATH' - } - } else { - 'PATH' - } - - load-env { $path_name : $env._OLD_VIRTUAL_PATH } - - let-env PROMPT_COMMAND = $env._OLD_PROMPT_COMMAND - - # Hiding the environment variables that were created when activating the env - hide _OLD_VIRTUAL_PATH - hide _OLD_PROMPT_COMMAND - hide VIRTUAL_ENV - hide VIRTUAL_PROMPT -} - -deactivate-virtualenv - -hide pydoc -hide deactivate From b217e17c1a3ef73f42527ae75067e925a9e53f13 Mon Sep 17 00:00:00 2001 From: kubouch Date: Sat, 17 Sep 2022 22:24:23 +0200 Subject: [PATCH 3/7] Remove unused import --- src/virtualenv/activation/nushell/__init__.py | 1 - 1 file changed, 1 deletion(-) diff --git a/src/virtualenv/activation/nushell/__init__.py b/src/virtualenv/activation/nushell/__init__.py index cc991b67d..02b342b46 100644 --- a/src/virtualenv/activation/nushell/__init__.py +++ b/src/virtualenv/activation/nushell/__init__.py @@ -1,4 +1,3 @@ -import os from pathlib import Path from ..via_template import ViaTemplateActivator From 623eb7cb75e1e535f7f93e9108496a9bf450f31d Mon Sep 17 00:00:00 2001 From: kubouch Date: Sat, 17 Sep 2022 22:32:33 +0200 Subject: [PATCH 4/7] Ignore flake8 error --- src/virtualenv/activation/nushell/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/virtualenv/activation/nushell/__init__.py b/src/virtualenv/activation/nushell/__init__.py index 02b342b46..72640a2e8 100644 --- a/src/virtualenv/activation/nushell/__init__.py +++ b/src/virtualenv/activation/nushell/__init__.py @@ -7,7 +7,7 @@ class NushellActivator(ViaTemplateActivator): def templates(self): yield Path("activate.nu") - def replacements(self, creator, dest_folder): + def replacements(self, creator, dest_folder): # noqa: U100 return { "__VIRTUAL_PROMPT__": "" if self.flag_prompt is None else self.flag_prompt, "__VIRTUAL_ENV__": str(creator.dest), From 276756177eb19dca9f4365c40c9c9459d5e99866 Mon Sep 17 00:00:00 2001 From: kubouch Date: Sat, 17 Sep 2022 22:43:16 +0200 Subject: [PATCH 5/7] Rename `str join` back to `str collect` --- src/virtualenv/activation/nushell/activate.nu | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/virtualenv/activation/nushell/activate.nu b/src/virtualenv/activation/nushell/activate.nu index c3e5c9608..78c528fd5 100644 --- a/src/virtualenv/activation/nushell/activate.nu +++ b/src/virtualenv/activation/nushell/activate.nu @@ -47,7 +47,7 @@ export-env { ) let venv_path = ([$virtual_env $bin] | path join) - let new_path = ($old_path | prepend $venv_path | str join $path_sep) + let new_path = ($old_path | prepend $venv_path | str collect $path_sep) # Creating the new prompt for the session let virtual_prompt = if ('__VIRTUAL_PROMPT__' == '') { @@ -82,7 +82,7 @@ export-env { load-env { $path_name : $new_path VIRTUAL_ENV : $virtual_env - _OLD_VIRTUAL_PATH : ($old_path | str join $path_sep) + _OLD_VIRTUAL_PATH : ($old_path | str collect $path_sep) _OLD_PROMPT_COMMAND : $old_prompt_command PROMPT_COMMAND : $new_prompt VIRTUAL_PROMPT : $virtual_prompt From b5885be8c6277ce283d5bb991599d5b77a4a8f1c Mon Sep 17 00:00:00 2001 From: kubouch Date: Sat, 17 Sep 2022 23:05:06 +0200 Subject: [PATCH 6/7] Add changelog entry --- docs/changelog/2422.feature.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 docs/changelog/2422.feature.rst diff --git a/docs/changelog/2422.feature.rst b/docs/changelog/2422.feature.rst new file mode 100644 index 000000000..e4a1fd516 --- /dev/null +++ b/docs/changelog/2422.feature.rst @@ -0,0 +1 @@ +Change Nushell activation script to be a module meant to be activated as an overlay. From aff7c91ab2075c1629a51ea861e8210901a38f96 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20=C5=BD=C3=A1dn=C3=ADk?= Date: Sun, 27 Nov 2022 18:45:20 +0200 Subject: [PATCH 7/7] Fix operator --- src/virtualenv/activation/nushell/activate.nu | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/virtualenv/activation/nushell/activate.nu b/src/virtualenv/activation/nushell/activate.nu index 78c528fd5..c5568c9a5 100644 --- a/src/virtualenv/activation/nushell/activate.nu +++ b/src/virtualenv/activation/nushell/activate.nu @@ -57,7 +57,7 @@ export-env { } # Back up the old prompt builder - let old_prompt_command = if (has-env 'VIRTUAL_ENV') && (has-env '_OLD_PROMPT_COMMAND') { + let old_prompt_command = if (has-env 'VIRTUAL_ENV') and (has-env '_OLD_PROMPT_COMMAND') { $env._OLD_PROMPT_COMMAND } else { if (has-env 'PROMPT_COMMAND') {