From 0f77a8f8dcad8e0cecd1c2fd815eb118990d48fc Mon Sep 17 00:00:00 2001 From: Alan Guedes Date: Mon, 24 Jul 2023 11:08:53 +0100 Subject: [PATCH] add_sh_scripts_as_aliases and add_ps1_scripts_as_aliases --- README.md | 38 ++++++++++++++++++++++---------------- init.sh | 3 +-- os_any.bash | 15 +++++++++++++++ os_win.bash | 41 +++++++++++++++++++++++------------------ 4 files changed, 61 insertions(+), 36 deletions(-) diff --git a/README.md b/README.md index 1d5e29c6..f0d124da 100644 --- a/README.md +++ b/README.md @@ -10,24 +10,30 @@ The project logo refers to the synthetic chemical element Bohrium, which also ha flowchart LR bashrc["~/.bashrc"] subgraph bash-helpers - init["init.sh"] - any["os_any.bash"] - win["os_win.bash"] - ubu["os_ub.bash"] - mac["os_mac.bash"] - command-dependent[" - commands/COMMAND_NAME_1.bash - commands/COMMAND_NAME_2.bash - commands/COMMAND_NAME_3.bash - ... - "] + init["init.sh"] + any["os_any.bash"] + win["os_win.bash"] + ubu["os_ub.bash"] + mac["os_mac.bash"] + command-dependent[" + commands/COMMAND_NAME_1.bash + commands/COMMAND_NAME_2.bash + ... + "] + scripts[" + scripts/SCRIPT_1.ps1 + scripts/SCRIPT_2.sh + ... + "] end bashrc --> |"load"| init - init --> |"always load"|any - init --> |"if $OSTYPE == msys* || -n $wsl_DISTRO_NAME then load"|win - init --> |"if $OSTYPE == linux* then load"|ubu - init --> |"if $OSTYPE == mac* then load"|mac - init --> |"foreach in commands/*: if type COMMAND_NAME then load"|command-dependent + init --> |"load"| any + any --> |"alias *.sh"| scripts + win --> |"alias *.ps1"| scripts + init --> |"if $OSTYPE==msys* || -n $WSL_DISTRO_NAME then load"| win + init --> |"if $OSTYPE==linux* then load"| ubu + init --> |"if $OSTYPE==mac* then load"| mac + init --> |"if type COMMAND_NAME then load"| command-dependent ``` diff --git a/init.sh b/init.sh index 56ef8a97..63ecc9b7 100644 --- a/init.sh +++ b/init.sh @@ -9,7 +9,7 @@ BH_DIR="$(dirname "${BASH_SOURCE[0]}")" source "$BH_DIR/os_any.bash" if [[ $OSTYPE == msys* || -n $WSL_DISTRO_NAME ]]; then - source "$BH_DIR/os_win.bash"; + source "$BH_DIR/os_win.bash" fi if [[ $OSTYPE == linux* ]]; then @@ -20,7 +20,6 @@ if [[ $OSTYPE == darwin* ]]; then "$BH_DIR/os_mac.bash" fi - ######################### # load .bash files ######################### diff --git a/os_any.bash b/os_any.bash index 7cf2d8a3..6a621f56 100644 --- a/os_any.bash +++ b/os_any.bash @@ -1,3 +1,18 @@ +######################### +# load scripts as alias +######################### + +function add_sh_scripts_as_aliases(){ + for file in "$BH_DIR/scripts/"*.sh; do + if test -f $file &>/dev/null; then + script_name=$(basename ${file%.*}) + eval "alias $script_name='sh $file'" + fi + done +} + +add_sh_scripts_as_aliases + ######################### # basic ######################### diff --git a/os_win.bash b/os_win.bash index 9a2b7895..334e1a4a 100644 --- a/os_win.bash +++ b/os_win.bash @@ -1,16 +1,30 @@ -######################### -# basic -######################### - -alias ls='ls --color=auto -I NTUSER\* -I ntuser\* -I AppData -I IntelGraphicsProfiles* -I MicrosoftEdgeBackups' -BH_LIB_PS1="$BH_DIR/scripts/" - if [[ -n $WSL_DISTRO_NAME ]]; then alias winpath='wslpath -m' elif [[ $OSTYPE == linux* ]]; then alias winpath='cygpath -m' fi +######################### +# load scripts as alias +######################### + +function add_ps1_scripts_as_aliases(){ + for file in "$BH_DIR/scripts/"win_*.ps1; do + if test -f $file &>/dev/null; then + script_name=$(basename ${file%.*}) + eval "alias $script_name='powershell.exe $(winpath $file)'" + fi + done +} + +add_ps1_scripts_as_aliases + +######################### +# basic +######################### + +alias ls='ls --color=auto -I NTUSER\* -I ntuser\* -I AppData -I IntelGraphicsProfiles* -I MicrosoftEdgeBackups' + function home_clean_win() { if [[ -n $WSL_DISTRO_NAME ]]; then home_clean $(wslpath $(wslvar USERPROFILE)) @@ -117,7 +131,7 @@ function win_path_add() { # using ps1 script local dir=$(winpath "$@") local dircyg=$(cygpath "$@") # export in win - powershell.exe -c "$(winpath $BH_LIB_PS1/win_path_add.ps1)" \'$dir\' + powershell.exe -c "$(winpath $BH_DIR/scripts/win_path_add.ps1)" \'$dir\' # export in bash (it will reolad from win in new shell) if [[ ":$PATH:" != *":$dircyg:"* ]]; then export PATH=${PATH}:$dircyg; fi } @@ -151,13 +165,4 @@ function win_policy_reset() { gsudo cmd.exe /C 'RD /S /Q %WinDir%\\System32\\GroupPolicyUsers ' gsudo cmd.exe /C 'RD /S /Q %WinDir%\System32\GroupPolicy ' gsudo gpupdate.exe /force -} - -function ps1_script() { powershell.exe "$(winpath $BH_LIB_PS1/$1)"; } -function win_disable_apps_preinstalled() { ps1_script win_disable_apps_preinstalled.ps1; } -function win_disable_hotkeys() { ps1_script win_disable_hotkeys.ps1; } -function win_disable_password_policy() { ps1_script win_disable_password_policy.ps1; } -function win_disable_pc_folders() { ps1_script win_disable_pc_folders.ps1; } -function win_disable_sounds() { ps1_script win_disable_sounds.ps1; } -function win_enable_hyper_v() { ps1_script win_enable_hyper_v.ps1; } -function win_path_add() { ps1_script win_path_add.ps1; } +} \ No newline at end of file