了解$ PATH变量:已安装的宝石不在路径

时间:2016-01-18 15:56:27

标签: ruby path rubygems

早上好!

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

我想在构建它时测试宝石(使用TTD)。我已经运行bundle exe rake buildbundle 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)

谢谢!

1 个答案:

答案 0 :(得分:1)

您应该在gemspec中提及您的可执行文件,如下所示:

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

安装后,rubygems会将其复制到/Users/throw/.rvm/gems/ruby-2.2.1/bin上的$PATH

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