Skip to content

Instantly share code, notes, and snippets.

@nouphet
Forked from chsh/facebook-app-template.rb
Created August 21, 2012 17:18
Show Gist options
  • Save nouphet/3417486 to your computer and use it in GitHub Desktop.
Save nouphet/3417486 to your computer and use it in GitHub Desktop.
Rails3 template to generate facebook app.
# facebootstrap: Rails3 template
app_h_name = app_name.gsub(/_/, '-')
app_c_name = app_name.classify
ruby_version = '1.9.3-p194' # Specify your ruby version.
# Specify your git repo path
repository_path = "git@github.com:chsh/#{app_h_name}.git"
target_branch = 'master' # or develop or ...
deploy_server = 'Specify your deploy server FQDN.'
target_mode = 'staging' # or 'live' or ...
# Specify additional paths for deploy server.
additional_path = '' # e.g. '/usr/pgsql-9.1/bin'
# codeplane = {
# username: '',
# api_key: ''
# }
# db_config = {
# user: '',
# password: ''
# }
# some functions
def get_ssl(url)
open(url,"r",{:ssl_verify_mode=>OpenSSL::SSL::VERIFY_NONE}).read
end
def get_ssl_with_keys(url, keys = {})
content = get_ssl(url)
content.gsub(/@@(.+?)@@/) do |match|
keys[$1.downcase.to_sym]
end
end
git :init
append_file '.gitignore', <<-EOL
/config/database.yml
/config/settings/production.rb
/config/settings/development.rb
/config/*.production
/public/system
EOL
gem 'therubyracer', group: :assets
gem 'bootstrap-sass', group: :assets
gem 'font-awesome-sass-rails'
gem 'slim-rails'
gem 'formtastic',
git: 'git://github.com/justinfrench/formtastic.git',
branch: '2.1-stable'
gem 'formtastic-bootstrap',
git: 'https://github.com/cgunther/formtastic-bootstrap.git',
branch: 'bootstrap2-rails3-2-formtastic-2-1'
gem 'configuration'
gem 'devise'
gem 'omniauth'
gem 'omniauth-facebook'
gem 'koala'
gem 'unicorn'
gem 'capistrano'
gem 'thin', group: :development
require 'open-uri'
# generate capistrano settings.
create_file 'Capfile' do
<<EOL
load 'deploy'
load 'deploy/assets'
Dir['vendor/gems/*/recipes/*.rb','vendor/plugins/*/recipes/*.rb'].each { |plugin| load(plugin) }
load 'config/deploy' # remove this line to skip loading any of the default tasks
EOL
end
repo_prefix = 'https://raw.github.com/chsh/facebootstrap-for-template/master'
create_file 'config/deploy.rb' do
get_ssl_with_keys "#{repo_prefix}/config/deploy.rb.template", {
app_name: app_h_name,
repository_path: repository_path,
target_branch: target_branch,
deploy_server: deploy_server,
target_mode: target_mode, # or 'live'
ruby_version: ruby_version,
additional_path: additional_path
}
end
run 'mkdir config/deploy'
## install deploy modules.
# config symlink module
create_file 'config/deploy/config.rb' do
get_ssl_with_keys "#{repo_prefix}/config/deploy/config.rb.template", {
yaml_files: 'database.yml'
}
end
# unicorn.module
create_file 'config/deploy/unicorn.rb' do
get_ssl_with_keys "#{repo_prefix}/config/deploy/unicorn.rb.template"
end
# unicorn server config
create_file 'config/unicorn.rb' do
get_ssl_with_keys "#{repo_prefix}/config/unicorn.rb.template"
end
create_file '.rvmrc' do
"rvm use #{ruby_version} --create"
end
run "bundle"
## setup database.
run 'cp config/database.yml config/database.yml.example'
if defined? db_config
remove_file 'config/database.yml'
create_file 'config/database.yml' do
<<EOL
development:
adapter: postgresql
encoding: unicode
database: #{app_name}_d
pool: 5
username: #{db_config[:user]}
password: #{db_config[:password]}
test:
adapter: postgresql
encoding: unicode
database: #{app_name}_t
pool: 5
username: #{db_config[:user]}
password: #{db_config[:password]}
EOL
end
end
# add lib/utils to auto load paths.
inject_into_file 'config/application.rb', after: /# config\.autoload_paths.+?\n/ do
<<EOL
config.autoload_paths += %W(\#{config.root}/lib/utils)
EOL
end
remove_file 'app/views/layouts/application.html.erb'
git add: '.', commit: '-m "Initial commit"'
rake 'db:create'
###### create devise related files
generate 'devise:install'
generate 'devise', 'User'
run 'mkdir app/controllers/users'
# omniauth_callbacks
%w(
app/assets/stylesheets/layouts.css.scss
app/controllers/users/omniauth_callbacks_controller.rb
app/controllers/users/sessions_controller.rb
app/views/layouts/application.html.slim
app/models/facebook.rb
config/initializers/configuration.rb
config/initializers/formtastic.rb
config/initializers/fix_openssl_verification_error.rb
config/settings/application.rb
config/settings/development.rb
config/settings/production.rb
lib/utils/config_or_env.rb
lib/utils/hash_with_method.rb
).each do |path|
create_file path do
get_ssl_with_keys "#{repo_prefix}/#{path}", {
app_c_name: app_c_name
}
end
end
gsub_file 'config/routes.rb', /devise_for :users/ do |match|
<<EOL
devise_for :users, controllers: {
omniauth_callbacks: "users/omniauth_callbacks",
sessions: 'users/sessions'
}
EOL
end
# replace auto-generated migration file which has unknown name.
run "wget -q -O db/migrate/*.rb #{repo_prefix}/db/migrate/devise_create_users.rb"
remove_file 'app/models/user.rb'
create_file 'app/models/user.rb' do
get_ssl_with_keys "#{repo_prefix}/app/models/user.rb"
end
inject_into_file 'config/initializers/devise.rb', before: /^end/ do
<<EOL
config.omniauth :facebook, Facebook::Config.app_id, Facebook::Config.app_secret,
scope: Facebook::Config.app_scope
EOL
end
git add: '.', commit: '-m "Add authentication related files."'
## remote repository integration
if defined? codeplane
require 'codeplane'
Codeplane.configure do |config|
config.username = codeplane[:username]
config.api_key = codeplane[:api_key]
end
cp = Codeplane::Client.new
repo = cp.repositories.create(name: app_h_name)
if repo.id.nil?
puts "CodePlane repository:#{app_h_name} creation failed."
else
puts "CodePlane repository:#{app_h_name} created"
run "git remote add origin #{repository_path}"
git push: 'origin master'
end
end
if defined? gitflow
git flow: 'init -d'
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment