Skip to content

Commit

Permalink
Use Rainbow directly instead of relying on String refinement.
Browse files Browse the repository at this point in the history
  • Loading branch information
jan-bw committed Oct 22, 2024
1 parent 3683613 commit bb9f0ae
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 45 deletions.
2 changes: 1 addition & 1 deletion exe/bundle_report
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ at_exit do
begin
option_parser.parse!
rescue OptionParser::ParseError => e
STDERR.puts e.message.red
STDERR.puts Rainbow(e.message).red
puts option_parser
exit 1
end
Expand Down
14 changes: 6 additions & 8 deletions exe/deprecations
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
#!/usr/bin/env ruby
require "json"
require "rainbow/refinement"
require "rainbow"
require "optparse"
require "set"

using Rainbow

def run_tests(deprecation_warnings, opts = {})
tracker_mode = opts[:tracker_mode]
next_mode = opts[:next_mode]
Expand All @@ -30,11 +28,11 @@ def print_info(deprecation_warnings, opts = {})
end
end.sort_by {|message, data| data[:occurrences] }.reverse.to_h

puts "Ten most common deprecation warnings:".underline
puts Rainbow("Ten most common deprecation warnings:").underline
frequency_by_message.take(10).each do |message, data|
puts "Occurrences: #{data.fetch(:occurrences)}".bold
puts Rainbow("Occurrences: #{data.fetch(:occurrences)}").bold
puts "Test files: #{data.fetch(:test_files).to_a.join(" ")}" if verbose
puts message.red
puts Rainbow(message).red
puts "----------"
end
end
Expand Down Expand Up @@ -106,10 +104,10 @@ case options.fetch(:mode, "info")
when "run" then run_tests(deprecation_warnings, next_mode: options[:next], tracker_mode: options[:tracker_mode])
when "info" then print_info(deprecation_warnings, verbose: options[:verbose])
when nil
STDERR.puts "Must pass a mode: run or info".red
STDERR.puts Rainbow("Must pass a mode: run or info").red
puts option_parser
exit 1
else
STDERR.puts "Unknown mode: #{options[:mode]}".red
STDERR.puts Rainbow("Unknown mode: #{options[:mode]}").red
exit 1
end
8 changes: 3 additions & 5 deletions lib/deprecation_tracker.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
require "rainbow/refinement"
require "rainbow"
require "json"

using Rainbow

# A shitlist for deprecation warnings during test runs. It has two modes: "save" and "compare"
#
# DEPRECATION_TRACKER=save
Expand Down Expand Up @@ -152,7 +150,7 @@ def compare
end

