-
Notifications
You must be signed in to change notification settings - Fork 0
/
create
executable file
·131 lines (110 loc) · 4.8 KB
/
create
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
#!/bin/sh
DIR="$(pwd)"
PROJECT_NAME="$(basename $1)"
DB_NAME_PREFIX="$(echo "$PROJECT_NAME" | sed -r 's/[-]+/_/g')"
# Install rails if necessary
if ! gem list -i "^rails" > /dev/null 2>&1; then gem install rails; fi
# Generate empty Rails project with basic configuration
rails new $1 --rc=./.railsrc
# Copy all Rails static files
cp -R -L ./rails/src/. $1
# Go to project directory
cd $1
# Remove unused files
rm -rf ./config/credentials.yml.enc ./config/master.key
# Remove default ERB layouts
rm -rf ./app/views/layouts/*.erb
rm -rf ./vendor
# Replace placeholders with real project names
sed -i "s/##DB_NAME_PREFIX##/$DB_NAME_PREFIX/gi" ./config/database.yml
sed -i "s/##PROJECT_NAME##/$PROJECT_NAME/gi" ./.ruby-gemset
# Ask to use preferred database
read -r -p "Use PostgreSQL [1 - default] or MySQL [2] database? " DB_CHOICE
case "$DB_CHOICE" in
2)
sed -i "s/##DB_ADAPTER##/mysql2/gi" ./config/database.yml
sed -i "s/##DB_ENCODING##/utf8mb4/gi" ./config/database.yml
sed -i "s/##DB##/mysql2/gi" ./Gemfile
;;
*)
sed -i "s/##DB_ADAPTER##/postgresql/gi" ./config/database.yml
sed -i "s/##DB_ENCODING##/unicode/gi" ./config/database.yml
sed -i "s/##DB##/pg/gi" ./Gemfile
esac
# Ask to use UUIDs for primary keys - for PostgreSQL only
if [ -z "$DB_CHOICE" ] || [ $DB_CHOICE -ne 2 ]; then
read -r -p "Use UUID as type for primary keys? [Y/n] " UUID_CHOICE
case "$UUID_CHOICE" in
[nN][oO]|[nN])
sed -i "/##UUID##/d" ./config/initializers/generators.rb
;;
*)
sed -i -e "s/##UUID##/$(sed 's:/:\\/:g' $DIR/uuid/subs/generators.rb)/" ./config/initializers/generators.rb
cp -R -L $DIR/uuid/src/. ./
;;
esac
fi
# Ask to use rails-data-modeller
read -r -p "Add rails-data-modeller? [Y/n] " APPGEN_CHOICE
case "$APPGEN_CHOICE" in
[nN][oO]|[nN])
sed -i "/##RAILS_DATA_MODELLER##/d" ./Gemfile
sed -i "/##RAILS_DATA_MODELLER##/d" ./Rakefile
;;
*)
sed -i -e "s/##RAILS_DATA_MODELLER##/$(sed 's:/:\\/:g' $DIR/rails-data-modeller/subs/Gemfile)/" ./Gemfile
awk -v r="$(cat $DIR/rails-data-modeller/subs/Rakefile)" '{gsub(/##RAILS_DATA_MODELLER##/,r)}1' ./Rakefile > ./Rakefile.tmp && mv ./Rakefile.tmp ./Rakefile
cp -R -L $DIR/rails-data-modeller/src/. ./
;;
esac
# Ask to use Sidekiq
read -r -p "Use Sidekiq for background job processing? [Y/n] " SIDEKIQ_CHOICE
case "$SIDEKIQ_CHOICE" in
[nN][oO]|[nN])
sed -i "/##SIDEKIQ##/d" ./README.md
sed -i "/##SIDEKIQ##/d" ./config/application.rb
sed -i "/##SIDEKIQ##/d" ./Gemfile
;;
*)
awk -v r="$(cat $DIR/sidekiq/subs/README.md)" '{gsub(/##SIDEKIQ##/,r)}1' ./README.md > ./README.md.tmp && mv ./README.md.tmp ./README.md
awk -v r="$(cat $DIR/sidekiq/subs/application.rb)" '{gsub(/##SIDEKIQ##/,r)}1' ./config/application.rb > ./config/application.rb.tmp && mv ./config/application.rb.tmp ./config/application.rb
awk -v r="$(cat $DIR/sidekiq/subs/Gemfile)" '{gsub(/##SIDEKIQ##/,r)}1' ./Gemfile > ./Gemfile.tmp && mv ./Gemfile.tmp ./Gemfile
cp -R -L $DIR/sidekiq/src/. ./
;;
esac
# Ask to use Shrine
read -r -p "Use Shrine for file uploading? [Y/n] " SHRINE_CHOICE
case "$SHRINE_CHOICE" in
[nN][oO]|[nN])
sed -i "/##SHRINE##/d" ./README.md
sed -i "/##SHRINE##/d" ./config/routes.rb
sed -i "/##SHRINE##/d" ./config/application.rb
sed -i "/##SHRINE##/d" ./Gemfile
sed -i "/##SHRINE##/d" ./.env
;;
*)
awk -v r="$(cat $DIR/shrine/subs/README.md)" '{gsub(/##SHRINE##/,r)}1' ./README.md > ./README.md.tmp && mv ./README.md.tmp ./README.md
awk -v r="$(cat $DIR/shrine/subs/routes.rb)" '{gsub(/##SHRINE##/,r)}1' ./config/routes.rb > ./config/routes.rb.tmp && mv ./config/routes.rb.tmp ./config/routes.rb
awk -v r="$(cat $DIR/shrine/subs/application.rb)" '{gsub(/##SHRINE##/,r)}1' ./config/application.rb > ./config/application.rb.tmp && mv ./config/application.rb.tmp ./config/application.rb
awk -v r="$(cat $DIR/shrine/subs/Gemfile)" '{gsub(/##SHRINE##/,r)}1' ./Gemfile > ./Gemfile.tmp && mv ./Gemfile.tmp ./Gemfile
awk -v r="$(cat $DIR/shrine/subs/.env)" '{gsub(/##SHRINE##/,r)}1' ./.env > ./.env.tmp && mv ./.env.tmp ./.env
cp -R -L $DIR/shrine/src/. ./
;;
esac
# Install rubocop if necessary
if ! gem list -i "^rubocop$" > /dev/null 2>&1; then gem install rubocop; fi
if ! gem list -i "^rubocop-performance$" > /dev/null 2>&1; then gem install rubocop-performance; fi
if ! gem list -i "^rubocop-rails$" > /dev/null 2>&1; then gem install rubocop-rails; fi
if ! gem list -i "^rubocop-rspec$" > /dev/null 2>&1; then gem install rubocop-rspec; fi
# Fix Rubocop offences in Rails-generated files
rubocop -A
# Create staging environment from production
cp ./config/environments/production.rb ./config/environments/staging.rb
# Install gems
#bundle install
# Initialize git repo
git init
# Install all git hooks
./script/install-git-hooks.sh
# Return to this directory
cd $DIR