Ruby-on-rails 如何在我的 Gemfile 中指定本地 gem?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4487948/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 23:56:40  来源:igfitidea点击:

How can I specify a local gem in my Gemfile?

ruby-on-railsrubyruby-on-rails-3rubygemsbundler

提问by picardo

I'd like Bundler to load a local gem. Is there an option for that? Or do I have to move the gem folder into the .bundle directory?

我希望 Bundler 加载本地 gem。有没有选择?或者我是否必须将 gem 文件夹移动到 .bundle 目录中?

回答by Jimmy Cuadra

I believe you can do this:

我相信你可以这样做:

gem "foo", path: "/path/to/foo"

回答by bloudermilk

In addition to specifying the path (as Jimmy mentioned) you can also force Bundler to use a local gem for your environment onlyby using the following configuration option:

除了指定路径(如 Jimmy 提到的),您还可以强制 Bundler通过使用以下配置选项为您的环境使用本地 gem :

$ bundle config local.GEM_NAME /path/to/local/git/repository

This is extremely helpful if you're developing two gems or a gem and a rails app side-by-side.

如果您同时开发两个 gem 或一个 gem 和一个 rails 应用程序,这将非常有用。

Note though, that this only works when you're already using git for your dependency, for example:

但请注意,这仅在您已经将 git 用于您的依赖项时才有效,例如:

# In Gemfile
gem 'rack', :github => 'rack/rack', :branch => 'master'

# In your terminal
$ bundle config local.rack ~/Work/git/rack

As seen on the docs.

文档所示

回答by Rimian

You can also reference a local gem with git if you happen to be working on it.

如果您碰巧正在处理它,您还可以使用 git 引用本地 gem。

gem 'foo',
  :git => '/Path/to/local/git/repo',
  :branch => 'my-feature-branch'

Then, if it changes I run

然后,如果它改变我运行

bundle exec gem uninstall foo
bundle update foo

But I am not sure everyone needs to run these two steps.

但我不确定每个人都需要运行这两个步骤。

回答by gotqn

In order to use local gem repository in a Rails project, follow the steps below:

要在 Rails 项目中使用本地 gem 存储库,请按照以下步骤操作:

  1. Check if your gem folder is a git repository (the command is executed in the gem folder)

    git rev-parse --is-inside-work-tree
    
  2. Getting repository path (the command is executed in the gem folder)

    git rev-parse --show-toplevel
    
  3. Setting up a local override for the rails application

    bundle config local.GEM_NAME /path/to/local/git/repository
    

    where GEM_NAMEis the name of your gem and /path/to/local/git/repositoryis the output of the command in point 2

  4. In your application Gemfileadd the following line:

    gem 'GEM_NAME', :github => 'GEM_NAME/GEM_NAME', :branch => 'master'
    
  5. Running bundle installshould give something like this:

    Using GEM_NAME (0.0.1) from git://github.com/GEM_NAME/GEM_NAME.git (at /path/to/local/git/repository) 
    

    where GEM_NAMEis the name of your gem and /path/to/local/git/repositoryfrom point 2

  6. Finally, run bundle list, not gem listand you should see something like this:

    GEM_NAME (0.0.1 5a68b88)
    

    where GEM_NAMEis the name of your gem

  1. 检查你的gem文件夹是否是git仓库(命令在gem文件夹中执行)

    git rev-parse --is-inside-work-tree
    
  2. 获取仓库路径(命令在gem文件夹中执行)

    git rev-parse --show-toplevel
    
  3. 为 rails 应用程序设置本地覆盖

    bundle config local.GEM_NAME /path/to/local/git/repository
    

    哪里GEM_NAME是你的 gem 的名称,/path/to/local/git/repository是命令的输出点2

  4. 在您的应用程序中Gemfile添加以下行:

    gem 'GEM_NAME', :github => 'GEM_NAME/GEM_NAME', :branch => 'master'
    
  5. 运行bundle install应该给出这样的东西:

    Using GEM_NAME (0.0.1) from git://github.com/GEM_NAME/GEM_NAME.git (at /path/to/local/git/repository) 
    

    GEM_NAME您的宝石名称和/path/to/local/git/repository起点在哪里2

  6. 最后,运行bundle list, not gem list,您应该会看到如下内容:

    GEM_NAME (0.0.1 5a68b88)
    

    GEM_NAME你的宝石的名字在哪里



A few important cases I am observing using:

我观察到的几个重要案例使用:

Rails 4.0.2  
ruby 2.0.0p247 (2013-06-27 revision 41674) [x86_64-linux] 
Ubuntu 13.10  
RubyMine 6.0.3
  • It seems RubyMineis not showing local gems as an external library. More information about the bug can be found hereand here
  • When I am changing something in the local gem, in order to be loaded in the rails application I should stop/startthe rails server
  • If I am changing the versionof the gem, stopping/startingthe Rails server gives me an error. In order to fix it, I am specifying the gem version in the rails application Gemfilelike this:

    gem 'GEM_NAME', '0.0.2', :github => 'GEM_NAME/GEM_NAME', :branch => 'master'
    
  • 似乎RubyMine没有将本地 gem 显示为外部库。可以在此处此处找到有关该错误的更多信息
  • 当我在本地 gem 中更改某些内容时,为了加载到 rails 应用程序中,我应该stop/start使用 rails 服务器
  • 如果我正在更改versiongem 的,stopping/startingRails 服务器会给我一个错误。为了修复它,我在 rails 应用程序中指定 gem 版本,Gemfile如下所示:

    gem 'GEM_NAME', '0.0.2', :github => 'GEM_NAME/GEM_NAME', :branch => 'master'
    

回答by Nesha Zoric

You can reference gems with source: source: 'https://source.com', git repository (:github => 'git/url')and with local path :path => '.../path/gem_name'.
You can learn more about Gemfiles and how to use themin this article.

您可以使用 source:source: 'https://source.com', git repository (:github => 'git/url')和 local path引用 gems :path => '.../path/gem_name'
您可以在本文中了解有关Gemfiles 以及如何使用它们的更多信息。

回答by C Johnson

If you want the branch too:

如果你也想要分支:

gem 'foo', path: "point/to/your/path", branch: "branch-name"