Skip to content

Commit

Permalink
feat(update): add support for self-update
Browse files Browse the repository at this point in the history
  • Loading branch information
Deavon M. McCaffery committed Dec 14, 2016
1 parent 8d6e2fb commit ff98806
Show file tree
Hide file tree
Showing 10 changed files with 168 additions and 10 deletions.
13 changes: 13 additions & 0 deletions bootstrap.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
#!/usr/bin/env bash
curl_opt='-s'
if [ ! -z "${GH_TOKEN:-}" ]; then
curl_opt='$curl_opt -H "Authorization: token $GH_TOKEN"'
fi

sha_uri="https://api.github.com/repos/pulsebridge/prompt/commits/master"
sha=$(curl $curl_opt $sha_uri | grep sha | head -n 1 | sed 's#.*\:.*"\(.*\).*",#\1#')
sha_path=$HOME/.pulsebridge/prompt/$sha

if [ -f $sha_path ]; then
echo "prompt: latest version already installed: $sha"
exit 0
fi

uri="https://github.com/pulsebridge/prompt/archive/master.tar.gz"
temp=$(mktemp -d -t pb_prompt)
Expand Down
10 changes: 10 additions & 0 deletions install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ if [ -f $backup_path/scripts/bookmarks.sh ]; then
cp -R $backup_path/scripts/bookmarks.sh $PULSEBRIDGE_PROMPT/scripts 1>/dev/null
fi

if [ -f $backup_path/scripts/variables.sh ]; then
success "Restoring variables..."
cp -R $backup_path/scripts/variables.sh $PULSEBRIDGE_PROMPT/scripts 1>/dev/null
fi

