Ruby-on-rails 如何将 Bundler 与离线 .gem 文件一起使用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4271947/
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:42:25  来源:igfitidea点击:

How to use Bundler with offline .gem file?

ruby-on-railsrubyrubygemsbundler

提问by shedd

For my application, I need to handle encrypted ZIP files. Despite their horrific looking site, it seems that Chilkat's commercial Zip gemis probably the best way to go to implement this.

对于我的应用程序,我需要处理加密的 ZIP 文件。尽管他们的网站看起来很可怕,但似乎 Chilkat 的商业 Zip gem可能是实现这一点的最佳方式。

Because this is a commercial gem, they don't have it in any of the typical gem sources that Bundler looks at. I was able to install the Linux 64-bit version of the gem under Mac OS X (though I haven't tried to run it yet, so no word yet on if that will actually work). However, I'm first trying to get Bundler to recognize and load the gem from the .gem file that I downloaded.

因为这是一种商业宝石,所以在 Bundler 所关注的任何典型宝石来源中都没有它。我能够在 Mac OS X 下安装 Linux 64 位版本的 gem(虽然我还没有尝试运行它,所以还没有关于它是否真的可以工作的消息)。但是,我首先尝试让 Bundler 从我下载的 .gem 文件中识别并加载 gem。

Bundler has a path attributewhich I've tried to utilize in several ways, but I haven't gotten it to work yet:

Bundler 有一个path 属性,我已经尝试以多种方式利用它,但我还没有让它工作:

  1. I tried using path to point to the .gem file itself, but path expects a directory.
  2. I tried adding .gz to the end of the .gem file and extracting it - I got a directory with a data.tar.gz and metadata.gz inside. path pointed to the extracted directory with these two files didn't work.
  3. I tried extracting the data.tar.gz and metadata.gz and placing the extracted versions inside the directory that I pointed path to. This failed.
  4. I noticed that the gem didn't have a gemspec file - I created one and placed it inside the directory. No luck.
  1. 我尝试使用 path 指向 .gem 文件本身,但 path 需要一个目录。
  2. 我尝试将 .gz 添加到 .gem 文件的末尾并提取它 - 我得到了一个包含 data.tar.gz 和 metadata.gz 的目录。指向带有这两个文件的提取目录的路径不起作用。
  3. 我尝试提取 data.tar.gz 和 metadata.gz 并将提取的版本放在我指向路径的目录中。这失败了。
  4. 我注意到 gem 没有 gemspec 文件 - 我创建了一个并将它放在目录中。没运气。

This is the error that I get:

这是我得到的错误:

$ bundle install
Fetching source index for http://rubygems.org/
Fetching source index for http://gems.github.com/
Could not find gem 'chilkat (>= 0, runtime)' in source at /Users/username/appname/vendor/cache/chilkat-9.1.0-x86_64-linux.
Source does not contain any versions of 'chilkat (>= 0, runtime)'

Any ideas on how I can get Bundler to see that the gem is indeed in this directory? Any other options other than the path attribute which doesn't seem to be working?

关于如何让 Bundler 看到 gem 确实在这个目录中的任何想法?除了 path 属性之外的其他选项似乎不起作用?

Many thanks!

非常感谢!

回答by aceofspades

I'm using Rails 3.0.3, new to Rails 3 and bundler.

我正在使用 Rails 3.0.3,这是 Rails 3 和 bundler 的新手。

I got this same error with:

我遇到了同样的错误:

gem 'mygem', :path => '/path/to/gem'

Resolved by specifying the version number:

通过指定版本号解决:

gem 'mygem', '0.0.1', :path => '/path/to/gem'

Using >=0.0.1for the version reverted to the original error. I can't explain any of this, however.

使用>=0.0.1用于恢复到原来的错误版本。然而,我无法解释这些。

Quoting JD's helpful commment, from the Gemfile man page: "Similar to the semantics of the :git option, the :path option requires that the directory in question either contains a .gemspec for the gem, or that you specify an explicit version that bundler should use."

从 Gemfile 手册页引用 JD 的有用评论:“类似于 :git 选项的语义,:path 选项要求相关目录包含 gem 的 .gemspec,或者您指定一个显式版本应该用。”

回答by semanticart

Try unpacking the gem and then using the path in your Gemfile.

尝试解压 gem,然后使用 Gemfile 中的路径。

i.e.

IE

gem unpack my-gem-file.gem /my-rails-app/vendor/gems/

then add a line like so to your Gemfile

然后在你的 Gemfile 中添加这样一行

gem 'my-gem', '0.0.1', :path => 'vendor/gems/my-gem'

Obviously paths and version numbers will vary. You also might need to make the vendor/gems directory in your app root if it doesn't already exist.

显然路径和版本号会有所不同。如果不存在,您可能还需要在应用程序根目录中创建 vendor/gems 目录。

回答by Indika K

Copy the gem in to vendor/cache directory in your application's root folder.

将 gem 复制到应用程序根文件夹中的 vendor/cache 目录中。

bundle install --local

This will install the local gem.

这将安装本地 gem。

回答by Kelvin

Since this gem will be local to any machine you'll be running your app on, just specify the gem in the Gemfile, then manually install the gem. When you run "bundle install", bundler will see it's already installed and move on.

由于此 gem 将位于您将在其上运行应用程序的任何机器的本地,因此只需在 Gemfile 中指定 gem,然后手动安装 gem。当您运行“bundle install”时,bundler 会看到它已经安装并继续。

This worked for me when installing a version of ruby-debug-base19 that wasn't available on rubygems.org yet.

在安装 ruby​​gems.org 上尚不可用的 ruby​​-debug-base19 版本时,这对我有用。

Another way would be to set up your own gem server that's accessible to all your app servers. See http://guides.rubygems.org/run-your-own-gem-server/

另一种方法是设置所有应用服务器都可以访问的自己的 gem 服务器。见http://guides.rubygems.org/run-your-own-gem-server/

I've never done this myself but it looks very simple. Just make sure you don't violate any of the Chilkat terms of service if your gem server is going to be on the Internet.

我自己从来没有这样做过,但它看起来很简单。如果您的 gem 服务器要在 Internet 上运行,请确保您没有违反任何 Chilkat 服务条款。

回答by liangzan

First unpack the gem using the solution by semanticart. Then add a gemspec in the unpacked gem. Bundler will be able to run properly.

首先使用semanticart的解决方案解压gem。然后在解压的 gem 中添加一个 gemspec。Bundler 将能够正常运行。

Gem::Specification.new do |s|
  s.name         = "chilkat"
  s.version      = "9.4.1"
  s.platform     = Gem::Platform::RUBY
  s.required_rubygems_version = ">= 1.3.6"
  s.files        = Dir.glob("lib/**/*")
  s.require_path = "lib"
  s.summary      = "Make do with a self written gemspec"
end