Skip to content

Commit

Permalink
✨ Add a new update manager
Browse files Browse the repository at this point in the history
The update manager now checks for updates asynchronously.
Another change here is that it will check for the last release on
github and compare it against the last release on local machine.
It notifies for new updates when the last release on github is
greater than the one on local machine.
  • Loading branch information
diogocavilha committed Sep 19, 2020
1 parent 10d5978 commit 9d845e5
Show file tree
Hide file tree
Showing 20 changed files with 200 additions and 152 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ app_config
*.sublime-*
last_update_at
config-override.sh

tmpversions
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

> Any trouble, please visit: https://github.com/diogocavilha/fancy-git#troubleshooting-pick
### v6.4.1

- Improve the update manager. Now it checks for updates asynchronously.

### v6.3.1

- Add one more question to "gd" command.
Expand Down
2 changes: 1 addition & 1 deletion commands.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#
# Commands manager.

. ~/.fancy-git/update_checker.sh
. ~/.fancy-git/modules/update-manager.sh
. ~/.fancy-git/version.sh

fg_script_help() {
Expand Down
165 changes: 165 additions & 0 deletions modules/update-manager.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
#!/bin/bash
#
# Author: Diogo Alexsander Cavilha <diogocavilha@gmail.com>
# Date: 12.05.2018

check_for_update() {
if __fancygit_must_check_for_update
then
__fancygit_update_notification
(__fancygit_update_checker &)
fi
}

__fancygit_update_notification() {
local option
local current_version
local new_version

if ! __fancygit_is_git_repo || ! __fancygit_must_check_for_update
then
return
fi

if [[ ! -e ~/.fancy-git/tmpversions ]]
then
return
fi

current_version=$(grep -o '.*[:]' ~/.fancy-git/tmpversions | sed 's/://')
new_version=$(grep -o ':.*' ~/.fancy-git/tmpversions | sed 's/://')

echo -e "\n Hey! Fancy Git $(tput bold; tput setaf 2)$new_version$(tput sgr0) has been released!"
echo " You're running Fancy Git $(tput bold; tput setaf 6)$current_version$(tput sgr0)"
echo ""
read -r -p " $(tput bold)Would you like to update it? [Y/n]: $(tput sgr0)" option
echo ""

case "$option" in
"y"|"Y"|"")
fancygit_update
return;;
esac

__fancygit_reset_update_checker
}

__fancygit_update_checker() {
if ! __fancygit_is_git_repo || ! __fancygit_must_check_for_update
then
return
fi

if [[ -e ~/.fancy-git/tmpversions ]]
then
return
fi

local current_version
local new_version

cd ~/.fancy-git
current_version=$(git tag | tail -1)

git fetch -t 2> /dev/null
new_version=$(git tag | tail -1)
new_version="v8.8.8"

current_version_filter_number=$(echo "$current_version" | sed 's/[vV\.]//g')
new_version_filter_number=$(echo "$new_version" | sed 's/[vV\.]//g')

if [[ "$current_version_filter_number" -lt "$new_version_filter_number" ]]
then
echo "$current_version:$new_version" > ~/.fancy-git/tmpversions
return
fi

__fancygit_reset_update_checker
}

fancygit_update() {
local current_dir

current_dir=$(pwd)

cd ~/.fancy-git/ && git pull origin master > /dev/null 2> /dev/null

__fancygit_create_app_config
__fancygit_copy_style_from_mode_file_to_app_config
__fancygit_safetly_remove_mode_file
__fancygit_reset_update_checker

cd "$current_dir" || return

echo "$(tput bold; tput setaf 2) FancyGit successfully updated ;D$(tput sgr0)"
echo ""

head ~/.fancy-git/CHANGELOG.md | grep -zo '>.*[#]' | sed 's/###//g' | sed 's/^>/ >/' | sed 's/^-/ -/'
}

__fancygit_is_git_repo() {
local branch_name

branch_name=$(git rev-parse --abbrev-ref HEAD 2> /dev/null)

if [ "$branch_name" = "" ]
then
return 1
fi

return 0
}

__fancygit_must_check_for_update() {
local current_date
local last_update_at

last_update_at=$(cat ~/.fancy-git/last_update_at 2> /dev/null)
current_date=$(date +%Y-%m-%d)

if [ "$current_date" = "$last_update_at" ]
then
return 1
fi

return 0
}

__fancygit_create_app_config() {
if [ ! -f ~/.fancy-git/app_config ]
then
touch -f ~/.fancy-git/app_config
cat ~/.fancy-git/app_config_sample > ~/.fancy-git/app_config
fi
}

__fancygit_copy_style_from_mode_file_to_app_config() {
if [ -f ~/.fancy-git/mode -a -f ~/.fancy-git/app_config ]
then
local style
style=$(cat ~/.fancy-git/mode)
sed -i "s#style:.*#style:${style}#" ~/.fancy-git/app_config
fi
}

__fancygit_safetly_remove_mode_file() {
local app_config_file_status

app_config_file_status=$(grep -io 'fresh_file' < ~/.fancy-git/app_config)

if [ "$app_config_file_status" = "fresh_file" ]
then
sed -i '/fresh_file/d' ~/.fancy-git/app_config
rm -f mode
fi
}

__fancygit_reset_update_checker() {
local current_date

current_date=$(date +%Y-%m-%d)

echo "$current_date" > ~/.fancy-git/last_update_at

rm ~/.fancy-git/tmpversions 2> /dev/null
}
4 changes: 2 additions & 2 deletions prompt_styles/dark-col-double-line.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@

fancygit_prompt_builder() {
. ~/.fancy-git/config.sh
. ~/.fancy-git/update_checker.sh
. ~/.fancy-git/modules/update-manager.sh

fancygit_update_checker
check_for_update

local blue="\\033[95;38;5;45m"
local bold="\\[\\e[1m\\]"
Expand Down
4 changes: 2 additions & 2 deletions prompt_styles/dark-double-line.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@

fancygit_prompt_builder() {
. ~/.fancy-git/config.sh
. ~/.fancy-git/update_checker.sh
. ~/.fancy-git/modules/update-manager.sh

fancygit_update_checker
check_for_update

# Prompt style
user_at_host="${white}${bg_dark_gray_01}${bold}"
Expand Down
4 changes: 2 additions & 2 deletions prompt_styles/dark.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@

fancygit_prompt_builder() {
. ~/.fancy-git/config.sh
. ~/.fancy-git/update_checker.sh
. ~/.fancy-git/modules/update-manager.sh

fancygit_update_checker
check_for_update

# Prompt style
user_at_host="${white}${bg_dark_gray_01}${bold}"
Expand Down
6 changes: 3 additions & 3 deletions prompt_styles/default.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@

fancygit_prompt_builder() {
. ~/.fancy-git/config.sh
. ~/.fancy-git/update_checker.sh

fancygit_update_checker
. ~/.fancy-git/modules/update-manager.sh
check_for_update

# Prompt style
user_at_host="${white}${bg_dark_gray}${bold}"
Expand Down
4 changes: 2 additions & 2 deletions prompt_styles/fancy-double-line.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@

fancygit_prompt_builder() {
. ~/.fancy-git/config.sh
. ~/.fancy-git/update_checker.sh
. ~/.fancy-git/modules/update-manager.sh

fancygit_update_checker
check_for_update

# Prompt style
path="${bg_blue}${white}${bold}"
Expand Down
4 changes: 2 additions & 2 deletions prompt_styles/human-dark-single-line.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
. ~/.fancy-git/commands.sh

fancygit_prompt_builder() {
. ~/.fancy-git/update_checker.sh
. ~/.fancy-git/modules/update-manager.sh

fancygit_update_checker
check_for_update

local branch_name=$(git rev-parse --abbrev-ref HEAD 2> /dev/null)

Expand Down
4 changes: 2 additions & 2 deletions prompt_styles/human-dark.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
. ~/.fancy-git/commands.sh

fancygit_prompt_builder() {
. ~/.fancy-git/update_checker.sh
. ~/.fancy-git/modules/update-manager.sh

fancygit_update_checker
check_for_update

local branch_name=$(git rev-parse --abbrev-ref HEAD 2> /dev/null)

Expand Down
4 changes: 2 additions & 2 deletions prompt_styles/human-single-line.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
. ~/.fancy-git/commands.sh

fancygit_prompt_builder() {
. ~/.fancy-git/update_checker.sh
. ~/.fancy-git/modules/update-manager.sh

fancygit_update_checker
check_for_update

local branch_name=$(git rev-parse --abbrev-ref HEAD 2> /dev/null)

Expand Down
4 changes: 2 additions & 2 deletions prompt_styles/human.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
. ~/.fancy-git/commands.sh

fancygit_prompt_builder() {
. ~/.fancy-git/update_checker.sh
. ~/.fancy-git/modules/update-manager.sh

fancygit_update_checker
check_for_update

local branch_name=$(git rev-parse --abbrev-ref HEAD 2> /dev/null)

Expand Down
4 changes: 2 additions & 2 deletions prompt_styles/light-double-line.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@

fancygit_prompt_builder() {
. ~/.fancy-git/config.sh
. ~/.fancy-git/update_checker.sh
. ~/.fancy-git/modules/update-manager.sh

fancygit_update_checker
check_for_update

# Prompt style
user_symbol="${bg_light_gray}${bold}${black}"
Expand Down
4 changes: 2 additions & 2 deletions prompt_styles/light.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@

fancygit_prompt_builder() {
. ~/.fancy-git/config.sh
. ~/.fancy-git/update_checker.sh
. ~/.fancy-git/modules/update-manager.sh

fancygit_update_checker
check_for_update

# Prompt style
user_at_host="${black}${bg_light_gray}${bold}"
Expand Down
4 changes: 2 additions & 2 deletions prompt_styles/simple-double-line.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ fg_branch_name() {

fancygit_prompt_builder() {
. ~/.fancy-git/config.sh
. ~/.fancy-git/update_checker.sh
. ~/.fancy-git/modules/update-manager.sh

fancygit_update_checker
check_for_update

local user="${light_green}\u${none}"
local at="${none}@${none}"
Expand Down
4 changes: 2 additions & 2 deletions prompt_styles/simple.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ fg_branch_name() {

fancygit_prompt_builder() {
. ~/.fancy-git/config.sh
. ~/.fancy-git/update_checker.sh
. ~/.fancy-git/modules/update-manager.sh

fancygit_update_checker
check_for_update

local user
local at
Expand Down
2 changes: 1 addition & 1 deletion random_messages.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ _fancygit_after_pulling_random_message() {
)

list_size=${#messages[@]}
option=$((RANDOM % $list_size))
option=$((RANDOM % list_size))

echo ""
echo "=> ${messages[$option]}"
Expand Down
Loading

0 comments on commit 9d845e5

Please sign in to comment.