Skip to content

Instantly share code, notes, and snippets.

@gnarg
Created October 9, 2012 18:19
Show Gist options
  • Save gnarg/3860478 to your computer and use it in GitHub Desktop.
Save gnarg/3860478 to your computer and use it in GitHub Desktop.
test app setup script
#!/usr/bin/env ruby
require 'yaml'
module Timmy
class Step
def self.priority
@priority
end
end
class NewRelicYml < Step
@priority = 10
def process(options)
config = YAML::load(File.read('config/newrelic.yml'))
min_config = {
'license_key' => 'bootstrap_newrelic_admin_license_key_000',
'host' => 'localhost',
'api_host' => 'localhost',
'port' => 8081,
'log_level' => 'debug',
'ssl' => false,
'app_name' => options[:name]
}
new_config = {}
config.keys.map{|key| new_config[key] = config[key].merge(min_config)}
File.open('config/newrelic.yml', 'w').write(YAML::dump(new_config))
options
end
end
class Gemfile < Step
@priority = 20
def process(options)
new_file = ''
File.open('Gemfile', 'r').each do |line|
case line
when /^\s*#/
new_file << line
when /^\s*gem\s+[',"]newrelic_rpm[',"]/
# skip it
when /^\s*gem\s+[',"]rails[',"],\s*[',"](.*)[',"]/
options[:rails_version] = $1
new_file << line
when /^\s*gem\s+[',"]sqlite3-ruby[',"]/
@found_sqlite = true
new_file << line
when /^\s*gem\s+[',"]shoulda[',"]/
@found_shoulda = true
new_file << line
else
new_file << line
end
end
new_file << %{gem "newrelic_rpm", :path => "../ruby_agent"\n}
new_file << %{gem "sqlite3-ruby"\n} unless @found_sqlite
new_file << %{gem "shoulda"\n} unless @found_shoulda
File.open('Gemfile', 'w') {|f| f.write(new_file) }
options
end
end
class EnvironmentRb < Step
@priority = 30
def process(options)
new_file = ''
File.open('config/environment.rb', 'r').each do |line|
case line
when /^\s*#/
new_file << line
when /^\s*RAILS_GEM_VERSION/
new_file << %[RAILS_GEM_VERSION = '#{options[:rails_version]}'\n]
else
new_file << line
end
end
File.open('config/environment.rb', 'w') {|f| f.write(new_file) }
options
end
end
class Bundle < Step
@priority = 40
def process(options)
system('bundle')
options
end
end
class Processor
def initialize
@to_run = []
end
def register(klass)
@to_run << klass
end
def process(arg)
options = {:name => arg}
Timmy::Step.subclasses.each do |klass|
register(klass)
end
@to_run.sort{|a,b| a.priority <=> b.priority}.each do |klass|
puts klass.name
instance = klass.new
options = instance.process(options)
end
end
end
class ::Object
def self.subclasses(direct = false)
classes = []
if direct
ObjectSpace.each_object(Class) do |c|
next unless c.superclass == self
classes << c
end
else
ObjectSpace.each_object(Class) do |c|
next unless c.ancestors.include?(self) and (c != self)
classes << c
end
end
classes
end
end
end
if __FILE__ == $0
processor = Timmy::Processor.new
processor.process(ARGV[0] || 'test')
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment