ruby – 了解$PATH变量:已安装的gem不在路径中

早上好!

我正在研究ruby gem.到目前为止,宝石还没有被推到任何服务器 – 所有本地开发目前.

我想在构建它时测试宝石(使用TTD).我已经运行bundle exe rake build和bundle exe rake install来构建和安装我的本地环境中的gem.我现在希望当我在终端的任何地方键入gem_name时,应该识别gem_name.不幸的是,它不是;相反,我得到这个错误:

    `$-bash gem_name: command not found`

我可以做到的唯一可能的事情可能是导致这是我的道路上的一些问题.

这是$PATH的输出:

    echo $PATH
    /Users/throw/.rvm/gems/ruby-2.2.1/bin:
    /Users/throw/.rvm/gems/ruby-2.2.1@global/bin:
    /Users/throw/.rvm/rubies/ruby-2.2.1/bin:
    /usr/local:/usr/local/bin:
   /usr/local/sbin:/usr/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:
   /usr/local/git/bin:/Users/throw/.rvm/bin:/Users/throw/.nvm/nvm.sh:
  /Users/throw/.node/bin:/Applications/Postgres.app/Contents/9.5.0.0/latest/bin

这是命令gem env的输出:

    gem env
    RubyGems Environment:
      - RUBYGEMS VERSION: 2.5.1
      - RUBY VERSION: 2.2.1 (2015-02-26 patchlevel 85) [x86_64-darwin14]
      - INSTALLATION DIRECTORY: /Users/throw/.rvm/gems/ruby-2.2.1
      - USER INSTALLATION DIRECTORY: /Users/throw/.gem/ruby/2.2.0
      - RUBY EXECUTABLE: /Users/throw/.rvm/rubies/ruby-2.2.1/bin/ruby
      - EXECUTABLE DIRECTORY: /Users/throw/.rvm/gems/ruby-2.2.1/bin
      - SPEC CACHE DIRECTORY: /Users/throw/.gem/specs
      - SYSTEM CONFIGURATION DIRECTORY: /Users/throw/.rvm/rubies/ruby-2.2.1/etc
      - RUBYGEMS PLATFORMS:
        - ruby
        - x86_64-darwin-14
      - GEM PATHS:
         - /Users/throw/.rvm/gems/ruby-2.2.1
         - /Users/throw/.rvm/gems/ruby-2.2.1@global
      - GEM CONFIGURATION:
         - :update_sources => true
         - :verbose => true
         - :backtrace => false
         - :bulk_threshold => 1000
      - REMOTE SOURCES:
         - https://rubygems.org/
      - SHELL PATH:
         - /Users/throw/.rvm/gems/ruby-2.2.1/bin
         - /Users/throw/.rvm/gems/ruby-2.2.1@global/bin
         - /Users/throw/.rvm/rubies/ruby-2.2.1/bin
         - /usr/local
         - /usr/local/bin
         - /usr/local/sbin
         - /usr/bin
         - /usr/local/bin
         - /usr/bin
         - /bin
         - /usr/sbin
         - /sbin
         - /usr/local/git/bin
         - /Users/throw/.rvm/bin
         - /Users/throw/.nvm/nvm.sh
         - /Users/throw/.node/bin
         - /Applications/Postgres.app/Contents/9.5.0.0/latest/bin

到目前为止,我认为gem_name路径包含在我的$PATH中,但我不知道……我错过了什么?

以下是gem_name.gemspec的内容:

    # coding: utf-8
    lib = File.expand_path('../lib', __FILE__)
    $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
    require 'instructortools/version'

    Gem::Specification.new do |spec|
      spec.name          = "instructortools"
      spec.version       = Instructortools::VERSION
      spec.authors       = ["Throw"]
      spec.email         = ["throw@gmail.com"]

      spec.summary       = %q{The command line interface instructor tools}
      spec.homepage      = "https://github.com/throw/instructortools-gem"
      spec.license       = "MIT"

      # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
      # delete this section to allow pushing this gem to any host.
      if spec.respond_to?(:metadata)
        spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
      else
        raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
      end

      spec.files         = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
      spec.bindir        = "exe"
      spec.executables   = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
      spec.require_paths = ["lib"]

      spec.add_development_dependency "bundler", "~> 1.11"
      spec.add_development_dependency "rake", "~> 10.0"
      spec.add_development_dependency "rspec", "~> 3.0"
      spec.add_development_dependency "pry"

      spec.add_runtime_dependency "thor"
    end

我有一个exe目录,其中包含以下行:

    #!/usr/bin/env ruby

    require "instructortools"

    Instructortools::CLI.start(ARGV)

谢谢!

最佳答案 你应该在gemspec中提到你的可执行文件,如下所示:

spec.bindir "exe"
spec.executables ["your_script_name"]

因此,在安装时,rubygems将其复制到$PATH上的/Users/throw/.rvm/gems/ruby-2.2.1/bin

编辑:
您的gemspec从git ls-files获取文件列表,因此所有文件都应在gem构建之前提交以包含在gem中.

点赞