for template in template/*; do
name=$(basename "$template")
path=$HOME/.${name}
Expand Down Expand Up @@ -98,6 +103,11 @@ if [ "$result" = "200" ]; then
chmod +x "$PROMPT_COMPLETION/$GIT_COMPLETE_NAME" 1>/dev/null 2>&1
fi

SHA=$(git rev-parse HEAD)
SHA_PATH=$PULSEBRIDGE_PROMPT/$SHA

touch $SHA_PATH 1>/dev/null 2>&1

echo -e "${CLR_SUCCESS}"
echo "#######################################"
echo "#######################################"
Expand Down
34 changes: 34 additions & 0 deletions src/scripts/add-var.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/usr/bin/env bash

if [ ! -z "${PROMPT_DEBUG:-}" ]; then
echo 'add-var'
fi

add-var-usage() {
echo "add-var <name> <value>"
echo ' name : a name for your variable'
echo ' value : a value for your variable'
}

function add-var() {
# make sure that a bookmark was specified.
if [ -z "${1:-}" ]; then
echo 'add-var: missing <name>'
add-var-usage
return
fi

if [ -z "${2:-}" ]; then
echo 'add-var: missing <value>'
add-var-usage
return
fi

# make sure we remove any existing variable before defining another one
remove-var $1 silent
local v="$1"=\"$2\"

echo Adding varable: $v
echo $v >> $HOME/.pulsebridge/prompt/scripts/variables.sh
. $HOME/.pulsebridge/prompt/scripts/variables.sh
}
39 changes: 35 additions & 4 deletions src/scripts/git-extras.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,41 @@ function git-commits() {
git summary
}

function lines() {
git-lines
function git-sha() {
local ORG_NAME=$1
local REPO_NAME=$2
local BRANCH_NAME=$3

if [ -z "${ORG_NAME:-}" ]; then
local ORG_NAME="pulsebridge"
fi

if [ -z "${REPO_NAME:-}" ]; then
local REPO_NAME="prompt"
fi

if [ -z "${BRANCH_NAME:-}" ]; then
local BRANCH_NAME="master"
fi

CURL_OPT="-s"

if [ ! -z "${GH_TOKEN:-}" ]; then
CURL_OPT="$CURL_OPT -H 'Authorization: $GH_TOKEN'"
fi

local SHA_URI=https://api.github.com/repos/$ORG_NAME/$REPO_NAME/commits/$BRANCH_NAME
local SHA=$(curl $CURL_OPT $SHA_URI | grep sha | head -n 1 | sed 's#.*\:.*"\(.*\).*",#\1#')

echo "$SHA"
}

function commits() {
git-commits
function git-token() {
local GH_TOKEN=$1

if [ -z "${GH_TOKEN:-}" ]; then
echo "usage: git-token [token]"
else
add-var GH_TOKEN $GH_TOKEN
fi
}
9 changes: 9 additions & 0 deletions src/scripts/remove-backup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env bash

if [ ! -z "${PROMPT_DEBUG:-}" ]; then
echo 'remove-backup'
fi

remove-backup() {
rm -rf $HOME/.pulsebridge/prompt/backup 2>/dev/null
}
10 changes: 5 additions & 5 deletions src/scripts/remove-bookmark.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,20 @@ remove-bookmark() {
local scripts_path=$HOME/.pulsebridge/prompt/scripts

if [ ! -z "${b:-}" ]; then
r=$(command grep -s -m 1 ^$b $scripts_path/bookmarks.sh)
r=$(grep -s -m 1 ^$b $scripts_path/bookmarks.sh)
elif [ -z "${2:-}" ]; then
r=$(command grep -s -m 1 \"$PWD\"$ $scripts_path/bookmarks.sh)
r=$(grep -s -m 1 \"$PWD\"$ $scripts_path/bookmarks.sh)
fi

if [ ! -z "${r:-}" ]; then
[ -z "${2:-}" ] && ( echo Removing bookmark: $r )
[ -z "${2:-}" ] && ( echo "Removing bookmark: $r" )

command grep -s -v $r $scripts_path/bookmarks.sh >> $scripts_path/bookmarks1.sh
grep -s -v $r $scripts_path/bookmarks.sh >> $scripts_path/bookmarks1.sh
mv -f $scripts_path/bookmarks1.sh $scripts_path/bookmarks.sh

. $scripts_path/bookmarks.sh
elif [ -z "${2:-}" ]; then
echo No bookmark was declared for the specified title or address.
echo "No bookmark was declared for the specified title or address."
fi
}

Expand Down
26 changes: 26 additions & 0 deletions src/scripts/remove-var.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/usr/bin/env bash

if [ ! -z "${PROMPT_DEBUG:-}" ]; then
echo 'remove-var'
fi

remove-var() {
local v=$1
local r=
local scripts_path=$HOME/.pulsebridge/prompt/scripts

if [ -z "${v:-}" ]; then
r=$(grep -s -m 1 ^$v $scripts_path/variables.sh)
fi

if [ ! -z "${r:-}" ]; then
[ -z "${2:-}" ] && (echo Removing variable: $r)

grep -s -v $r $scripts_path/variables.sh >> $scripts_path/variables1.sh
mv -f $scripts_path/variables1.sh $scripts_path/variables.sh

. $scripts_path/variables.sh
elif [ -z "${2:-}" ]; then
echo No variable was declared for the specified name.
fi
}
2 changes: 1 addition & 1 deletion src/scripts/show-boomarks.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ if [ ! -z "${PROMPT_DEBUG:-}" ]; then
fi

function show-bookmarks() {
cat $HOME/.pulsebridge/prompt/scripts/bookmarks.sh
cat $HOME/.pulsebridge/prompt/scripts/bookmarks.sh 2>/dev/null
}

function lbm() {
Expand Down
9 changes: 9 additions & 0 deletions src/scripts/show-vars.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env bash

if [ ! -z "${PROMPT_DEBUG:-}" ]; then
echo 'show-vars'
fi

function show-vars() {
cat $HOME/.pulsebridge/prompt/scripts/variables.sh 2>/dev/null
}
26 changes: 26 additions & 0 deletions src/scripts/update-prompt.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/usr/bin/env bash

if [ ! -z "${PROMPT_DEBUG:-}" ]; then
echo 'update-prompt'
fi

function update-prompt() {
local SHA=$(git-sha)
local SHA_PATH=$HOME/.pulsebridge/prompt/$SHA

if [ -f $SHA_PATH ]; then
echo "prompt: latest version already installed: $SHA"
return 1
fi

local UPDATE_URI="https://github.com/pulsebridge/prompt/archive/master.tar.gz"
local UPDATE_TEMP=$(mktemp -d -t pb_prompt)

pushd $UPDATE_TEMP 1>/dev/null
curl -skL $UPDATE_URI | tar zx
pushd prompt-master 1>/dev/null
./install.sh
popd 1>/dev/null

rm -rf $UPDATE_TEMP 1>/dev/null
}

0 comments on commit ff98806

Please sign in to comment.