-
Notifications
You must be signed in to change notification settings - Fork 3
/
init.sh
61 lines (50 loc) · 1.8 KB
/
init.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
HELPERS_DIR="$(dirname "${BASH_SOURCE[0]}")"
# -- bash basic --
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"
fi
# -- load <program>.bash files --
for file in "$HELPERS_DIR/programs/"*.bash; do
program=$(basename ${file%.*})
if type $program &>/dev/null; then
source $file
fi
done