gem-prune 1.4.1 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md ADDED
@@ -0,0 +1,27 @@
1
+ # gem-prune
2
+
3
+ Keep your local gem installation from growing wild.
4
+
5
+ ## Installation
6
+
7
+ $ gem install gem-prune
8
+
9
+ ## Usage
10
+
11
+ # keep the latest sinatra
12
+ $ gem keep sinatra
13
+
14
+ # keep the latest rails and one from the 2.3 series
15
+ $ gem keep rails
16
+ $ gem keep rails -v "~> 2.3.0"
17
+
18
+ # remove unwanted gems
19
+ $ gem prune
20
+
21
+ ## License
22
+
23
+ MIT License
24
+
25
+ ## Copyright
26
+
27
+ Copyright (c) 2010 David Dollar. See LICENSE for details.
@@ -0,0 +1,66 @@
1
+ require 'rubygems/command'
2
+ require 'rubygems/command_manager'
3
+
4
+ class Gem::Commands::KeepCommand < Gem::Command
5
+
6
+ def initialize
7
+ super 'keep', 'Mark a gem for keeping'
8
+
9
+ add_option('-v', '--version VERSION', 'Which version to keep',
10
+ '(3.0, >=2.5, ~>1.5)') do |value, options|
11
+ options[:version] = value
12
+ end
13
+ end
14
+
15
+ def execute
16
+ gem = get_one_gem_name
17
+ load_configuration
18
+ gems_to_keep[gem] ||= []
19
+ gems_to_keep[gem] << options[:version]
20
+ gems_to_keep[gem] = gems_to_keep[gem].uniq.compact
21
+ save_configuration
22
+ end
23
+
24
+ private ######################################################################
25
+
26
+ def load_configuration
27
+ @configuration ||= begin
28
+ config = YAML::load_file(settings_filename)
29
+ config = upgrade_configuration(config) if config.first.first == "keep"
30
+ unpack_configuration(config)
31
+ end
32
+ end
33
+
34
+ def save_configuration
35
+ File.open(settings_filename, "w") do |file|
36
+ file.puts(YAML::dump(pack_configuration(@configuration)))
37
+ end
38
+ end
39
+
40
+ def unpack_configuration(config)
41
+ config.inject({}) do |memo, (gem, keep)|
42
+ memo.update(gem => keep)
43
+ end
44
+ end
45
+
46
+ def pack_configuration(config)
47
+ config.map do |gem, keep|
48
+ [gem, keep]
49
+ end.sort_by(&:first)
50
+ end
51
+
52
+ def upgrade_configuration(config)
53
+ config["keep"].map do |gem|
54
+ [gem, []]
55
+ end
56
+ end
57
+
58
+ def gems_to_keep
59
+ @configuration
60
+ end
61
+
62
+ def settings_filename
63
+ File.expand_path('~/.gem-prune')
64
+ end
65
+
66
+ end
@@ -11,42 +11,13 @@ class Gem::Commands::PruneCommand < Gem::Command
11
11
  end
12
12
 
13
13
  def execute
14
- return if leaves.empty?
15
- leaves.each do |name, versions|
16
- leaf_versions = versions.map { |v| v.version }.join(', ')
17
- kept_versions = (gems[name].versions - versions).map { |v| v.version }.join(', ')
18
- begin
19
- question = "#{name} (#{leaf_versions}) keeping (#{kept_versions})\n"
20
- question << " [k] keep this gem\n"
21
- question << " [u] uninstall this gem\n"
22
- question << " [s] skip (default)\n"
23
- question << "> "
24
- print question
25
- case $stdin.gets.chomp
26
- when 'k' then keep_gem(name)
27
- when 'u' then uninstall(name)
28
- end
29
- rescue Gem::FilePermissionError
30
- puts 'Unable to uninstall. Try sudo?'
31
- next
32
- end
33
- end
34
- rescue Exception => ex
35
- puts "Unhandled Exception: #{ex.message}"
36
- end
37
-
38
- ## commands ##################################################################
39
-
40
- def keep_gem(name)
41
- kept = load_kept_gems
42
- kept << name
43
- save_kept_gems(kept.uniq)
44
- end
45
-
46
- def uninstall(name)
47
- leaves[name].each do |version|
48
- uninstaller(name, version.version).uninstall
14
+ load_configuration
15
+ mark_kept_versions
16
+ versions_to_prune.each do |version|
17
+ ui = Gem::Uninstaller.new(version.name, :version => version.version, :ignore => true)
18
+ ui.uninstall
49
19
  end
