Skip to content

Instantly share code, notes, and snippets.

@naholyr
Created September 6, 2011 15:27
Show Gist options
  • Save naholyr/1197854 to your computer and use it in GitHub Desktop.
Save naholyr/1197854 to your computer and use it in GitHub Desktop.
Install or update Sublime Text from web site
#!/bin/bash
# Requirements: sed, grep, curl, pkill
# User configuration
LOCAL_INSTALL="/usr/lib/sublime-text-2" # Must be user-writable
REPO="dev" # "dev" for dev releases, or "2" for beta releases
TARGET_BUILD="Linux 32 bit" # can be one of "Windows", "Windows 64 bit", "OS X", "Linux 32 bit", "Linux 64 bit"
# Check if sublime text is running
if ( pkill -0 -f "$LOCAL_INSTALL/sublime_text" ); then
echo "Sublime Text 2 is running!" > /dev/stderr
exit 1
fi
# Check if local install is available
if [ ! -d "$LOCAL_INSTALL" ]; then
echo "Local install folder does not exist, you should create $LOCAL_INSTALL" > /dev/stderr
exit 1
fi
# Standard configuration
REMOTE_SITE="http://www.sublimetext.com"
# Generated configuration
CURR_VERSION="$( "$LOCAL_INSTALL/sublime_text" -v | sed 's/^.*Build \([0-9]\+\).*$/\1/' )"
HTML_FILE="$( tempfile )"
curl "$REMOTE_SITE/$REPO" > "$HTML_FILE"
LATEST_TBZ="$( cat "$HTML_FILE" | grep "$TARGET_BUILD" | sed 's/^.*<a href="\(.*\)".*$/\1/' )"
LATEST_VERSION="$( echo "$LATEST_TBZ" | sed 's/^.*Build \([0-9]\+\).*$/\1/' )"
LATEST_TBZ_URL="$( ( ( echo "$LATEST_TBZ" | grep -F '://' ) && echo "$LATEST_TBZ" || echo "$REMOTE_SITE$LATEST_TBZ" ) | sed 's/ /%20/g' )"
if [ $LATEST_VERSION -gt $CURR_VERSION ]; then
echo "Updating from $CURR_VERSION to $LATEST_VERSION..."
[ -d "/tmp/sublime-text-2-$LATEST_VERSION" ] && rm -rf "/tmp/sublime-text-2-$LATEST_VERSION"
mkdir "/tmp/sublime-text-2-$LATEST_VERSION"
echo "Downloading and extracting $LATEST_TBZ_URL..."
if ( curl "$LATEST_TBZ_URL" | tar jx -C "/tmp/sublime-text-2-$LATEST_VERSION" ); then
if ( cp -rfv "/tmp/sublime-text-2-$LATEST_VERSION/Sublime Text 2"/* "$LOCAL_INSTALL/" ); then
echo "Successfully updated from $CURR_VERSION to $LATEST_VERSION"
else
echo "Failed copying extracted files" > /dev/stderr
fi
else
echo "Failed downloading or extracting latest version! URL = $LATEST_TBZ_URL" > /dev/stderr
fi
rm -rf "/tmp/sublime-text-2-$LATEST_VERSION"
# Generate changelog
LATEST_BUILD_LINE=$( grep -n -m1 '<h2>Build' "$HTML_FILE" | sed 's/^\([0-9]*\):.*$/\1/' )
CURR_BUILD_LINE="$( grep -n '<h2>Build' "$HTML_FILE" | tail -n +2 | while read match; do CURR_BUILD_NUM=$( echo $match | sed 's/^.*<h2>Build \([0-9]*\).*$/\1/' ); if [ $CURR_BUILD_NUM -ge $CURR_VERSION ]; then echo $match | sed 's/:.*$//'; fi; done | tail -n 1 )"
echo "Changelog:"
tail -n +$LATEST_BUILD_LINE $HTML_FILE | head -n +$(( $CURR_BUILD_LINE - $LATEST_BUILD_LINE )) | html2text
else
echo "Already up-to-date ($CURR_VERSION)" > /dev/stderr
fi
rm -f "$HTML_FILE"
@naholyr
Copy link
Author

naholyr commented Sep 28, 2011 via email

@rik
Copy link

rik commented Sep 28, 2011

I'm not licensed so I guess it is for OS X.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment