Skip to content

Commit

Permalink
[rubygems/rubygems] Enhance bundle open with --path option
Browse files Browse the repository at this point in the history
  • Loading branch information
yoka authored and matzbot committed Jan 4, 2023
1 parent 3d6500e commit 87c17a1
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 6 deletions.
1 change: 1 addition & 0 deletions lib/bundler/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,7 @@ def exec(*args)
subcommand "config", Config

desc "open GEM", "Opens the source directory of the given bundled gem"
method_option "path", :type => :string, :banner => "Open relative path of the gem source."
def open(name)
require_relative "cli/open"
Open.new(options, name).run
Expand Down
9 changes: 5 additions & 4 deletions lib/bundler/cli/open.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

module Bundler
class CLI::Open
attr_reader :options, :name
attr_reader :options, :name, :path
def initialize(options, name)
@options = options
@name = name
@path = options[:path] unless options[:path].nil?
end

def run
Expand All @@ -15,10 +16,10 @@ def run
if spec.default_gem?
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."
else
path = spec.full_gem_path
Dir.chdir(path) do
root_path = spec.full_gem_path
Dir.chdir(root_path) do
require "shellwords"
command = Shellwords.split(editor) + [path]
command = Shellwords.split(editor) << File.join([root_path, path].compact)
Bundler.with_original_env do
system(*command)
end || Bundler.ui.info("Could not run '#{command.join(" ")}'")
Expand Down
22 changes: 21 additions & 1 deletion lib/bundler/man/bundle-open.1
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
\fBbundle\-open\fR \- Opens the source directory for a gem in your bundle
.
.SH "SYNOPSIS"
\fBbundle open\fR [GEM]
\fBbundle open\fR [GEM] [\-\-path=PATH]
.
.SH "DESCRIPTION"
Opens the source directory of the provided GEM in your editor\.
Expand All @@ -30,3 +30,23 @@ bundle open \'rack\'
.
.P
Will open the source directory for the \'rack\' gem in your bundle\.
.
.IP "" 4
.
.nf

bundle open \'rack\' \-\-path \'README\.md\'
.
.fi
.
.IP "" 0
.
.P
Will open the README\.md file of the \'rack\' gem source in your bundle\.
.
.SH "OPTIONS"
.
.TP
\fB\-\-path\fR
Specify GEM source relative path to open\.

10 changes: 9 additions & 1 deletion lib/bundler/man/bundle-open.1.ronn
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ bundle-open(1) -- Opens the source directory for a gem in your bundle

## SYNOPSIS

`bundle open` [GEM]
`bundle open` [GEM] [--path=PATH]

## DESCRIPTION

Expand All @@ -17,3 +17,11 @@ Example:
bundle open 'rack'

Will open the source directory for the 'rack' gem in your bundle.

bundle open 'rack' --path 'README.md'

Will open the README.md file of the 'rack' gem source in your bundle.

## OPTIONS
* `--path`:
Specify GEM source relative path to open.
53 changes: 53 additions & 0 deletions spec/bundler/commands/open_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,59 @@
expect(out).to include("bundler_editor #{default_bundle_path("gems", "activerecord-2.3.2")}")
end

it "opens subpath of the gem" do
bundle "open activerecord --path lib/activerecord", :env => { "EDITOR" => "echo editor", "VISUAL" => "", "BUNDLER_EDITOR" => "" }
expect(out).to include("editor #{default_bundle_path("gems", "activerecord-2.3.2")}/lib/activerecord")
end

it "opens subpath file of the gem" do
bundle "open activerecord --path lib/version.rb", :env => { "EDITOR" => "echo editor", "VISUAL" => "", "BUNDLER_EDITOR" => "" }
expect(out).to include("editor #{default_bundle_path("gems", "activerecord-2.3.2")}/lib/version.rb")
end

it "opens deep subpath of the gem" do
bundle "open activerecord --path lib/active_record", :env => { "EDITOR" => "echo editor", "VISUAL" => "", "BUNDLER_EDITOR" => "" }
expect(out).to include("editor #{default_bundle_path("gems", "activerecord-2.3.2")}/lib/active_record")
end

it "suggests alternatives for similar-sounding gems when using subpath" do
bundle "open Rails --path README.md", :env => { "EDITOR" => "echo editor", "VISUAL" => "", "BUNDLER_EDITOR" => "" }, :raise_on_error => false
expect(err).to match(/did you mean rails\?/i)
end

it "suggests alternatives for similar-sounding gems when using deep subpath" do
bundle "open Rails --path some/path/here", :env => { "EDITOR" => "echo editor", "VISUAL" => "", "BUNDLER_EDITOR" => "" }, :raise_on_error => false
expect(err).to match(/did you mean rails\?/i)
end

it "opens subpath of the short worded gem" do
bundle "open rec --path CHANGELOG.md", :env => { "EDITOR" => "echo editor", "VISUAL" => "", "BUNDLER_EDITOR" => "" }
expect(out).to include("editor #{default_bundle_path("gems", "activerecord-2.3.2")}/CHANGELOG.md")
end

it "opens deep subpath of the short worded gem" do
bundle "open rec --path lib/activerecord", :env => { "EDITOR" => "echo editor", "VISUAL" => "", "BUNDLER_EDITOR" => "" }
expect(out).to include("editor #{default_bundle_path("gems", "activerecord-2.3.2")}/lib/activerecord")
end

it "opens subpath of the selected matching gem", :readline do
env = { "EDITOR" => "echo editor", "VISUAL" => "echo visual", "BUNDLER_EDITOR" => "echo bundler_editor" }
bundle "open active --path CHANGELOG.md", :env => env do |input, _, _|
input.puts "2"
end

expect(out).to match(%r{bundler_editor #{default_bundle_path('gems', 'activerecord-2.3.2')}/CHANGELOG\.md\z})
end

it "opens deep subpath of the selected matching gem", :readline do
env = { "EDITOR" => "echo editor", "VISUAL" => "echo visual", "BUNDLER_EDITOR" => "echo bundler_editor" }
bundle "open active --path lib/activerecord/version.rb", :env => env do |input, _, _|
input.puts "2"
end

expect(out).to match(%r{bundler_editor #{default_bundle_path('gems', 'activerecord-2.3.2')}/lib/activerecord/version\.rb\z})
end

it "select the gem from many match gems", :readline do
env = { "EDITOR" => "echo editor", "VISUAL" => "echo visual", "BUNDLER_EDITOR" => "echo bundler_editor" }
bundle "open active", :env => env do |input, _, _|
Expand Down

0 comments on commit 87c17a1

Please sign in to comment.