Skip to content

Commit

Permalink
rename func _ps_aliases_to_funcs_at_ps1_file
Browse files Browse the repository at this point in the history
  • Loading branch information
alanlivio committed Dec 4, 2024
1 parent be069af commit 2bda3d6
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 35 deletions.
11 changes: 8 additions & 3 deletions init.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,14 @@ $scriptToLoad = Join-Path -Path $HELPERS_DIR -ChildPath "os/win.ps1"

# -- load <program>.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
61 changes: 29 additions & 32 deletions init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,46 @@ 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'
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]} <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
}

# -- load os/<name>.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"
Expand All @@ -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]} <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

0 comments on commit 2bda3d6

Please sign in to comment.