-
Notifications
You must be signed in to change notification settings - Fork 0
/
startup.sh
134 lines (103 loc) · 3.57 KB
/
startup.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
#!/bin/sh
set +xe
help() {
echo "
Startup.SH
Run Rails on docker using a single command by Petar <Shannarra> Angelov (https://github.com/Shannarra/)
By default this script will own all of the project's files to your user, create an .env file and
setup the database with basic models' data.
This does NOT include authorization with Devise, for that see --device.
Commands:
-d, --devise [NAME] => setup the application using Devise with given NAME
-r, --run => start the container up when the setup has finished.
-h, --help => display this message
Found a bug? Have any suggestions?
You can always open a PR at https://github.com/Shannarra/rails7template
";
}
while [ $# -gt 0 ]; do
case $1 in
-d|--devise)
USES_DEVISE=1
shift # past argument
if [ -z $1 ]; then
echo "ERROR: If you want to create a devise, please provide a name! Use --help for more info."
exit 1
fi
DEVISE_NAME=$1
shift # past value
;;
-r|--run) # Run when everything has been built
RUN_WHEN_DONE=1
shift # past argument
;;
-h|--help)
help
shift
exit 0
;;
-*|--*|*)
echo -e "Invalid option \"$1\". Check -h or --help for more info."
exit 1
;;
esac
done
setup_device() {
echo "[STARTUP] Setting up Devise gem..."
sed -i "65 i \\
# Useful to bootstrap authentication. https://stackoverflow.com/a/42190260/11542917 \\
" Gemfile
sed -i "66 i gem 'devise'" Gemfile
# install gem
docker compose run --rm web bundle
# do the setup
docker compose run --rm web rails g devise:install
# enable :turbo_stream
sed -i "266 i \\
config.navigational_formats = ['*/*', :html, :turbo_stream] \\
" config/initializers/devise.rb
echo "[STARTUP] Creating devise $DEVISE_NAME...."
# https://www.digitalocean.com/community/tutorials/how-to-set-up-user-authentication-with-devise-in-a-rails-7-application
docker compose run --rm web rails g devise $DEVISE_NAME
if [ $? -eq 0 ]; then
echo "[STARTUP] Setting up database"
# doing db:setup here doesn't work
docker compose run --rm web rails db:create
docker compose run --rm web rails db:migrate
docker compose run --rm web rails db:seed
echo "[STARTUP] Devise $DEVISE_NAME successfully created!"
else
echo "[STARTUP] Could not create device $DEVISE_NAME :("
exit 1
fi
sed -i "42 i \\
<device-buttons>
" app/views/layouts/application.html.erb
sed -i "/<device-buttons>/ {
s/<device-buttons>//g
a\ <form class=''> \\
<%- if !current_user.present? %> \\
<%= link_to 'Login', new_user_session_path, class: 'btn btn-outline-primary mr-2' %> \\
<%= link_to 'Become member', new_user_registration_path, class: 'btn btn-outline-success' %> \\
<%- else %> \\
<%= link_to 'Log out', destroy_user_session_path, class: 'btn btn-outline-danger', data: { turbo_method: :delete }%> \\
<% end %> \\
</form>
}" app/views/layouts/application.html.erb
}
setup_application() {
chmod u+x -R ./bin/*
cp .env.example .env
docker compose build
if [ $USES_DEVISE -eq 1 ]; then
setup_device
else
echo "[STARTUP] Setting up database"
docker compose run --rm web rails db:setup
fi
chmod u+x -R ./bin/*
if [ $RUN_WHEN_DONE -eq 1 ]; then
docker compose up
fi
}
setup_application