Skip to content

Instantly share code, notes, and snippets.

@jwaldrip
Last active December 17, 2015 02:48
Show Gist options
  • Save jwaldrip/5538342 to your computer and use it in GitHub Desktop.
Save jwaldrip/5538342 to your computer and use it in GitHub Desktop.
A rails 3.2.x/4.x template, with GitHub integration.
--database=postgresql
--skip-test-unit
--skip-bundle
--template=https://gist.github.com/jwaldrip/5538342/raw/rails-template.rb
require 'rubygems'
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.prompt 'Choose what team owns this repo'
menu.choices *teams_for_account.map(&:name)
end
teams_for_account.find { |team| team.name == name }
end
def login!
puts "\n"
# 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
# Production Gems
gem "oj"
# Development Gems
gem_group :development, :test do
gem 'factory_girl_rails', '~> 4.2.1'
gem 'guard', '~> 1.8.0'
gem 'guard-bundler', '~> 1.0.0'
gem 'guard-rspec', '~> 3.0.0'
gem 'pry'
gem 'pry-debugger'
gem 'pry-remote'
gem 'rb-inotify', :require => false
gem 'rb-fsevent', :require => false
gem 'rspec-rails', '~> 2.0'
gem 'terminal-notifier-guard'
gem 'thin'
end
run "bundle install"
run "createuser #{app_name} --superuser" if options[:database] == 'postgresql'
run "rake db:create"
run "rails generate rspec:install"
run "guard init rspec bundler"
git :init
git add: '.'
git commit: %Q{ -m 'Initial commit' }
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 origin #{repo.ssh_url}"
git push: '-u origin master'
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment