3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

指定した時点での gem のバージョンを調べる

Posted at

StackOverflow や blog を見てると "gem を最新にしたら..." などの記述がしばしばあるが、そのときの最新のバージョンが何なのか書かれていない事がある。

gem のバージョンとリリースされた日付の一覧は https://rubygems.org/gems/[gem name]/versions で見ることができる (例: rails) ので、記事の日付からそのときの最新のバージョンを特定できる。

が、面倒なのでスクリプト書いた。rubygems.org の API を使う gems という gem を使っている。

#!/usr/bin/env ruby
# Usage: ./find_gem_version_at.rb GEM_NAME DATETIME_AS_ISO8601 [REQUIREMENT]
gem "gems", "~> 0.8.3"
require "gems"
require "time"

name, at, requirement = ARGV
at = Time.parse(at)
requirement ||= ">= 0"
requirement = Gem::Requirement.create(requirement)

$stderr.puts "Find gem `#{name}` (#{requirement}) at #{at.iso8601}"

versions = Gems.versions(name)
found = false

versions.each{|v|
  v["built_at"] = Time.parse(v["built_at"]).getlocal
  v["number"] = Gem::Version.create(v["number"])
}.select{|v|
  requirement.satisfied_by?(v["number"])
}.map{|v|
  if !found && v["built_at"] < at
    found = true
    [true, v]
  else
    [false, v]
  end
}.each{|(latest, v)|
  line = "#{v["number"]} (#{v["built_at"].iso8601})"
  if latest
    puts "> #{line}"
  else
    puts "  #{line}"
  end
}

こんな感じで使える。

# rails gem の 2015年1月1日時点の最新のバージョンを調べる
$ ./find_gem_version_at rails 2015-01-01
Find gem `rails` (>= 0) at 2015-01-01T00:00:00+09:00
  4.2.1 (2015-03-19T09:00:00+09:00)
  4.2.1.rc4 (2015-03-12T09:00:00+09:00)
  4.2.1.rc3 (2015-03-02T09:00:00+09:00)
  4.2.1.rc2 (2015-02-25T09:00:00+09:00)
  4.2.1.rc1 (2015-02-20T09:00:00+09:00)
> 4.2.0 (2014-12-20T09:00:00+09:00)
  4.2.0.rc3 (2014-12-13T09:00:00+09:00)
  4.2.0.rc2 (2014-12-05T09:00:00+09:00)
  4.2.0.rc1 (2014-11-28T09:00:00+09:00)
...

# Rails 3 系だけに絞る
$ ./find_gem_version_at rails 2015-01-01 '~> 3.0'
Find gem `rails` (~> 3.0) at 2015-01-01T00:00:00+09:00
> 3.2.21 (2014-11-16T09:00:00+09:00)
  3.2.20 (2014-10-29T09:00:00+09:00)
  3.2.19 (2014-07-02T12:00:00+09:00)
  3.2.18 (2014-05-06T09:00:00+09:00)
  3.2.17 (2014-02-18T12:00:00+09:00)
  3.2.16 (2013-12-03T09:00:00+09:00)
  3.2.15 (2013-10-16T09:00:00+09:00)
...
3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?