gem_require 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,6 +2,7 @@ require 'rubygems/command'
2
2
  require 'rubygems/dependency_installer'
3
3
  require 'rubygems/install_update_options'
4
4
  require 'rubygems/version_option'
5
+ require 'rubygems/spec_fetcher'
5
6
 
6
7
  class Gem::Commands::RequireCommand < Gem::Command
7
8
 
@@ -16,13 +17,19 @@ class Gem::Commands::RequireCommand < Gem::Command
16
17
  :format_executable => false,
17
18
  :test => false,
18
19
  :version => Gem::Requirement.default,
20
+ :latest => false,
19
21
  }
20
22
  )
21
23
 
22
24
  super 'require', 'Install or update a gem', defaults
23
25
 
24
- add_install_update_options
25
26
  add_version_option
27
+ add_option('--latest',
28
+ 'Get the latest matching version') do |value, options|
29
+ options[:latest] = true
30
+ end
31
+
32
+ add_install_update_options
26
33
 
27
34
  end
28
35
 
@@ -66,23 +73,41 @@ and installs them if necessary.
66
73
 
67
74
  end
68
75
 
76
+ private
77
+
69
78
  def require_gem(name, version, installer_options)
79
+ if installed_version = gem_uptodate?(name, version)
80
+ say "#{name} (#{installed_version}) is already installed"
81
+ else
82
+ say "Installing #{name} ..."
83
+ install_gem(name, version, installer_options)
84
+ end
85
+ end
70
86
 
87
+ def gem_uptodate?(name, version)
71
88
  dependency = Gem::Dependency.new(name, version)
72
-
73
- if !Gem.source_index.search(dependency).empty?
74
- say "#{dependency} is already installed"
75
- return false
89
+ installed = current_spec(dependency)
90
+ if installed
91
+ if !options[:latest] || installed.version == latest_available_version_of(dependency)
92
+ installed.version
93
+ end
76
94
  end
95
+ end
77
96
 
78
- say "Installing #{dependency} ..."
79
-
97
+ def current_spec(dependency)
98
+ Gem.source_index.search(dependency).last
99
+ end
100
+
101
+ def latest_available_version_of(dependency)
102
+ Gem::SpecFetcher.fetcher.fetch(dependency).map { |x| x.first.version }.max
103
+ end
104
+
105
+ def install_gem(name, version, installer_options)
80
106
  installer = Gem::DependencyInstaller.new(installer_options)
81
107
  installer.install(name, version)
82
-
83
108
  installer.installed_gems.each do |spec|
84
109
  say "Installed #{spec.full_name}"
85
110
  end
86
111
  end
87
-
112
+
88
113
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gem_require
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
4
+ hash: 27
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 1
10
- version: 0.0.1
9
+ - 2
10
+ version: 0.0.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Mike Williams