Skip to content

Commit

Permalink
feat: add script to ease conversion from template to real formula
Browse files Browse the repository at this point in the history
  • Loading branch information
dafyddj committed Nov 30, 2019
1 parent d8b539a commit b907232
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions bin/convert-formula.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#!/usr/bin/env sh
set -o nounset # Treat unset variables as an error and immediately exit
set -o errexit # If a command fails exit the whole script

if [ "${DEBUG:-false}" = "true" ]; then
set -x # Run the entire script in debug mode
fi

usage() {
echo "usage: $(basename "$0") <new-formula-name>" 1>&2
echo 1>&2
echo "Convert template-formula to <new-formula-name>-formula." 1>&2
echo "<new-formula-name> should be a string of lowercase characters and numbers only." 1>&2
echo "<new-formula-name> should not be any of 'bin' 'docs' 'test'." 1>&2
}

args() {
if [ $# -ne 1 ]; then
usage
exit 1
fi
NEW_NAME=$1
if echo "$NEW_NAME" | grep -E --quiet --invert-match '^[a-z0-9]+$'; then
usage
exit 1
fi
if echo bin docs test | grep --quiet --word-regexp "$NEW_NAME"; then
usage
exit 1
fi
}

convert_formula() {
# Empty history and make commit message `semantic-release`-compliant
# Works for forks of `template-formula` as well as GitHub "Use this template"
git reset \
"$(echo 'feat: initial commit' \
| git commit-tree 'HEAD^{tree}')"
git rm --quiet bin/convert-formula.sh AUTHORS.md CHANGELOG.md
git mv template__ "${NEW_NAME}"
grep --recursive --files-with-matches --exclude-dir=.git template__ . \
| xargs -L 1 ex -sc '%s/template__/'"${NEW_NAME}"'/g|x'
# shellcheck disable=SC2016 # Expressions don't expand in single quotes
git commit --quiet --all \
--message 'feat: convert `template-formula` to `'"${NEW_NAME}"'-formula`
BREAKING CHANGE: changed all state names and ids'
}

args "$@"
convert_formula

0 comments on commit b907232

Please sign in to comment.