Skip to content

Commit

Permalink
Improved sharedocs script
Browse files Browse the repository at this point in the history
  • Loading branch information
KOMATSU Seiji committed Feb 23, 2017
1 parent 6eba3dd commit adc71e4
Show file tree
Hide file tree
Showing 2 changed files with 188 additions and 21 deletions.
38 changes: 25 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,32 +20,44 @@ or

## Setup for Development Environment

### Setup PostgreSQL
### Setup in one go

#### mac/*unix

$ ./sharedocs setup

#### Windows

*Currently not supported*

### Setup in steps

#### Setup PostgreSQL

Setup PostgreSQL server on your machine and create database.

$ createuser -P sharedocs
$ createdb -E UTF8 -T template0 --lc-collate=ja_JP.UTF-8 --lc-ctype=ja_JP.UTF-8 sharedocs

### Prepare Tables
#### Prepare Tables

#### mac/*nix
##### mac/*nix

./sharedocs db:migrate

#### Windows
##### Windows

./sharedocs.bat db:migrate
sharedocs.bat db:migrate

### Set Environment Variables to sharedocsEnv script
#### Set Environment Variables to sharedocsEnv script

#### mac/*nix
##### mac/*nix

cp sharedocsEnv-template sharedocsEnv

#### Windows
##### Windows

cp sharedocsEnv-template.bat sharedocsEnv.bat
copy sharedocsEnv-template.bat sharedocsEnv.bat

##### Environment Variables

Expand Down Expand Up @@ -80,15 +92,15 @@ Setup PostgreSQL server on your machine and create database.
| AWS_S3_BASE_DIR | (if UPLOAD_DESTINATION==s3)<br />base pash to upload image file | "images/" |
| AWS_S3_BASE_URL | (if UPLOAD_DESTINATION==s3)<br />base url to access uploaded image file | "https://xxxxxxxx.s3.amazonaws.com/" |

### Run Application
#### Run Application

#### mac/*nix
##### mac/*nix

./sharedocs run

#### Windows
##### Windows

./sharedocs.bat run
sharedocs.bat run


## Additional setup for Development Environment
Expand Down
171 changes: 163 additions & 8 deletions sharedocs
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 $@

0 comments on commit adc71e4

Please sign in to comment.