-
Notifications
You must be signed in to change notification settings - Fork 0
/
alias-creator.sh
executable file
·129 lines (97 loc) · 2.69 KB
/
alias-creator.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/bin/bash
set -ue
HOME=$(echo $HOME)
PATH_TARGET_ALIAS="$HOME/.my-aliases"
profile_file_path=""
target_folder="${1:-}"
help() {
echo
echo "Alias Creator for UNIX system"
echo
echo "Usage: alias-creator ~/path-folder/"
echo
echo "Example:"
echo " alias-creator.sh ~/Documents/"
}
if [ -z $target_folder ]; then
echo "[ERROR]: Specify the path of the containing folder to set the aliases"
help
exit 1
fi
if [ ! -d $target_folder ]; then
echo "[ERROR]: Path '${target_folder}' not exist, specify an existing path"
exit 1
fi
append_data_to_file() {
local file_path=$1
local data_to_append=$2
echo "${data_to_append}" >>"$file_path"
}
get_profile_file() {
SUPPORTED_PROFILE_FILES=(.zshrc bashrc bash_alias bash_profile .profile)
for file in "${SUPPORTED_PROFILE_FILES[@]}"; do
path="${HOME}/${file}"
if [ -f $path ]; then
profile_file_path="$HOME/$file"
break
fi
done
}
ensure_path_target_file() {
if [ ! -d $PATH_TARGET_ALIAS ]; then
mkdir -p "$PATH_TARGET_ALIAS"
fi
}
ensure_profile_file() {
if [ -z $profile_file_path ]; then
echo "[ERROR]: None of the supported profile files are found on your file system"
echo " Supported profile files: ${SUPPORTED_PROFILE_FILES[@]}"
exit 1
fi
}
has_contains_text() {
local pattern=$1
local file_name=$2
local matches=$(grep -n "${pattern}" $file_name | wc -l)
if [ $matches == 0 ]; then
echo false
else
echo true
fi
}
append_path_target_file_to_profile_file() {
local path_target_alias=$1
local profile_file_path=$2
local is_loaded_target_file=$(has_contains_text ". ${path_target_alias}" $profile_file_path)
if ! $is_loaded_target_file ; then
append_data_to_file $profile_file_path ". ${path_target_alias}"
fi
}
add_alias_to_target_file() {
local path_target_alias=$1
local alias_name=$2
loaded_alias=false
if [ -f $path_target_alias ]; then
local matches=$(has_contains_text $alias_name $path_target_alias)
loaded_alias=$matches
fi
if ! $loaded_alias; then
append_data_to_file $path_target_alias "alias $alias_name"
fi
}
get_profile_file
ensure_profile_file
ensure_path_target_file
filename_target_alias=$(basename $target_folder)
path_target_alias="$PATH_TARGET_ALIAS/$filename_target_alias"
folders=($(ls -d $target_folder/*/))
for folder in "${folders[@]}"; do
alias_name=$(basename $folder)
echo "[INFO]: Creating alias for -> ${alias_name}"
add_alias_to_target_file $path_target_alias "${alias_name}=${folder}"
done
append_path_target_file_to_profile_file $path_target_alias $profile_file_path
echo
echo "[INFO]: Alias created in $path_target_alias"
echo "[INFO]: Run: source $profile_file_path"
echo "[INFO]: Happy coding :)"