diff --git a/README.md b/README.md index 3b8b57da..697d1cd1 100644 --- a/README.md +++ b/README.md @@ -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/.*` 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/.*` 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 diff --git a/init.ps1 b/init.ps1 index 2591ce9c..dfded042 100644 --- a/init.ps1 +++ b/init.ps1 @@ -3,4 +3,14 @@ $HELPERS_DIR = Split-Path -Parent $MyInvocation.MyCommand.Path # load os_.ps1 files $scriptToLoad = Join-Path -Path $HELPERS_DIR -ChildPath "os_win.ps1" -. $scriptToLoad \ No newline at end of file +. $scriptToLoad + +# -- load .bash files -- + +# TODO + + +# -- load funcs from init.sh as aliases -- + +# TODO +# skip if exists \ No newline at end of file diff --git a/init.sh b/init.sh index 6bad42b1..52ec7f37 100644 --- a/init.sh +++ b/init.sh @@ -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]} "} + # 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 diff --git a/os_win.bash b/os_win.bash index 6e770022..fc431cf2 100644 --- a/os_win.bash +++ b/os_win.bash @@ -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]} "} - # 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