简体   繁体   English

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

[英]Install bundle of gem within other rails application

I have following setup: 我有以下设置:

a rails 4.0.0 application => my master application 一个rails 4.0.0 application =>我的主应用程序

through this application developers can create gem skeletons 通过这个应用程序,开发人员可以创建gem骨架

now I´d like to create the gem skeleton source code and run bundle install of the gem Gemfile through a call in the rails master application: 现在我想创建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

Anybody an idea how I can run such a "bundle install" call within my rails master application, to install the bundle in the new gem-skeleton and not touch the rails bundle? 任何人都知道如何在我的rails主应用程序中运行这样的“bundle install”调用,在新的gem-skeleton中安装bundle而不是触摸rails bundle?

I use rails 4.0.0 and ruby 2.0.0-p195 我使用rails 4.0.0和ruby 2.0.0-p195

Thanks! 谢谢!

Mat

You should wrap your backticks calls in a block passed to Bundler.with_clean_env . 你应该将一个反引号调用包装在一个传递给Bundler.with_clean_env的块中。 This will ensure that it doesn't pick up your app's Gemfile: 这将确保它不会获取您的应用程序的Gemfile:

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

See the bundle-exec man page for details. 有关详细信息,请参见bundle-exec手册页

Would it be a good solution for you to create a gem with dependencies? 对于您来说创建具有依赖关系的gem是一个很好的解决方案吗?

The gem it self would not contain any particular code, but every time you need a new dependency you just have to modify its gemspec , and on other app simply run bundle update would update your gem and install its new dependencies. 它自己的gem不会包含任何特定代码,但每次你需要一个新的依赖项时你只需要修改它的gemspec ,而在其他应用程序上运行bundle update会更新你的gem并安装它的新依赖项。

I think you have to touch your master bundle, at least to include your gem. 我认为你必须触摸你的主束,至少包括你的宝石。 when you bundle your master application, all gem dependencies are installed and locked into your master Gemfile.lock for dependency resolution. 捆绑主应用程序时,所有gem依赖项都会安装并锁定到主Gemfile.lock中以进行依赖项解析。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM