Skip to content

Instantly share code, notes, and snippets.

@davidrichards
Created July 4, 2012 17:16
Show Gist options
  • Save davidrichards/3048420 to your computer and use it in GitHub Desktop.
Save davidrichards/3048420 to your computer and use it in GitHub Desktop.
spike: an easy way to start throw-away code (and still test drive)
#!/usr/bin/env ruby
require 'fileutils'
class SpikeBuilder
def self.build!(name, gemset)
obj = new(name, gemset)
obj.build!
end
attr_reader :name, :gemset
def initialize(name, gemset=nil)
@name = name
@gemset = gemset || name
end
def build!
self.methods.sort.grep(/build_/).each do |build_method|
send(build_method)
end
end
def build_readme
contents = <<-END
#{name}
#{"=" * name.size}
Start entering some notes here. You can call guard to turn this into an HTML page.
END
writefile 'README.md', contents
end
def build_guardfile
contents =<<-END
guard 'markdown', :convert_on_start => true, :dry_run => false do
watch (/README.md/) {|m| "README.md|./README.html"}
end
guard 'minitest' do
watch(%r|^spec/(.*)_spec\.rb|)
watch(%r|^lib/(.*)\.rb|) { |m| "spec/\#{m[1]}_spec.rb" }
watch(%r|^spec/minitest_helper_lite\.rb|) { "spec" }
end
END
writefile 'Guardfile', contents
end
def build_mainfile
contents =<<-END
class #{new_class_name}
end
END
FileUtils.mkdir_p('lib')
writefile "lib/#{name}.rb", contents
end
def build_spechelper
contents =<<-END
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '../lib'))
$LOAD_PATH.unshift(File.dirname(__FILE__))
require "bundler/setup"
require "minitest/autorun"
require 'turn'
require 'rr'
require 'ostruct'
Turn.config.format = :outline
class MiniTest::Unit::TestCase
include RR::Adapters::MiniTest
end
# Requires supporting files with custom matchers and macros, etc,
# in ./support/ and its subdirectories.
Dir["\#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
END
FileUtils.mkdir_p('spec')
writefile 'spec/minitest_helper_lite.rb', contents
end
def underscore_gemset
self.gemset.downcase.gsub(/\W+/, '_')
end
def build_rvmrc
contents = "rvm use ruby-1.9.3-p194@#{underscore_gemset} --create\n"
writefile '.rvmrc', contents
end
def build_main_spec
contents =<<-END
require_relative 'minitest_helper_lite'
describe #{new_class_name} do
it "should just work" do
true
end
end
END
FileUtils.mkdir_p('spec')
writefile "spec/#{name}_spec.rb", contents
end
def build_gemspec
contents =<<-END
source 'http://rubygems.org'
gem 'minitest'
gem 'turn'
gem 'rr'
gem 'guard-markdown'
gem 'guard-minitest'
gem 'ruby-debug19'
END
writefile "Gemfile", contents
end
protected
def new_class_name
@new_class_name ||= name.to_s.downcase.split(/_/).map{|e| e.capitalize}.join
end
def writefile(filename, contents)
File.open(filename, 'w') {|f| f.puts contents}
end
end # class SpikeBuilder
name = ARGV[0]
gemset = ARGV[1] || name
unless name
puts '', "#{'=' * 46}", "You must provide at least a name."
puts "Usage: #{File.split(__FILE__)[-1]} spike_name [optional_gemset_name]", "#{'=' * 46}", ''
exit 1
end
FileUtils.mkdir_p(name)
FileUtils.chdir(name)
SpikeBuilder.build!(name, gemset)
@davidrichards
Copy link
Author

This is an easy way to build a little directory for throw-away Ruby code. I use it to learn while coding. I wrote about why I use it here: http://bit.ly/LnM9jf

It expects to use MiniTest, RVM, ruby-debug, Guard, and a few things like that to make things easier. To use it, put it somewhere like in /usr/local/bin, make sure it can be run (chmod 755 spike) and run it with:

spike some_creative_name, optional_gemset_name

Or just type spike to get similar instructions on the command line. Then:

cd some_creative_name
accept the .rvmrc
bundle
bundle exec guard

Now, write some code, it's test-driven. Update the README.md, it will produce a README.html (not sure how useful that is). But most importantly, play then throw away if you'd like.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment