Last active
August 29, 2015 14:15
-
-
Save AhmedNadar/122e559a386af55bf5ba to your computer and use it in GitHub Desktop.
Rails Template
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
module RailsTemplate | |
module Gem | |
extend self | |
def use(name, options={}) | |
require_path = options[:require] || name | |
begin | |
require require_path | |
rescue LoadError | |
system "gem install #{name}" | |
::Gem.clear_paths | |
self.use name, options | |
end | |
end | |
end | |
class GitHub | |
Gem.use 'github_api' | |
Gem.use 'highline', require: 'highline/import' | |
Gem.use 'colorize' | |
def initialize | |
@api = login! | |
@account = choose_account! | |
end | |
def create_repo(name) | |
options = { name: name, auto_init: false } | |
options[:private] = agree("Make repo private?") | |
unless @account == @api.login | |
options[:org] = @account | |
options[:team_id] = choose_team.id | |
end | |
@api.repos.create options | |
rescue | |
puts "repo name taken, try again..." | |
create_repo ask "what would you like to name the repo?" | |
end | |
protected | |
def orgs | |
@orgs ||= @api.orgs.list.map(&:login) | |
end | |
def choose_account! | |
return @api.login if orgs.size < 1 | |
HighLine.choose do |menu| | |
menu.prompt = "Please choose an account".yellow | |
menu.choice @api.login | |
menu.choices *orgs | |
end | |
end | |
def teams_for_account | |
@teams_for_account ||= @api.orgs.teams.list(@account) | |
end | |
def choose_team | |
name = HighLine.choose do |menu| | |
menu.choices *teams_for_account.map(&:name) | |
end | |
teams_for_account.find { |team| team.name == name } | |
end | |
def login! | |
puts "\n" | |
# Get the Gems we need | |
# Login | |
login = ask("Enter your github username: ") | |
password = ask("Enter your github password: ") { |q| q.echo = '*' } | |
gh = Github.new login: login, password: password | |
# Confirm Login | |
gh.repos.find('ruby', 'ruby') | |
puts "\n" | |
gh | |
rescue ::Github::Error::Unauthorized | |
puts 'Invalid Credentials, try again...' | |
return login! | |
rescue ::Github::Error::Forbidden | |
puts 'Too many attempts, goodbye!' | |
end | |
end | |
end | |
def get(prompt) | |
yes?(prompt + ' (y/n) >') | |
end | |
RAILS_VERSION = "4.2.0" | |
#RUBY_VERSION = IO.read("#{File.dirname(__FILE__)}/../../.ruby-version").strip | |
file ".ruby-version", '2.2.0-dev' | |
VERSION = 'v1.0.1' | |
# General Gems | |
gem "sass-rails", "~> 5.0" | |
gem 'figaro' | |
gem 'high_voltage' | |
gem 'font-awesome-sass' | |
gem 'haml-rails' | |
gem 'will_paginate', '~> 3.0.6' | |
gem 'friendly_id' | |
gem 'pygments.rb' | |
gem 'redcarpet' | |
gem 'paperclip', '~> 4.2' | |
gem "normalize-rails", "~> 3.0.0" | |
gem "uglifier" | |
# Development Gems | |
gem_group :development do | |
gem "spring-commands-rspec" | |
gem 'meta_request' | |
end | |
# Development ad test Gems | |
gem_group :development, :test do | |
gem 'byebug' | |
gem 'web-console', '~> 2.0' | |
gem 'spring' | |
gem "awesome_print" | |
gem "bundler-audit", require: false | |
gem "dotenv-rails" | |
gem "factory_girl_rails" | |
gem "pry-rails" | |
gem "rspec-rails", "~> 3.1.0" | |
gem 'pry' | |
gem 'quiet_assets' | |
gem 'better_errors' | |
end | |
# Test Gems | |
gem_group :test do | |
gem "capybara-webkit", ">= 1.2.0" | |
gem "database_cleaner" | |
gem "launchy" | |
gem "shoulda-matchers", require: false | |
end | |
# Production Gems | |
gem_group :production do | |
gem 'rails_12factor', '0.0.3' | |
gem 'thin' | |
end | |
#Bootstrap or Bourbon? | |
if get('Would you like to use either Bootstrap or Bourbon?') | |
if yes?('Use Bootstrap?') | |
gem 'bootstrap-sass' | |
if get('Would you like to use Simple Form?') | |
gem 'simple_form' | |
generate('simple_form:install --bootstrap') | |
end | |
puts 'Creating application.scss' | |
file 'app/assets/stylesheets/application.scss', <<-CODE | |
@import 'bootstrap-sprockets'; | |
@import 'bootstrap'; | |
CODE | |
puts 'Linking to bootstrap files' | |
run('rm app/assets/stylesheets/application.css') | |
puts 'Removing old application.css file' | |
puts 'Removing old application.js file' | |
run('rm app/assets/javascripts/application.js') | |
puts 'Creating application.js' | |
file 'app/assets/javascripts/application.js', <<-CODE | |
//= require jquery | |
//= require bootstrap-sprockets | |
//= require jquery_ujs | |
//= require turbolinks | |
//= require_tree . | |
CODE | |
puts 'Adding bootstrap-sprockets to require' | |
else | |
gem "bourbon", "~> 4.1.0" | |
gem "neat", "~> 1.7.0" | |
gem 'bitters' | |
gem 'simple_form' | |
run ('rails generate simple_form:install') | |
puts 'Creating application.scss' | |
file 'app/assets/stylesheets/application.scss', <<-CODE | |
@import "normalize-rails"; | |
@import "bourbon"; | |
@import "base/grid-settings"; | |
@import "neat"; | |
@import "base/base"; | |
@import "refills/flashes"; | |
@import 'base/base'; | |
CODE | |
puts 'Removing old application.css' | |
run('rm app/assets/stylesheets/application.css') | |
puts 'Installing Bitters library' | |
inside('app/assets/stylesheets') do | |
run('bitters install') | |
end | |
puts 'Removing old _base.scss' | |
run('rm app/assets/stylesheets/base/_base.scss') | |
puts 'Creating _base.scss' | |
file 'app/assets/stylesheets/base/_base.scss', <<-CODE | |
@import "variables"; | |
@import "grid-settings"; | |
@import "buttons"; | |
@import "forms"; | |
@import "lists"; | |
@import "tables"; | |
@import "typography"; | |
CODE | |
end | |
end | |
# Update list of gems | |
run 'bundle install' | |
# Generate an rspec helper | |
# run "rails g rspec:install" | |
# | |
puts 'Installing rspec' | |
generate "rspec:install" | |
puts 'Installing simple_form' | |
generate 'simple_form:install' | |
puts 'Installing Figaro' | |
run "figaro install" | |
# Rename README | |
run "rm README.rdoc" | |
run "touch README.md" | |
file "README.md", <<-END | |
## Welcome to #{app_name.humanize} Application | |
### Some of the tools and Gems here: | |
+ Bourbon | |
+ Neat | |
+ HAML | |
+ Simple_form | |
+ Font Awesome | |
END | |
# Make HAML default Application Layout | |
layout = <<-LAYOUT | |
!!! | |
%html | |
%head | |
%title #{app_name.humanize} Application | |
= stylesheet_link_tag :all | |
= javascript_include_tag :defaults | |
= csrf_meta_tag | |
%body | |
= yield | |
LAYOUT | |
remove_file "app/views/layouts/application.html.erb" | |
create_file "app/views/layouts/application.html.haml", layout | |
file "notes.md", <<-END | |
### Application Proccess Notes | |
END | |
file ".gitignore", <<-END | |
config/database.yml | |
!.keep | |
*notes.md | |
*.DS_Store | |
*.swo | |
*.swp | |
/.bundle | |
/.env | |
/.foreman | |
/coverage/* | |
/db/*.sqlite3 | |
/log/* | |
/public/system | |
/tags | |
/tmp/* | |
END | |
file ".rspec", <<-END | |
--color | |
--require spec_helper | |
END | |
#file ".ruby-version", RUBY_VERSION | |
inside('config') do | |
run 'sed -i -E "s/username.*$/username:\ \<%=\ ENV[\'DB_USER\']\ %\>/g" database.yml' | |
end | |
# Generate a controller and static_pages | |
generate(:controller, "home", "index", "--no-assets", "--no-helper", "--no-view-specs") | |
# Default route | |
route "root to: 'home#index'" | |
# Finally, generate a database | |
rake "db:create db:migrate" | |
# Github | |
# Put everything under revision control | |
git :init | |
git add: "--all" | |
git commit: %Q{ -a -m 'Initial Commit [Generated by Ahmed Rails Template (#{VERSION})]'} | |
# Add to Github? This is what needs the first part | |
puts "\n" | |
if yes? "Would you like to push the project to github?".cyan | |
repo = RailsTemplate::GitHub.new.create_repo(app_name) | |
git remote: "add Github #{repo.ssh_url}" | |
git push: '-u Github master' | |
end | |
#after_bundle do | |
# if get('Would you like to create a new git repo and add everything to it?') | |
# git :init | |
# git add: '--all' | |
# git commit: "-a -m 'Initial Commit [Generated by Ahmed Rails Template (#{VERSION})]'" | |
# if get('Would you like to push your git repo to github?') | |
# run('hub create') | |
# git push: '-u origin master' | |
# if get('Would you like to open your newly created repo on github?') | |
# remote = `git remote -v` | |
# unless remote.empty? | |
# url_pieces = remote.scan(/github\.com:(.*)\.git/) | |
# url = "https://github.com/#{url_pieces.first.first}" | |
# run("open #{url}") | |
# end | |
# end | |
# end | |
# end | |
#Heroku | |
if get('Would you like to create a new Heroku repo?') | |
run('heroku create') | |
if get('Would you like to push your repo to Heroku') | |
git push: 'heroku master' | |
run('heroku run rake db:migrate') | |
end | |
end | |
# Success! | |
puts('Complete! Your new rails app is finished and ready to go!') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment