【问题标题】:Execute without `bundle exec` via rubygems-bundler通过 ruby​​gems-bundler 不使用 `bundle exec` 执行
【发布时间】:2013-05-26 19:28:34
【问题描述】:

我有一个标准的 gem 脚手架,在 bin 目录中我有一个简单的可执行文件 (bin/do_things.rb):

#!/usr/bin/env ruby
require 'my_gem'
MyGem::doThings()

gem 尚未通过gem install 安装,因此在没有bundle exec 的情况下运行bin/do_things.rb 会失败(我的Gemfile 中包含gemspec 行)。

有没有一种简单的方法让 ruby​​gems-bundler 在捆绑器上下文中执行我的脚本?我应该只修改可执行文件中的$LOAD_PATH 吗?

我也可以创建一个包装脚本以在 Bundler 下执行,但我宁愿以某种方式利用 ruby​​gems-bundler。

或者我应该只输入bundle exec

【问题讨论】:

  • 你能澄清你的脚本的目的吗?它是您仅在开发中使用的东西还是您的 gem 用户使用的东西?无论如何,我不建议直接在可执行文件中修改你的加载路径。
  • 安装后,gem 可执行文件的目的是做一些 shell 实用程序的东西——在我的例子中,移动一些文件。安装后用户可以访问它。

标签: ruby rubygems rvm bundler


【解决方案1】:

尝试:

bundle exec bash -l

它会将您设置为捆绑程序上下文 - 它是一个额外的 shell,因此运行 exit 将使您回到较少捆绑程序的上下文。

【讨论】:

  • 谢谢!这正是我一直在寻找的。无需修改脚本。这对我有用:bundle exec zsh -l
  • 另外,在 OSX 上,我必须取出第一个 bash 才能工作:bundle exec bash -l
【解决方案2】:

更改文件权限

chmod a+x bin/do_things.rb

并解析 bin 路径,忽略符号链接

require "pathname"
bin_file = Pathname.new(__FILE__).realpath

发布,将自己添加到 libpath

$:.unshift File.expand_path("../../lib", bin_file)

【讨论】:

    【解决方案3】:

    通过bundle install --binstubs 生成 binstubs 会为我的 gemspec 中列出的所有可执行文件创建一个包装脚本。

    #!/usr/bin/env ruby
    #
    # This file was generated by Bundler.
    #
    # The application 'do_things.rb' 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('my_gem', 'do_things.rb')
    

    但是,默认情况下,binstubs 路径为bin,与gem 的可执行路径冲突,会覆盖bin 中的文件。

    运行bundle install --binstubs=SOME_DIR,然后将SOME_DIR 添加到.gitignore 似乎是最易于维护的方式。

    然后,我可以简单地执行SOME_DIR/do_things 或我添加的任何其他项目特定的可执行文件。

    【讨论】:

      猜你喜欢
      • 2021-09-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-05
      • 2021-09-25
      • 2015-04-25
      • 2011-02-10
      • 1970-01-01
      相关资源
      最近更新 更多