Created
January 4, 2013 19:31
-
-
Save jmgarnier/4455232 to your computer and use it in GitHub Desktop.
bootstrap a Rails project
This file contains hidden or 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
#!/usr/bin/env ruby | |
# encoding: utf-8 | |
# This command will help a new developer to contribute, see github http://zachholman.com/talk/ruby-patterns | |
# GOTO line 61 for the script itself | |
# Just for fun, a micro RSpec framework to run and document bootstrap process ;) | |
module Kernel | |
def bootstrap(description = "project", &block) | |
examples = Bootstrap::Dsl.new.parse(description, block) | |
examples.execute | |
end | |
end | |
class Object | |
def should | |
self | |
end | |
end | |
module Bootstrap | |
class Dsl | |
def initialize | |
@examples = [] | |
end | |
def parse(description, block) | |
self.instance_eval(&block) | |
ExamplesRunner.new(description, @examples) | |
end | |
def it(specification, &block) | |
@examples << Example.new(specification, &block) | |
end | |
end | |
class Example | |
attr_reader :specification, :block | |
def initialize(specification, &block) | |
@specification, @block = specification, block | |
end | |
end | |
class ExamplesRunner | |
def initialize(description, examples) | |
@description = description | |
@examples = examples | |
end | |
def execute | |
puts "Bootstraping #{@description} ..." | |
@examples.each do |example| | |
print "- #{example.specification}" | |
unless example.block | |
puts " ✔" | |
next | |
end | |
result = example.block.call | |
if result | |
puts " ✔" | |
else | |
puts " FAILED" | |
exit | |
end | |
end | |
end | |
end | |
end | |
require 'yaml' | |
bootstrap do | |
it "you might want to use rvm and a gemset to automate your development process" do | |
check_dependency("rvm", :help => "curl -L https://get.rvm.io | bash -s stable --ruby") | |
true.should == true # optionnal dependency | |
end | |
it "ruby >= 1.9.3" do | |
RUBY_VERSION.should >= "1.9.3" | |
end | |
it "bundler" do | |
check_dependency("bundle", :help => "gem install bundler").should == true | |
end | |
it "homebrew" do | |
check_dependency("brew", :help => "ruby -e \"$(curl -fsSkL raw.github.com/mxcl/homebrew/go)\"").should == true | |
end | |
it "mysql" do | |
check_dependency("mysql", :help => "Run 'brew install mysql', see http://mxcl.github.com/homebrew/" | |
).should == true | |
end | |
it "qmake for building capybara-webkit" do | |
check_dependency("qmake" , :help => "Run 'brew install qt' on Mac and on linux, see https://github.com/thoughtbot/capybara-webkit/wiki/Installing-Qt-and-compiling-capybara-webkit" | |
).should == true | |
end | |
it "phantomjs for poltergeist js driver" do | |
check_dependency("phantomjs" , :help => "Run 'brew install phantomjs' on Mac" | |
).should == true | |
end | |
it "gems" do | |
run("bundle").should == true | |
end | |
it "creates a config/database.yml with user 'root' and no password." do | |
database_yml = File.join('config', 'database.yml') | |
create_file_unless_exists database_yml, <<DBCONFIG | |
development: &DEVELOPMENT | |
adapter: mysql2 | |
database: testing-rails-workshop_development | |
username: root | |
password: | |
encoding: utf8 | |
test: &TEST | |
<<: *DEVELOPMENT | |
database: testing-rails-workshop_test<%= ENV['TEST_ENV_NUMBER'] %> | |
cucumber: | |
<<: *TEST | |
DBCONFIG | |
File.exist?(database_yml).should == true | |
end | |
it "checks it can connect to mysql with configuration in database.yml" do | |
# %x{} returns the standard output of running cmd in a subshell | |
%x{#{mysql_command_line} --execute 'SELECT 1;'}.include?("1").should == true | |
end | |
it "databases creation: rake db:create:all" do | |
rake("db:create:all").should == true | |
end | |
#it "parallel_test databases creation: rake parallel:create" do | |
# rake("parallel:create").should == true | |
#end | |
it "databases migration: rake db:migrate" do | |
rake("db:migrate").should == true | |
rake("db:test:load").should == true | |
# For parallel_tests | |
#rake("parallel:prepare").should == true | |
end | |
it "run specs: rake" do | |
rake.should == true | |
end | |
it "seeds development DB with an user [email protected] and password 'please'" do | |
rake('db:seed').should == true | |
end | |
# Helper methods | |
def rake(task = "") | |
run "rake #{task} 2>&1" # does not print to the console as well while the command is executing | |
end | |
def run(command) | |
output = %x{#{command}} | |
success = $?.exitstatus == 0 | |
puts output unless success | |
success | |
end | |
def check_dependency(executable, args) | |
present = present?(executable) | |
puts(" Please run #{args[:help]}") unless present | |
present | |
end | |
def present?(executable) | |
`which #{executable}`.include?(executable) | |
end | |
def create_file_unless_exists(file, content) | |
return if File.exist?(file) | |
File.open(file, 'w') do |f| | |
f.write content | |
end | |
end | |
# DB helpers | |
def mysql_command_line | |
"mysql #{mysql_credentials}" | |
end | |
def mysql_credentials | |
"-u #{database_config['username']} --password='#{database_config['password']}'" | |
end | |
def database_config | |
YAML::load_file('config/database.yml')['development'] | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment