-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy patheject.sh
executable file
·97 lines (78 loc) · 2.36 KB
/
eject.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/bin/bash
set -euo pipefail
REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" &>/dev/null && pwd -P)"
usage() {
cat <<EOF
Usage: $(basename "${BASH_SOURCE[0]}") [-hr] project_name
Prepare the template repo for a new project, updating all relevant references in
the template to \${project_name}
Available options:
-h, --help Print this help and exit
-r, --reverse Untemplate the project back
EOF
}
msg() {
echo >&2 -e "${1-}"
}
die() {
local msg=$1
local code=${2-1} # default exit status 1
msg "$msg"
exit "$code"
}
parse_params() {
# default values of variables set from params
REVERSE=0
while :; do
case "${1-}" in
-h | --help) usage && exit;;
-r | --reverse) REVERSE=1;;
-?*) die "Unknown option: $1" ;;
*) break ;;
esac
shift
done
args=("$@")
[[ ${#args[@]} -eq 0 ]] && usage && die "Missing script argument"
return 0
}
validate_name() {
if [[ ! ${1} =~ ^([[:alnum:]_]+)$ ]]; then
die "Project name contains invalid characters, must contain only unicode alphanumerics or an underscore"
fi
PROJECT_NAME=${1}
}
parse_params "$@"
validate_name "${args[*]-}"
if [ $REVERSE -eq "1" ]; then
msg "Reverse flag is set, untemplating project"
SOURCE_ARG=${PROJECT_NAME}
DEST_ARG="\${project}"
SOURCE_FOLDER=${PROJECT_NAME}
DEST_FOLDER="project"
else
SOURCE_ARG="\${project}"
DEST_ARG=${PROJECT_NAME}
SOURCE_FOLDER="project"
DEST_FOLDER=${PROJECT_NAME}
fi
if [ -d "${REPO_ROOT}/${SOURCE_FOLDER}" ]; then
msg "Renaming directory ${SOURCE_FOLDER} to ${DEST_FOLDER}"
mv ${REPO_ROOT}/${SOURCE_FOLDER} ${REPO_ROOT}/${DEST_FOLDER}
else
msg "${REPO_ROOT}/${SOURCE_FOLDER} doesn't exist - likely already renamed"
fi
msg "Updating all references to ${SOURCE_ARG} to ${DEST_ARG}"
# BSD and GNU sed handle the `-i` argument for in-place edits differently
INPLACEOPT=(-i)
if [[ "$OSTYPE" == "darwin"* ]]; then
# BSD sed expects an explicit null arg to follow the -i flag. We use an
# array expansion to accomplish this, since otherwise it gets swallowed
# if quoted in any quote character.
INPLACEOPT=(-i "")
fi
find ${REPO_ROOT} \
-type f \
-not -path "${REPO_ROOT}/.git/*" \
-not -path "${REPO_ROOT}/eject.sh" \
-exec sed "${INPLACEOPT[@]}" -e "s/${SOURCE_ARG}/${DEST_ARG}/g" {} \;