2
votes

I'm trying to create custom generator for Rails 3.1. And I wrote this:

module SomeGem
  module Generators
    class InstallGenerator < Rails::Generators::Base
      source_root File.expand_path('../templates', __FILE__)
      desc "This adds devise"

      def install
        gem "devise"
        run "bundle install"
      end
    end
  end
end

But when I run this generator (rails g somegem:install, in the fresh-generated rails app) I've get this error:

 gemfile  devise
 run      bundle install

Could not find gem 'devise (>= 0)' in any of the gem sources listed in your Gemfile.
Run `bundle install` to install missing gems.

My generator adds Devise into Gemfile properly, but it fails when run 'bundle install' command from generator. When I run 'bundle install' from console it installs all gems without any errors.

Why is this happening?


Here is my Gemfile after I run 'rails g somegem:install' (I removed comments from listing):

source 'http://rubygems.org'
source 'http://gemcutter.org'
source "http://gems.github.com"

gem 'rails', '3.1.0'
gem 'mysql2'

group :assets do
  gem 'sass-rails', "  ~> 3.1.0"
  gem 'coffee-rails', "~> 3.1.0"
  gem 'uglifier'
end

gem 'jquery-rails'

group :test do
  # Pretty printed test output
  gem 'turn', :require => false
end

gem 'therubyracer'
gem "devise"
1
make sure you are connected with net while running bundle install commandMuhammad Sannan Khalid

1 Answers

3
votes

As pointed here, this is a bug in Bundler. To make it work:

module SomeGem
  module Generators
    class InstallGenerator < Rails::Generators::Base
      source_root File.expand_path('../templates', __FILE__)
      desc "This adds devise"

      def install
        gem "devise"
        Bundler.with_clean_env do
          run "bundle install"
        end
      end
    end
  end
end