我有一个使用捆绑器的应用程序,目前工作正常,但我必须从项目的 bin 目录中运行它。现在我正在尝试将其转换为宝石。
bin/myexecutable
(没有变化):
#!/usr/bin/env ruby
require 'mygem'
MyGem::MyExecutable.new.main(ARGV)
lib/mygem.rb
(没有变化):
require 'rubygems'
require 'bundler/setup'
Bundler.require
require 'mygem/version'
require 'mygem/my_executable'
mygem.gemspec
(新的):
# coding: utf-8
lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'mygem/version'
Gem::Specification.new do |spec|
# ... omitting boilerplate specs of gem ...
spec.files = `git ls-files`.split($/)
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
spec.require_paths = ['lib']
spec.add_development_dependency 'bundler', '~> 1.3'
spec.add_development_dependency 'rake'
spec.add_runtime_dependency 'bindata'
end
Gemfile
(将依赖项移至 mygem.gemspec):
source 'https://rubygems.org'
gemspec
当我安装 gem 并尝试运行可执行文件时,我得到:
.../resource_file.rb:2:in `<class:ResourceFile>': uninitialized constant ResourceFile::BinData (NameError)
将依赖项复制回去Gemfile
使其再次工作,但现在我在两个位置有相同依赖项的冗余声明。
为什么使用声明时它不起作用gemspec
?