mirror of
https://github.com/Cian-H/dotfiles.git
synced 2026-02-22 16:08:04 +00:00
Changed . token to _dot
This change allows the dotfiles to work with chezmoi (e.g: on windows) and improves grepability with neovim/telescope
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
# Auto virtual environment scripts
|
||||
|
||||
The scripts in this directory activate virtual environments whenever you cd into a directory with a "trigger" file
|
||||
|
||||
## Usage
|
||||
|
||||
1. set `$env.AUTO_VENV_TRIGGER` as the preferred name of a trigger file
|
||||
1. import `auto-venv` into your environment
|
||||
1. Create a symlink via `ln -s` of the script file `./auto-venv/venvs/python-venv.nu` to a trigger file in project at `/path/to/project/$AUTO_VENV_TRIGGER`
|
||||
1. `cd` into `/path/to/project/`
|
||||
|
||||
For example:
|
||||
```nu
|
||||
# config.nu
|
||||
|
||||
export-env {
|
||||
$env.AUTO_VENV_TRIGGER = '__auto-venv.nu'
|
||||
|
||||
source-env ~/path/to/nu_scripts/modules/virtual_environments/auto-venv/auto-venv.nu
|
||||
}
|
||||
|
||||
use ~/path/to/nu_scripts/modules/virtual_environments/auto-venv/auto-venv.nu *
|
||||
```
|
||||
|
||||
When `auto-venv` detects that a file of that name exists in the folder you cd'ed into (or one of it's parents), it will automatically enable an overlay (as defined by the trigger file).
|
||||
|
||||
An example overlay for python venv is in `./venvs`
|
||||
|
||||
NOTE: the trigger file *must* export a custom command called `auto-venv-on-enter` that takes in the `$env` (this is the environment *before* the overlay was enabled).
|
||||
|
||||
|
||||
## Limitations
|
||||
|
||||
- Due to limitations with overlay naming, you cannot nest auto-venv triggers, even for separate languages / toolchains
|
||||
@@ -0,0 +1,86 @@
|
||||
# must source-env and 'use *' this file
|
||||
|
||||
# must be in 'global' scope when `condition` is called
|
||||
export use venv_helpers.nu
|
||||
export use path_extensions.nu
|
||||
|
||||
|
||||
export-env {
|
||||
|
||||
# this needs to be set somewhere
|
||||
# $env.AUTO_VENV_TRIGGER = '__auto-venv.nu'
|
||||
|
||||
let hooks = (default-hooks)
|
||||
|
||||
let hooks = ($hooks | append (build-hooks))
|
||||
|
||||
$env.config = ($env.config | upsert hooks.env_change.PWD $hooks )
|
||||
}
|
||||
|
||||
|
||||
def default-hooks [] {
|
||||
(if ($env.config.hooks.env_change.PWD != null) {
|
||||
[$env.config.hooks.env_change.PWD]
|
||||
}
|
||||
else {
|
||||
[]
|
||||
})
|
||||
}
|
||||
|
||||
def build-hooks [] {
|
||||
|
||||
let trigger = $env.AUTO_VENV_TRIGGER
|
||||
|
||||
let on_enter = '
|
||||
let _env = $env
|
||||
let pwd = $_env.PWD
|
||||
|
||||
let trigger = (path_extensions path find-sub . __trigger__ --type ["symlink", "file"])
|
||||
|
||||
cd ($trigger | path dirname)
|
||||
overlay use __trigger__ as __auto_venv
|
||||
|
||||
cd ($pwd)
|
||||
|
||||
auto-venv-on-enter $_env
|
||||
|
||||
hide _env
|
||||
hide pwd
|
||||
hide trigger
|
||||
'
|
||||
|
||||
let on_exit = '
|
||||
overlay hide __auto_venv --keep-env [PWD]
|
||||
'
|
||||
|
||||
let on_enter = ($on_enter | str replace -a '__trigger__' $trigger)
|
||||
|
||||
let hook = [
|
||||
# activate on entry
|
||||
{
|
||||
condition: {|before, after| venv_helpers has-entered-venv $after }
|
||||
code: $'
|
||||
($on_enter)
|
||||
'
|
||||
}
|
||||
|
||||
# re-activate on swap
|
||||
{
|
||||
condition: {|before, after| venv_helpers has-swapped-venv $after }
|
||||
code: $'
|
||||
($on_exit)
|
||||
($on_enter)
|
||||
'
|
||||
}
|
||||
|
||||
# deactivate on exit
|
||||
{
|
||||
condition: { |before, after| venv_helpers has-exited-venv $after }
|
||||
code: $'
|
||||
($on_exit)
|
||||
'
|
||||
}
|
||||
]
|
||||
|
||||
$hook
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
|
||||
# Returns a list of full paths starting from root, to the path given
|
||||
#
|
||||
#
|
||||
# walk '/home/user/projects';
|
||||
# > [
|
||||
# > '/home/',
|
||||
# > '/home/user/',
|
||||
# > '/home/user/projects/',
|
||||
# > ]
|
||||
#
|
||||
export def "path walk" [
|
||||
path: any
|
||||
] {
|
||||
let list = ($path | path expand | path split);
|
||||
|
||||
$list | enumerate | each { |$part| (
|
||||
$list | first ($part.index + 1) | path join;
|
||||
)}
|
||||
|
||||
}
|
||||
|
||||
# Returns true if 'subfolder' is found along the path of 'folder', with a given type
|
||||
export def "path check-sub" [
|
||||
folder: any,
|
||||
subfolder: string,
|
||||
--type: list
|
||||
] {
|
||||
|
||||
(ls -a $folder
|
||||
| where (
|
||||
($type == null or $it.type in $type)
|
||||
and ($it.name | path basename) == $subfolder
|
||||
)
|
||||
| length
|
||||
) > 0;
|
||||
}
|
||||
|
||||
# Walks the path along 'folder', and returns the path of the first subfolder found, or nothing if the subfolder was not found.
|
||||
#
|
||||
# path find-sub '/home/user/projects/code' '.venv';
|
||||
# > /home/user/projects/.venv
|
||||
export def "path find-sub" [
|
||||
folder: any,
|
||||
subfolder: string,
|
||||
--type: list
|
||||
] {
|
||||
let paths = (path walk $folder);
|
||||
|
||||
let paths = ( $paths
|
||||
| where (
|
||||
path check-sub $it $subfolder --type $type
|
||||
)
|
||||
);
|
||||
|
||||
if ($paths != null) and ($paths | length) > 0 {
|
||||
[ ($paths | first), $subfolder ] | path join
|
||||
} else {[]}
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
use path_extensions.nu *
|
||||
|
||||
def has-sub [
|
||||
folder: string,
|
||||
subfolder: string
|
||||
] {
|
||||
not (path find-sub $folder $subfolder | is-empty);
|
||||
}
|
||||
|
||||
def get-env [
|
||||
name: string
|
||||
default: any
|
||||
] {
|
||||
(if ($name in $env) {
|
||||
$env | get $name
|
||||
}
|
||||
else {
|
||||
$default
|
||||
})
|
||||
}
|
||||
|
||||
export def venv-is-active [] {
|
||||
'__auto_venv' in (overlay list)
|
||||
}
|
||||
|
||||
# Creates a virtual environment under the current directory
|
||||
export def 'venv-create' [] {
|
||||
let venv_path = $env.PWD
|
||||
let venv_name = ($env.PWD | path basename)
|
||||
|
||||
let is_posix = ($nu.os-info.family == 'unix')
|
||||
let python_name = if $is_posix {'python3'} else {'py.exe'}
|
||||
run-external $python_name "-m" "venv" ".venv" "--clear" "--prompt" $venv_name
|
||||
run-external $".venv/bin/($python_name)" "-m" "pip" "install" "-U" "pip" "wheel" "setuptools"
|
||||
|
||||
let trigger_file = ([$env.PWD, $env.AUTO_VENV_TRIGGER] | path join)
|
||||
ln -sf $'($env.FILE_PWD)/venvs/python-venv.nu' $trigger_file
|
||||
}
|
||||
|
||||
export def has-entered-venv [
|
||||
after: path,
|
||||
] {
|
||||
|
||||
let target = (path find-sub $after $env.AUTO_VENV_TRIGGER)
|
||||
|
||||
(if ($target | is-empty) {
|
||||
false
|
||||
}
|
||||
else {
|
||||
# if venv is already active, handle it in "venv swap" hook
|
||||
not (venv-is-active)
|
||||
})
|
||||
}
|
||||
|
||||
export def has-swapped-venv [
|
||||
after: path,
|
||||
] {
|
||||
|
||||
(if not (venv-is-active) {
|
||||
false
|
||||
}
|
||||
else {
|
||||
let target = (path find-sub $after $env.AUTO_VENV_TRIGGER)
|
||||
|
||||
(if ($target | is-empty) {
|
||||
false
|
||||
}
|
||||
else {
|
||||
(if ('VIRTUAL_ENV' in $env) {
|
||||
$env.VIRTUAL_ENV != $target
|
||||
} else {
|
||||
false # should it default to `false`?
|
||||
})
|
||||
})
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
export def has-exited-venv [
|
||||
after: path,
|
||||
] {
|
||||
|
||||
(if not (venv-is-active) {
|
||||
false
|
||||
}
|
||||
else {
|
||||
not (has-sub $after $env.AUTO_VENV_TRIGGER)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -0,0 +1,107 @@
|
||||
|
||||
###
|
||||
# An example auto-venv module.
|
||||
# Copy this into `~/your/project/__auto-venv.nu` (or whatever you named your trigger file)
|
||||
|
||||
# adapted from https://github.com/pypa/virtualenv/blob/46f68d67c79f2280554f47f3c21265b3a1e899a4/src/virtualenv/activation/nushell/activate.nu
|
||||
|
||||
export def --env auto-venv-on-enter [
|
||||
_env: record,
|
||||
] {
|
||||
|
||||
def is-string [x] {
|
||||
($x | describe) == 'string'
|
||||
}
|
||||
|
||||
def has-env [name: string] {
|
||||
$name in ($_env)
|
||||
}
|
||||
|
||||
let virtual_env = (path_extensions path find-sub ($_env.PWD | into string) '.venv')
|
||||
let bin = ([$virtual_env, "bin"] | path join)
|
||||
let virtual_prompt = ""
|
||||
|
||||
let is_windows = $nu.os-info.name == 'windows'
|
||||
let path_name = if $is_windows {
|
||||
if (has-env 'Path') {
|
||||
'Path'
|
||||
} else {
|
||||
'PATH'
|
||||
}
|
||||
} else {
|
||||
'PATH'
|
||||
}
|
||||
|
||||
let path_sep = (if $nu.os-info.name == "windows" {
|
||||
'\'
|
||||
}
|
||||
else {
|
||||
'/'
|
||||
})
|
||||
|
||||
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 join $path_sep)
|
||||
let new_path = ($old_path | prepend $venv_path)
|
||||
|
||||
# Creating the new prompt for the session
|
||||
let virtual_prompt = if ($virtual_prompt == '') {
|
||||
$'(char lparen)($virtual_env | path split | drop 1 | path join | path basename)(char rparen) '
|
||||
} else {
|
||||
'(' + $virtual_prompt + ') '
|
||||
}
|
||||
|
||||
let old_prompt_command = 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) in ['block', 'closure']) {
|
||||
$'($virtual_prompt)(do $old_prompt_command)'
|
||||
} else {
|
||||
$'($virtual_prompt)($old_prompt_command)'
|
||||
}
|
||||
} else {
|
||||
$'($virtual_prompt)'
|
||||
}
|
||||
|
||||
# Add current PWD to NU_LIB_DIRS so we can enter sub-directory without an error
|
||||
let new_lib_dirs = if not $env.PWD in $env.NU_LIB_DIRS {
|
||||
$env.NU_LIB_DIRS | prepend $env.PWD
|
||||
} else {
|
||||
$env.NU_LIB_DIRS
|
||||
}
|
||||
# Environment variables that will be batched loaded to the virtual env
|
||||
let new_env = {
|
||||
$path_name : $new_path
|
||||
VIRTUAL_ENV : $virtual_env
|
||||
PROMPT_COMMAND : $new_prompt
|
||||
VIRTUAL_PROMPT : $virtual_prompt
|
||||
NU_LIB_DIRS : $new_lib_dirs
|
||||
}
|
||||
|
||||
# Activate the environment variables
|
||||
load-env $new_env
|
||||
}
|
||||
|
||||
export alias pydoc = python -m pydoc
|
||||
export alias pip = python -m pip
|
||||
Reference in New Issue
Block a user