Search code examples
rubydependenciesrakebundler

gem dependencies not installed


I am sure this is really obvious, but I am quite new to ruby. I want to use rake / albacore to automate some tasks. I want to package it up to use on my build server using bundler. Right now I want to make one dumb tasks that impersonates a sys account using mixlib-shellout. To this end I have the following Gemfile:

source 'http://rubygems.org'
gem 'mixlib-shellout'
gem 'rake'

and the following rake file:

require 'rubygems'
require 'bundler/setup'

require 'mixlib/shellout'

task :default do
    whomai = Mixlib::ShellOut.new("whoami.exe", :user => "username", :domain => "DOMAIN", :password => "password")
    whoami.run_command
end

I run

bundle install

and I only see rake being installed... none of the other dependencies in the Gemfile.lock dep tree... is that normal?

PS C:\Users\Ben\src\ruby_test> bundle install
Fetching gem metadata from http://rubygems.org/...........
Fetching gem metadata from http://rubygems.org/..
Resolving dependencies...
Installing rake (10.1.0)
Using bundler (1.3.5)
Your bundle is complete!
Use `bundle show [gemname]` to see where a bundled gem is installed.

I then run

bundle exec rake

and I get in return

rake aborted!
cannot load such file -- mixlib/shellout
C:/Users/Ben/src/ruby_test/rakefile.rb:4:in `require'
C:/Users/Ben/src/ruby_test/rakefile.rb:4:in `<top (required)>'
(See full trace by running task with --trace)

I am using ruby 2.0 and bundler 1.3.5

Any help gratefully received.


Solution

  • I recommend setting up your gem with a *.gemspec file. To do this, your Gemfile becomes really simple:

    source 'https://rubygems.org'
    gemspec
    

    Then write a new file "GEM_NAME.gemspec". Here's an example:

    Gem::Specification.new do |spec|
      spec.name          = GAME_NAME
      spec.version       = VERSION
      spec.authors       = AUTHORS
      spec.email         = EMAILS
      spec.summary       = SUMMARY
      spec.description   = DESCRIPTION
      spec.homepage      = HOMEPAGE
    
      spec.files         = Dir['rakefile.rb', '*.gemspec']
      spec.files         += Dir['bin/**', 'lib/**/*.rb']
    
      spec.executables   = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
      spec.require_paths = ["lib"]
    
      spec.add_runtime_dependency "ruby-terminfo", "~> 0.1"
    
      spec.add_development_dependency "bundler", "~> 1.7"
      spec.add_development_dependency "rake", "~> 10.0"
    end
    

    You need to add a separate spec.add_runtime_dependency for each dependent gem. The example above includes the "ruby-terminfo" gem.

    Also, you need to set up the spec.files field to reflect your gem's file and folder structure.

    See RubyGem Guide for more details.