Skip to content

Commit

Permalink
load bash alias from init.ps1
Browse files Browse the repository at this point in the history
  • Loading branch information
alanlivio committed Nov 18, 2024
1 parent 3bafd12 commit ee70126
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 55 deletions.
42 changes: 26 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,39 +1,49 @@
# ps-sh-helpers

This project is template for creating PowerShell and Bash helpers. It let you organize helpers in `OS-dependent` from `os_*` files and loads `program-dependent` from `programs/<program>.*` files. It is initialized at `~/.bashrc` by loading `init.sh` or at `PowerShell_profile.ps1` by loading `init.ps1` (see diagram below).
`ps-sh-helpers` is a template for creating your library PowerShell and Bash helpers. It is very usfeull for Windows users that wants take the best of WSL Bash and integrate it with PowerShell.

`ps-sh-helpers` organize helpers in OS-dependent from `os_*` files and loads program-dependent from `programs/<program>.*` files. It is initialized at `.bashrc` by loading `init.sh` or at `PowerShell_profile.ps1` by loading `init.ps1` (see diagram below).

```mermaid
%%{init: {'theme':'dark'}}%%
flowchart LR
bashrc["~/.bashrc"]
init["ps-sh-hepers/init.sh"]
oswinps1["os_win.ps1"]
bashrc[".bashrc"]
ps-init["init.ps1"]
sh-init["init.sh"]
program-dependent["
programs/[program].bash
...
"]
win["os_win.bash"]
ubu["os_ubu.bash"]
bashrc --> |"loads"| init
init --> |"loads if program exists"| program-dependent
win --> |"bash alias to each function at"| oswinps1
init --> |"loads if runing from WSL or MSSYS2"| win
init --> |"loads if running from Ubu"| ubu
OS-dependent["
os_win.bash
os_ubu.bash
"]
bashrc --> |"loads"| sh-init
sh-init --> |"loads if program exists"| program-dependent
sh-init --> |"loads if runing at OS"| OS-dependent
sh-init --> |"bash alias to each function at"| ps-init
```

```mermaid
%%{init: {'theme':'dark'}}%%
flowchart LR
psprofile["Microsoft.PowerShell_profile.ps1"]
ps-init["init.ps1"]
sh-init["init.sh"]
program-dependent["
programs/[program].ps1
...
"]
init --> |"loads if program exists"| program-dependent
init["ps-sh-hepers/init.ps1"]
oswinps1["os_win.ps1"]
psprofile--> |"loads"| init
init --> |"loads"| oswinps1
OS-dependent["
os_win.ps1
os_ubu.ps1
"]
psprofile--> |"loads"| ps-init
ps-init --> |"loads if program exists"| program-dependent
ps-init --> |"loads if runing at OS"| OS-dependent
ps-init --> |"bash alias to each function at"| sh-init
```

## How to install
Expand Down
12 changes: 11 additions & 1 deletion init.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,14 @@ $HELPERS_DIR = Split-Path -Parent $MyInvocation.MyCommand.Path
# load os_<name>.ps1 files

$scriptToLoad = Join-Path -Path $HELPERS_DIR -ChildPath "os_win.ps1"
. $scriptToLoad
. $scriptToLoad

# -- load <program>.bash files --

# TODO


# -- load funcs from init.sh as aliases --

# TODO
# skip if exists
34 changes: 34 additions & 0 deletions init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,37 @@ for file in "$HELPERS_DIR/programs/"*.bash; do
source $file
fi
done

# -- load funcs from init.ps1 as aliases --

if type powershell &>/dev/null; then

function _ps_call() {
powershell -command "& { . $(wslpath -w $HELPERS_DIR/init.ps1); $* }"
}

function _ps_def_func() {
if ! typeset -f $1 >/dev/null 2>&1; then
eval 'function '$1'() { _ps_call' $1 '$*; }'
fi
}

function ps_def_funcs_from_ps1() {
: ${1?"Usage: ${FUNCNAME[0]} <ps1_file>"}
# load functions from file that does not start with _
# TODO: skip if exists
if test -f $1; then
_regex_no_underscore_func='function\s([^_][^{]+)\('
while read -r line; do
if [[ $line =~ $_regex_no_underscore_func ]]; then
func=${BASH_REMATCH[1]}
_ps_def_func $func
fi
done <$1
else
echo "$1 does not exist"
fi
}

ps_def_funcs_from_ps1 $HELPERS_DIR/init.ps1
fi
38 changes: 0 additions & 38 deletions os_win.bash
Original file line number Diff line number Diff line change
Expand Up @@ -11,44 +11,6 @@ else
alias win_dir_as_unix_format='cygpath -m'
fi

# -- load funcs from init.ps1 as aliases --

function _ps_call() {
powershell.exe -command "& { . $(wslpath -w $HELPERS_DIR/os_win.ps1); $* }"
}

function _ps_def_func() {
if ! typeset -f $1 >/dev/null 2>&1; then
eval 'function '$1'() { _ps_call' $1 '$*; }'
fi
}

function ps_def_funcs_from_ps1() {
: ${1?"Usage: ${FUNCNAME[0]} <ps1_file>"}
# load functions from file that does not start with _
if test -f $1; then
_regex_no_underscore_func='function\s([^_][^{]+)\('
while read -r line; do
if [[ $line =~ $_regex_no_underscore_func ]]; then
func=${BASH_REMATCH[1]}
_ps_def_func $func
fi
done <$1
else
echo "$1 does not exist"
fi
}

_regex_no_underscore_func='function\s([^_][^{]+)\('
while read -r line; do
if [[ $line =~ $_regex_no_underscore_func ]]; then
func=${BASH_REMATCH[1]}
if [[ ! $func =~ ^(log_msg)$ ]]; then # also exists in win.ps1
_ps_def_func $func
fi
fi
done <$HELPERS_DIR/os_win.ps1

function wsl_install_cuda_cudnn() {
# https://canonical-ubuntu-wsl.readthedocs-hosted.com/en/latest/tutorials/gpu-cuda/
sudo apt-key del 7fa2af80
Expand Down

0 comments on commit ee70126

Please sign in to comment.