diff --git a/init.ps1 b/init.ps1 index 8a07fd62..533b5c83 100644 --- a/init.ps1 +++ b/init.ps1 @@ -18,9 +18,14 @@ $scriptToLoad = Join-Path -Path $HELPERS_DIR -ChildPath "os/win.ps1" # -- load .bash files -- -# TODO - -# -- load funcs from init.sh as aliases -- +# TODO: redo this code to load .bash if program exist. +# make sure to create aliases for the windows .exe. Maybe do it at _sh_aliases_to_funcs_at_bash_file +# for file in "$HELPERS_DIR/programs/"*.bash; do +# program=$(basename ${file%.*}) +# if type $program &>/dev/null; then +# source $file +# fi +# end # TODO # skip if exists \ No newline at end of file diff --git a/init.sh b/init.sh index eee7b650..88f16aac 100644 --- a/init.sh +++ b/init.sh @@ -4,6 +4,7 @@ HELPERS_DIR="$(dirname "${BASH_SOURCE[0]}")" function log_error() { echo -e "\033[00;31m-- $* \033[00m"; } function log_msg() { echo -e "\033[00;33m-- $* \033[00m"; } +# TODO: convert usefull from ps1 to functions alias bashrc_reload='source $HOME/.bashrc' alias folder_count_files='find . -maxdepth 1 -type f | wc -l' alias folder_count_files_recusive='find . -maxdepth 1 -type f | wc -l' @@ -11,10 +12,38 @@ alias folder_list_sorted_by_size='du -ahd 1 | sort -h' alias folder_find_file_with_crlf='find . -not -type d -exec file "{}" ";" | grep CRLF' alias passwd_generate='echo $(tr -dc "A-Za-z0-9!?%=" < /dev/urandom | head -c 12)' +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_aliases_to_funcs_at_ps1_file() { + : ${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 +} + # -- load os/.bash files -- if [[ $OSTYPE == msys* || -n $WSL_DISTRO_NAME ]]; then source "$HELPERS_DIR/os/win.bash" + _ps_aliases_to_funcs_at_ps1_file "$HELPERS_DIR/os/win.ps1" fi if [[ $OSTYPE == linux* ]]; then source "$HELPERS_DIR/os/ubu.bash" @@ -29,36 +58,4 @@ for file in "$HELPERS_DIR/programs/"*.bash; do 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