-
Notifications
You must be signed in to change notification settings - Fork 0
/
prelang-init
executable file
·221 lines (181 loc) · 5.23 KB
/
prelang-init
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
#!/bin/bash
# ================================================
# SHELL->PRELANG->INIT ===========================
# ================================================
_skip_browser_prompt=false
_skip_git_push=false
# Parse arguments/flags
while :
do
case $1 in
--skip-browser-prompt)
_skip_browser_prompt=true
shift
;;
--skip-git-push)
_skip_git_push=true
shift
;;
--) # End of all options
shift
break
;;
-*)
printf >&2 'Warning: Unknown option (ignored): %s\n' "$1"
shift
;;
*) # No more options. Break from loop.
break
;;
esac
done
# ------------------------------------------------
# UTILITY ----------------------------------------
# ------------------------------------------------
_fatal_error() {
echo "fatal: $@"
exit 1
}
_command_exists() {
(command -v $1 >/dev/null 2>&1)
}
_require_command() {
if ( ! `_command_exists $1` ) ; then
_fatal_error "Prelang init script requires '$1' but it is not installed."
fi
}
_topic() {
echo "-----> $@"
}
_execute_sucessfully_or_exit() {
# Raw execution of the command
$@
# Check exit status
if [[ $? != 0 ]] ; then
_fatal_error "Command '$@' failed."
fi
}
_git_commit_all_with_message() {
git add .
git commit --all --message="$1" --author="Prelang Builder <[email protected]>"
}
_git_push() {
# FIX: Make sure the user is on master
git push origin master
}
_prompt() {
echo -e -n "\033[1;32m"
echo -n $1" (y/n): "
echo -e -n '\033[0m'
read answer
if [[ $answer == "y" ]] ; then
return 0
fi
return 1
}
_sleep_and_open_rails_url() {
_rails_url="http://localhost:3000"
# FIX: Make a better way to check if the Rails server is running.
sleep 7
if [[ `uname` == "Darwin" ]] ; then
open $_rails_url
elif [[ `uname` == "Linux" ]] ; then
if ( `_command_exists xdg-open` ) ; then
xdg-open $_rails_url
fi
fi
}
_initialize_rvm_if_exists() {
# In case RVM already is sourced don't source it.
# FIX: Not sure this can happen since we're launching from a shebang script.
if ( ! `_command_exists $1` ) ; then
return 0
fi
# Source RVM if it exists
if [[ -f $HOME/.rvm/scripts/rvm ]] ; then
source $HOME/.rvm/scripts/rvm
fi
}
#_initialize_rbenv_if_exists() {
#if [[ -d $HOME/.rbenv ]] ; then
#export RBENV_ROOT="${HOME}/.rbenv"
#export PATH="${RBENV_ROOT}/bin:${PATH}"
#eval "$(rbenv init -)"
#fi
#}
_gem_exists_in_gemfile() {
(git grep -E "gem '$1'" Gemfile >/dev/null 2>&1)
}
# FIX: Not the most thorough way to check for database type but will work for
# now.
_database_is_postgresql() {
(_gem_exists_in_gemfile pg)
}
_database_is_sqlite3() {
(_gem_exists_in_gemfile sqlite3)
}
_postgres_is_running() {
if [[ `ps aux | grep postgres | grep -v grep | wc -l | sed "s/ //g" ` -gt 0 ]] ; then
return 0
fi
return 1
}
# ------------------------------------------------
# MAIN -------------------------------------------
# ------------------------------------------------
#_initialize_rbenv_if_exists
_initialize_rvm_if_exists
_require_command "git"
# FIX: Check for non-Prelang Builder commits
# Run "bundle install"
# --------------------
_require_command "bundle"
_topic "Running 'bundle install' to fetch and install required Ruby Gems"
_execute_sucessfully_or_exit bundle install
# FIX: Do we need this commit?
_git_commit_all_with_message "Executed 'bundle install' to fetch and install required Ruby Gems"
# Require "rake" for next few commands
_require_command "rake"
# If application is PostgreSQL, check/require server to be running before
# database creation.
if ( `_database_is_postgresql` && ! `_postgres_is_running` ) ; then
_fatal_error "PostgreSQL server is not running. Cannot create databases."
fi
# Run "rake db:create"
# --------------------
_topic "Running 'rake db:create' to create development and test databases"
_execute_sucessfully_or_exit rake db:create
# Run "rake db:migrate"
# --------------------
_topic "Running 'rake db:migrate' (development and test) to migrate to the proper database schema"
_execute_sucessfully_or_exit rake db:migrate
_execute_sucessfully_or_exit rake db:migrate RAILS_ENV=test
# Track schema.rb
_git_commit_all_with_message "Executed 'rake db:migrate' (development and test) to migrate all generated migrations which created schema.rb"
# Clean up
# --------
#_prompt "Would you like to keep this script (./prelang-init) in your repository? This will allow you to run it later if you need to reinitialize your Rails project."
#if [[ $? != 0 ]] ; then
#_topic "Removing 'prelang-init' script"
#if ( ! test -f ./prelang-init ) ; then
#_fatal_error "'./prelang-init' does not exist."
#fi
#_execute_sucessfully_or_exit git rm ./prelang-init
#_git_commit_all_with_message "Removed 'prelang-init' script after successful execution"
#fi
# Push
# ----
#if ( ! $_skip_git_push ) ; then
#_topic "Pushing git repository changes back to origin"
#_git_push
#fi
# Rails Server
# ------------
if ( ! $_skip_browser_prompt ) ; then
_prompt "Would you like to start the Rails server and open your running Rails application in a browser?"
if [[ $? == 0 ]] ; then
_sleep_and_open_rails_url &
# FIX: Will this be using the correct Rails? RVM?
rails server
fi
fi