Skip to content

Commit

Permalink
Change Nushell activation script to module (#2422)
Browse files Browse the repository at this point in the history
  • Loading branch information
kubouch authored Nov 27, 2022
1 parent e6925a6 commit ffa04ae
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 56 deletions.
1 change: 1 addition & 0 deletions docs/changelog/2422.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Change Nushell activation script to be a module meant to be activated as an overlay.
10 changes: 1 addition & 9 deletions src/virtualenv/activation/nushell/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import os
from pathlib import Path

from ..via_template import ViaTemplateActivator
Expand All @@ -7,20 +6,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

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),
"__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"),
}


Expand Down
31 changes: 16 additions & 15 deletions src/virtualenv/activation/nushell/activate.nu
Original file line number Diff line number Diff line change
@@ -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'
}
Expand All @@ -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'
Expand Down Expand Up @@ -50,7 +57,7 @@ def-env activate-virtualenv [] {
}

# 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') {
Expand All @@ -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_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
32 changes: 0 additions & 32 deletions src/virtualenv/activation/nushell/deactivate.nu

This file was deleted.

7 changes: 7 additions & 0 deletions tests/unit/activation/test_nushell.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit ffa04ae

Please sign in to comment.