Skip to content

Instantly share code, notes, and snippets.

@dugancathal
Created December 4, 2012 05:37
Show Gist options
  • Save dugancathal/4201015 to your computer and use it in GitHub Desktop.
Save dugancathal/4201015 to your computer and use it in GitHub Desktop.
A simple Thor script for generating a Twitter Bootstrap Rails app with some basic options
#!/usr/bin/env ruby
require 'thor'
class BootstrapApp < Thor
include Thor::Actions
desc "create", "Create a Twitter Bootstrap app"
method_option :database, aliases: '-d', type: :string, desc: 'The database adapter to use: sqlite3 oracle mysql2 etc.', default: 'sqlite3'
method_option :test, aliases: '-t', type: :string, desc: 'The test framework to use: test-unit rspec-rails etc.', default: 'rspec-rails'
method_option :verbose, aliases: '-v', type: :boolean, desc: 'Verbose output', default: true
method_option :simple_form, aliases: '-s', type: :boolean, desc: 'Use Simple Form or not', default: true
method_option :haml, aliases: '-h', type: :boolean, desc: 'Use Haml or not', default: true
method_option :datatables, aliases: '-j', type: :boolean, desc: 'Use AjaxDatatablesRails', default: false
def create(app_name)
say "Creating app: #{app_name}"
run rails_creation(app_name, options)
inside app_name, verbose: options[:verbose] do
insert_into_file "Gemfile", after: /jquery-rails.*\n/ do
gemfile_additions
end
run_generators
end
end
private
def self.source_root
File.dirname(__FILE__)
end
def not_test_unit? option
option !~ /unit/i
end
def rails_creation name, options
"rails new #{name} -d #{options[:database]} #{'--skip-test-unit' if not_test_unit?(options[:test])} --skip-bundle"
end
def run_generators
run "bundle install"
run "rails g rspec:install" if options[:test] =~ /rspec/
run "rails g simple_form:install --bootstrap" if options[:simple_form]
run "rails g bootstrap:install"
end
def gemfile_additions
gemfile = <<-GEMS
gem 'twitter-bootstrap-rails'
#{"gem 'haml-rails'" if options[:haml]}
#{"gem 'simple_form'" if options[:simple_form]}
#{"gem 'jquery-datatables-rails'" if options[:datatables]}
#{"gem 'ajax-datatables-rails'" if options[:datatables]}
group :test, :development do
#{"gem '#{options[:test]}'" if not_test_unit?(options[:test])}
end
GEMS
gemfile.gsub(/^#{gemfile[/\A\s*/]}/, '')
end
end
BootstrapApp.start
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment