pry-gem 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5c17d0cff41b4c1a89c229937e8eeca12067ea59
4
+ data.tar.gz: 013cb8d75c4213b0498ebaa97346e29df6b85b36
5
+ SHA512:
6
+ metadata.gz: d42c4e0a9262050378fba8502682e6eaf6d102d42711631a194fa04c2c1d7da7cb8ae3e6361cc9c5e903e6ee2033ac8876f1e09228fdc1a4eb8033c2b0847aea
7
+ data.tar.gz: 4a772ee8ac8ee3bfdb43bbbc5a2ee6038dd6a5eb006b98b6b58f8b5febfdf51e5d25326feb187255d0794bea05059d785bab335bc5286f3255e264558b7a4aea
@@ -0,0 +1,10 @@
1
+ require 'pry'
2
+
3
+ require 'pry-gem/rubygem'
4
+ require 'pry-gem/gem_cd'
5
+ require 'pry-gem/gem_list'
6
+ require 'pry-gem/gem_install'
7
+ require 'pry-gem/gem_open'
8
+ require 'pry-gem/gem_readme'
9
+ require 'pry-gem/gem_search'
10
+ require 'pry-gem/gem_stat'
@@ -0,0 +1,27 @@
1
+ module PryGem
2
+ # @since 1.0.0
3
+ class GemCd < Pry::ClassCommand
4
+ match 'gem-cd'
5
+ group 'Gems'
6
+ description "Change working directory to specified gem's directory."
7
+ command_options argument_required: true
8
+
9
+ banner <<-'BANNER'
10
+ Usage: gem-cd GEM_NAME
11
+
12
+ Change the current working directory to that in which the given gem is
13
+ installed.
14
+ BANNER
15
+
16
+ def process(gem)
17
+ Dir.chdir(Rubygem.spec(gem).full_gem_path)
18
+ output.puts(Dir.pwd)
19
+ end
20
+
21
+ def complete(str)
22
+ Rubygem.complete(str)
23
+ end
24
+ end
25
+ end
26
+
27
+ Pry::Commands.add_command(PryGem::GemCd)
@@ -0,0 +1,33 @@
1
+ module PryGem
2
+ # @since 1.0.0
3
+ class GemInstall < Pry::ClassCommand
4
+ match 'gem-install'
5
+ group 'Gems'
6
+ description 'Install a gem and refresh the gem cache.'
7
+ command_options argument_required: true
8
+
9
+ banner <<-'BANNER'
10
+ Usage: gem-install GEM_NAME
11
+
12
+ Installs the given gem, refreshes the gem cache, and requires the gem for
13
+ you based on a best guess from the gem name.
14
+
15
+ gem-install pry-stack_explorer
16
+ BANNER
17
+
18
+ def setup
19
+ require 'rubygems/dependency_installer' unless defined? Gem::DependencyInstaller
20
+ end
21
+
22
+ def process(gem)
23
+ Rubygem.install(gem)
24
+ output.puts "Gem `#{green(gem)}` installed."
25
+ require gem
26
+ rescue LoadError
27
+ require_path = gem.split('-').join('/')
28
+ require require_path
29
+ end
30
+ end
31
+ end
32
+
33
+ Pry::Commands.add_command(PryGem::GemInstall)
@@ -0,0 +1,41 @@
1
+ module PryGem
2
+ # @since 1.0.0
3
+ class GemList < Pry::ClassCommand
4
+ match 'gem-list'
5
+ group 'Gems'
6
+ description 'List and search installed gems.'
7
+
8
+ banner <<-'BANNER'
9
+ Usage: gem-list [REGEX]
10
+
11
+ List all installed gems, when a regex is provided, limit the output to
12
+ those that match the regex.
13
+ BANNER
14
+
15
+ def process(pattern = nil)
16
+ gems(pattern).each do |gem, specs|
17
+ sort_specs!(specs)
18
+
19
+ versions = specs.each_with_index.map do |spec, index|
20
+ index == 0 ? bright_green(spec.version.to_s) : green(spec.version.to_s)
21
+ end
22
+
23
+ output.puts "#{default gem} (#{versions.join ', '})"
24
+ end
25
+ end
26
+
27
+ private
28
+
29
+ def gems(pattern)
30
+ Rubygem.list(Regexp.compile(pattern || '')).group_by(&:name)
31
+ end
32
+
33
+ def sort_specs!(specs)
34
+ specs.sort! do |a, b|
35
+ Gem::Version.new(b.version) <=> Gem::Version.new(a.version)
36
+ end
37
+ end
38
+ end
39
+ end
40
+
41
+ Pry::Commands.add_command(PryGem::GemList)
@@ -0,0 +1,30 @@
1
+ module PryGem
2
+ # @since 1.0.0
3
+ class GemOpen < Pry::ClassCommand
4
+ match 'gem-open'
5
+ group 'Gems'
6
+ description 'Opens the working directory of the gem in your editor.'
7
+ command_options argument_required: true
8
+
9
+ banner <<-'BANNER'
10
+ Usage: gem-open GEM_NAME
11
+
12
+ Change the current working directory to that in which the given gem is
13
+ installed, and then opens your text editor.
14
+
15
+ gem-open pry-exception_explorer
16
+ BANNER
17
+
18
+ def process(gem)
19
+ Dir.chdir(Rubygem.spec(gem).full_gem_path) do
20
+ Pry::Editor.new(_pry_).invoke_editor(".", 0, false)
21
+ end
22
+ end
23
+
24
+ def complete(str)
25
+ Rubygem.complete(str)
26
+ end
27
+ end
28
+ end
29
+
30
+ Pry::Commands.add_command(PryGem::GemOpen)
@@ -0,0 +1,30 @@
1
+ module PryGem
2
+ # @since 1.0.0
3
+ class GemReadme < Pry::ClassCommand
4
+ match 'gem-readme'
5
+ description 'Show the readme bundled with a rubygem'
6
+ group 'Gems'
7
+ command_options argument_required: true
8
+ banner <<-BANNER
9
+ gem-readme gem
10
+ Show the readme bundled with a rubygem
11
+ BANNER
12
+
13
+ def process(name)
14
+ spec = Gem::Specification.find_by_name(name)
15
+ glob = File.join(spec.full_gem_path, 'README*')
16
+ readme = Dir[glob][0]
17
+
18
+ unless File.exist?(readme.to_s)
19
+ raise Pry::CommandError, "Gem '#{name}' doesn't appear to have a README"
20
+ end
21
+
22
+ _pry_.pager.page File.read(readme)
23
+ rescue Gem::LoadError
24
+ raise Pry::CommandError,
25
+ "Gem '#{name}' wasn't found. Are you sure it is installed?"
26
+ end
27
+ end
28
+ end
29
+
30
+ Pry::Commands.add_command(PryGem::GemReadme)
@@ -0,0 +1,46 @@
1
+ module PryGem
2
+ # @since 1.0.0
3
+ class GemSearch < Pry::ClassCommand
4
+ match 'gem-search'
5
+ description 'Search for a gem with the rubygems.org JSON API'
6
+ group 'Gems'
7
+ command_options argument_required: true
8
+ banner <<-BANNER
9
+ gem-search [options] gem
10
+
11
+ Search for a gem with the rubygems.org HTTP API
12
+ BANNER
13
+
14
+ API_ENDPOINT = 'https://rubygems.org/api/v1/search.json'.freeze
15
+
16
+ def setup
17
+ require 'json' unless defined?(JSON)
18
+ require 'net/http' unless defined?(Net::HTTP)
19
+ end
20
+
21
+ def options(opt)
22
+ opt.on :l, :limit, 'Limit the number of results (max: 30)',
23
+ default: 10,
24
+ as: Integer,
25
+ argument: true
26
+ end
27
+
28
+ def process(str)
29
+ uri = URI.parse(API_ENDPOINT)
30
+ uri.query = URI.encode_www_form(query: str)
31
+ gems = JSON.parse(Net::HTTP.get(uri))
32
+ _pry_.pager.page list_as_string(gems, opts[:limit])
33
+ end
34
+
35
+ private
36
+
37
+ def list_as_string(gems, limit = 10)
38
+ gems[0..limit - 1].map do |gem|
39
+ name, version, info = gem.values_at 'name', 'version', 'info'
40
+ "#{bold(name)} #{bold('v' + version)} \n#{info}\n\n"
41
+ end.join
42
+ end
43
+ end
44
+ end
45
+
46
+ Pry::Commands.add_command(PryGem::GemSearch)
@@ -0,0 +1,91 @@
1
+ module PryGem
2
+ # @since 1.0.0
3
+ class GemStat < Pry::ClassCommand
4
+ require 'json'
5
+ require 'net/http'
6
+ STAT_HOST = "rubygems.org".freeze
7
+ STAT_PORT = 443
8
+ STAT_PATH = "/api/v1/gems/%s.json".freeze
9
+ FAIL_WHALE = <<-FAILWHALE.freeze
10
+ W W W
11
+ W W W W
12
+ '. W
13
+ .-""-._ \ \.--|
14
+ / "-..__) .-'
15
+ | _ /
16
+ \'-.__, .__.,'
17
+ `'----'._\--'
18
+ VVVVVVVVVVVVVVVVVVVVV
19
+ FAILWHALE
20
+
21
+ match 'gem-stat'
22
+ description 'Show the statistics of a gem (requires internet connection)'
23
+ group 'Gems'
24
+ command_options argument_required: true
25
+ banner <<-BANNER
26
+ gem-stats name
27
+
28
+ Show the statistics of a gem.
29
+ Requires an internet connection.
30
+ BANNER
31
+
32
+ def process(name)
33
+ client = Net::HTTP.start STAT_HOST, STAT_PORT, use_ssl: true
34
+ res = client.get STAT_PATH % URI.encode_www_form_component(name)
35
+ case res
36
+ when Net::HTTPOK
37
+ _pry_.pager.page format_gem(JSON.parse(res.body))
38
+ when Net::HTTPServiceUnavailable
39
+ _pry_.pager.page <<-FAILURE
40
+ #{bright_blue(FAIL_WHALE)}
41
+ #{bright_red('Ruby On Rails')}
42
+ #{bright_red('Net::HTTPServiceUnavailable')}
43
+ FAILURE
44
+ else
45
+ raise Pry::CommandError, "Bad response (#{res.class})"
46
+ end
47
+ ensure
48
+ client.finish if client
49
+ end
50
+
51
+ private
52
+
53
+ def format_gem(h)
54
+ h = Pry::Config.from_hash(h)
55
+ format_str = unindent <<-FORMAT
56
+ %{name} %{version}
57
+ --
58
+ Total Downloads : %{downloads}
59
+ Version Downloads : %{version_downloads}
60
+
61
+ #{red('Dependencies')} (runtime)
62
+ --
63
+ %{rdependencies}
64
+
65
+ #{red('Dependencies')} (development)
66
+ %{ddependencies}
67
+ FORMAT
68
+ format(
69
+ format_str,
70
+ name: green(h.name),
71
+ version: bold("v#{h.version}"),
72
+ downloads: h.downloads,
73
+ version_downloads: h.version_downloads,
74
+ rdependencies: format_dependencies(h.dependencies.runtime),
75
+ ddependencies: format_dependencies(h.dependencies.development)
76
+ )
77
+ end
78
+
79
+ def format_dependencies(rdeps)
80
+ return bold('None') if rdeps.empty?
81
+
82
+ with_line_numbers(
83
+ rdeps.map { |h| "#{h['name']} (#{h['requirements']})" }.join("\n"),
84
+ 1,
85
+ :bold
86
+ )
87
+ end
88
+ end
89
+ end
90
+
91
+ Pry::Commands.add_command(PryGem::GemStat)
@@ -0,0 +1,83 @@
1
+ require 'rubygems'
2
+
3
+ module PryGem
4
+ # @since 1.0.0
5
+ module Rubygem
6
+ class << self
7
+ def installed?(name)
8
+ if Gem::Specification.respond_to?(:find_all_by_name)
9
+ Gem::Specification.find_all_by_name(name).any?
10
+ else
11
+ Gem.source_index.find_name(name).first
12
+ end
13
+ end
14
+
15
+ # Get the gem spec object for the given gem name.
16
+ #
17
+ # @param [String] name
18
+ # @return [Gem::Specification]
19
+ def spec(name)
20
+ specs = if Gem::Specification.respond_to?(:each)
21
+ Gem::Specification.find_all_by_name(name)
22
+ else
23
+ Gem.source_index.find_name(name)
24
+ end
25
+
26
+ first_spec = specs.max_by { |spec| Gem::Version.new(spec.version) }
27
+
28
+ first_spec || raise(CommandError, "Gem `#{name}` not found")
29
+ end
30
+
31
+ # List gems matching a pattern.
32
+ #
33
+ # @param [Regexp] pattern
34
+ # @return [Array<Gem::Specification>]
35
+ def list(pattern = /.*/)
36
+ if Gem::Specification.respond_to?(:each)
37
+ Gem::Specification.select { |spec| spec.name =~ pattern }
38
+ else
39
+ Gem.source_index.gems.values.select { |spec| spec.name =~ pattern }
40
+ end
41
+ end
42
+
43
+ # Completion function for gem-cd and gem-open.
44
+ #
45
+ # @param [String] so_far what the user's typed so far
46
+ # @return [Array<String>] completions
47
+ def complete(so_far)
48
+ if so_far =~ / ([^ ]*)\z/
49
+ list(/\A#{Regexp.last_match(2)}/).map(&:name)
50
+ else
51
+ list.map(&:name)
52
+ end
53
+ end
54
+
55
+ # Installs a gem with all its dependencies.
56
+ #
57
+ # @param [String] name
58
+ # @return [void]
59
+ def install(name)
60
+ require 'rubygems/dependency_installer'
61
+ gem_config = Gem.configuration['gem']
62
+ gemrc_opts = (gem_config.nil? ? "" : gem_config.split(' '))
63
+ destination = if gemrc_opts.include?('--user-install')
64
+ Gem.user_dir
65
+ elsif File.writable?(Gem.dir)
66
+ Gem.dir
67
+ else
68
+ Gem.user_dir
69
+ end
70
+ installer = Gem::DependencyInstaller.new(install_dir: destination)
71
+ installer.install(name)
72
+ rescue Errno::EACCES
73
+ raise CommandError,
74
+ "Insufficient permissions to install #{green(name)}."
75
+ rescue Gem::GemNotFoundException
76
+ raise CommandError,
77
+ "Gem #{green(name)} not found. Aborting installation."
78
+ else
79
+ Gem.refresh
80
+ end
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,3 @@
1
+ module PryGem
2
+ VERSION = '1.0.0'.freeze
3
+ end
@@ -0,0 +1,17 @@
1
+ RSpec.describe "gem-list" do
2
+ let(:pry) { Pry.new }
3
+
4
+ it 'should work arglessly' do
5
+ list = pry_eval('gem-list')
6
+ expect(list).to match(/rspec \(/)
7
+ end
8
+
9
+ it 'should find arg' do
10
+ prylist = pry_eval('gem-list rspec')
11
+ expect(prylist).to match(/rspec-core \(/)
12
+ end
13
+
14
+ it 'should return non-results as silence' do
15
+ expect(pry_eval('gem-list aoeuoueouaou')).to be_empty
16
+ end
17
+ end
@@ -0,0 +1,10 @@
1
+ require 'pry-gem'
2
+ require 'pry/testable'
3
+ require 'rspec'
4
+
5
+ RSpec.configure do |c|
6
+ c.order = 'random'
7
+ c.color = true
8
+ c.disable_monkey_patching!
9
+ c.include Pry::Testable::Evalable
10
+ end
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pry-gem
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Kyrylo Silin
8
+ - Robert Gleeson
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2019-03-10 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: pry
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: '0.12'
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: '0.12'
28
+ - !ruby/object:Gem::Dependency
29
+ name: rspec
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: '3.8'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: '3.8'
42
+ - !ruby/object:Gem::Dependency
43
+ name: rake
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - "~>"
47
+ - !ruby/object:Gem::Version
48
+ version: '10.0'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - "~>"
54
+ - !ruby/object:Gem::Version
55
+ version: '10.0'
56
+ description:
57
+ email:
58
+ - silin@kyrylo.org
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - lib/pry-gem.rb
64
+ - lib/pry-gem/gem_cd.rb
65
+ - lib/pry-gem/gem_install.rb
66
+ - lib/pry-gem/gem_list.rb
67
+ - lib/pry-gem/gem_open.rb
68
+ - lib/pry-gem/gem_readme.rb
69
+ - lib/pry-gem/gem_search.rb
70
+ - lib/pry-gem/gem_stat.rb
71
+ - lib/pry-gem/rubygem.rb
72
+ - lib/pry-gem/version.rb
73
+ - spec/gem_list_spec.rb
74
+ - spec/spec_helper.rb
75
+ homepage: http://pryrepl.org
76
+ licenses:
77
+ - MIT
78
+ metadata: {}
79
+ post_install_message:
80
+ rdoc_options: []
81
+ require_paths:
82
+ - lib
83
+ required_ruby_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: '1.9'
88
+ required_rubygems_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ requirements: []
94
+ rubyforge_project:
95
+ rubygems_version: 2.6.13
96
+ signing_key:
97
+ specification_version: 4
98
+ summary: A collection of commands to work with gems within Pry
99
+ test_files:
100
+ - spec/spec_helper.rb
101
+ - spec/gem_list_spec.rb