summaryrefslogtreecommitdiff
path: root/Rakefile
blob: c0d1cd7d42f1a52a1af1f5e2d09bbdabb9f554a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# -*- ruby encoding: utf-8 -*-

require 'rubygems'
require 'hoe'
require 'rake/clean'

Hoe.plugin :doofus
Hoe.plugin :gemspec2
Hoe.plugin :git
Hoe.plugin :minitest
Hoe.plugin :travis
Hoe.plugin :email unless ENV['CI'] or ENV['TRAVIS']

spec = Hoe.spec 'mime-types' do
  developer('Austin Ziegler', 'halostatue@gmail.com')
  self.need_tar = true

  self.require_ruby_version '>= 1.9.2'

  self.history_file = 'History.rdoc'
  self.readme_file = 'README.rdoc'
  self.extra_rdoc_files = FileList["*.rdoc"].to_a
  self.licenses = ["MIT", "Artistic 2.0", "GPL-2"]

  self.extra_dev_deps << ['hoe-doofus', '~> 1.0']
  self.extra_dev_deps << ['hoe-gemspec2', '~> 1.1']
  self.extra_dev_deps << ['hoe-git', '~> 1.6']
  self.extra_dev_deps << ['hoe-rubygems', '~> 1.0']
  self.extra_dev_deps << ['hoe-travis', '~> 1.2']
  self.extra_dev_deps << ['minitest', '~> 5.3']
  self.extra_dev_deps << ['minitest-autotest', '~>1.0']
  self.extra_dev_deps << ['rake', '~> 10.0']
  self.extra_dev_deps << ['simplecov', '~> 0.7']
  self.extra_dev_deps << ['coveralls', '~> 0.8']
end

task :support do
  %w(lib support).each { |path|
    $LOAD_PATH.unshift(File.join(Rake.application.original_dir, path))
  }
end

task 'support:nokogiri' => :support do
  begin
    gem 'nokogiri'
  rescue Gem::LoadError
    fail "Nokogiri is not installed. Please install it."
  end
end

namespace :benchmark do
  desc 'Benchmark Load Times'
  task :load, [ :repeats ] => :support do |_, args|
    require 'benchmarks/load'
    Benchmarks::Load.report(File.join(Rake.application.original_dir, 'lib'),
                            args.repeats)
  end

  desc 'Allocation counts'
  task :allocations, [ :top_x, :mime_types_only ] => :support do |_, args|
    if RUBY_VERSION < '2.1'
      $stderr.puts "Cannot count allocations on #{RUBY_VERSION}."
      exit 1
    end

    begin
      require 'allocation_tracer'
    rescue LoadError
      $stderr.puts "Allocation tracking requires the gem 'allocation_tracer'."
      exit 1
    end

    allocations = ObjectSpace::AllocationTracer.trace do
      require 'mime/types'
    end

    count = ObjectSpace::AllocationTracer.allocated_count_table.values.
      inject(:+)

    if args.top_x
      require 'pp'
      top_x = args.top_x.to_i
      top_x = 10 if top_x <= 0

      mime_types_only = !!args.mime_types_only

      table = allocations.sort_by { |_, v| v.first }.reverse.first(top_x)
      table.map! { |(location, allocs)|
        next if mime_types_only and location.first !~ %r{mime-types/lib}
        [ location.join(':').gsub(%r{^#{Dir.pwd}/}, ''), *allocs ]
      }.compact!

      head = (ObjectSpace::AllocationTracer.header - [ :line ]).map {|h|
        h.to_s.split(/_/).map(&:capitalize).join(' ')
      }
      table.unshift head

      max_widths = [].tap do |mw|
        table.map { |row| row.lazy.map(&:to_s).map(&:length).to_a }.tap do |w|
          w.first.each_index do |i|
            mw << w.lazy.map { |r| r[i] }.max
          end
        end
      end

      pattern = [ "%%-%ds" ]
      pattern << ([ "%% %ds" ] * (max_widths.length - 1))
      pattern = pattern.join("\t") % max_widths
      table.each { |row| puts pattern % row }
      puts
    end

    puts "TOTAL Allocations: #{count}"
  end

  desc 'Show object counts'
  task objects: :support do
    GC.start
    objects_before = ObjectSpace.count_objects

    require "mime/types"
    GC.start
    objects_after = ObjectSpace.count_objects
    objects_before.keys.grep(/T_/).map { |key|
      [ key, objects_after[key] - objects_before[key] ]
    }.sort_by { |key, delta| -delta }.each { |key, delta|
      puts "%10s +%6d" % [ key, delta ]
    }
  end
end

namespace :test do
  task :coveralls do
    spec.test_prelude = [
      'require "psych"',
      'require "simplecov"',
      'require "coveralls"',
      'SimpleCov.formatter = Coveralls::SimpleCov::Formatter',
      'SimpleCov.start("test_frameworks") { command_name "Minitest" }',
      'gem "minitest"'
    ].join('; ')
    Rake::Task['test'].execute
  end

  desc 'Run test coverage'
  task :coverage do
    spec.test_prelude = [
      'require "simplecov"',
      'SimpleCov.start("test_frameworks") { command_name "Minitest" }',
      'gem "minitest"'
    ].join('; ')
    Rake::Task['test'].execute
  end
end

namespace :mime do
  desc "Download the current MIME type registrations from IANA."
  task :iana, [ :destination ] => 'support:nokogiri' do |t, args|
    require 'iana_registry'
    IANARegistry.download(to: args.destination)
  end

  desc "Download the current MIME type configuration from Apache."
  task :apache, [ :destination ] => 'support:nokogiri' do |t, args|
    require 'apache_mime_types'
    ApacheMIMETypes.download(to: args.destination)
  end
end

namespace :convert do
  namespace :docs do
    task :setup do
      gem 'rdoc'
      require 'rdoc/rdoc'
      @doc_converter ||= RDoc::Markup::ToMarkdown.new
    end

    %w(README History History-Types).each do |name|
      rdoc = "#{name}.rdoc"
      mark = "#{name}.md"

      file mark => [ rdoc, :setup ] do |t|
        puts "#{rdoc} => #{mark}"
        File.open(t.name, 'wb') { |target|
          target.write @doc_converter.convert(IO.read(t.prerequisites.first))
        }
      end

      CLEAN.add mark

      task run: [ mark ]
    end
  end

  desc "Convert documentation from RDoc to Markdown"
  task docs: 'convert:docs:run'

  namespace :yaml do
    desc "Convert from YAML to JSON"
    task :json, [ :source, :destination, :multiple_files ] => :support do |t, args|
      require 'convert'
      Convert.from_yaml_to_json(args)
    end

    desc "Convert from YAML to TXT"
    task :txt, [ :source, :destination ] => :support do |t, args|
      require 'convert'
      Convert.from_yaml_to_txt(args)
    end
  end

  namespace :json do
    desc "Convert from JSON to YAML"
    task :yaml, [ :source, :destination, :multiple_files ] => :support do |t, args|
      require 'convert'
      Convert.from_json_to_yaml(args)
    end
  end
end

Rake::Task['travis'].prerequisites.replace(%w(test:coveralls))
Rake::Task['gem'].prerequisites.unshift("convert:yaml:json")

# vim: syntax=ruby