Skip to content

Commit

Permalink
Move back documentation and developer scripts. (#23920)
Browse files Browse the repository at this point in the history
  • Loading branch information
reitermarkus committed Aug 19, 2016
1 parent ecf761a commit 1517168
Show file tree
Hide file tree
Showing 75 changed files with 5,606 additions and 0 deletions.
190 changes: 190 additions & 0 deletions developer/bin/cask-switch-https
@@ -0,0 +1,190 @@
#!/bin/bash

set -o pipefail

readonly program="$(basename "$0")"
skip_curl_verify=0
verbose=0

syntax_error() {
echo "$program: $1" >&2
echo "Try \`$program --help\` for more information." >&2
exit 1
}

depends_on() {
formula="$1"
[[ "$#" -eq 2 ]] && cmd="$2" || cmd=$(basename "${formula}")

if [[ ! $(which ${cmd}) ]]; then
echo -e "$(tput setaf 1)
This script depends on '${cmd}'.
If you have [Homebrew](http://brew.sh), you can install it with 'brew install ${formula}'.
$(tput sgr0)" | sed -E 's/ {6}//' >&2
exit 1
fi
}

depends_on 'tsparber/tiny-scripts/curl-check-url'

usage() {
echo "
This script changes the url, appcast and homepage stanzas to https
After changing to https a HTTP GET request is performed to verify if the url is reachable.
If the https url is not reachable it is reverted to the previous version.
Known Issues: If multiple url/appcast stanzas are present, all urls are changed but only
those for the current os are verified.
If no cask name is given the current work directory is scanned with the given options.
usage: $program [options] [<cask_name>]
options:
-s, --skip-verify Skip checking for a HTTP 200 Status Code using curl.
--verbose Show more verbose output.
-h, --help Show this help.
Based on: https://github.com/vitorgalvao/tiny-scripts/blob/master/cask-repair
" | sed -E 's/^ {4}//'
}

# available flags
while [[ "$1" ]]; do
case "$1" in
-h | --help)
usage
exit 0
;;
-s | --skip-verify)
skip_curl_verify=1
;;
--verbose)
verbose=1
;;
-*)
syntax_error "unrecognized option: $1"
;;
*)
break
;;
esac
shift
done

# define function to check if given URL exists and is reachable using HTTPS
check_url_for_https() {
cask_url="$1"
verbose_option=""

[[ ${verbose} -ne 0 ]] && verbose_option="-v "

# check if the URL sends a 200 HTTP code, else abort
curl-check-url ${verbose_option} "${cask_url}" > /dev/null
exit_code=$?

if [[ exit_code -ne 0 ]]; then
echo "curl returned ${exit_code}: FAIL for ${cask_url}"
return 1
fi

return 0
}

# define function to modify part of stanza
replace_protocol_of_stanza() {
cask_file="$1"
stanza="$2"
old_value="$3"
new_value="$4"

sed "s|${stanza} \(['\"]\)${old_value}://|${stanza} \1${new_value}://|g" "${cask_file}" > tmpfile
mv tmpfile "${cask_file}"
}

# define abort function, that will reset the state
finish() {
# show message
if [[ "$1" == 'abort' ]]; then
echo -e "$(tput setaf 1)$2$(tput sgr0)\n"
[[ ! -z "${cask_file}" ]] && git checkout -- "${cask_file}"
exit 1
elif [[ "$1" == 'success' ]]; then
echo -e "$(tput setaf 2)Updated: ${cask_name} is now using HTTPS$(tput sgr0)\n"
exit 0
fi
}

# cleanup if aborted with 鈱僀
trap 'finish abort "You aborted"' SIGINT

# exit if not inside a 'homebrew-*/Casks' directory
casks_dir=$(pwd | perl -ne 'print m{homebrew-[^/]+/Casks}')
if [[ -z "${casks_dir}" ]]; then
echo -e "\n$(tput setaf 1)You need to be inside a '/homebrew-*/Casks' directory$(tput sgr0)\n"
exit 1
fi

# exit if no argument was given: Run in current directory
if [[ -z "$1" ]]; then
options=""
[[ ${skip_curl_verify} -ne 0 ]] && options+=" --skip-verify"
[[ ${verbose} -ne 0 ]] && options+=" --verbose"

for file in *.rb;
do
"$0" ${options} ${file}
done

exit 0
fi

