Class: TenYearsRails::BundleReport

Inherits:
Object
  • Object
show all
Defined in:
lib/ten_years_rails/bundle_report.rb

Class Method Summary collapse

Class Method Details

.compatibility(rails_version:, include_rails_gems:) ⇒ Object



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
# File 'lib/ten_years_rails/bundle_report.rb', line 8

def self.compatibility(rails_version:, include_rails_gems:)
  incompatible_gems = TenYearsRails::GemInfo.all.reject do |gem|
    gem.compatible_with_rails?(rails_version: rails_version) || (!include_rails_gems && gem.from_rails?)
  end.sort_by do |gem|
    [
      gem.latest_version.compatible_with_rails?(rails_version: rails_version) ? 0 : 1,
      gem.name
    ].join("-")
  end

  incompatible_gems_by_state = incompatible_gems.group_by { |gem| gem.state(rails_version) }

  template = <<~ERB
    <% if incompatible_gems_by_state[:latest_compatible] -%>
    <%= Rainbow("=> Incompatible with Rails #{rails_version} (with new versions that are compatible):").white.bold %>
    <%= Rainbow("These gems will need to be upgraded before upgrading to Rails #{rails_version}.").italic %>

    <% incompatible_gems_by_state[:latest_compatible].each do |gem| -%>
    <%= gem_header(gem) %> - upgrade to <%= gem.latest_version.version %>
    <% end -%>

    <% end -%>
    <% if incompatible_gems_by_state[:incompatible] -%>
    <%= Rainbow("=> Incompatible with Rails #{rails_version} (with no new compatible versions):").white.bold %>
    <%= Rainbow("These gems will need to be removed or replaced before upgrading to Rails #{rails_version}.").italic %>

    <% incompatible_gems_by_state[:incompatible].each do |gem| -%>
    <%= gem_header(gem) %> - new version, <%= gem.latest_version.version %>, is not compatible with Rails #{rails_version}
    <% end -%>

    <% end -%>
    <% if incompatible_gems_by_state[:no_new_version] -%>
    <%= Rainbow("=> Incompatible with Rails #{rails_version} (with no new versions):").white.bold %>
    <%= Rainbow("These gems will need to be upgraded by us or removed before upgrading to Rails #{rails_version}.").italic %>
    <%= Rainbow("This list is likely to contain internal gems, like Cuddlefish.").italic %>

    <% incompatible_gems_by_state[:no_new_version].each do |gem| -%>
    <%= gem_header(gem) %> - new version not found
    <% end -%>

    <% end -%>
    <%= Rainbow(incompatible_gems.length.to_s).red %> gems incompatible with Rails <%= rails_version %>
  ERB

  puts ERB.new(template, nil, "-").result(binding)
end

.gem_header(_gem) ⇒ Object



55
56
57
58
59
# File 'lib/ten_years_rails/bundle_report.rb', line 55

def self.gem_header(_gem)
  header = Rainbow("#{_gem.name} #{_gem.version}").bold
  header << Rainbow(" (loaded from git)").magenta if _gem.sourced_from_git?
  header
end

.outdatedObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/ten_years_rails/bundle_report.rb', line 61

def self.outdated
  gems = TenYearsRails::GemInfo.all
  out_of_date_gems = gems.reject(&:up_to_date?).sort_by(&:created_at)
  percentage_out_of_date = ((out_of_date_gems.count / gems.count.to_f) * 100).round
  sourced_from_git = gems.select(&:sourced_from_git?)

  out_of_date_gems.each do |_gem|
    header = "#{_gem.name} #{_gem.version}"

    puts <<~MESSAGE
      #{Rainbow(header).bold.white}: released #{_gem.age} (latest version, #{_gem.latest_version.version}, released #{_gem.latest_version.age})
    MESSAGE
  end

  puts ""
  puts <<~MESSAGE
    #{Rainbow(sourced_from_git.count.to_s).yellow} gems are sourced from git
    #{Rainbow(out_of_date_gems.length.to_s).red} of the #{gems.count} gems are out-of-date (#{percentage_out_of_date}%)
  MESSAGE
end