在其他rails应用程序中安装gem包

时间:2013-08-01 00:52:50

标签: ruby-on-rails ruby rubygems bundler

我有以下设置:

rails 4.0.0 application =>我的主应用程序

通过这个应用程序开发人员可以创建gem骨架

现在我想创建gem骨架源代码并通过rails master应用程序中的调用运行gem Gemfile的bundle install:

class MyClass

  # this works
  def create_gem_skeleton
    path = "path-to-gem-skeleton-outside-the-rails-master-app"
    FileUtils.mkdir_p(path)
    `cd #{path} && bundle gem my-new-gem`
  end

  # this method gets called, after I created the gem skeleton and manipulated it a bit with my preferences
  def my_method
    path = "path-to-gem-skeleton-outside-the-rails-master-app"
    exec `cd #{path} && bundle install`   # does not work, installs always the rails master bundle inside my rails master application, never touches the new gem-skeleton
    system `cd #{path} && bundle install` # =||= .. same here
    `cd #{path} && bundle install`        # =||= .. same here

  end

end

任何人都知道如何在我的rails主应用程序中运行这样的“bundle install”调用,在新的gem-skeleton中安装bundle而不是触摸rails bundle?

我使用rails 4.0.0和ruby 2.0.0-p195

谢谢!

3 个答案:

答案 0 :(得分:6)

你应该在传递给Bundler.with_clean_env的块中包装反引号调用。这将确保它不会获取您的应用程序的Gemfile:

Bundler.with_clean_env { `cd #{path} && bundle install` }

有关详细信息,请参阅bundle-exec man page

答案 1 :(得分:0)

创建具有依赖关系的gem会不会是一个很好的解决方案?

它自己的gem不会包含任何特定的代码,但每次你需要一个新的依赖项时你只需要修改它的gemspec,而在其他应用程序上运行bundle update会更新你的gem和安装新的依赖项。

答案 2 :(得分:0)

我认为你必须触摸你的主束,至少包括你的宝石。捆绑主应用程序时,所有gem依赖项都会安装并锁定到主Gemfile.lock中以进行依赖性解析。