public
Last active

A bundler+gemspec setup for managing dependencies

  • Download Gist
Gemfile
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
#############################################################################
# Dependencies in this Gemfile are managed through the gemspec. Add/remove
# depenencies there, rather than editing this file ex:
#
# Gem::Specification.new do |s|
# ...
# s.add_dependency("sinatra")
# s.add_development_dependency("rack-test")
# end
#
#############################################################################
source :gemcutter
 
project_dir = File.expand_path('..', __FILE__)
gemspec_path = File.expand_path('example.gemspec', project_dir)
 
#
# Setup gemspec dependencies
#
 
gemspec = eval(File.read(gemspec_path))
gemspec.dependencies.each do |dep|
group = dep.type == :development ? :development : :default
gem dep.name, dep.requirement, :group => group
end
gem(gemspec.name, gemspec.version, :path => project_dir)
Rakefile
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
require 'rake'
 
#
# Dependency tasks
#
 
desc 'Bundle dependencies'
task :bundle do
output = `bundle check 2>&1`
unless $?.to_i == 0
puts output
sh "bundle install"
puts
end
end
 
#
# Test tasks
#
 
desc 'Default: Run tests.'
task :default => :test
 
desc 'Run the tests'
task :test => :bundle do
tests = Dir.glob('test/**/*_test.rb')
sh('ruby', '-w', '-e', 'ARGV.dup.each {|test| load test}', *tests)
end
example.gemspec
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
Gem::Specification.new do |s|
s.name = "example"
s.version = "0.0.1"
s.author = "Your Name Here"
s.email = "your.email@pubfactory.edu"
s.homepage = ""
s.platform = Gem::Platform::RUBY
s.summary = ""
s.require_path = "lib"
s.rubyforge_project = ""
# add dependencies
s.add_dependency("sinatra")
s.add_development_dependency("rack-test")
s.has_rdoc = true
s.rdoc_options.concat %W{--main README -S -N --title Example}
# list extra rdoc files
s.extra_rdoc_files = %W{
}
# list the files you want to include
s.files = %W{
}
end
test/example_test.rb
Ruby
1 2 3 4 5 6 7 8 9
require File.expand_path('../test_helper', __FILE__)
require 'sinatra'
require 'rack/test'
 
class ExampleTest < Test::Unit::TestCase
def test_all_the_dependencies_are_available
assert true, 'the requires would have failed if not'
end
end
test/test_helper.rb
Ruby
1 2 3 4 5 6 7 8 9
begin
require File.expand_path('../../.bundle/environment', __FILE__)
rescue LoadError
require "rubygems"
require "bundler"
Bundler.setup
end
 
require 'test/unit'

Cool, I like the idea of looking through the gem dependencies and assigning them to a test group

Please don't use this Gemfile. Bundler has the ability to read gemspecs built in. Documentation is at http://gembundler.com/rubygems.html. There's also an (as yet incomplete) guide to gem development with Bundler at http://github.com/radar/guides/blob/master/gem-development.md. Hope that helps!

Thanks for the tip! I started using this system before the built-in support.

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.