Skip to content

Instantly share code, notes, and snippets.

@fnando
Created November 30, 2010 02:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fnando/721070 to your computer and use it in GitHub Desktop.
Save fnando/721070 to your computer and use it in GitHub Desktop.
Bootstrapping RubyGems with Thor
require "active_support/all"
class Newgem < Thor::Group
include Thor::Actions
argument :path
class_option :rspec, :type => :boolean, :group => :test_framework, :desc => "Use RSpec as testing framework"
class_option :test_unit, :type => :boolean, :group => :test_framework, :desc => "Use Test::Unit as testing framework"
def prepare_for_invocation
self.destination_root = path
end
def create_directories
empty_directory "lib/#{name}"
empty_directory test_directory
end
def create_files
create_file "README.rdoc" do
<<-SOURCE
= #{name}
Some description
== Installation
gem install #{name}
== Usage
Usage info
== Maintainer
* Your Name (http://yoursite.com)
== License
(The MIT License)
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
SOURCE
end
create_file "Gemfile" do
<<-SOURCE
source :rubygems
gemspec
SOURCE
end
create_file ".rvmrc" do
"rvm 1.9.2@#{name}"
end
create_file "#{name}.gemspec" do
<<-SOURCE
# -*- encoding: utf-8 -*-
$:.push File.expand_path("../lib", __FILE__)
require "#{name}"
Gem::Specification.new do |s|
s.name = "#{name}"
s.version = #{module_name}::Version::STRING
s.platform = Gem::Platform::RUBY
s.authors = ["Your name"]
s.email = ["Your e-mail"]
s.homepage = "http://rubygems.org/gems/#{name}"
s.summary = "TODO: Write a description"
s.description = s.summary
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 "activesupport"
# s.add_development_dependency "rspec", ">= 2.0.0"
end
SOURCE
end
create_file "lib/#{name}/version.rb" do
<<-SOURCE
module #{module_name}
module Version
MAJOR = 0
MINOR = 1
PATCH = 0
STRING = "\#{MAJOR}.\#{MINOR}.\#{PATCH}"
end
end
SOURCE
end
create_file "lib/#{name}.rb" do
<<-SOURCE
module #{module_name}
autoload :Version, "#{name}/version"
end
SOURCE
end
create_file ".gitignore" do
<<-SOURCE
.DS_Store
pkg
tmp
SOURCE
end
send("create_#{test_directory}_files")
end
def run_commands
in_root do
run "rvm 1.9.2@#{name} --create"
run "git init"
run "gem install bundler"
end
end
private
def create_spec_files
create_file "Rakefile" do
<<-SOURCE
require "bundler"
Bundler::GemHelper.install_tasks
require "rspec/core/rake_task"
RSpec::Core::RakeTask.new
SOURCE
end
create_file "spec/spec_helper.rb" do
%[require "#{name}"\n]
end
create_file "spec/#{name}_spec.rb" do
<<-SOURCE
require "spec_helper"
describe #{module_name} do
end
SOURCE
end
end
def create_test_files
create_file "Rakefile" do
<<-SOURCE
require "bundler"
Bundler::GemHelper.install_tasks
require "rake/testtask"
Rake::TestTask.new do |t|
t.libs << "test" << "lib"
t.test_files = FileList["test/{test_*,*_test}.rb"]
t.verbose = true
end
SOURCE
end
create_file "test/test_helper.rb" do
<<-SOURCE
require "test/unit"
require "#{name}"
SOURCE
end
create_file "test/#{name}_test.rb" do
<<-SOURCE
require "test_helper"
class #{module_name}Test < Test::Unit::TestCase
end
SOURCE
end
end
def name
File.basename(path)
end
def module_name
name.underscore.classify
end
def test_directory
options[:test_unit] ? "test" : "spec"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment