-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
KOMATSU Seiji
committed
Feb 23, 2017
1 parent
6eba3dd
commit adc71e4
Showing
2 changed files
with
188 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,168 @@ | ||
#!/usr/bin/env bash | ||
|
||
cd $(dirname $0) | ||
source ./sharedocsEnv | ||
MYDIR=$(dirname "$0") | ||
ENV_FILE=${MYDIR}/sharedocsEnv | ||
ENV_FILE_TEMPLATE=${MYDIR}/sharedocsEnv-template | ||
|
||
command="$1" | ||
if [ "$command" == "run" -o "$command" == "s" -o "$command" == "server" -o "$command" == "debug" -o "$command" == "d" ]; then | ||
if [ ! -e src/main/webapp/assets/dist/version.txt ]; then | ||
npm run webpack | ||
usage() { | ||
cat <<EOT >&2 | ||
Usage: | ||
$0 help: Show help of sharedocs commands | ||
$0 help-skinny: Show help of skinny commands | ||
$0 setup: Setup to build | ||
$0 <skinny command>: Run skinny command (for more details, run $0 help-skinny) | ||
EOT | ||
|
||
exit 9 | ||
} | ||
|
||
info() { | ||
echo -e "\e[1m[INFO]\e[0m $*" >&2 | ||
} | ||
|
||
warn() { | ||
echo -e "\e[1m[\e[33mWARN\e[39m]\e[0m $*" >&2 | ||
} | ||
|
||
|
||
ok() { | ||
echo -e "\e[1m[\e[32m OK \e[39m]\e[0m $*" >&2 | ||
} | ||
|
||
ng() { | ||
echo -e "\e[1m[\e[31m NG \e[39m]\e[0m $*" >&2 | ||
} | ||
|
||
npm_install() { | ||
npm install | ||
if [ $? -eq 0 ]; then | ||
ok "npm install succeeded" | ||
else | ||
ng "npm install failed" | ||
exit 11 | ||
fi | ||
} | ||
|
||
npm_run_webpack() { | ||
npm run webpack | ||
if [ $? -eq 0 ]; then | ||
ok "npm run webpack succeeded" | ||
else | ||
ng "npm run webpack failed" | ||
exit 12 | ||
fi | ||
} | ||
|
||
create_db_user() { | ||
info "Creating db user for Sharedocs." | ||
read -p "Username: " DATABASE_USER | ||
export DATABASE_USER | ||
read -p "Password: " DATABASE_PASSWORD | ||
export DATABASE_PASSWORD | ||
|
||
info "Checking db user \`${DATABASE_USER}' exists" | ||
result=$(psql postgres -tAc "SELECT 1 FROM pg_roles WHERE rolname='${DATABASE_USER}'") | ||
if [ "$result" = "1" ]; then | ||
ok "DB user \`${DATABASE_USER}' already exists." | ||
return 0 | ||
fi | ||
|
||
info "Creating db user \`${DATABASE_USER}'" | ||
psql postgres -tAc "create user ${DATABASE_USER} with password '${DATABASE_PASSWORD}';" | ||
if [ $? -eq 0 ]; then | ||
ok "Creating user \`${DATABASE_USER}' succeeded" | ||
else | ||
ng "Creating user \`${DATABASE_USER}' failed" | ||
exit 13 | ||
fi | ||
fi | ||
} | ||
|
||
create_db() { | ||
info "Creating database for Sharedocs." | ||
read -p "Database Name: " DATABASE_DBNAME | ||
export DATABASE_DBNAME | ||
|
||
info "Checking db \`${DATABASE_DBNAME}' exists" | ||
result=$(psql postgres -tAc "SELECT 1 FROM pg_database WHERE datname='${DATABASE_DBNAME}'") | ||
if [ "$result" = "1" ]; then | ||
ok "Database \`${DATABASE_DBNAME}' already exists." | ||
return 0 | ||
fi | ||
|
||
info "Creating database \`${DATABASE_DBNAME}'" | ||
createdb -E UTF8 -T template0 -O ${DATABASE_USER} --lc-collate=ja_JP.UTF-8 --lc-ctype=ja_JP.UTF-8 ${DATABASE_DBNAME} | ||
if [ $? -eq 0 ]; then | ||
ok "Creating database \`${DATABASE_DBNAME}' succeeded" | ||
else | ||
ng "Creating user \`${DATABASE_DBNAME}' failed" | ||
exit 14 | ||
fi | ||
} | ||
|
||
migrate_db() { | ||
info "Migrating database" | ||
./skinny db:migrate | ||
if [ $? -eq 0 ]; then | ||
ok "Migrating database succeeded" | ||
else | ||
ng "Migrating database failed" | ||
exit 16 | ||
fi | ||
} | ||
|
||
setup() { | ||
npm_install | ||
npm_run_webpack | ||
create_db_user | ||
create_db | ||
migrate_db | ||
|
||
if [ ! -f "${ENV_FILE}" ]; then | ||
warn "${ENV_FILE} does not exist." | ||
warn "Copying from ${ENV_FILE_TEMPLATE} to ${ENV_FILE}." | ||
cp -p ${ENV_FILE_TEMPLATE} ${ENV_FILE} | ||
fi | ||
|
||
sed -e 's/\(DATABASE_DBNAME="\).*\("\)/\1'"${DATABASE_DBNAME}"'\2/' -i '' ${ENV_FILE} | ||
sed -e 's/\(DATABASE_USER="\).*\("\)/\1'"${DATABASE_USER}"'\2/' -i '' ${ENV_FILE} | ||
sed -e 's/\(DATABASE_PASSWORD="\).*\("\)/\1'"${DATABASE_PASSWORD}"'\2/' -i '' ${ENV_FILE} | ||
|
||
info "" | ||
info "***** Getting Started to Run Sharedocs *****" | ||
info "" | ||
info "To run Sharedocs, at least the following variables must be configured in ${ENV_FILE}." | ||
info " DATABASE_HOST" | ||
info " DATABASE_DBNAME" | ||
info " DATABASE_USER" | ||
info " DATABASE_PASSWORD" | ||
info " LOGIN_PERMITTED_EMAIL_DOMAINS" | ||
info "" | ||
info "If you have configured variables, you're now ready to run Sharedocs!" | ||
info "Run the following command:" | ||
info "" | ||
info " ./sharedocs run" | ||
info "" | ||
} | ||
|
||
cd ${MYDIR} | ||
|
||
command="$1" | ||
case "$command" in | ||
setup) | ||
setup | ||
;; | ||
help|--help|-h) | ||
usage | ||
;; | ||
help-skinny) | ||
./skinny help | ||
;; | ||
*) | ||
if [ $# -eq 0 ]; then | ||
usage | ||
fi | ||
[ -f ${ENV_FILE} ] && . ${ENV_FILE} | ||
./skinny $@ | ||
;; | ||
esac | ||
|
||
./skinny $@ |