I have a thor-based CLI that goes with a Rails app, and among the teeming examples of using thor to implement a CLI, I don't find any examples of a simple binstub that would execute in the context of bundler.
I want to be able to call my_cli
from the command line like this:
$ my_cli do something
I do NOT want to:
$ BUNDLE_GEMFILE=/path/to/Gemfile/of/Rails/app bundle exec my_cli
And I do NOT want to:
$ thor do something
The following binstub works. I have to require ../config/boot
. Requiring 'thor/rails'
in my_cli.rb
is not enough.
I am asking, is there a better way to do this?
#!/usr/bin/env ruby
APP_PATH = File.expand_path('../../config/application', __FILE__)
require_relative '../config/boot'
require_relative '../lib/my_cli'
MyCli.start(ARGV)
A little cleaner:
#!/usr/bin/env ruby
ENV['BUNDLE_GEMFILE'] = File.absolute_path(File.join(__dir__, '../Gemfile'))
ENV['RAKEOPT'] = "--silent"
ENV['RAILS_ENV'] ||= 'development'
APP_PATH = File.absolute_path(File.join(__dir__, '../config/application.rb'))
require 'rubygems'
require 'bundler/setup'
require_relative '../config/environment'
require 'my_cli'
MyCli.start(ARGV)