development

설치된 보석 목록?

big-blog 2020. 7. 2. 07:15
반응형

설치된 보석 목록?


설치된 gem 목록을 얻기 위해 호출 할 수있는 Ruby 메소드가 있습니까?

의 출력을 구문 분석하고 싶습니다 gem list. 다른 방법이 있습니까?


Gem 명령은 현재 Ruby 1.9+에 포함되어 있으며 Ruby pre-1.9에 추가 된 표준입니다.

require 'rubygems'

name = /^/i
dep = Gem::Dependency.new(name, Gem::Requirement.default)
specs = Gem.source_index.search(dep)
puts specs[0..5].map{ |s| "#{s.name} #{s.version}" }
# >> Platform 0.4.0
# >> abstract 1.0.0
# >> actionmailer 3.0.5
# >> actionpack 3.0.5
# >> activemodel 3.0.5
# >> activerecord 3.0.5

목록을 얻는 업데이트 된 방법은 다음과 같습니다.

require 'rubygems'

def local_gems
   Gem::Specification.sort_by{ |g| [g.name.downcase, g.version] }.group_by{ |g| g.name }
end

local_gems의존 하기 때문에 group_by보석의 해시를 반환합니다. 여기서 키는 보석의 이름이고 값은 보석 사양의 배열입니다. 값은 버전 번호별로 정렬되어 설치된 해당 gem 인스턴스의 배열입니다.

따라서 다음과 같은 작업을 수행 할 수 있습니다.

my_local_gems = local_gems()

my_local_gems['actionmailer']
# => [Gem::Specification.new do |s|
#       s.authors = ["David Heinemeier Hansson"]
#       s.date = Time.utc(2013, 12, 3)
#       s.dependencies = [Gem::Dependency.new("actionpack",
#         Gem::Requirement.new(["= 4.0.2"]),
#         :runtime),
#        Gem::Dependency.new("mail",
#         Gem::Requirement.new(["~> 2.5.4"]),
#         :runtime)]
#       s.description = "Email on Rails. Compose, deliver, receive, and test emails using the familiar controller/view pattern. First-class support for multipart email and attachments."
#       s.email = "david@loudthinking.com"
#       s.homepage = "http://www.rubyonrails.org"
#       s.licenses = ["MIT"]
#       s.name = "actionmailer"
#       s.require_paths = ["lib"]
#       s.required_ruby_version = Gem::Requirement.new([">= 1.9.3"])
#       s.requirements = ["none"]
#       s.rubygems_version = "2.0.14"
#       s.specification_version = 4
#       s.summary = "Email composition, delivery, and receiving framework (part of Rails)."
#       s.version = Gem::Version.new("4.0.2")
#       end]

과:

puts my_local_gems.map{ |name, specs| 
  [ 
    name,
    specs.map{ |spec| spec.version.to_s }.join(',')
  ].join(' ') 
}
# >> actionmailer 4.0.2
...
# >> arel 4.0.1,5.0.0
...
# >> ZenTest 4.9.5
# >> zucker 13.1

마지막 예는 gem query --local명령 줄 과 비슷 하며 특정 gem 사양에 대한 모든 정보에 액세스 할 수 있습니다.


이것은 내가 설치 한 모든 보석을 나열합니다.

gem query --local

http://guides.rubygems.org/command-reference/#gem-list

See 2.7 Listing all installed gems


Both

gem query --local

and

 ruby -S gem list --local

list 69 entries

While

ruby -e 'puts Gem::Specification.all_names'

gives me 82

I used wc -l to get the numbers. Not sure if that is the right way to check. Tried to redirect the output to text files and diff'ed but that didn't help - will need to compare manually one by one.


There's been a method for this for ages:

ruby -e 'puts Gem::Specification.all_names'

Gem::Specification.map {|a| a.name}

However, if your app uses Bundler it will return only list of dependent local gems. To get all installed:

def all_installed_gems
   Gem::Specification.all = nil    
   all = Gem::Specification.map{|a| a.name}  
   Gem::Specification.reset
   all
end

use this code (in console mode):

Gem::Specification.all_names

A more modern version would be to use something akin to the following...

require 'rubygems'
puts Gem::Specification.all().map{|g| [g.name, g.version.to_s].join('-') }

NOTE: very similar the first part of an answer by Evgeny... but due to page formatting, it's easy to miss.


Try it in the terminal:

ruby -S gem list --local

Here's a really nice one-liner to print all the Gems along with their version, homepage, and description:

Gem::Specification.sort{|a,b| a.name <=> b.name}.map {|a| puts "#{a.name} (#{a.version})"; puts "-" * 50; puts a.homepage; puts a.description; puts "\n\n"};nil

Maybe you can get the files (gems) from the gems directory?

gemsdir = "gems directory"
gems = Dir.new(gemsdir).entries

From within your debugger type $LOAD_PATH to get a list of your gems. If you don't have a debugger, install pry:

gem install pry
pry
Pry(main)> $LOAD_PATH

This will output an array of your installed gems.

참고URL : https://stackoverflow.com/questions/5177634/list-of-installed-gems

반응형