-
Notifications
You must be signed in to change notification settings - Fork 8
/
setup.sh
executable file
·183 lines (150 loc) · 5.11 KB
/
setup.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#!/usr/bin/env bash
# Error constants.
ERROR_SYSTEM_UNKNOWN='Unknown system found "%s".\n'
ERROR_RUN_IN_EMPTY_DIRECTORY='Please run the script from an empty directory\n'
ERROR_COULD_NOT_FIND_COMMAND='Could not find "%s", try installing it by running "%s".'
# System Constants
SYSTEM_NAME_LINUX='Linux'
SYSTEM_NAME_MAC_OS='Darwin'
SYSTEM_NAME_FREEBSD='FreeBSD'
ALL_SYSTEM_SUPPORTED="${SYSTEM_NAME_LINUX} ${SYSTEM_NAME_MAC_OS} ${SYSTEM_NAME_FREEBSD}"
# Prerequisite constants.
PREREQUISITE_CONSTANT_PREFIX='ALL_PREREQUISITE_'
ALL_PREREQUISITE_GLOBAL='git php composer qrencode jq'
ALL_PREREQUISITE_LINUX=''
ALL_PREREQUISITE_DARWIN='brew'
ALL_PREREQUISITE_FREEBSD=''
ALL_EXTENSION_PHP_REQUIRED='mbstring curl json'
# Installation method constants.
COMMAND_INSTALLATION_PREFIX='COMMAND_INSTALLATION_'
COMMAND_INSTALLATION_LINUX='apt-get install'
COMMAND_INSTALLATION_DARWIN='brew install'
COMMAND_INSTALLATION_FREEBSD='pkg install'
# Version index
INDEX_VERSION_MAJOR='0'
INDEX_VERSION_MINOR='1'
INDEX_VERSION_PATCH='2'
# Platform specific sed command
COMMAND_SED_PREFIX='COMMAND_SED_'
COMMAND_SED_LINUX='sed -rn'
COMMAND_SED_DARWIN='sed -En'
COMMAND_SED_FREEBSD='sed -rn'
function assertIsSystemSupported
{
contains "${ALL_SYSTEM_SUPPORTED}" "$(determineSystemName)" \
&& return 0 || printf "${ERROR_SYSTEM_UNKNOWN}" "$(determineSystemName)" >&2 && exit 1
}
function assertIsRanInEmptyDirectory
{
[ "$(ls -1A | wc -l)" -eq 0 ] && return 0 || printf "${ERROR_RUN_IN_EMPTY_DIRECTORY}" >&2 && exit 1
}
function assertAllPrerequisitePresent
{
allPrerequisiteMissing="$(determineAllPrerequisiteMissing)"
if [ -z "${allPrerequisiteMissing}" ]; then
# All prerequisites are available
return 0
else
echo -n "$(determineInstructionInstallationAllPrerequisiteMissing "${allPrerequisiteMissing}")"
exit 1
fi
}
function determineAllPrerequisiteMissing
{
for prerequisite in $(determineAllPrerequisite); do
which "${prerequisite}" > /dev/null || echo "${prerequisite}"
done
}
function determineAllPrerequisite
{
echo "$(determineAllPrerequisiteSystemSpecific) ${ALL_PREREQUISITE_GLOBAL}"
}
function determineAllPrerequisiteSystemSpecific
{
prerequisiteConstantName="${PREREQUISITE_CONSTANT_PREFIX}$(capitalize $(determineSystemName))"
echo "${!prerequisiteConstantName}"
}
function determineInstructionInstallationAllPrerequisiteMissing
{
allPrerequisiteMissing="${1}"
systemName="$(determineSystemName)"
for prerequisiteMissing in ${1}; do
prerequisiteMissingCapitalized="$(capitalizeFirstLetter ${prerequisiteMissing})"
if declare -F "determineInstructionInstallation${prerequisiteMissingCapitalized}${systemName}" > /dev/null ; then
echo "$(determineInstructionInstallation${prerequisiteMissingCapitalized}${systemName})" >&2
elif declare -F "determineInstructionInstallation${prerequisiteMissingCapitalized}" > /dev/null ; then
echo "$(determineInstructionInstallation${prerequisiteMissingCapitalized})" >&2
else
echo "$(determineInstructionInstallation "${prerequisiteMissing}" "${systemName}")" >&2
fi
done
}
function determineInstructionInstallationBrewDarwin
{
echo -ne "Could not find \"brew\", install this by running: \033[1m/usr/bin/ruby -e \"\$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)\"\033[0m"
echo ", or checkout how to install this by going to https://brew.sh."
}
function determineInstructionInstallationPhpDarwin
{
determineInstructionInstallation "php" "$(determineSystemName)" "php70 --with-homebrew-curl"
}
function determineInstructionInstallationPhpFreeBSD
{
determineInstructionInstallation "php" "$(determineSystemName)" "php70 php70-mbstring php70-curl php70-json"
}
function determineInstructionInstallationComposerFreeBSD
{
determineInstructionInstallation "composer" "$(determineSystemName)" "php70-composer"
}
function determineInstructionInstallationQrencodeFreeBSD
{
determineInstructionInstallation "qrencode" "$(determineSystemName)" "libqrencode"
}
function determineInstructionInstallation
{
programName="${1}"
systemName="${2}"
programPackageName=${3:-${programName}}
commandInstallationConstantName="${COMMAND_INSTALLATION_PREFIX}$(capitalize ${systemName})"
printf "${ERROR_COULD_NOT_FIND_COMMAND}" "${programName}" "${!commandInstallationConstantName} ${programPackageName}"
}
function determineSystemName
{
echo "$(uname -s)"
}
function determineSedCommand
{
commandSedConstantName="${COMMAND_SED_PREFIX}$(capitalize $(determineSystemName))"
echo "${!commandSedConstantName}"
}
function cloneTinkerPhp
{
git clone https://github.com/bunq/tinker_php.git .
}
function composerInstall
{
composer install
}
function startTinker
{
./go-tinker
}
function contains
{
[[ ${1} =~ (^|[[:space:]])${2}($|[[:space:]]) ]] && return 0 || return 1
}
function capitalize
{
echo "${@}" | tr [:lower:] [:upper:]
}
function capitalizeFirstLetter
{
echo "$(capitalize ${1:0:1})${1:1}"
}
assertIsSystemSupported
assertIsRanInEmptyDirectory
assertAllPrerequisitePresent
cloneTinkerPhp
composerInstall
startTinker
exit 0