# clean the cask's name, and check if it is valid
cask_name="$1"
[[ "${cask_name}" == *'.rb' ]] && cask_name=$(echo "${cask_name}" | sed 's|\.rb$||')
cask_file="./${cask_name}.rb"
[[ ! -f "${cask_file}" ]] && finish abort 'There is no such cask'

# initial tasks
git checkout -- "${cask_file}"

# check if a http url exists
cask_contains_http=$(grep "['\"]http://" "${cask_file}")
if [[ -z ${cask_contains_http} ]]; then
echo -e "Skipped ${cask_name} no http found\n"
exit 0
fi

updated_stanzas=0
for stanza in url appcast homepage; do
# Check if the stanza exists
stanza_contained=$(grep "${stanza} ['\"]" "${cask_file}")
[[ -z ${stanza_contained} ]] && continue

stanza_contains_https=$(grep "${stanza} ['\"]http://" "${cask_file}")
if [[ -z ${stanza_contains_https} ]]; then
# echo "Skipped stanza ${stanza} in ${cask_name} no http url found"
continue
fi

replace_protocol_of_stanza ${cask_file} ${stanza} "http" "https"

if [[ ${skip_curl_verify} -eq 0 ]]; then
check_url_for_https $(brew cask _stanza ${stanza} "${cask_name}")
else
true
fi

if [[ $? -ne 0 ]]; then
echo "Restored original value for stanza ${stanza} as curl check failed"
replace_protocol_of_stanza ${cask_file} ${stanza} "https" "http"
else
updated_stanzas=$((updated_stanzas+1))
fi
done

if [[ ${updated_stanzas} -ne 0 ]]; then
finish success
else
finish abort "no updated stanzas after verify for ${cask_name}"
fi
199 changes: 199 additions & 0 deletions developer/bin/develop_brew_cask
@@ -0,0 +1,199 @@
#!/bin/bash
#
# develop_brew_cask
#
# Called via symlink as:
# production_brew_cask
#

called_as="$(basename "$0")"

###
### 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
###

taps_subdir="Library/Taps"
cask_tap_subdir="caskroom/homebrew-cask"
dev_links=("cmd" "lib" "Casks")

###
### 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 git_root
script_dir="$(/usr/bin/dirname "$0")"
cd "$script_dir"
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_tap_dir () {
local taps_dir="$1"
local tap_dir="$2"
if [[ ! -d "$tap_dir" ]]; then
die "ERROR: Could not find tap dir under $taps_dir/"
fi
cd "$tap_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 () {
for link_name in "${dev_links[@]}"; do
remove_dev_link "$link_name"
done
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 git_root="$1"
for link_name in "${dev_links[@]}"; do
create_dev_link "$git_root" "$link_name"
done
printf "brew-cask is now in development mode\n"
printf "Note: it is not safe to run 'brew update' while in development mode\n"
}

remove_dev_link () {
local link_name="$1"
/bin/rm -- "$link_name"
/bin/mv -- "production_$link_name" "$link_name"
}

create_dev_link () {
local git_root="$1"
local link_name="$2"
/bin/mv -- "$link_name" "production_$link_name"
/bin/ln -s -- "$git_root/$link_name" .
}

###
### main
###

_develop_brew_cask_develop_action () {
die "brew-cask is already set up for development"
}

_develop_brew_cask_production_action () {
create_dev_links "$git_root"
}

_production_brew_cask_develop_action () {
remove_dev_links
}

_production_brew_cask_production_action () {
die "brew-cask is already set up for production"
}

_main () {
local git_root brew_repository taps_dir tap_dir

# initialization
cd_to_project_root
git_root="$(/bin/pwd)"
brew_repository="$(brew --repository)"
taps_dir="$brew_repository/$taps_subdir"
tap_dir="$taps_dir/$cask_tap_subdir"

# sanity check
not_inside_homebrew "$tap_dir" "$git_root"

# action
cd_to_tap_dir "$taps_dir" "$tap_dir"
if [[ -e "production_lib" ]]; then
eval "_${called_as}_develop_action"
else
eval "_${called_as}_production_action"
fi

}

_develop_brew_cask_usage () {

printf "develop_brew_cask
Symlink private repo directories into Homebrew's Cellar, so
that the 'brew cask' command will use code and Casks from
the current development branch in your private repo.
Saves the production Homebrew directories under new names.
You can reverse this operation with 'production_brew_cask'.
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
_main "${@}"

#

0 comments on commit 1517168

Please sign in to comment.