This commit is contained in:
Waylon Walker 2022-03-31 20:20:07 -05:00
commit 38355d2442
No known key found for this signature in database
GPG key ID: 66E2BF2B4190EFE4
9083 changed files with 1225834 additions and 0 deletions

View file

@ -0,0 +1,28 @@
from __future__ import absolute_import, unicode_literals
import os
from virtualenv.util.path import Path
from virtualenv.util.six import ensure_text
from ..via_template import ViaTemplateActivator
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__": ensure_text(str(creator.dest)),
"__VIRTUAL_NAME__": creator.env_name,
"__BIN_NAME__": ensure_text(str(creator.bin_dir.relative_to(creator.dest))),
"__PATH_SEP__": ensure_text(os.pathsep),
"__DEACTIVATE_PATH__": ensure_text(str(Path(dest_folder) / "deactivate.nu")),
}

View file

@ -0,0 +1,92 @@
# This command prepares the required environment variables
def-env activate-virtualenv [] {
def is-string [x] {
($x | describe) == 'string'
}
def has-env [name: string] {
$name in (env).name
}
let is-windows = ((sys).host.name | str downcase) == 'windows'
let virtual-env = '__VIRTUAL_ENV__'
let bin = '__BIN_NAME__'
let path-sep = '__PATH_SEP__'
let path-name = if $is-windows {
if (has-env 'Path') {
'Path'
} else {
'PATH'
}
} else {
'PATH'
}
let old-path = (
if $is-windows {
if (has-env 'Path') {
$env.Path
} else {
$env.PATH
}
} else {
$env.PATH
} | if (is-string $in) {
# if Path/PATH is a string, make it a list
$in | split row $path-sep | path expand
} else {
$in
}
)
let venv-path = ([$virtual-env $bin] | path join)
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__' == '') {
$'(char lparen)($virtual-env | path basename)(char rparen) '
} else {
'(__VIRTUAL_PROMPT__) '
}
# Back up the old prompt builder
let old-prompt-command = if (has-env 'VIRTUAL_ENV') && (has-env '_OLD_PROMPT_COMMAND') {
$env._OLD_PROMPT_COMMAND
} else {
if (has-env 'PROMPT_COMMAND') {
$env.PROMPT_COMMAND
} else {
''
}
}
# If there is no default prompt, then only the env is printed in the prompt
let new-prompt = if (has-env 'PROMPT_COMMAND') {
if ($old-prompt-command | describe) == 'block' {
{ $'($virtual-prompt)(do $old-prompt-command)' }
} else {
{ $'($virtual-prompt)($old-prompt-command)' }
}
} else {
{ $'($virtual-prompt)' }
}
# Environment variables that will be batched loaded to the virtual env
let new-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__'

View file

@ -0,0 +1,32 @@
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