gem_unused 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm use ruby-1.9.3-p0@gem_unsed --create
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in gem_unused.gemspec
4
+ gemspec
@@ -0,0 +1,8 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new do |task|
5
+ task.test_files = FileList["test/**/*_test.rb"]
6
+ task.verbose = true
7
+ task.warning = true
8
+ end
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "gem_unused/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "gem_unused"
7
+ s.version = GemUnused::VERSION
8
+ s.authors = ["Chris Apolzon"]
9
+ s.email = ["apolzon@gmail.com"]
10
+ s.homepage = ""
11
+ s.summary = %q{Clean your gemset!}
12
+ s.description = %q{Removes rvm gemset gems not being used by your project}
13
+
14
+ s.rubyforge_project = "gem_unused"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+ s.required_rubygems_version = ">= 1.8.0"
21
+
22
+ s.add_runtime_dependency "bundler", "~> 1.0.0"
23
+
24
+ s.add_development_dependency "ruby-debug-pry"
25
+ end
@@ -0,0 +1,5 @@
1
+ require "gem_unused/version"
2
+
3
+
4
+ module GemUnused
5
+ end
@@ -0,0 +1,3 @@
1
+ module GemUnused
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,140 @@
1
+ require "bundler"
2
+ require "rubygems/uninstaller"
3
+ require "rubygems/specification"
4
+
5
+
6
+ # Because I don't really understand whats going on with their massive callback tree...
7
+ class Gem::Specification
8
+
9
+ def self.remove_spec spec
10
+ end
11
+ end
12
+
13
+ class Gem::Commands::UnusedCommand < Gem::Command
14
+ def description
15
+ "Show and remove unused gems and old versions of gems"
16
+ end
17
+
18
+ def arguments
19
+
20
+ end
21
+
22
+ def usage
23
+ "#{program_name}"
24
+ end
25
+
26
+ def initialize
27
+ super "unused", description
28
+
29
+ add_option("-r", "--remove", "Remove all gems found by #{program_name}") do |value, options|
30
+ options[:remove] = true
31
+ end
32
+
33
+ add_option("", "--branches BRANCH", "
34
+ Set branch names to check for currently used gems.
35
+ Space separated, surround with a string (\"master staging production\")"
36
+ ) do |value, options|
37
+ options[:branches] = value
38
+ end
39
+
40
+ add_option("", "--exclude GEM", "
41
+ Set gem names to exclude from check.
42
+ These gems should really be installed in the global gemset.
43
+ Surround with a string for multiples (\"gem_a gem_b gem_c\")"
44
+ ) do |value, options|
45
+ options[:excluded_gems] = value
46
+ end
47
+
48
+ add_option("-q", "--quiet",
49
+ "Make this script perform its work quietly"
50
+ ) do |value, options|
51
+ options[:quiet] = true
52
+ end
53
+ end
54
+
55
+ def execute
56
+ if options[:branches].nil?
57
+ log "no branch names provided. using master, staging, and production as default."
58
+ options[:branches] = %w(master staging production)
59
+ else
60
+ options[:branches] = options[:branches].split
61
+ end
62
+ find_unused_gems
63
+ remove_unused_gems if options[:remove]
64
+ end
65
+
66
+ def find_unused_gems
67
+ retrieve_requirements
68
+ retrieve_installed
69
+
70
+ @unneeded = @installed.delete_if do |installed_spec|
71
+ # remove any required installed gems
72
+ @requirements.any? do |spec|
73
+ spec.name == installed_spec.name &&
74
+ spec.version == installed_spec.version
75
+ end
76
+ end
77
+
78
+ log "====REMOVE THESE GEMS===="
79
+ @unneeded.each do |bad_gem|
80
+ log "#{bad_gem.name} #{bad_gem.version}"
81
+ end
82
+ end
83
+
84
+ def retrieve_requirements
85
+ @requirements = []
86
+ options[:branches].each do |branch|
87
+ `git co #{branch}`
88
+ bundle = ::Bundler::LockfileParser.new(File.read("Gemfile.lock"))
89
+ @requirements.concat bundle.specs
90
+ end
91
+ @requirements.uniq! { |spec| "#{spec.name} #{spec.version}" }
92
+ log "These are the requirements of your lockfiles:"
93
+ @requirements.each do |spec|
94
+ log "#{spec.name} #{spec.version}"
95
+ end
96
+ # TODO: somewhere we need to skip/omit those excluded gems
97
+ end
98
+
99
+ def retrieve_installed
100
+ handle_excluded_gems_option
101
+ @installed = Gem::Specification.find_all do |installed_spec|
102
+ !installed_spec.gem_dir.match(/@global/) && !options[:excluded_gems].include?(installed_spec.name)
103
+ end.uniq { |spec| "#{spec.name} #{spec.version}" }
104
+ end
105
+
106
+ def handle_excluded_gems_option
107
+ if options[:excluded_gems].nil?
108
+ options[:excluded_gems] = []
109
+ log "Not excluding any gems."
110
+ else
111
+ options[:excluded_gems] = options[:excluded_gems].split
112
+ log "Excluding the following gems: #{options[:excluded_gems]}"
113
+ end
114
+ end
115
+
116
+ def remove_unused_gems
117
+ log "I'm removing your unused gems....mwuahahahha!!!"
118
+ # Gem::Uninstaller.new(gem_name, options).uninstall
119
+ # -I (options[:ignore]): ignore depedencies
120
+ # -x (options[:executables]): don't prompt for removal of app executables
121
+ # -i (options[:install_dir]): install dir, directory to uninstall gem from (WIN)
122
+ uninstall_options = {
123
+ :ignore => true,
124
+ :executables => true
125
+ }
126
+ @unneeded.each do |bad_gem|
127
+ uninstall_options[:install_dir] = bad_gem.base_dir
128
+ uninstall_options[:user_install] = true
129
+ uninstall_options[:version] = bad_gem.version
130
+ uninstall_options[:prerelease] = true if bad_gem.version.prerelease?
131
+ uninstaller = Gem::Uninstaller.new(bad_gem, uninstall_options)
132
+ uninstaller.instance_variable_set(:@user_install, true)
133
+ uninstaller.uninstall_gem(bad_gem)
134
+ end
135
+ end
136
+
137
+ def log message
138
+ puts message unless options[:quiet]
139
+ end
140
+ end
@@ -0,0 +1,6 @@
1
+ require "rubygems/command_manager"
2
+ require "rubygems/command"
3
+ require "rubygems/dependency"
4
+ require "rubygems/version_option"
5
+
6
+ Gem::CommandManager.instance.register_command :unused
metadata ADDED
@@ -0,0 +1,76 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gem_unused
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Chris Apolzon
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-02-20 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: &70224871329400 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 1.0.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70224871329400
25
+ - !ruby/object:Gem::Dependency
26
+ name: ruby-debug-pry
27
+ requirement: &70224871329000 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *70224871329000
36
+ description: Removes rvm gemset gems not being used by your project
37
+ email:
38
+ - apolzon@gmail.com
39
+ executables: []
40
+ extensions: []
41
+ extra_rdoc_files: []
42
+ files:
43
+ - .gitignore
44
+ - .rvmrc
45
+ - Gemfile
46
+ - Rakefile
47
+ - gem_unused.gemspec
48
+ - lib/gem_unused.rb
49
+ - lib/gem_unused/version.rb
50
+ - lib/rubygems/commands/unused_command.rb
51
+ - lib/rubygems_plugin.rb
52
+ homepage: ''
53
+ licenses: []
54
+ post_install_message:
55
+ rdoc_options: []
56
+ require_paths:
57
+ - lib
58
+ required_ruby_version: !ruby/object:Gem::Requirement
59
+ none: false
60
+ requirements:
61
+ - - ! '>='
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: 1.8.0
70
+ requirements: []
71
+ rubyforge_project: gem_unused
72
+ rubygems_version: 1.8.17
73
+ signing_key:
74
+ specification_version: 3
75
+ summary: Clean your gemset!
76
+ test_files: []