20
+ save_configuration
50
21
  end
51
22
 
52
23
  private ######################################################################
@@ -69,7 +40,6 @@ private ######################################################################
69
40
  gems.each do |name, gem|
70
41
  gem.versions.each do |version|
71
42
  version.raw.dependencies.each do |dep|
72
- next if ignore_dependency(dep)
73
43
  next unless gems[dep.name]
74
44
  match = gems[dep.name].versions.sort.reverse.detect { |v| dep =~ v }
75
45
  next unless match
@@ -80,53 +50,77 @@ private ######################################################################
80
50
  end
81
51
  end
82
52
 
83
- def leaves
84
- @leaves ||= gems.keys.inject({}) do |memo, name|
85
- leaves = gems[name].versions.select do |version|
86
- version.dependants.length.zero? && !ignore_version(version)
87
- end
88
- memo[name] = leaves unless leaves.length.zero?
89
- memo
53
+ def load_configuration
54
+ @configuration ||= begin
55
+ config = YAML::load_file(settings_filename)
56
+ config = upgrade_configuration(config) if config.first.first == "keep"
57
+ unpack_configuration(config)
90
58
  end
91
59
  end
92
60
 
93
- def ignore_version(version)
94
- name = version.name
95
- highest_version = gems[name].versions.sort.last
96
- needed_versions = gems[name].versions.select { |v| v.dependants.length > 0 }
97
- ignored_versions = gems[name].versions.select { |v| ignore_specification(v.raw) }
98
- return true if ignore_specification(version.raw)
99
- return true if load_kept_gems.include?(name) && version == highest_version
100
- return true if needed_versions.length > 0 && version == highest_version
101
- return true if ignored_versions.length > 0 && version == highest_version
102
- false
61
+ def save_configuration
62
+ File.open(settings_filename, "w") do |file|
63
+ file.puts(YAML::dump(pack_configuration(@configuration)))
64
+ end
103
65
  end
104
66
 
105
- def ignore_specification(specification)
106
- return true if specification.loaded_from =~ /\/System\/Library/
107
- false
67
+ def unpack_configuration(config)
68
+ config.inject({}) do |memo, (gem, keep)|
69
+ memo.update(gem => keep)
70
+ end
108
71
  end
109
72
 
110
- def ignore_dependency(dependency)
111
- false
73
+ def pack_configuration(config)
74
+ config.map do |gem, keep|
75
+ [gem, keep]
76
+ end.sort_by(&:first)
112
77
  end
113
78
 
114
- def uninstaller(name, version)
115
- Gem::Uninstaller.new(name, :version => version, :executables => true)
79
+ def upgrade_configuration(config)
80
+ config["keep"].map do |gem|
81
+ [gem, []]
82
+ end
116
83
  end
117
84
 
118
- def load_kept_gems
119
- YAML::load_file(settings_filename)['keep']
120
- rescue
121
- []
85
+ def gems_to_keep
86
+ @configuration
122
87
  end
123
88
 
