我有一个我们将调用的 gem, mygem它有一个用作 CLI 工具的可执行文件。
文件mygem/bin/mygem:
#!/usr/bin/env ruby
lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'bundler/setup'
....code goes here....
mygem.gemspec:
Gem::Specification.new do |gem|
gem.bindir = 'bin'
gem.executables = ['mygem']
end
构建和安装:
gem build mygem.gemspec
gem install mygem-0.1.0.gem
<optionally bundle install, but that won't fix the error>
最后,我可以mygem从命令行成功调用并且脚本运行。
问题是它只能从不包含Gemfile. 否则,系统会输出错误:
WARN: Unresolved specs during Gem::Specification.reset:
json (>= 0)
WARN: Clearing out unresolved specs.
Please report a bug if this causes problems.
Could not find differ-0.1.2 in any of the sources
Run `bundle install` to install missing gems.
此错误消息中显示的实际 gem 取决于当前目录中的 Gemfile。
运行bundle exec mygem会抑制部分错误消息,但仍无法执行,除非bundle install在其他应用程序的 Gemfile 上运行:
Could not find differ-0.1.2 in any of the sources
Run `bundle install` to install missing gems.
如何解决这个问题?