-
Notifications
You must be signed in to change notification settings - Fork 194
/
ihp-new
executable file
路289 lines (261 loc) 路 9.07 KB
/
ihp-new
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#!/usr/bin/env bash
set -e
readlink "$(command -v "$0")" || echo
NIXPKGS_ATTR="nixpkgs"
if [ -f /etc/os-release ]; then
grep '^ID=nixos$' /etc/os-release >/dev/null 2>&1 && NIXPKGS_ATTR="nixos"
fi
IHP_NEW_VERSION="1.3.0"
BOILERPLATE_GIT_URL="https://github.com/digitallyinduced/ihp-boilerplate.git"
for _ in once; do
# requires at least one argument
# two could be wrong, more definitely are
# if it's one but it starts with a minus, that's invalid
if [ $# -lt 1 ] || [ $# -ge 2 ] || [[ "$#" == "1" && "$1" =~ ^-.* ]]; then
# two arguments where the first one is a valid option and the second doesn't start with minus are valid
if [[ "$#" == "2" ]] && [[ "$1" == "--default" || "$1" == "--elm" || "$1" == "--purescript-halogen" ]] && [[ "$2" =~ ^[^-].* ]]; then
break
fi
if [[ "$#" == "1" && "$1" == "--version" ]]; then
echo "ihp-new version: $IHP_NEW_VERSION"
exit 0
fi
echo "Usage: ihp-new PROJECT_NAME"
echo ""
echo "or: ihp-new --OPTION PROJECT_NAME"
echo ""
echo "Generates an empty IHP project"
echo ""
echo "Full documentation: https://ihp.digitallyinduced.com/Guide/installation.html"
echo "Project template: $BOILERPLATE_GIT_URL"
echo ""
echo "In case there already exists a directory named like the given project name, this script will stop execution."
echo ""
echo "Available options:"
echo ""
echo " --version"
echo " Print version of ihp-new"
echo ""
echo " --default"
echo " (recommended, default, code on branch master)"
echo " Generates a pure IHP project."
echo ""
echo " --elm"
echo " (code on branch elm)"
echo " Generates an IHP project with Elm ready to go."
echo ""
echo " --purescript-halogen"
echo " (code on branch purescript-halogen)"
echo " Generates an IHP project with PureScript + Halogen ready to go."
echo ""
if [[ "$#" == "1" && "$1" == "--help" ]]; then
exit 0
fi
exit 1
fi
done
PROJECT_NAME=$1
if [[ "$#" == "2" ]]; then
PROJECT_NAME=$2
fi
if [ -d "$PROJECT_NAME" ]; then
echo "Directory \"$PROJECT_NAME\" already exists. Please remove the directory and re-run the generator"
exit 1
fi
if ! [ -x "$(command -v direnv)" ]; then
echo -e "\e[1m\e[35mDIRENV MISSING\e[0m"
echo -e "IHP uses direnv to manage env variables."
echo -e "Learn more about direnv here: \e[4mhttps://direnv.net/\e[0m"
echo ""
echo ""
echo -e "\e[1mWe will install direnv for you now. Continue? (Type y to proceed) \e[0m"
read -r -n1 choice
echo ""
case "$choice" in
y | Y) nix-env -iA ${NIXPKGS_ATTR}.direnv ;;
*)
echo -e "\e[31mPlease install direnv manually and then re-run this program.\e[0m"
exit 1
;;
esac
echo -e "\e[1mDirenv needs to be hooked into your shell.\e[0m"
DIRENV_HOOK_ADDED=0
if [ "${SHELL##*/}" == "bash" ] || [ "${SHELL##*/}" == "zsh" ]; then
echo -e "\e[1mWould you like to attempt to do this automatically? (Type y to proceed) \e[0m"
while true; do
read -r -n1 choice
echo ""
case "$choice" in
y | Y)
echo "Great!"
case "${SHELL##*/}" in
zsh )
echo "Adding hook to ~/.zshrc...";
echo 'eval "$(direnv hook zsh)"' >> $HOME/.zshrc
DIRENV_HOOK_ADDED=1
;;
bash )
echo "Adding hook to ~/.bashrc...";
echo 'eval "$(direnv hook bash)"' >> $HOME/.bashrc
DIRENV_HOOK_ADDED=1
;;
default )
echo "\e[31mError: Unrecognised shell[0m";
;;
esac
break
;;
n | N)
break
;;
*) echo -e "\e[1mWould you like to attempt to do this automatically? Type \e[33my\e[0m\e[1m for 'yes', \e[33mn\e[0m\e[1m for 'no'. \e[0m" ;;
esac
done
else
echo -e "\e[31mCouldn't detect what shell you're using.\e[0m"
fi
if [ $DIRENV_HOOK_ADDED = 0 ]; then
echo ""
echo "Please add the hook manually."
echo -e "Bash: Add \e[4meval \"\$(direnv hook bash)\"\e[0m to ~/.bashrc"
echo -e "ZSH: Add \e[4meval \"\$(direnv hook zsh)\"\e[0m to ~/.zshrc"
echo "Other shells: See https://direnv.net/#README"
echo -e "\e[1mHave you hooked direnv into your shell? (Type y to proceed) \e[0m"
while true; do
read -r -n1 choice
echo ""
case "$choice" in
y | Y)
echo "Great!"
break
;;
n | N)
echo -e "\e[1m\e[33mHook direnv into your shell after this script has finished.\e[0m"
break
;;
*) echo -e "\e[1mHave you hooked direnv into your shell? Type \e[33my\e[0m\e[1m for 'yes', \e[33mn\e[0m\e[1m for 'no'. \e[0m" ;;
esac
done
DIRENV_HOOK_ADDED=1
fi
fi
DIRENV_SETUP=0;
case "${SHELL##*/}" in
zsh )
if grep direnv "$HOME/.zshrc" "$HOME/.zprofile" 2>&1 >/dev/null; then
DIRENV_SETUP=1;
fi;
;;
bash )
if grep direnv "$HOME/.bashrc" "$HOME/.bash_profile" 2>&1 >/dev/null; then
DIRENV_SETUP=1;
fi;
;;
default )
echo "\e[1mUnable to check if direnv is setup correctly (unrecognised shell).\e[0m";
;;
esac
if [ $DIRENV_SETUP = 0 ] && [ DIRENV_HOOK_ADDED != 1 ]; then
echo -e "\e[1mDirenv shell hook doesn't seem correct.\e[0m"
echo -e "Direnv needs to be hooked into your shell. Please follow the steps below:"
echo -e "Bash: Add \e[4meval \"\$(direnv hook bash)\"\e[0m to ~/.bashrc"
echo -e "ZSH: Add \e[4meval \"\$(direnv hook zsh)\"\e[0m to ~/.zshrc"
echo "Other shells: See https://direnv.net/#README"
echo -e "This could be a false negative if you use a more complex shell profile"
echo ""
fi
if ! [ -x "$(command -v git)" ]; then
echo -e "\e[1m\e[35mGIT MISSING\e[0m"
echo -e "IHP uses git for pulling the project boilerplate."
echo ""
echo ""
echo -e "\e[1mWe will install git for you now using 'nix-env -i git'. Continue? (Type y to proceed) \e[0m"
read -r -n1 choice
echo ""
case "$choice" in
y | Y) nix-env -iA ${NIXPKGS_ATTR}.git ;;
*)
echo -e "\e[31mPlease install git manually and then re-run this program.\e[0m"
exit 1
;;
esac
fi
if ! [ -x "$(command -v make)" ]; then
echo -e "\e[1m\e[35mMAKE MISSING\e[0m"
echo -e "IHP uses make for the building process."
echo ""
echo ""
echo -e "\e[1mWe will install make for you now using 'nix-env -i gnumake'. Continue? (Type y to proceed) \e[0m"
read -r -n1 choice
echo ""
case "$choice" in
y | Y) nix-env -iA ${NIXPKGS_ATTR}.gnumake ;;
*)
echo -e "\e[31mPlease install make manually and then re-run this program.\e[0m"
exit 1
;;
esac
fi
if ! [ -x "$(command -v cachix)" ]; then
echo -e "\e[1m\e[35mCACHIX MISSING\e[0m"
echo -e "IHP uses cachix as a binary cache."
echo -e "Learn more about cachix here: \e[4mhttps://cachix.org/\e[0m"
echo ""
echo ""
echo -e "\e[1mWe will install cachix for you now. Continue? (Type y to proceed) \e[0m"
read -r -n1 choice
echo ""
case "$choice" in
y | Y) nix-env -iA ${NIXPKGS_ATTR}.cachix ;;
*)
echo -e "\e[31mPlease install cachix manually and then re-run this program.\e[0m"
exit 1
;;
esac
fi
if ! grep -s -q "digitallyinduced.cachix.org-1:y+wQvrnxQ+PdEsCt91rmvv39qRCYzEgGQaldK26hCKE=" ~/.config/nix/nix.conf; then
echo -e "\e[1mCachix doesn't seem to be configured\e[0m"
echo -e "IHP provides a cachix cache with binaries for all IHP packages and commonly used dependencies."
echo -e "While not required, this greatly speeds up the installation & build process."
echo -e "This could be a false negative if you use a more complex nix configuration."
echo ""
echo ""
echo -e "\e[1mWe will configure the cache for you now using 'cachix use digitallyinduced'. Continue? (Type y to proceed) \e[0m"
read -r -n1 choice
echo ""
case "$choice" in
y | Y) cachix use digitallyinduced ;;
*) echo -e "Skipping cachix configuration" ;;
esac
fi
BOILERPLATE_GIT_BRANCH="master"
if [[ "$1" == "--elm" ]]; then
echo "We will now create your new IHP project with Elm. This may take up to 30 seconds."
BOILERPLATE_GIT_BRANCH="elm"
elif [[ "$1" == "--purescript-halogen" ]]; then
echo "We will now create your new IHP project with PureScript + Halogen. This may take up to 30 seconds."
BOILERPLATE_GIT_BRANCH="purescript-halogen"
else
echo "We will now create your new IHP project. This may take up to 30 seconds."
fi
git clone --quiet --depth=1 --branch "$BOILERPLATE_GIT_BRANCH" "$BOILERPLATE_GIT_URL" -- "$PROJECT_NAME"
cd "$PROJECT_NAME"
rm -rf ".git"
chmod go-w .ghci || true
# Generating Web/ directory
nix --extra-experimental-features "nix-command flakes" develop --impure --accept-flake-config --command bash -c "make -s all; new-application Web"
# Warming up direnv
direnv allow
direnv exec . "true"
if [[ "$BOILERPLATE_GIT_BRANCH" == "elm" ]]; then
nix --extra-experimental-features "nix-command flakes" develop --impure --accept-flake-config --command bash -c 'patch Web/View/Layout.hs -i Layout.hs.patch; patch Web/View/Static/Welcome.hs -i Welcome.hs.patch; npm install'
rm Layout.hs.patch Welcome.hs.patch
elif [[ "$BOILERPLATE_GIT_BRANCH" == "purescript-halogen" ]]; then
nix --extra-experimental-features "nix-command flakes" develop --impure --accept-flake-config --command bash -c 'patch Web/View/Layout.hs -i Layout.hs.patch; patch Web/View/Static/Welcome.hs -i Welcome.hs.patch; npm install'
rm Layout.hs.patch Welcome.hs.patch
fi
echo ""
echo "馃殌 Project created, happy coding :)"
echo "You can start the server by running:"
echo " cd $PROJECT_NAME"
echo " devenv up"