gem_leaves 1.0.6

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt ADDED
@@ -0,0 +1,38 @@
1
+ == 1.0.6 / 2009-01-05
2
+
3
+ * Fixed doubling documentation in the installed gem.
4
+ * First release on RubyForge. Celebrate! :-)
5
+
6
+ == 1.0.5 / 2008-12-07
7
+
8
+ * New --ignore option.
9
+ * New pipe output format.
10
+
11
+ == 1.0.4 / 2008-09-27
12
+
13
+ * Renamed Manifest.txt; one step closer to Echoe standard.
14
+ * Changed gem's name, for consistency's sake.
15
+ * Added the gemspec generated by Echoe.
16
+ * Tweaks to general doc.
17
+ * Changed a bit the way we talk to RubyGems, to avoid deprecation warnings
18
+ with the new RubyGems 1.3.0.
19
+
20
+ == 1.0.3 / 2008-01-08
21
+
22
+ * Adios, hoe! Moved to echoe.
23
+ * New output format: DOT diagram.
24
+
25
+ == 1.0.2 / 2007-12-03
26
+
27
+ * Now the pruning uses the exact match on the gem's short name (so, ignoring
28
+ capistrano doesn't mask capistrano-ext too).
29
+
30
+ == 1.0.1 / 2007-12-01
31
+
32
+ * No leaves, no output.
33
+ * Corrected a huge bug in the configuration file loading function.
34
+ * Reformatted the options' descriptions.
35
+
36
+ == 1.0.0 / 2007-11-30
37
+
38
+ * Birthday!
data/Manifest ADDED
@@ -0,0 +1,7 @@
1
+ bin/gem_leaves
2
+ gem_leaves.gemspec
3
+ History.txt
4
+ lib/gem_leaves.rb
5
+ Manifest
6
+ Rakefile
7
+ README.txt
data/README.txt ADDED
@@ -0,0 +1,55 @@
1
+ gem_leaves
2
+ by Gufo Pelato
3
+
4
+ == DESCRIPTION:
5
+
6
+ gem_leaves is a rather dumb tool which lists the _leaves_ of your system. I
7
+ call _leaves_ the gems with no reverse dependency which can be safely
8
+ removed without breaking anything.
9
+
10
+ == FEATURES/PROBLEMS:
11
+
12
+ * User can customize the tool's behaviour with a simple, YAML configuration
13
+ file.
14
+ * The tool can generate/regenerate its own configuration file.
15
+
16
+ == SYNOPSIS:
17
+
18
+ $ gem_leaves
19
+
20
+ For options' list see output of
21
+
22
+ $ gem_leaves -h
23
+
24
+ == REQUIREMENTS:
25
+
26
+ * RubyGems
27
+
28
+ == INSTALL:
29
+
30
+ $ sudo gem install gem_leaves
31
+
32
+ == LICENSE:
33
+
34
+ (The MIT License)
35
+
36
+ Copyright (c) 2007, 2008, 2009 Gufo Pelato
37
+
38
+ Permission is hereby granted, free of charge, to any person obtaining
39
+ a copy of this software and associated documentation files (the
40
+ 'Software'), to deal in the Software without restriction, including
41
+ without limitation the rights to use, copy, modify, merge, publish,
42
+ distribute, sublicense, and/or sell copies of the Software, and to
43
+ permit persons to whom the Software is furnished to do so, subject to
44
+ the following conditions:
45
+
46
+ The above copyright notice and this permission notice shall be
47
+ included in all copies or substantial portions of the Software.
48
+
49
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
50
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
51
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
52
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
53
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
54
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
55
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,14 @@
1
+ require 'rubygems'
2
+ require 'echoe'
3
+ require './lib/gem_leaves.rb'
4
+
5
+ Echoe.new('gem_leaves', GemLeaves::VERSION) do |p|
6
+ p.author = 'Gufo Pelato'
7
+ p.summary = 'A dumb tool to list removable gems.'
8
+ p.need_tar_gz = false
9
+ p.project = 'gemleaves'
10
+ p.gemspec_format = :yaml
11
+ p.retain_gemspec = true
12
+ p.rdoc_pattern = /^README|^LICENSE/
13
+ p.url = 'http://github.com/baldowl/gem_leaves'
14
+ end
data/bin/gem_leaves ADDED
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+ require 'gem_leaves'
3
+ GemLeaves.new(ARGV).run
@@ -0,0 +1,70 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gem_leaves
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.6
5
+ platform: ruby
6
+ authors:
7
+ - Gufo Pelato
8
+ autorequire:
9
+ bindir: bin
10
+
11
+ date: 2009-01-05 00:00:00 +01:00
12
+ default_executable:
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: echoe
16
+ type: :development
17
+ version_requirement:
18
+ version_requirements: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: "0"
23
+ version:
24
+ description: A dumb tool to list removable gems.
25
+ email: ""
26
+ executables:
27
+ - gem_leaves
28
+ extensions: []
29
+
30
+ extra_rdoc_files:
31
+ - README.txt
32
+ files:
33
+ - bin/gem_leaves
34
+ - gem_leaves.gemspec
35
+ - History.txt
36
+ - lib/gem_leaves.rb
37
+ - Manifest
38
+ - Rakefile
39
+ - README.txt
40
+ has_rdoc: true
41
+ homepage: http://github.com/baldowl/gem_leaves
42
+ post_install_message:
43
+ rdoc_options:
44
+ - --line-numbers
45
+ - --inline-source
46
+ - --title
47
+ - Gem_leaves
48
+ - --main
49
+ - README.txt
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: "0"
57
+ version:
58
+ required_rubygems_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: "1.2"
63
+ version:
64
+ requirements: []
65
+
66
+ rubyforge_project: gemleaves
67
+ rubygems_version: 1.3.1
68
+ specification_version: 2
69
+ summary: A dumb tool to list removable gems.
70
+ test_files: []
data/lib/gem_leaves.rb ADDED
@@ -0,0 +1,194 @@
1
+ require 'optparse'
2
+ require 'yaml'
3
+
4
+ # Amateurish prototype of a tool to check the installed gems looking for
5
+ # _leaves_ that can be removed.
6
+ #
7
+ # I call _leaves_ those gems with no reverse dependency that can be removed
8
+ # without breaking anything. Of course, users may desire to keep something
9
+ # around even if nothing depends on it, so this tool looks for and loads a
10
+ # simple YAML configuration file:
11
+ #
12
+ # ignore:
13
+ # RedCloth: >= 0
14
+ # capistrano: >= 2
15
+ # mongrel_cluster: >= 0
16
+ # piston: >= 0
17
+ # rake: >= 0.7
18
+ # rubygems-update: >= 0
19
+ # ruport-util: = 0.10.0
20
+ # sources: >= 0
21
+ #
22
+ # <tt>gem_leaves</tt> can generate its own configuration file merging the
23
+ # leaves list with the content of an already loaded configuration file (if
24
+ # any). The tool looks for the default configuration file,
25
+ # <tt>.gem_leaves.yml</tt>, in the current working directory and the user's
26
+ # home directory.
27
+
28
+ class GemLeaves
29
+ VERSION = '1.0.6'
30
+
31
+ def initialize(args)
32
+ @options = {:color => "d0ed0e"}
33
+ @configuration = {}
34
+ @leaves = []
35
+ parse_options(args)
36
+ end
37
+
38
+ def run
39
+ load_config_file
40
+ find_leaves
41
+ show_leaves
42
+ generate_config_file
43
+ end
44
+
45
+ protected
46
+
47
+ # Parses the command line arguments.
48
+ def parse_options(args)
49
+ OptionParser.new('Usage: gem_leaves [OPTIONS]') do |p|
50
+ p.separator ''
51
+ p.on('-c', '--config-file=FILE', 'Load the named configuration file') {|v| @options[:config_file] = v}
52
+ p.on('-C', '--color=COLOR', "Set the leaves' color in the DOT diagram") {|v| @options[:color] = v}
53
+ p.on('-d', '--diagram', 'Generate a DOT diagram on stdout') {|v| @options[:diagram] = v}
54
+ p.on('-g', '--generate-config-file=FILE',
55
+ "Generate a new configuration file merging",
56
+ "the leaves' list with the content of the",
57
+ "old configuration file (if any)") {|v| @options[:new_config_file] = v}
58
+ p.on('-i', '--ignore', 'Ignore every configuration file') {|v| @options[:ignore] = v}
59
+ p.on('-p', '--pipe', 'Pipe Format (name --version ver)') {|v| @options[:pipe] = v}
60
+ p.on('-r', '--reverse', "Reverse the edges in the DOT diagram") {|v| @options[:reverse] = v}
61
+ p.parse(args)
62
+ end
63
+ end
64
+
65
+ # Looks for a YAML configuration file in:
66
+ #
67
+ # 1. the PWD;
68
+ # 2. the user's home directory.
69
+ #
70
+ # Optionally load a specific configuration file whose name has been set by
71
+ # the user.
72
+ def load_config_file
73
+ if @options[:ignore]
74
+ @configuration = {'ignore' => {}}
75
+ else
76
+ if @options[:config_file].nil?
77
+ cf = ['.gem_leaves.yml', File.join(find_home, '.gem_leaves.yml')]
78
+ cf.each {|f| (@configuration = YAML.load_file(f); return) rescue nil}
79
+ @configuration = {'ignore' => {}}
80
+ else
81
+ @configuration = YAML.load_file(@options[:config_file])
82
+ end
83
+ end
84
+ end
85
+
86
+ # Lifted from RubyGems 0.9.5; it's a semi-portable way to find the user's
87
+ # home directory.
88
+ def find_home
89
+ ['HOME', 'USERPROFILE'].each do |homekey|
90
+ return ENV[homekey] if ENV[homekey]
91
+ end
92
+ if ENV['HOMEDRIVE'] && ENV['HOMEPATH']
93
+ return "#{ENV['HOMEDRIVE']}:#{ENV['HOMEPATH']}"
94
+ end
95
+ begin
96
+ File.expand_path("~")
97
+ rescue StandardError => ex
98
+ if File::ALT_SEPARATOR
99
+ "C:/"
100
+ else
101
+ "/"
102
+ end
103
+ end
104
+ end
105
+
106
+ # Looks at the installed gems to find the _leaves_.
107
+ def find_leaves
108
+ root = Gem::Dependency.new //, Gem::Requirement.default
109
+ srcindex = Gem::SourceIndex.from_installed_gems
110
+ @gems = srcindex.search root
111
+ srcindex = prune(srcindex)
112
+ @leaves = srcindex.search(root).select {|s| s.dependent_gems.empty?}
113
+ end
114
+
115
+ # Remove from the list of installed gems those gems that *must* be kept
116
+ # and not shown to the user.
117
+ def prune(srcindex)
118
+ @configuration['ignore'].each do |k, v|
119
+ keep = srcindex.find_name(k, v)
120
+ next if keep.nil?
121
+ if keep.respond_to?(:each)
122
+ keep.each {|s| srcindex.remove_spec(s.full_name)}
123
+ else
124
+ srcindex.remove_spec(keep.full_name)
125
+ end
126
+ end
127
+ srcindex
128
+ end
129
+
130
+ # Show the leaves using one of the supported output format.
131
+ def show_leaves
132
+ if @options[:diagram]
133
+ diagrammatical_output
134
+ elsif @options[:pipe]
135
+ pipe_output
136
+ else
137
+ textual_output
138
+ end
139
+ end
140
+
141
+ # Simply puts the list of _leaves_ to STDOUT in a format suitable to pipe it
142
+ # to RubyGems.
143
+ def pipe_output
144
+ @leaves.sort.each do |leaf|
145
+ puts "#{leaf.name} --version #{leaf.version}"
146
+ end
147
+ end
148
+
149
+ # Simply puts the list of _leaves_ to STDOUT. It optionally merges the
150
+ # content of the already loaded configuration file with the leaves list.
151
+ def textual_output
152
+ @leaves.sort.each do |leaf|
153
+ puts "#{leaf.name} (#{leaf.version})"
154
+ end
155
+ end
156
+
157
+ # Build a DOT diagram of the installed gems, highlighting the leaves with
158
+ # a customizable color, leaving those gems that *must* be kept according
159
+ # to the current configuration in the standard color.
160
+ def diagrammatical_output
161
+ diagram = "digraph leaves_diagram {\n"
162
+ @gems.each do |g|
163
+ if @options[:reverse]
164
+ g.dependent_gems.each {|d| diagram << %Q( "#{d[0].full_name}" -> "#{g.full_name}"\n)}
165
+ else
166
+ g.dependent_gems.each {|d| diagram << %Q( "#{g.full_name}" -> "#{d[0].full_name}"\n)}
167
+ end
168
+ if g.dependent_gems.empty?
169
+ str = %Q( "#{g.full_name}")
170
+ if @leaves.include?(g)
171
+ str += %Q( [color="##{@options[:color]}", style=filled]\n)
172
+ else
173
+ str += "\n"
174
+ end
175
+ diagram << str
176
+ end
177
+ end
178
+ diagram << "}"
179
+ puts diagram
180
+ end
181
+
182
+ # Writes the new configuration file merging the optionally already loaded
183
+ # one with the leaves just found.
184
+ def generate_config_file
185
+ if @options[:new_config_file]
186
+ @leaves.sort.each do |leaf|
187
+ @configuration['ignore']["#{leaf.name}"] = "= #{leaf.version}"
188
+ end
189
+ File.open(@options[:new_config_file], 'w') do |out|
190
+ YAML.dump(@configuration, out)
191
+ end
192
+ end
193
+ end
194
+ end
metadata ADDED
@@ -0,0 +1,73 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gem_leaves
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.6
5
+ platform: ruby
6
+ authors:
7
+ - Gufo Pelato
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-01-05 00:00:00 +01:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: echoe
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ description: A dumb tool to list removable gems.
26
+ email: ""
27
+ executables:
28
+ - gem_leaves
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - README.txt
33
+ files:
34
+ - bin/gem_leaves
35
+ - gem_leaves.gemspec
36
+ - History.txt
37
+ - lib/gem_leaves.rb
38
+ - Manifest
39
+ - Rakefile
40
+ - README.txt
41
+ has_rdoc: true
42
+ homepage: http://github.com/baldowl/gem_leaves
43
+ post_install_message:
44
+ rdoc_options:
45
+ - --line-numbers
46
+ - --inline-source
47
+ - --title
48
+ - Gem_leaves
49
+ - --main
50
+ - README.txt
51
+ require_paths:
52
+ - lib
53
+ required_ruby_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: "0"
58
+ version:
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: "1.2"
64
+ version:
65
+ requirements: []
66
+
67
+ rubyforge_project: gemleaves
68
+ rubygems_version: 1.3.1
69
+ signing_key:
70
+ specification_version: 2
71
+ summary: A dumb tool to list removable gems.
72
+ test_files: []
73
+