Run dry-cli script in other ruby projects

I have this cli built with dry-cli and a couple of other gems.

The way I’d like to run it is to keep it in my current directory (the repo where I’m working on it) and be able to run it globally on my machine

What I did was to symlink it to my ~/bin directory and add that directory to PATH
This allows me to run it from everywhere, but I have a different problem

Whenever I run it in one of my repos that is a ruby repo and has a Gemfile , it tries to run with that repo’s Gemfile and it’s dependencies, and naturally it will fail because that project does not have my CLIs dependencies.

The error I get when I’m in another ruby project

▶ bud release patch
Could not find activesupport-7.0.8, dry-cli-1.0.0, paint-2.3.0, tty-command-0.10.1, concurrent-ruby-1.2.2, i18n-1.14.1, pastel-0.8.0, tty-color-0.6.0 in locally installed gems
Run `bundle install` to install missing gems.

Is there any way to tell it to use it’s own Gemfile from it’s own repo?

bundler/setup is dependent on the Gemfile being present in the $PWD hierarchy. This only works for scripts you run while working in a project directory.

If you want to have a global script, I would recommend using bundler/inline to put the Gem dependencies into the script.

You can add this bit of code at the top of your bin/ scripts that detect whether the project is under git control or installed via rubygems.

root = File.expand_path(File.join(__dir__,'..'))
if File.file?(File.join(root,'Gemfile.lock'))
  Dir.chdir(root) do
    require 'bundler/setup'
  rescue LoadError => e
    warn e.message
    warn "Run `gem install bundler` to install Bundler"
    exit(-1)
  end
end

# rest of bin script

I would strongly recommend releasing your CLI tool as a rubygem.

Thanks for the reply @postmodern.

I still get an error about missing gems.
Any other thing I can try?

No, wait!
I was fiddling with the code and what made it work was not to navigate to the parent directory .. for setting the root.

root = File.expand_path(File.join(__dir__))
if File.file?(File.join(root,'Gemfile.lock'))
  Dir.chdir(root) do
    require 'bundler/setup'
  rescue LoadError => e
    warn e.message
    warn "Run `gem install bundler` to install Bundler"
    exit(-1)
  end
end

Thanks for setting me on the right path.

Why did you recommend to release this as a CLI? Do you think it’s valuable?