feat: initial commit
[arvados-formula.git] / bin / convert-formula.sh
1 #!/usr/bin/env sh
2 set -o nounset # Treat unset variables as an error and immediately exit
3 set -o errexit # If a command fails exit the whole script
4
5 if [ "${DEBUG:-false}" = "true" ]; then
6   set -x # Run the entire script in debug mode
7 fi
8
9 usage() {
10   echo "usage: $(basename "$0") <new-formula-name>" 1>&2
11   echo 1>&2
12   echo "Convert template-formula to <new-formula-name>-formula." 1>&2
13   echo "<new-formula-name> should be a string of lowercase characters and numbers only." 1>&2
14   echo "<new-formula-name> should not be any of 'bin' 'docs' 'test'." 1>&2
15 }
16
17 args() {
18   if [ $# -ne 1 ]; then
19     usage
20     exit 1
21   fi
22   NEW_NAME=$1
23   if echo "$NEW_NAME" | grep -E --quiet --invert-match '^[a-z0-9]+$'; then
24     usage
25     exit 1
26   fi
27   if echo bin docs test | grep --quiet --word-regexp "$NEW_NAME"; then
28     usage
29     exit 1
30   fi
31 }
32
33 convert_formula() {
34   # Empty history and make commit message `semantic-release`-compliant
35   # Works for forks of `template-formula` as well as GitHub "Use this template"
36   # See https://stackoverflow.com/a/15572071/5009408
37   git reset \
38     "$(echo 'feat: initial commit' \
39       | git commit-tree 'HEAD^{tree}')"
40   git rm --quiet bin/convert-formula.sh AUTHORS.md CHANGELOG.md \
41     docs/_static/css/custom.css docs/AUTHORS.rst docs/CHANGELOG.rst \
42     docs/conf.py docs/CONTRIBUTING_DOCS.rst docs/index.rst
43   git mv TEMPLATE "${NEW_NAME}"
44   grep --recursive --files-with-matches --exclude-dir=.git TEMPLATE . \
45     | xargs -L 1 ex -u NONE -sc '%s/TEMPLATE/'"${NEW_NAME}"'/g|x'
46   # Searching across multiple lines.
47   # See https://vim.fandom.com/wiki/Search_across_multiple_lines
48   ex -u NONE -sc '%s/^.. <REMOVEME\_.\{-}.. REMOVEME>/None/g|x' docs/README.rst
49   ex -u NONE -sc '%s/^\s*# <REMOVEME\_.\{-}# REMOVEME>\n//g|x' .travis.yml
50   ex -u NONE -sc '%s/^\s*# <REMOVEME\_.\{-}# REMOVEME>\n//g|x' .rubocop.yml
51   ex -u NONE -sc '%s/^\(version:\).*/\1 1.0.0/g|x' FORMULA
52   # shellcheck disable=SC2016 # Expressions don't expand in single quotes
53   git commit --quiet --all \
54     --message 'feat: convert `template-formula` to `'"${NEW_NAME}"'-formula`' \
55     --message 'BREAKING CHANGE: changed all state names and ids'
56 }
57
58 args "$@"
59 convert_formula