Skip to content

Instantly share code, notes, and snippets.

@kamontat
Created September 9, 2018 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kamontat/1a00999d038df593d44f3ac7b97c2d28 to your computer and use it in GitHub Desktop.
Save kamontat/1a00999d038df593d44f3ac7b97c2d28 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
# shellcheck disable=SC1000
# generate by v3.0.2
# link (https://github.com/Template-generator/script-genrating/tree/v3.0.2)
# set -x #DEBUG - Display commands and their arguments as they are executed.
# set -v #VERBOSE - Display shell input lines as they are read.
# set -n #EVALUATE - Check syntax of the script but don't execute.
#/ -----------------------------------
#/ Description: vscode extension manager
#/ -----------------------------------
#/ Create by: Kamontat Chantrachirathunrong <kamontat.c@hotmail.com>
#/ Since: 09/09/2018
#/ -----------------------------------
#/ Error code 1 -- error
#/ -----------------------------------
is_integer() {
[[ $1 =~ ^[0-9]+$ ]] 2>/dev/null && return 0 || return 1
}
# @helper
throw() {
printf '%s\n' "$1" >&2 && is_integer "$2" && exit "$2"
return 0
}
# @helper
throw_if_empty() {
local text="$1"
test -z "$text" && throw "First parameter must be action." 1
return 0
}
is_parameter() {
if [[ $2 == "less-than" ]]; then
[[ "$1" < "$3" ]] || [[ "$1" == "$3" ]]
elif [[ $2 == "greater-than" ]]; then
[[ "$1" > "$3" ]] || [[ "$1" == "$3" ]]
else
return 1
fi
}
throw_if_parameter() {
is_parameter "$1" "$2" "$3" ||
throw "Required 2 parameters, 1. action 2. filepath." 2
}
if_command_not_found() {
command -v "$1" &>/dev/null
}
throw_if_command_not_found() {
if_command_not_found "$1" ||
throw "Required $1 command." 3
}
# 1. func -> execute function
# 2. name -> name of this execution
# n. value for each execution
progressbar() {
local cmd="$1"
local title="$2"
shift 2
IFS=" " read -r -a array <<<"$@"
duration=$#
curr_bar=0
for ((elapsed = 1; elapsed <= duration; elapsed++)); do
barsize=$(($(tput cols) - 25))
unity=$((barsize / duration))
increment=$((barsize % duration))
skip=$((duration / (duration - increment)))
# Elapsed
((curr_bar += unity))
if [[ $increment -ne 0 ]]; then
if [[ $skip -eq 1 ]]; then
[[ $((elapsed % (duration / increment))) -eq 0 ]] && ((curr_bar++))
else
[[ $((elapsed % skip)) -ne 0 ]] && ((curr_bar++))
fi
fi
[[ $elapsed -eq 1 && $increment -eq 1 && $skip -ne 1 ]] && ((curr_bar++))
[[ $((barsize - curr_bar)) -eq 1 ]] && ((curr_bar++))
[[ $curr_bar -lt $barsize ]] || curr_bar=$barsize
printf "%-15s |" "$title"
# Exection
"$cmd" "${array[elapsed - 1]}" || exit $?
# Progress
for ((filled = 0; filled <= curr_bar; filled++)); do
printf "#"
done
# Remaining
for ((remain = curr_bar; remain < barsize; remain++)); do
printf " "
done
# Percentage
printf "| %s%%" $(((elapsed * 100) / duration))
# Return
printf '\r'
done
echo
}
install() {
"$code" "--install-extension" "$1" &>/dev/null
}
code="code"
action="$1"
throw_if_empty "$action"
throw_if_parameter "$#" "less-than" 2
if_command_not_found "code" ||
code="code-insiders"
throw_if_command_not_found "$code"
if [[ "$action" == "e" ]] ||
[[ "$action" == "exp" ]] ||
[[ "$action" == "export" ]]; then
"$code" --list-extensions >"${2}.extension"
elif [[ "$action" == "i" ]] ||
[[ "$action" == "imp" ]] ||
[[ "$action" == "import" ]]; then
IFS=$'\n' read -d '' -ra arr <"$2"
progressbar install "Installation" "${arr[@]}"
elif
[[ "$action" == "I" ]] ||
[[ "$action" == "inter" ]] ||
[[ "$action" == "active" ]] ||
[[ "$action" == "interactive" ]]
then
IFS=$'\n' read -d '' -ra arr <"$2"
for ext in "${arr[@]}"; do
printf '%s%s\n' "https://marketplace.visualstudio.com/items?itemName=" "$ext"
printf 'Install this? [Y/n] '
read -rn1 ans
if [[ "$ans" == "Y" ]] || [[ "$ans" == "y" ]]; then
install "$ext" && echo " Completed." || echo " Failure!"
else
echo " Reject."
fi
done
else
echo "Error: This parameters is not correct." && exit 4
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment