Last active
August 29, 2015 14:01
-
-
Save neves/c6180ddda37377a8613a to your computer and use it in GitHub Desktop.
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
# shell functions to setup rails features | |
# https://gist.github.com/c6180ddda37377a8613a | |
# rt == rails_template | |
# source rails-template.sh | |
# rails new appname -B | |
# cd appname | |
rt_init() { | |
git init | |
git add . | |
git commit -m "init" | |
} | |
rt_ruby_version() { | |
sed -i '' "2s/.*/ruby '2.1.2'/" Gemfile | |
git add Gemfile | |
git commit -m "ruby 2.1.2" | |
} | |
rt_sqlite() { | |
sed -i '' "s/gem 'sqlite3'/gem 'sqlite3', group: [:development, :test]/" Gemfile | |
git add Gemfile | |
git commit -m "sqlite3 only on group: :development, :test" | |
} | |
rt_ignore_idea() { | |
echo ".idea/" >> .gitignore | |
git add .gitignore | |
git commit -m ".gitignore .idea/" | |
} | |
rt_ignore_schema() { | |
echo "db/schema.rb" >> .gitignore | |
git add .gitignore | |
git commit -m ".gitignore db/schema.rb" | |
} | |
rt_rm_locale_en() { | |
git rm config/locales/en.yml | |
git commit -m "rm locales/en.yml" | |
} | |
rt_clean_routes_comments() { | |
# remove comments | |
sed -i '' '/ #/d' config/routes.rb | |
# remove empty lines | |
sed -i '' '/^\s*$/d' config/routes.rb | |
git add config/routes.rb | |
git commit -m "clean routes comments" | |
} | |
rt_timezone() { | |
sed -i '' "17s/.*/ config.time_zone = 'Brasilia'/" config/application.rb | |
git add config/application.rb | |
git commit -m "config.time_zone = 'Brasilia' in config/application.rb" | |
} | |
rt_editor_config() { | |
cat <<'EOF' >| .editorconfig | |
# editorconfig.org | |
root = true | |
[*] | |
indent_style = space | |
indent_size = 2 | |
end_of_line = lf | |
charset = utf-8 | |
trim_trailing_whitespace = true | |
insert_final_newline = true | |
EOF | |
git add .editorconfig | |
git commit -m "@file .editorconfig" | |
} | |
rt_local_mailer() { | |
sed -i '' "s|^end$| config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }|" config/environments/development.rb | |
echo 'end' >> config/environments/development.rb | |
git add config/environments/development.rb | |
git commit -m "local mailer" | |
} | |
rt_parameters() { | |
sed -i '' "s|^end$| config.action_controller.action_on_unpermitted_parameters = :raise|" config/environments/development.rb | |
echo 'end' >> config/environments/development.rb | |
git add config/environments/development.rb | |
git commit -m "raise exception when pass unpermitted parameters to controller" | |
} | |
rt_spring() { | |
bundle | |
spring binstub --all | |
cat <<'EOF' >>| .envrc | |
# adicionar bin/ ao path, para utilizar spring | |
# brew install direnv | |
export PATH=$PWD/bin:$PATH | |
EOF | |
direnv allow | |
git add bin/ .envrc | |
git commit -m "spring" | |
} | |
rt_metatags() { | |
cat <<'EOF' >| app/views/layouts/_meta.html.erb | |
<%# https://gist.github.com/kevinSuttle/1997924 -%> | |
<meta charset="utf-8" /> | |
<meta name="description" content=""> | |
<meta name="keywords" content="rails,html5,bootstrap,mobile"> | |
<meta name="author" content="Marcos Neves"> | |
<%# adaptar tela ao dispositivo -%> | |
<meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, target-densitydpi=device-dpi" /> | |
<%# fullscreen -%> | |
<meta name="apple-mobile-web-app-capable" content="yes"> | |
<%# status bar translucent -%> | |
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent"> | |
<%# desligar detecção de número de telefones na página -%> | |
<meta name="format-detection" content="telephone=no"> | |
EOF | |
sed -i '' 's|<head>|<head>\'$'\n <%= render \'layouts/meta\' %>|' app/views/layouts/application.html.erb | |
git add app/views/layouts/ | |
git commit -m "metatags" | |
} | |
rt_scaffold() { | |
sed -i '' 's|</head>| <%= javascript_include_tag \''//rawgithub.com/neves/9598842/raw/rails-scaffold-bootstrap.js\'' %>\'$'\n</head>|' app/views/layouts/application.html.erb | |
git add app/views/layouts/application.html.erb | |
git commit -m "scaffold" | |
} | |
rt_users() { | |
rails g scaffold user email:string:uniq --no-assets --no-helper --no-jbuilder --no-test-framework | |
sed -i '' "s|^$| root 'users#index'|" config/routes.rb | |
git add . | |
git reset Gemfile.lock | |
git commit -m "users scaffold" | |
} | |
rt_assets() { | |
sed -i '' '2s|^|source \''https://rails-assets.org\''\'$'\n|' Gemfile | |
git add Gemfile | |
git commit -m "add rails-assets.org" | |
} | |
rt_thin() { | |
echo "gem 'thin', require: false, group: :development" >> Gemfile | |
git add Gemfile | |
git commit -m "thin server" | |
} | |
rt_locale_br() { | |
sed -i '' "21s/.*/ config.i18n.default_locale = :'pt-BR'/" config/application.rb | |
cat <<'EOF' >> Gemfile | |
# https://github.com/svenfuchs/rails-i18n#configuration | |
gem 'rails-i18n' | |
EOF | |
git add config/application.rb Gemfile | |
git commit -m "config.i18n.default_locale = :'pt-BR' in config/application.rb" | |
} | |
rt_mysql() { | |
echo 'Digite o nome do banco:' | |
read banco | |
echo "gem 'mysql2', group: :development" >> Gemfile | |
cat <<EOF >| config/database.yml | |
default: &default | |
adapter: sqlite3 | |
development: | |
<<: *default | |
database: db/development.sqlite3 | |
development: | |
adapter: mysql2 | |
username: root | |
database: $banco | |
test: | |
<<: *default | |
database: db/test.sqlite3 | |
EOF | |
git add config/database.yml Gemfile | |
git commit -m "mysql" | |
} | |
rt_bootstrap() { | |
echo "gem 'bootstrap-sass'" >> Gemfile | |
wget -O public/bootstrap-3.2.0.css http://netdna.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.css | |
cat <<'EOF' >>| app/assets/stylesheets/bootstrap-custom.css.scss | |
// permite sobrescrever variáveis antes de importar o bootstrap | |
$body-bg: #fdfdfd; | |
@import "bootstrap-sprockets"; | |
@import 'bootstrap'; | |
h1 { | |
@extend .text-center; | |
color: $text-muted; // utilizar variáveis | |
} | |
EOF | |
sed -i '' 's|^//= require turbolinks$|//= require bootstrap-sprockets\'$'\n//= require turbolinks|g' app/assets/javascripts/application.js | |
git add app/assets/javascripts/application.js | |
git add app/assets/stylesheets/bootstrap-custom.css.scss | |
git add public/bootstrap-3.2.0.css | |
git add Gemfile | |
git commit -m "@gem bootstrap-sass" | |
} | |
rt_slim() { | |
echo "gem 'slim-rails'" >> Gemfile | |
sed -i '' "s|^end$| Slim::Engine.set_default_options pretty: true # Gerar o html formatado|" config/environments/development.rb | |
echo 'end' >> config/environments/development.rb | |
git add config/environments/development.rb | |
git add Gemfile | |
git commit -m "@gem slim-rails" | |
} | |
rt_simpleform() { | |
cat <<'EOF' >> Gemfile | |
gem 'simple_form' | |
gem 'simple_form_bootstrap3' | |
EOF | |
bundle | |
rails g simple_form:install | |
rm -f config/locales/simple_form.en.yml | |
git add Gemfile | |
rm -f config/initializers/simple_form.rb | |
cat <<'EOF' >| lib/templates/slim/scaffold/_form.html.slim | |
= horizontal_form_for(@<%= singular_table_name %>) do |f| | |
= f.error_notification | |
<%- attributes.each do |attribute| -%> | |
= f.<%= attribute.reference? ? :association : :input %> :<%= attribute.name %> | |
<%- end -%> | |
.form-group | |
.col-sm-offset-3.col-md-offset-3.col-lg-offset-2.col-sm-9.col-md-9.col-lg-10 | |
= f.button :submit | |
EOF | |
git add lib/templates/ | |
git commit -m "@gem simple_form + bootstrap3" | |
} | |
rt_bootstrap_helpers() { | |
echo '# http://fullscreen.github.io/bh/' >> Gemfile | |
echo "gem 'bh'" >> Gemfile | |
git add Gemfile | |
git commit -m "@gem bh" | |
} | |
rt_heroku() { | |
mkdir -p gemfiles | |
cat <<'EOF' >> Gemfile | |
group :production do | |
gem 'rails_12factor' # https://devcenter.heroku.com/articles/getting-started-with-rails4#heroku-gems | |
gem 'pg' # https://devcenter.heroku.com/articles/getting-started-with-rails4#use-postgres | |
gem 'unicorn' # https://devcenter.heroku.com/articles/getting-started-with-rails4#webserver | |
end | |
EOF | |
cat <<'EOF' > Procfile | |
web: bundle exec unicorn -p $PORT -c ./config/unicorn.rb | |
EOF | |
cat <<'EOF' >| config/unicorn.rb | |
worker_processes Integer(ENV["WEB_CONCURRENCY"] || 3) | |
timeout 15 | |
preload_app true | |
before_fork do |server, worker| | |
Signal.trap 'TERM' do | |
puts 'Unicorn master intercepting TERM and sending myself QUIT instead' | |
Process.kill 'QUIT', Process.pid | |
end | |
defined?(ActiveRecord::Base) and | |
ActiveRecord::Base.connection.disconnect! | |
end | |
after_fork do |server, worker| | |
Signal.trap 'TERM' do | |
puts 'Unicorn worker intercepting TERM and doing nothing. Wait for master to send QUIT' | |
end | |
defined?(ActiveRecord::Base) and | |
ActiveRecord::Base.establish_connection | |
end | |
EOF | |
git add Procfile Gemfile config/unicorn.rb | |
git commit -m "@file Procfile @gem rails_12factor, pg, unicorn" | |
} | |
rt_tool_bump() { | |
cat <<'EOF' >> Gemfile | |
# https://github.com/gregorym/bump#usage | |
gem 'bump', group: :development, require: false | |
EOF | |
git add Gemfile | |
git commit -m "@tool bump" | |
bundle | |
} | |
rt_version() { | |
cat <<'EOF' >> lib/version.rb | |
module MainApp | |
VERSION = '0.0.0' | |
end | |
EOF | |
sed -i '' "3s/.*/require 'version'/" config/environment.rb | |
git add lib/version.rb config/environment.rb | |
git commit -m "add version.rb" | |
bump minor --tag --no-bundle | |
} | |
rt_basic() { | |
rt_init | |
rt_ruby_version | |
rt_sqlite | |
rt_ignore_schema | |
rt_ignore_idea | |
rt_rm_locale_en | |
rt_clean_routes_comments | |
rt_timezone | |
rt_local_mailer | |
rt_parameters | |
rt_spring | |
rt_metatags | |
rt_scaffold | |
rt_users | |
rt_assets | |
rt_thin | |
rt_locale_br | |
rt_bootstrap | |
rt_slim | |
rt_simpleform | |
rt_heroku | |
rt_tool_bump | |
rt_version | |
} | |
# gemfiles inside Gemfile ------------------------------------------------------------------------------------------------------------- | |
# gemfiles EXTERNAL ------------------------------------------------------------------------------------------------------------- | |
return | |
rt_gemfiles() { | |
echo "Dir.glob('gemfiles/**/*.rb') {|f| eval_gemfile f}" >> Gemfile | |
mkdir gemfiles | |
touch gemfiles/.keep | |
git add Gemfile gemfiles/ | |
git commit -m "autoload gemfiles" | |
} | |
rt_thin() { | |
cat <<'EOF' >| gemfiles/thin.rb | |
gem 'thin', require: false, group: :development | |
EOF | |
git add gemfiles/thin.rb | |
git commit -m "thin server" | |
} | |
rt_tool_bump() { | |
mkdir -p gemfiles/tools | |
cat <<'EOF' >>| gemfiles/tools/bump.rb | |
# https://github.com/gregorym/bump#usage | |
gem 'bump', group: :development, require: false | |
EOF | |
git add gemfiles/tools/bump.rb | |
git commit -m "@tool bump" | |
} | |
rt_mysql() { | |
echo "gem 'mysql2', group: :development" >| gemfiles/mysql.rb | |
cat <<'EOF' >| config/database.yml | |
default: &default | |
adapter: sqlite3 | |
development: | |
<<: *default | |
database: db/development.sqlite3 | |
development: | |
adapter: mysql2 | |
username: root | |
database: test | |
test: | |
<<: *default | |
database: db/test.sqlite3 | |
EOF | |
git add config/database.yml gemfiles/ | |
git commit -m "mysql" | |
} | |
rt_locale_br() { | |
sed -i '' "21s/.*/ config.i18n.default_locale = :'pt-BR'/" config/application.rb | |
mkdir -p gemfiles | |
cat <<'EOF' >| gemfiles/rails-i18n.rb | |
# https://github.com/svenfuchs/rails-i18n#configuration | |
gem 'rails-i18n' | |
EOF | |
git add config/application.rb gemfiles/ | |
git commit -m "config.i18n.default_locale = :'pt-BR' in config/application.rb" | |
} | |
rt_bootstrap() { | |
cat <<'EOF' >>| Gemfile | |
gem 'bootstrap-sass' | |
EOF | |
wget -O public/bootstrap-3.2.0.css http://netdna.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.css | |
cat <<'EOF' >>| app/assets/stylesheets/bootstrap-custom.css.scss | |
// permite sobrescrever variáveis antes de importar o bootstrap | |
$body-bg: #fdfdfd; | |
@import "bootstrap-sprockets"; | |
@import 'bootstrap'; | |
h1 { | |
@extend .text-center; | |
color: $text-muted; // utilizar variáveis | |
} | |
EOF | |
sed -i '' 's|^//= require turbolinks$|//= require bootstrap-sprockets\'$'\n//= require turbolinks|g' app/assets/javascripts/application.js | |
git add app/assets/javascripts/application.js | |
git add app/assets/stylesheets/bootstrap-custom.css.scss | |
git add public/bootstrap-3.2.0.css | |
git add gemfiles/bootstrap-sass.rb | |
git commit -m "@gem bootstrap-sass" | |
} | |
rt_slim() { | |
cat <<'EOF' >| gemfiles/slim-rails.rb | |
gem 'slim-rails' | |
EOF | |
sed -i '' "s|^end$| Slim::Engine.set_default_options pretty: true # Gerar o html formatado|" config/environments/development.rb | |
echo 'end' >> config/environments/development.rb | |
git add config/environments/development.rb | |
git add gemfiles/slim-rails.rb | |
git commit -m "@gem slim-rails" | |
} | |
rt_simpleform() { | |
cat <<'EOF' >| gemfiles/simple_form.rb | |
gem 'simple_form' | |
gem 'simple_form_bootstrap3' | |
EOF | |
bundle | |
rails g simple_form:install | |
rm -f config/locales/simple_form.en.yml | |
git add lib/templates/ | |
git add gemfiles/simple_form.rb | |
git add config/initializers/simple_form.rb | |
git commit -m "simple_form + bootstrap3" | |
} | |
rt_heroku() { | |
mkdir -p gemfiles | |
cat <<'EOF' >| gemfiles/heroku.rb | |
group :production do | |
gem 'rails_12factor' # https://devcenter.heroku.com/articles/getting-started-with-rails4#heroku-gems | |
gem 'pg' # https://devcenter.heroku.com/articles/getting-started-with-rails4#use-postgres | |
gem 'unicorn' # https://devcenter.heroku.com/articles/getting-started-with-rails4#webserver | |
end | |
EOF | |
cat <<'EOF' > Procfile | |
web: bundle exec unicorn -p $PORT -c ./config/unicorn.rb | |
EOF | |
cat <<'EOF' >| config/unicorn.rb | |
worker_processes Integer(ENV["WEB_CONCURRENCY"] || 3) | |
timeout 15 | |
preload_app true | |
before_fork do |server, worker| | |
Signal.trap 'TERM' do | |
puts 'Unicorn master intercepting TERM and sending myself QUIT instead' | |
Process.kill 'QUIT', Process.pid | |
end | |
defined?(ActiveRecord::Base) and | |
ActiveRecord::Base.connection.disconnect! | |
end | |
after_fork do |server, worker| | |
Signal.trap 'TERM' do | |
puts 'Unicorn worker intercepting TERM and doing nothing. Wait for master to send QUIT' | |
end | |
defined?(ActiveRecord::Base) and | |
ActiveRecord::Base.establish_connection | |
end | |
EOF | |
git add Procfile gemfiles/ config/unicorn.rb | |
git commit -m "@file Procfile @gem rails_12factor, pg, unicorn" | |
} | |
rt_devise() { | |
cat <<'EOF' >| gemfiles/devise.rb | |
gem 'devise' | |
gem 'devise-i18n' | |
EOF | |
bundle | |
rails g devise:install | |
sed -i '' "s| config.password_length = 8..128| config.password_length = Rails.env.production? ? 8..128 : 1..128|" config/initializers/devise.rb | |
rails g devise user | |
cat <<'EOF' >| db/seeds.rb | |
unless User.exists? | |
User.create!(email: 'admin@admin.com', | |
password: 'a', | |
password_confirmation: 'a').confirm! | |
end | |
EOF | |
rm -f config/locales/devise.en.yml | |
git add gemfiles/devise.rb | |
git add db/migrate/*_devise_to_users.rb | |
git add config/initializers/devise.rb | |
git add app/models/user.rb | |
git add config/routes.rb | |
git add db/seeds.rb | |
git commit -m "add devise to users" | |
} | |
rt_activeadmin() { | |
cat <<'EOF' >| gemfiles/activeadmin.rb | |
gem 'devise' | |
gem 'devise-i18n' | |
gem 'activeadmin', github: 'activeadmin' | |
EOF | |
bundle | |
rails g active_admin:install | |
rake db:migrate | |
git add . | |
git reset Gemfile.lock | |
git commit -m "ActiveAdmin install/setup" | |
} | |
rt_tool_guard_livereload() { | |
cat <<'EOF' >>| Guardfile | |
# https://github.com/guard/guard#readme | |
# verificar se nao existe outra instancia rodando em outro projeto | |
guard :livereload do | |
watch %r{app/views/.+\.(erb|haml|slim)$} | |
watch %r{app/helpers/.+\.rb} | |
watch %r{public/.+\.(css|js|html)} | |
watch %r{config/locales/.+\.yml} | |
watch %r{config/.+_navigation\.rb} | |
# Rails Assets Pipeline | |
watch(%r{(app|vendor)(/assets/\w+/(.+\.(css|js|html|png|jpg))).*}) { |m| "/assets/#{m[3]}" } | |
end | |
EOF | |
mkdir -p gemfiles/tools | |
cat <<'EOF' > gemfiles/tools/guard-livereload.rb | |
# https://chrome.google.com/webstore/detail/livereload/jnihajbhpnppcggbcgedagnkighmdlei | |
gem 'guard-livereload', group: :development, require: false # https://github.com/guard/guard-livereload#install | |
EOF | |
git add Guardfile gemfiles/tools/guard-livereload.rb | |
git commit -m "@file Guardfile" | |
} | |
rt_simple_navigation() { | |
mkdir -p gemfiles | |
cat <<'EOF' >| gemfiles/simple-navigation.rb | |
# https://github.com/codeplant/simple-navigation/wiki#table-of-contents | |
gem 'simple-navigation' | |
gem 'simple_navigation_renderers' # melhor suporte para bootstrap3 | |
EOF | |
bundle | |
rails g navigation_config | |
git add gemfiles/simple-navigation.rb | |
git add config/navigation.rb | |
git commit -m "simple-navigation + bootstrap3" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment