diff options
author | yoka <jesse.ikonen@gmail.com> | 2022-12-22 08:39:00 +0200 |
---|---|---|
committer | git <svn-admin@ruby-lang.org> | 2023-01-04 13:13:19 +0000 |
commit | 87c17a141dc4491abf5282d164f2cba13ade18c4 (patch) | |
tree | 2c296503f3d5257210e9a61c602ae22fc1148429 | |
parent | 3d6500ee6e3676979d5199dc7b5feab0d15235de (diff) | |
download | ruby-87c17a141dc4491abf5282d164f2cba13ade18c4.tar.gz |
[rubygems/rubygems] Enhance bundle open with --path option
https://github.com/rubygems/rubygems/commit/3bf8e59304
-rw-r--r-- | lib/bundler/cli.rb | 1 | ||||
-rw-r--r-- | lib/bundler/cli/open.rb | 9 | ||||
-rw-r--r-- | lib/bundler/man/bundle-open.1 | 22 | ||||
-rw-r--r-- | lib/bundler/man/bundle-open.1.ronn | 10 | ||||
-rw-r--r-- | spec/bundler/commands/open_spec.rb | 53 |
5 files changed, 89 insertions, 6 deletions
diff --git a/lib/bundler/cli.rb b/lib/bundler/cli.rb index 6745740f11..3f8d0bd0e6 100644 --- a/lib/bundler/cli.rb +++ b/lib/bundler/cli.rb @@ -509,6 +509,7 @@ module Bundler 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 diff --git a/lib/bundler/cli/open.rb b/lib/bundler/cli/open.rb index ea504344f3..ebd86fd4b1 100644 --- a/lib/bundler/cli/open.rb +++ b/lib/bundler/cli/open.rb @@ -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 @@ -15,10 +16,10 @@ module Bundler 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(" ")}'") diff --git a/lib/bundler/man/bundle-open.1 b/lib/bundler/man/bundle-open.1 index a3014f34a8..c831bf9ce9 100644 --- a/lib/bundler/man/bundle-open.1 +++ b/lib/bundler/man/bundle-open.1 @@ -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\. @@ -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\. + diff --git a/lib/bundler/man/bundle-open.1.ronn b/lib/bundler/man/bundle-open.1.ronn index 497beac93f..a857f3a965 100644 --- a/lib/bundler/man/bundle-open.1.ronn +++ b/lib/bundler/man/bundle-open.1.ronn @@ -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 @@ -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. diff --git a/spec/bundler/commands/open_spec.rb b/spec/bundler/commands/open_spec.rb index 85f15176b4..570e0f5961 100644 --- a/spec/bundler/commands/open_spec.rb +++ b/spec/bundler/commands/open_spec.rb @@ -58,6 +58,59 @@ RSpec.describe "bundle open" do 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, _, _| |