Skip to content

Instantly share code, notes, and snippets.

@morganp
Created July 14, 2011 07:56
Show Gist options
  • Save morganp/1082076 to your computer and use it in GitHub Desktop.
Save morganp/1082076 to your computer and use it in GitHub Desktop.
Set up a new Ruby Project
#!/usr/bin/env ruby
## Setting up files for a new Bundler/RVM Ruby project
## $ make_ruby_project some_new_project
##
## This could be cleaner split into multiple files and seperate templates
## But a single file in the your bin directory is easier to manage
##
## A More mainstream version is part of bundler
## bundle gem some_new_project
##
## Copyright (C) 2011 Morgan Prior
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
## [http://www.gnu.org/licenses/gpl.txt]
require 'FileUtils'
if ARGV.size < 1
puts "Usage $ make_ruby_project new_project"
exit
end
#Command line parse
project_name = (ARGV.first.to_s).dup
#Add CamelCase method to singleton
module CaseFunctions
def camelcase
a = self.split /[-|\s| |_|\.]/ #Word Boundry or _
a = a.map do |word|
#[0..0] is ruby 1.8.7 safe [0] returns char code 1.8 and char in 1.9
word[0..0].upcase + word[1..-1]
end
return a.join('')
end
end
project_name.extend(CaseFunctions)
puts "Setting up new Project : " + project_name.camelcase
## TODO
## Get some enviroment variables
## Users default Ruby version 1.8.7 or 1.9.2 (from RVM?)
#if folder does not contain project name make a new directory
current_folder = File.basename( Dir.getwd )
unless ((current_folder == project_name) or (current_folder == project_name.camelcase))
if File.directory?( project_name )
puts "Working in #{File.expand_path( project_name)}"
Dir.chdir( project_name )
elsif File.directory?( project_name.camelcase )
puts "Working in #{File.expand_path( project_name.camelcase ) }"
Dir.chdir( project_name.camelcase )
else
FileUtils.mkdir_p project_name
Dir.chdir( project_name )
end
end
## Template Files
##
readme_contents = %{
#{project_name.camelcase}
#{'=' * project_name.camelcase.size}
EXAMPLES
========
}
dot_rvmrc_contents = %{
rvm_gemset_create_on_use_flag=1
rvm 1.9.2@#{project_name}
}
gemfile_contents = %{
source :rubygems
#If this is a gem
#Normal gems go in #{project_name}.gemspec
gemspec
#development and test not install on heroku deployment
group :development do
end
group :test do
gem "rspec", :require => "spec"
end
}
rakefile_contents = %{
require 'rspec/core/rake_task'
file_list = FileList['spec/*_spec.rb']
RSpec::Core::RakeTask.new('spec') do |t|
t.pattern = file_list
t.rspec_opts = ["--colour", "--format progress"]
end
desc 'Default: run specs.'
task :default => 'spec'
}
top_level_contents = %{
module #{project_name.camelcase}
VERSION = '0.0.1'
end
require '#{project_name}/#{project_name}'
}
module_base_contents = %{
module #{project_name.camelcase}
class X
end
end
}
spec_helper_contents = %{
$LOAD_PATH.unshift File.join(File.dirname(__FILE__), '..', 'lib')
require 'rspec'
require '#{project_name}'
}
#Add empty test
first_spec_contents = %{
require 'spec_helper'
describe #{project_name} do
it "should do something" do
test = #{project_name}::Erm.new()
test.contents.should == nil
end
end
}
gemspec_contents = %{
# -*- encoding: utf-8 -*-
$:.push File.expand_path("../lib")
require '#{project_name}'
Gem::Specification.new do |s|
s.name = '#{project_name}'
s.version = #{project_name.camelcase}::VERSION
s.platform = Gem::Platform::RUBY
s.authors = ["TODO: Write your name"]
s.email = ["TODO: Write your email address"]
s.homepage = ""
s.summary = %q{#{project_name} TODO: Write a gem summary}
s.description = %q{#{project_name} TODO: Write a gem description}
s.files = `git ls-files`.split("\n")
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
s.require_paths = ["lib"]
#s.add_dependency('some_gem', '>= 1.0.0')
end
}
# Create File if it does not exist and write the contents
def write( filename, contents, warning='')
if File.exists? filename
puts "#{filename} already exists"
else
puts warning unless warning == ''
puts "Creating #{filename}"
::File.open( filename, "w" ){ |f| f.write contents }
end
end
## Create ./spec ./bin ./lib
FileUtils.mkdir_p "spec"
FileUtils.mkdir_p "bin"
FileUtils.mkdir_p File.join( 'lib', project_name)
write ".rvmrc", dot_rvmrc_contents
write "Gemfile", gemfile_contents
write "Rakefile", rakefile_contents
write "README.md", readme_contents
write "#{project_name}.gemspec", gemspec_contents
write File.join('lib', project_name + '.rb'), top_level_contents
write File.join('lib', project_name, project_name + '.rb'), module_base_contents
write File.join('spec', 'spec_helper.rb'), spec_helper_contents
write File.join('spec', project_name + '_spec.rb'), first_spec_contents
license_warn= %{WARNING attempting to add the GPL3 License
If you do not want this applied to your project please remove ./LICENSE}
puts "Connecting to gnu.org to retrieve the GPL License"
require 'open-uri'
license_content = ""
open( "http://www.gnu.org/licenses/gpl.txt" ) { |s| license_content = s.read }
write "LICENSE", license_content, license_warn
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment