Class: Gem::Commands::TldrCommand

Inherits:
Gem::Command
  • Object
show all
Defined in:
lib/rubygems/commands/tldr_command.rb

Overview

TL;DR your gems with gem tldr

Constant Summary collapse

VERSION =
'1.0'

Instance Method Summary collapse

Constructor Details

#initializeTldrCommand

Returns a new instance of TldrCommand.



12
13
14
15
16
17
18
# File 'lib/rubygems/commands/tldr_command.rb', line 12

def initialize
  super 'tldr', 'Documentation? Tests? They use too much disk space!'

  add_option '--dry-run', "don't do anything" do |value, options|
    options[:dry_run] = true
  end
end

Instance Method Details

#executeObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/rubygems/commands/tldr_command.rb', line 24

def execute
  @verbose = Gem.configuration.really_verbose

  if @verbose then
    if options[:dry_run] then
      extend FileUtils::DryRun
    else
      extend FileUtils::Verbose
    end
  elsif options[:dry_run] then
    extend FileUtils::NoWrite
  else
    extend FileUtils
  end

  beginning_size = total_size

  Gem.source_index.each do |name, spec|
    say "TL;DR #{name}"
    Dir.chdir spec.full_gem_path do
      remove_tests spec
      remove_built_documentation spec
      remove_build_artifacts spec
      remove_file_comments spec
    end
  end

  ending_size = total_size

  saved = beginning_size - ending_size

  say "start: #{beginning_size}B end: #{ending_size}B saved: #{saved}B"
end

#remove_build_artifacts(spec) ⇒ Object

Remove build artifacts for C extensions from spec



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/rubygems/commands/tldr_command.rb', line 88

def remove_build_artifacts spec
  spec.extensions.each do |extension|
    next unless extension =~ /extconf\.rb$/
    extension_dir = File.dirname extension

    say "\tRemoving build artifacts in #{extension_dir}" if @verbose

    Dir["#{extension_dir}/**/*.{c,h,java,o}"].each do |artifact|
      rm_f artifact
    end

    Dir["#{extension_dir}/**/*.dSYM"].each do |artifact|
      rm_rf artifact
    end

    rm_f File.join(extension_dir, 'Makefile')
    rm_f File.join(extension_dir, 'extconf.rb')
    rm_f File.join(extension_dir, 'mkmf.log')
  end
end

#remove_built_documentation(spec) ⇒ Object

Remove the documentation for spec



78
79
80
81
82
83
# File 'lib/rubygems/commands/tldr_command.rb', line 78

def remove_built_documentation spec
  doc_dir = File.join spec.installation_path, 'doc', spec.full_name

  say "\tRemoving built documentation" if @verbose
  rm_rf doc_dir
end

#remove_file_comments(spec) ⇒ Object

Remove comments in the ruby files of spec



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/rubygems/commands/tldr_command.rb', line 112

def remove_file_comments spec
  say "\tStripping comments" if @verbose
  return if options[:dry_run]
  spec.require_paths.each do |path|
    Dir["#{path}/**/*.rb"].each do |file|
      ruby = File.read file

      stripped = strip_comments ruby

      open file, 'w' do |io|
        io.write stripped
      end
    end
  end
end

#remove_tests(spec) ⇒ Object

Remove the tests (and specs) of spec



131
132
133
134
135
136
# File 'lib/rubygems/commands/tldr_command.rb', line 131

def remove_tests spec
  say "\tRemoving tests" if @verbose
  Dir['{test,spec}'].each do |dir|
    rm_rf dir
  end
end

#strip_comments(ruby) ⇒ Object

Strips comments out of ruby. Destructive!



141
142
143
144
145
# File 'lib/rubygems/commands/tldr_command.rb', line 141

def strip_comments ruby
  ruby.gsub!(/^[ \t]*#[^{@$].*?\r?\n/, '')
  ruby.gsub!(/[ \t]*#[^{@$].*?$/, '')
  ruby
end

#total_sizeObject

Find the total size of your Gem path directories



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/rubygems/commands/tldr_command.rb', line 61

def total_size
  size = 0

  Gem.path.each do |directory|
    Find.find directory do |path|
      stat = File.stat path
      next unless stat.file?
      size += stat.size
    end
  end

  size
end

#usageObject

:nodoc:



20
21
22
# File 'lib/rubygems/commands/tldr_command.rb', line 20

def usage # :nodoc:
  program_name
end