124
- def save_kept_gems(gems)
125
- File.open(settings_filename, 'w') do |file|
126
- file.puts({ 'keep' => gems.sort }.to_yaml)
89
+ def mark_kept_versions
90
+ gems.each do |name, gem|
91
+ mark_kept(gem.versions.sort.last)
92
+ end
93
+ gems_to_keep.each do |name, keep|
94
+ keep = [">= 0"] if keep.empty?
95
+ keep.each do |req|
96
+ next unless gems[name]
97
+ requirement = Gem::Requirement.new(req)
98
+ gem_to_keep = gems[name].versions.select do |v|
99
+ requirement.satisfied_by?(Gem::Version.new(v.version))
100
+ end.sort.last
101
+ mark_kept(gem_to_keep) if gem_to_keep
102
+ end
127
103
  end
128
104
  end
129
105
 
106
+ def mark_kept(version)
107
+ return if version.keep
108
+ version.keep = true
109
+ version.dependencies.each { |v| mark_kept(v) }
110
+ end
111
+
112
+ def versions_to_keep
113
+ versions_with_kept_flag(true)
114
+ end
115
+
116
+ def versions_to_prune
117
+ versions_with_kept_flag(false)
118
+ end
119
+
120
+ def versions_with_kept_flag(value)
121
+ gems.map { |name, gem| gem.versions }.flatten.select { |v| v.keep == value }
122
+ end
123
+
130
124
  def settings_filename
131
125
  File.expand_path('~/.gem-prune')
132
126
  end
@@ -4,10 +4,12 @@ module Gem; module Prune; class Version
4
4
  attr_reader :raw
5
5
  attr_reader :dependants
6
6
  attr_reader :dependencies
7
+ attr_accessor :keep
7
8
 
8
9
  def initialize(gem, raw)
9
10
  @gem = gem
10
11
  @raw = raw
12
+ @keep = false
11
13
 
12
14
  clear_relationships
13
15
  end
data/lib/gem-prune.rb ADDED
@@ -0,0 +1,3 @@
1
+ module GemPrune
2
+ VERSION = "2.0.0"
3
+ end
@@ -1,3 +1,5 @@
1
+ require 'gem/commands/keep_command'
1
2
  require 'gem/commands/prune_command'
2
3
 
4
+ Gem::CommandManager.instance.register_command :keep
3
5
  Gem::CommandManager.instance.register_command :prune
metadata CHANGED
@@ -1,62 +1,139 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gem-prune
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.1
4
+ hash: 15
5
+ prerelease: false
6
+ segments:
7
+ - 2
8
+ - 0
9
+ - 0
10
+ version: 2.0.0
5
11
  platform: ruby
6
12
  authors:
7
- - David Dollar
13
+ - |
14
+ David Dollar
15
+
8
16
  autorequire:
9
17
  bindir: bin
10
18
  cert_chain: []
11
19
 
12
- date: 2009-06-18 00:00:00 -04:00
13
- default_executable: gem-prune
14
- dependencies: []
20
+ date: 2010-08-30 00:00:00 -04:00
21
+ default_executable:
22
+ dependencies:
23
+ - !ruby/object:Gem::Dependency
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
32
+ version: "0"
33
+ type: :development
34
+ name: parka
35
+ prerelease: false
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ hash: 3
44
+ segments:
45
+ - 0
46
+ version: "0"
47
+ type: :development
48
+ name: rcov
49
+ prerelease: false
50
+ version_requirements: *id002
51
+ - !ruby/object:Gem::Dependency
52
+ requirement: &id003 !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ hash: 3
58
+ segments:
59
+ - 0
60
+ version: "0"
61
+ type: :development
62
+ name: rr
63
+ prerelease: false
64
+ version_requirements: *id003
65
+ - !ruby/object:Gem::Dependency
66
+ requirement: &id004 !ruby/object:Gem::Requirement
67
+ none: false
68
+ requirements:
69
+ - - ~>
70
+ - !ruby/object:Gem::Version
71
+ hash: 62196421
72
+ segments:
73
+ - 2
74
+ - 0
75
+ - 0
76
+ - beta
77
+ - 19
78
+ version: 2.0.0.beta.19
79
+ type: :development
80
+ name: rspec
81
+ prerelease: false
82
+ version_requirements: *id004
83
+ description: Identify and remove old gems
84
+ email: |
85
+ <ddollar@gmail.com>
86
+
87
+ executables: []
15
88
 
