[rubygems/rubygems] Enhance bundle open with --path option

https://github.com/rubygems/rubygems/commit/3bf8e59304
This commit is contained in:
yoka 2022-12-22 08:39:00 +02:00 committed by git
parent 3d6500ee6e
commit 87c17a141d
5 changed files with 89 additions and 6 deletions

View File

@ -509,6 +509,7 @@ module Bundler
subcommand "config", Config subcommand "config", Config
desc "open GEM", "Opens the source directory of the given bundled gem" 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) def open(name)
require_relative "cli/open" require_relative "cli/open"
Open.new(options, name).run Open.new(options, name).run

View File

@ -2,10 +2,11 @@
module Bundler module Bundler
class CLI::Open class CLI::Open
attr_reader :options, :name attr_reader :options, :name, :path
def initialize(options, name) def initialize(options, name)
@options = options @options = options
@name = name @name = name
@path = options[:path] unless options[:path].nil?
end end
def run def run
@ -15,10 +16,10 @@ module Bundler
if spec.default_gem? 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." 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 else
path = spec.full_gem_path root_path = spec.full_gem_path
Dir.chdir(path) do Dir.chdir(root_path) do
require "shellwords" require "shellwords"
command = Shellwords.split(editor) + [path] command = Shellwords.split(editor) << File.join([root_path, path].compact)
Bundler.with_original_env do Bundler.with_original_env do
system(*command) system(*command)
end || Bundler.ui.info("Could not run '#{command.join(" ")}'") end || Bundler.ui.info("Could not run '#{command.join(" ")}'")

View File

@ -7,7 +7,7 @@
\fBbundle\-open\fR \- Opens the source directory for a gem in your bundle \fBbundle\-open\fR \- Opens the source directory for a gem in your bundle
. .
.SH "SYNOPSIS" .SH "SYNOPSIS"
\fBbundle open\fR [GEM] \fBbundle open\fR [GEM] [\-\-path=PATH]
. .
.SH "DESCRIPTION" .SH "DESCRIPTION"
Opens the source directory of the provided GEM in your editor\. Opens the source directory of the provided GEM in your editor\.
@ -30,3 +30,23 @@ bundle open \'rack\'
. .
.P .P
Will open the source directory for the \'rack\' gem in your bundle\. 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\.

View File

@ -3,7 +3,7 @@ bundle-open(1) -- Opens the source directory for a gem in your bundle
## SYNOPSIS ## SYNOPSIS
`bundle open` [GEM] `bundle open` [GEM] [--path=PATH]
## DESCRIPTION ## DESCRIPTION
@ -17,3 +17,11 @@ Example:
bundle open 'rack' bundle open 'rack'
Will open the source directory for the 'rack' gem in your bundle. 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.

View File

@ -58,6 +58,59 @@ RSpec.describe "bundle open" do
expect(out).to include("bundler_editor #{default_bundle_path("gems", "activerecord-2.3.2")}") expect(out).to include("bundler_editor #{default_bundle_path("gems", "activerecord-2.3.2")}")
end 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 it "select the gem from many match gems", :readline do
env = { "EDITOR" => "echo editor", "VISUAL" => "echo visual", "BUNDLER_EDITOR" => "echo bundler_editor" } env = { "EDITOR" => "echo editor", "VISUAL" => "echo visual", "BUNDLER_EDITOR" => "echo bundler_editor" }
bundle "open active", :env => env do |input, _, _| bundle "open active", :env => env do |input, _, _|