| 1 |
# frozen_string_literal: true |
| 2 |
|
| 3 |
module Bundler |
| 4 |
class CLI::Open |
| 5 |
attr_reader :options, :name, :path |
| 6 |
def initialize(options, name) |
| 7 |
@options = options |
| 8 |
@name = name |
| 9 |
@path = options[:path] unless options[:path].nil? |
| 10 |
end |
| 11 |
|
| 12 |
def run |
| 13 |
raise InvalidOption, "Cannot specify `--path` option without a value" if [email protected]? && @path.empty? |
| 14 |
editor = [ENV["BUNDLER_EDITOR"], ENV["VISUAL"], ENV["EDITOR"]].find {|e| !e.nil? && !e.empty? } |
| 15 |
return Bundler.ui.info("To open a bundled gem, set $EDITOR or $BUNDLER_EDITOR") unless editor |
| 16 |
return unless spec = Bundler::CLI::Common.select_spec(name, :regex_match) |
| 17 |
if spec.default_gem? |
| 18 |
Bundler.ui.info "Unable to open #{name} because it's a default gem, so the directory it would normally be installed to does not exist." |
| 19 |
else |
| 20 |
root_path = spec.full_gem_path |
| 21 |
require "shellwords" |
| 22 |
command = Shellwords.split(editor) << File.join([root_path, path].compact) |
| 23 |
Bundler.with_original_env do |
| 24 |
system(*command, { chdir: root_path }) |
| 25 |
end || Bundler.ui.info("Could not run '#{command.join(" ")}'") |
| 26 |
end |
| 27 |
end |
| 28 |
end |
| 29 |
end |