if changed_buckets.length > 0
message = <<-MESSAGE.red
message = <<-MESSAGE
⚠️ Deprecation warnings have changed!
Code called by the following spec files is now generating different deprecation warnings:
Expand All @@ -170,7 +168,7 @@ def compare
See \e[4;37mdev-docs/testing/deprecation_tracker.md\e[0;31m for more information.
MESSAGE

raise UnexpectedDeprecations, message
raise UnexpectedDeprecations, Rainbow(message).red
end
end

Expand Down
30 changes: 14 additions & 16 deletions lib/next_rails/bundle_report.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
require "rainbow/refinement"
require "rainbow"
require "cgi"
require "erb"
require "json"
require "net/http"

using Rainbow

module NextRails
module BundleReport
extend self
Expand All @@ -27,34 +25,34 @@ def compatibility(rails_version: nil, ruby_version: nil, include_rails_gems: nil
def erb_output(incompatible_gems_by_state, incompatible_gems, rails_version)
template = <<-ERB
<% if incompatible_gems_by_state[:found_compatible] -%>
<%= "=> Incompatible with Rails #{rails_version} (with new versions that are compatible):".white.bold %>
<%= "These gems will need to be upgraded before upgrading to Rails #{rails_version}.".italic %>
<%= 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[:found_compatible].each do |gem| -%>
<%= gem_header(gem) %> - upgrade to <%= gem.latest_compatible_version.version %>
<% end -%>
<% end -%>
<% if incompatible_gems_by_state[:incompatible] -%>
<%= "=> Incompatible with Rails #{rails_version} (with no new compatible versions):".white.bold %>
<%= "These gems will need to be removed or replaced before upgrading to Rails #{rails_version}.".italic %>
<%= 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] -%>
<%= "=> Incompatible with Rails #{rails_version} (with no new versions):".white.bold %>
<%= "These gems will need to be upgraded by us or removed before upgrading to Rails #{rails_version}.".italic %>
<%= "This list is likely to contain internal gems, like Cuddlefish.".italic %>
<%= 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 -%>
<%= incompatible_gems.length.to_s.red %> gems incompatible with Rails <%= rails_version %>
<%= Rainbow(incompatible_gems.length.to_s).red %> gems incompatible with Rails <%= rails_version %>
ERB

erb_version = ERB.version
Expand All @@ -70,8 +68,8 @@ def erb_output(incompatible_gems_by_state, incompatible_gems, rails_version)
end

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

Expand Down Expand Up @@ -158,14 +156,14 @@ def output_to_stdout(out_of_date_gems, total_gem_count, sourced_from_git_count)
header = "#{gem.name} #{gem.version}"

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

percentage_out_of_date = ((out_of_date_gems.count / total_gem_count.to_f) * 100).round
footer = <<-MESSAGE
#{sourced_from_git_count.to_s.yellow} gems are sourced from git
#{out_of_date_gems.count.to_s.red} of the #{total_gem_count} gems are out-of-date (#{percentage_out_of_date}%)
#{Rainbow(sourced_from_git_count.to_s).yellow} gems are sourced from git
#{Rainbow(out_of_date_gems.count.to_s).red} of the #{total_gem_count} gems are out-of-date (#{percentage_out_of_date}%)
MESSAGE

puts ''
Expand Down
12 changes: 5 additions & 7 deletions lib/next_rails/bundle_report/ruby_version_compatibility.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
require "rainbow/refinement"
require "rainbow"

class NextRails::BundleReport::RubyVersionCompatibility
using Rainbow

MINIMAL_VERSION = 1.0
attr_reader :gems, :options

Expand All @@ -20,11 +18,11 @@ def generate
private

def message
output = "=> Incompatible gems with Ruby #{ruby_version}:".white.bold
output = Rainbow("=> Incompatible gems with Ruby #{ruby_version}:").white.bold
incompatible.each do |gem|
output += "\n#{gem.name} - required Ruby version: #{gem.gem_specification.required_ruby_version}".magenta
output += Rainbow("\n#{gem.name} - required Ruby version: #{gem.gem_specification.required_ruby_version}").magenta
end
output += "\n\n#{incompatible.length} incompatible #{incompatible.one? ? 'gem' : 'gems' } with Ruby #{ruby_version}".red
output += Rainbow("\n\n#{incompatible.length} incompatible #{incompatible.one? ? 'gem' : 'gems' } with Ruby #{ruby_version}").red
output
end

Expand All @@ -37,7 +35,7 @@ def ruby_version
end

def invalid_message
"=> Invalid Ruby version: #{options[:ruby_version]}.".red.bold
Rainbow("=> Invalid Ruby version: #{options[:ruby_version]}.").red.bold
end

def valid?
Expand Down
13 changes: 5 additions & 8 deletions spec/next_rails/bundle_report_spec.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
# frozen_string_literal: true

require "rainbow/refinement"

using Rainbow

require "rainbow"
require "spec_helper"

RSpec.describe NextRails::BundleReport do
Expand Down Expand Up @@ -33,14 +30,14 @@
it 'invokes $stdout.puts properly', :aggregate_failures do
allow($stdout)
.to receive(:puts)
.with("#{'alpha 0.0.1'.bold.white}: released #{alpha_age} (latest version, 0.0.2, released #{bravo_age})\n")
.with("#{Rainbow('alpha 0.0.1').bold.white}: released #{alpha_age} (latest version, 0.0.2, released #{bravo_age})\n")
allow($stdout)
.to receive(:puts)
.with("#{'bravo 0.2.0'.bold.white}: released #{bravo_age} (latest version, 0.2.2, released #{charlie_age})\n")
.with("#{Rainbow('bravo 0.2.0').bold.white}: released #{bravo_age} (latest version, 0.2.2, released #{charlie_age})\n")
allow($stdout).to receive(:puts).with('')
allow($stdout).to receive(:puts).with(<<-EO_MULTLINE_STRING)
#{'1'.yellow} gems are sourced from git
#{'2'.red} of the 2 gems are out-of-date (100%)
#{Rainbow('1').yellow} gems are sourced from git
#{Rainbow('2').red} of the 2 gems are out-of-date (100%)
EO_MULTLINE_STRING
end
end
Expand Down

0 comments on commit bb9f0ae

Please sign in to comment.