From 31d4bf8ce32dce7141bbea96aa78edadb15d3704 Mon Sep 17 00:00:00 2001 From: Trey Harris Date: Sun, 14 Sep 2014 16:48:13 -0400 Subject: [PATCH] Refactor {develop,production}_brew_cask Almost all of these two scripts are identical. Refactor out the common bits, saving over 60 lines of repetition. Right now this is handled by moving all the logic into "develop_brew_cask" and symlinking "production_brew_cask". This will be followed by another patch to add a "manage_brew_cask_links" command to do the same with sub-commands, e.g., manage_brew_cask_links status returns "production" or "develop", manage_brew_cask_links production does the same as production_brew_cask, manage_brew_cask_links develop does the same as develop_brew_cask, etc. At that point, I'd like to rename the script "manage_brew_cask_links" and have the two present names be symlinked to that. This was motivated by (besides DRY) getting tired of having to manually check symlinks to see which state I was currently in. --- developer/bin/develop_brew_cask | 78 ++++++++++++++++-- developer/bin/production_brew_cask | 124 +---------------------------- 2 files changed, 73 insertions(+), 129 deletions(-) mode change 100755 => 120000 developer/bin/production_brew_cask diff --git a/developer/bin/develop_brew_cask b/developer/bin/develop_brew_cask index 440e23c081a..5c828b31e4c 100755 --- a/developer/bin/develop_brew_cask +++ b/developer/bin/develop_brew_cask @@ -2,6 +2,11 @@ # # develop_brew_cask # +# Called via symlink as: +# production_brew_cask +# + +called_as="$(basename $0)" ### ### settings @@ -61,6 +66,19 @@ not_inside_homebrew () { fi } +remove_dev_links () { + local tap_dir="$1" + /bin/rm -- rubylib Casks + /bin/mv -- production_rubylib rubylib + /bin/mv -- production_Casks Casks + cd "$tap_dir" + /bin/rm -- lib Casks + /bin/mv -- production_lib lib + /bin/mv -- production_Casks Casks + printf "brew-cask is now in production mode\n" + printf "It is safe to run 'brew update' if you are in production mode for all Caskroom repos.\n" +} + create_dev_links () { local tap_dir="$1" local git_root="$2" @@ -81,7 +99,23 @@ create_dev_links () { ### main ### -_develop_brew_cask () { +_develop_brew_cask_develop_action () { + die "brew-cask is already set up for development" +} + +_develop_brew_cask_production_action () { + create_dev_links "$tap_dir" "$git_root" +} + +_production_brew_cask_develop_action () { + remove_dev_links "$tap_dir" +} + +_production_brew_cask_production_action () { + die "brew-cask is already set up for production" +} + +_main () { # initialization cd_to_project_root @@ -97,15 +131,15 @@ _develop_brew_cask () { # action cd_to_version_dir "$cellar_dir" "$version_dir" if [[ -e "production_rubylib" ]]; then - die "brew-cask is already set up for development" + eval "_${called_as}_develop_action" else - create_dev_links "$tap_dir" "$git_root" + eval "_${called_as}_production_action" fi } -# process args -if [[ $1 =~ ^-+h(elp)?$ ]]; then +_develop_brew_cask_usage () { + printf "develop_brew_cask Symlink private repo directories into Homebrew's Cellar, so @@ -120,10 +154,42 @@ Note: it is not safe to run 'brew update' while development mode is in effect. " + +} + +_production_brew_cask_usage () { + + printf "production_brew_cask + +Undo all symlinks created by 'develop_brew_cask' so that the +'brew cask' command will use only released code and Casks +within Homebrew. + +After running this command it is safe to run 'brew update', +unless you are using similar scripts to create symlinks into +other Caskroom development repos. + +" + +} + +# ensure we're called by a valid name +case "${called_as}" in + develop_brew_cask) ;; + production_brew_cask) ;; + *) + die "ERROR: name ${called_as} not recognized" + ;; +esac + + +# process args +if [[ $1 =~ ^-+h(elp)?$ ]]; then + eval "_${called_as}_usage" exit fi # dispatch main -_develop_brew_cask "${@}" +_main "${@}" # diff --git a/developer/bin/production_brew_cask b/developer/bin/production_brew_cask deleted file mode 100755 index 55a30281fd7..00000000000 --- a/developer/bin/production_brew_cask +++ /dev/null @@ -1,123 +0,0 @@ -#!/bin/bash -# -# production_brew_cask -# - -### -### settings -### - -set -e # exit on any uncaught error -set +o histexpand # don't expand history expressions -shopt -s nocasematch # case-insensitive regular expressions - -### -### configurable global variables -### - -tap_subdir="Library/Taps/caskroom/homebrew-cask" - -### -### functions -### - -warn () { - local message="$@" - message="${message//\\t/$'\011'}" - message="${message//\\n/$'\012'}" - message="${message%"${message##*[![:space:]]}"}" - printf "%s\n" "$message" 1>&2 -} - -die () { - warn "$@" - exit 1 -} - -cd_to_project_root () { - local script_dir="$(/usr/bin/dirname "$0")" - cd "$script_dir" - local git_root="$(git rev-parse --show-toplevel)" - if [[ -z "$git_root" ]]; then - die "ERROR: Could not find git project root" - fi - cd "$git_root" -} - -cd_to_version_dir () { - local cellar_dir="$1" - local version_dir="$2" - if [[ -z "$version_dir" ]]; then - die "ERROR: Could not find version dir under $cellar_dir/" - fi - cd "$cellar_dir/$version_dir" -} - -not_inside_homebrew () { - local tap_dir="$1" - local git_root="$2" - if [[ "$(/usr/bin/stat -L -f '%i' -- "$tap_dir")" -eq "$(/usr/bin/stat -L -f '%i' -- "$git_root")" ]]; then - die "\nERROR: Run this script in your private repo, not inside Homebrew.\n" - fi -} - -remove_dev_links () { - local tap_dir="$1" - /bin/rm -- rubylib Casks - /bin/mv -- production_rubylib rubylib - /bin/mv -- production_Casks Casks - cd "$tap_dir" - /bin/rm -- lib Casks - /bin/mv -- production_lib lib - /bin/mv -- production_Casks Casks - printf "brew-cask is now in production mode\n" - printf "It is safe to run 'brew update' if you are in production mode for all Caskroom repos.\n" -} - -### -### main -### - -_production_brew_cask () { - - # initialization - cd_to_project_root - local git_root="$(/bin/pwd)" - local brew_prefix="$(brew --prefix)" - local cellar_dir="$brew_prefix/Cellar/brew-cask" - local version_dir="$(/bin/ls -- "$cellar_dir/" | /usr/bin/sort | /usr/bin/tail -1)" - local tap_dir="$brew_prefix/$tap_subdir" - - # sanity check - not_inside_homebrew "$tap_dir" "$git_root" - - # action - cd_to_version_dir "$cellar_dir" "$version_dir" - if [[ -e "production_rubylib" ]]; then - remove_dev_links "$tap_dir" - else - die "brew-cask is already set up for production" - fi - -} - -# process args -if [[ $1 =~ ^-+h(elp)?$ ]]; then - printf "production_brew_cask - -Undo all symlinks created by 'develop_brew_cask' so that the -'brew cask' command will use only released code and Casks -within Homebrew. - -After running this command it is safe to run 'brew update', -unless you are using similar scripts to create symlinks into -other Caskroom development repos. - -" - exit -fi - -# dispatch main -_production_brew_cask "${@}" - -# diff --git a/developer/bin/production_brew_cask b/developer/bin/production_brew_cask new file mode 120000 index 00000000000..4afaf77a65c --- /dev/null +++ b/developer/bin/production_brew_cask @@ -0,0 +1 @@ +develop_brew_cask \ No newline at end of file