Created
October 15, 2012 03:28
-
-
Save passcod/3890678 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
if ! `command -v hub >/dev/null 2>&1` | |
then | |
echo "Missing dependency: hub." | |
echo "Install it with \`brew install hub\`." | |
exit 1 | |
fi | |
if ! `command -v ruby >/dev/null 2>&1` | |
then | |
echo "Missing dependency: ruby." | |
echo "Is your Mac broken or something?" | |
exit 1 | |
fi | |
if ! `command -v perl >/dev/null 2>&1` | |
then | |
echo "Missing dependency: perl." | |
echo "Is your Mac broken or something?" | |
exit 1 | |
fi | |
PREFIX=$(brew --prefix) | |
CONFIG="$HOME/.caskrc" | |
if [[ -z "$1" ]] | |
then | |
echo "Usage: $0 [TAP/]CASK_NAME" | |
echo "" | |
echo " CASK_NAME should be in hyphenated lowercase," | |
echo " to match the filename. E.g., \`x-quartz\`." | |
echo "" | |
echo " The TAP to work on defaults to \`phinze-cask\`," | |
echo " overridable in the config at 'contribute.tap'." | |
echo "" | |
exit 1 | |
fi | |
if [ ! -e "$CONFIG" ] | |
then | |
echo "---" > $CONFIG | |
echo "contribute:" >> $CONFIG | |
echo "" >> $CONFIG | |
echo " # Preferred tap to use for editing." >> $CONFIG | |
echo " # Defaults to phinze-cask" >> $CONFIG | |
echo " #tap: user-tapname" >> $CONFIG | |
echo "" >> $CONFIG | |
echo " local:" >> $CONFIG | |
echo "" >> $CONFIG | |
echo " # Local prefix, where your 'local' copy of" >> $CONFIG | |
echo " # (your fork of) homebrew-cask lives (as well" >> $CONFIG | |
echo " # as any other tap." >> $CONFIG | |
echo " # Defaults to \`\$HOME\`." >> $CONFIG | |
echo " #prefix: \"$HOME\" " >> $CONFIG | |
echo "" >> $CONFIG | |
echo " # Upstream remote name. Used for pulling latest" >> $CONFIG | |
echo " # changes. Should point to phinze/homebrew-cask." >> $CONFIG | |
echo " # Defaults to \`upstream\`." >> $CONFIG | |
echo " upstream: upstream" >> $CONFIG | |
fi | |
read_config () { | |
read -r -d '' CODE <<'COD' | |
c=YAML.load_file(File.expand_path(ARGV[1] || "~/.caskrc")); | |
def dotted(s,q) q.split('.').each{ | |
|p|s.include?(p) ? (s=s[p]) : (return false)}; s; end; | |
puts dotted(c, ARGV[0]); | |
COD | |
ruby -ryaml -e "$CODE" "$1" "$CONFIG" | |
} | |
#echo "Updating brew and all taps..." | |
#brew update | |
TAP=$(read_config contribute.tap) | |
if [ "$TAP" = "false" ] | |
then | |
TAP="phinze-cask" | |
fi | |
if `echo "$1" | grep -q "/"` | |
then | |
TAP=$(echo "$1" | cut -d / -f 1) | |
fi | |
CASK=$(echo "$1" | cut -d / -f 2) | |
ORIG="$PREFIX/Library/Taps/$TAP/Casks/$CASK.rb" | |
local_prefix=$(read_config contribute.local.prefix) | |
if [ "$local_prefix" = "false" ] | |
then | |
local_prefix="$HOME" | |
fi | |
local_name=$(echo "$TAP" | cut -d "-" -f 2) | |
local_user=$(echo "$TAP" | cut -d "-" -f 1) | |
local_repo="$local_prefix/homebrew-$local_name" | |
local_path="$local_repo/Casks/$CASK.rb" | |
cd "$local_repo" | |
if `git status -s | grep -q "fatal"` | |
then | |
echo "$local_repo isn't a git repo, aborting." | |
exit 1 | |
fi | |
if ! `git remote | grep -q "origin"` | |
then | |
echo "No origin remote, aborting." | |
exit 1 | |
fi | |
if `git status -s | grep -qE "."` | |
then | |
echo "$local_repo isn't clean, aborting." | |
exit 1 | |
fi | |
upstream_branch=$(read_config contribute.local.upstream) | |
if [ "$upstream_branch" = "false" ] | |
then | |
upstream_branch="upstream" | |
fi | |
if ! `git remote | grep -q "$upstream_branch"` | |
then | |
echo "No $upstream_branch remote, aborting." | |
echo "Add an $upstream_branch remote to your repo" | |
echo "pointing to phinze/homebrew-cask or edit" | |
echo "contribute.local.upstream in $CONFIG to" | |
echo "match your setup." | |
exit 1 | |
fi | |
echo "Updating local repo..." | |
git pull $upstream_branch master | |
if [ ! -e "$ORIG" ] | |
then | |
read -p "$TAP/$CASK does not exist, create it? [Yn]: " ans | |
if ! `echo "$ans" | grep -qE "^[Yy]"` | |
then | |
echo "Abort." | |
exit 1 | |
fi | |
# Create new file in local copy. | |
touch $local_path | |
else | |
# Copy file over to local copy. | |
echo "Copying $TAP/$CASK to $local_repo" | |
cp -i $ORIG $local_path | |
fi | |
edit_file () { | |
echo "Opening $local_path with $EDITOR..." | |
$EDITOR "$local_path" | |
} | |
install_cask () { | |
test_path="$PREFIX/Library/Taps/cask-test/Casks/$CASK.rb" | |
echo "Copying to $test_path..." | |
cp -f $local_path $test_path | |
brew cask install "cask-test/$CASK" | |
read -p "Run linkapps? [Yn]: " ans | |
case `echo "$ans" | cut -c 1` in | |
n) | |
echo "Not linking.";; | |
*) | |
brew cask linkapps;; | |
esac | |
rm -r "$PREFIX/Library/Taps/cask-test" | |
} | |
edit_file | |
while true | |
do | |
read -p "What do you want to do? [Hcetqa]: " ans | |
case `echo "$ans" | cut -c 1` in | |
e) | |
edit_file | |
continue;; | |
t) | |
install_cask | |
continue;; | |
q) | |
exit 0;; | |
a) | |
echo "Resetting local repo..." | |
git reset --hard HEAD | |
exit 0;; | |
c) | |
break;; | |
*) | |
echo "h: Help - This screen." | |
echo "c: Continue - Commit, Push, Pull Request." | |
echo "e: Edit - Edit the file again." | |
echo "t: Test - Install for testing." | |
echo "q: Quit - Quit immediately." | |
echo "a: Abort - Abort, reset local repo." | |
continue;; | |
esac | |
done | |
read -p "Branch name? [$CASK]: " ans | |
if [[ -z "$ans" ]] | |
then | |
ans="$CASK" | |
fi | |
branch_name="$ans" | |
git co -b "$ans" | |
git add "$local_path" | |
cap_name=$(echo "$CASK" | perl -anF/-/ -e 'foreach $wrd(@F){print ucfirst($wrd).' ';}') | |
cap_vers=$(cat "$local_path" | grep -oE "version '.+'" | cut -d "'" -f 2) | |
git commit -m "$cap_name $cap_vers" | |
git commit --amend | |
read -p "Push to origin? [Yn]: " ans | |
case `echo "$ans" | cut -c 1` in | |
n) | |
echo "Not pushing.";; | |
*) | |
git push origin "$branch_name";; | |
esac | |
read -p "Open pull request? [Yn]: " ans | |
case `echo "$ans" | cut -c 1` in | |
n) | |
echo "Not opening pull request.";; | |
*) | |
hub pull-request -b phinze:master;; | |
esac | |
echo "DONE" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment