2

我正在尝试为 Rails 3.1 创建自定义生成器。我写了这个:

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

但是当我运行这个生成器(rails g somegem:install,在新生成的 rails 应用程序中)时,我收到了这个错误:

 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.

我的生成器将 Devise 正确添加到 Gemfile 中,但是从生成器运行“捆绑安装”命令时失败。当我从控制台运行“捆绑安装”时,它会安装所有 gem,没有任何错误。

为什么会这样?


这是我运行 'rails g somegem:install' 后的 Gemfile(我从列表中删除了评论):

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"
4

1 回答 1

3

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
于 2012-01-24T16:41:59.293 回答