16
- description:
17
- email: <ddollar@gmail.com>
18
- executables:
19
- - gem-prune
20
89
  extensions: []
21
90
 
22
- extra_rdoc_files:
23
- - LICENSE
24
- - README.rdoc
91
+ extra_rdoc_files: []
92
+
25
93
  files:
94
+ - README.md
95
+ - bin/gem-prune
96
+ - lib/gem-prune.rb
97
+ - lib/gem/commands/keep_command.rb
26
98
  - lib/gem/commands/prune_command.rb
27
99
  - lib/gem/prune/gem.rb
28
100
  - lib/gem/prune/version.rb
29
101
  - lib/rubygems_plugin.rb
30
- - LICENSE
31
- - README.rdoc
102
+ - spec/gem-prune_spec.rb
103
+ - spec/spec_helper.rb
32
104
  has_rdoc: true
33
105
  homepage: http://github.com/ddollar/gem-prune
34
106
  licenses: []
35
107
 
36
108
  post_install_message:
37
- rdoc_options:
38
- - --charset=UTF-8
109
+ rdoc_options: []
110
+
39
111
  require_paths:
40
112
  - lib
41
113
  required_ruby_version: !ruby/object:Gem::Requirement
114
+ none: false
42
115
  requirements:
43
116
  - - ">="
44
117
  - !ruby/object:Gem::Version
118
+ hash: 3
119
+ segments:
120
+ - 0
45
121
  version: "0"
46
- version:
47
122
  required_rubygems_version: !ruby/object:Gem::Requirement
123
+ none: false
48
124
  requirements:
49
125
  - - ">="
50
126
  - !ruby/object:Gem::Version
127
+ hash: 3
128
+ segments:
129
+ - 0
51
130
  version: "0"
52
- version:
53
131
  requirements: []
54
132
 
55
- rubyforge_project:
56
- rubygems_version: 1.3.5
133
+ rubyforge_project: nowarning
134
+ rubygems_version: 1.3.7
57
135
  signing_key:
58
136
  specification_version: 3
59
- summary: Identify and remove old Rubygems
60
- test_files:
61
- - spec/gem-prune_spec.rb
62
- - spec/spec_helper.rb
137
+ summary: Identify and remove old gems
138
+ test_files: []
139
+
data/LICENSE DELETED
@@ -1,20 +0,0 @@
1
- Copyright (c) 2009 David Dollar
2
-
3
- Permission is hereby granted, free of charge, to any person obtaining
4
- a copy of this software and associated documentation files (the
5
- "Software"), to deal in the Software without restriction, including
6
- without limitation the rights to use, copy, modify, merge, publish,
7
- distribute, sublicense, and/or sell copies of the Software, and to
8
- permit persons to whom the Software is furnished to do so, subject to
9
- the following conditions:
10
-
11
- The above copyright notice and this permission notice shall be
12
- included in all copies or substantial portions of the Software.
13
-
14
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
- LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
- OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
- WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc DELETED
@@ -1,25 +0,0 @@
1
- = gem-prune
2
-
3
- Adds a 'prune' command to rubygems to help keep things tidy. Traverses your
4
- dependency tree, finds the leaves, and asks you whether or not you want to
5
- keep them. Remembers which gems you want to keep in ~/.gem-prune
6
-
7
- Installation
8
-
9
- $ gem install ddollar-gem-prune
10
-
11
- Usage
12
-
13
- $ sudo gem prune
14
-
15
- Example
16
-
17
- json_pure (1.1.6)
18
- [k] keep this gem
19
- [u] uninstall this gem
20
- [s] skip (default)
21
- >
22
-
23
- == Copyright
24
-
25
- Copyright (c) 2009 David Dollar. See LICENSE for details.