"$ bin/rails"中的"bin"是什么意思?

rov*_*lli 6 ruby ruby-on-rails .bash-profile

我正在尝试按照"入门引擎"Rails指南的教程进行操作,并发现:

$ bin/rails plugin new blorgh --mountable
Run Code Online (Sandbox Code Playgroud)

当我在终端上输入时,我得到:

bash: bin/rails: No such file or directory
Run Code Online (Sandbox Code Playgroud)

当我尝试$ rails它只是 工作正常.

我一直在使用命令行几个月,还有Rails.我知道$符号,但我对此之后"bin"的含义感到困惑.我习惯在$之后输入"Rails",但本教程说使用"bin"...

什么bin/意思?

Chr*_*ald 8

TL;博士

跑bundle install --binstubs,它会工作.你只需要做一次,Bundler会记住你要求它(对于那个项目).

更长的答案

当您安装捆绑包时bundle --binstubs,Bundler会将所有特定于捆绑包的binstub安装到bin/子目录中(相对于Gemfile),而不是将它们放入gemset的全局binstub路径中.

您通常会这样做:

  1. 确保rails您运行的binstub是Gemfile中的binstub(而不是稍后安装到gemset中的不同版本,与Bundler分开),以及
  2. 改进执行时间(因为它在功能上等同于bundle exec rails减去一个子进程调用).

全局binstubs与Bundler binstubs

如果你输入which rails,你会得到一条很长的路径/usr/local/rvm/gems/jruby-1.7.18/bin/rails,其内容如下:cat `which rails`

#!/usr/bin/env jruby_executable_hooks
#
# This file was generated by RubyGems.
#
# The application 'railties' is installed as part of a gem, and
# this file is here to facilitate running it.
#

require 'rubygems'

version = ">= 0"

if ARGV.first
  str = ARGV.first
  str = str.dup.force_encoding("BINARY") if str.respond_to? :force_encoding
  if str =~ /\A_(.*)_\z/
    version = $1
    ARGV.shift
  end
end

gem 'railties', version
load Gem.bin_path('railties', 'rails', version)
Run Code Online (Sandbox Code Playgroud)

跑完之后bundler --binstubs,如果你cat bin/rails,你会得到类似的东西:

#!/usr/bin/env jruby
#
# This file was generated by Bundler.
#
# The application 'rails' is installed as part of a gem, and
# this file is here to facilitate running it.
#

require 'pathname'
ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
  Pathname.new(__FILE__).realpath)

require 'rubygems'
require 'bundler/setup'

load Gem.bin_path('railties', 'rails')
Run Code Online (Sandbox Code Playgroud)

关键的区别在于bin/版本设置了BUNDLE_GEMFILE环境变量,它需要bundler/setup,这就是告诉Bundler用gem路径做的事情.该bin/rails脚本具有捆绑器感知功能,并且可以识别您的Gemfile,因此它可以立即为您的环境做好准备.

我通常建议使用--binstubs和bin/x调用gemset版本.