railbow 0.6.0 → 0.7.0

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.
@@ -1,85 +1,21 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Defer prepend until CodeStatistics is available.
4
- # CodeStatistics is autoloaded by Rails when the `stats` task runs,
5
- # but is not available at rake-file load time.
6
- #
7
- # Rails 8.1 namespaced the class to Rails::CodeStatistics, removed
8
- # the bare `require "code_statistics"` shim, and dropped the built-in
9
- # `stats` rake task. We handle both old and new layouts.
10
- Rails.application.config.after_initialize do
11
- klass = nil
3
+ require_relative "../stats_formatter"
12
4
 
13
- begin
14
- require "rails/code_statistics"
15
- klass = Rails::CodeStatistics
16
- rescue LoadError
17
- # fall through
18
- end
19
-
20
- unless klass
21
- begin
22
- require "code_statistics"
23
- klass = CodeStatistics
24
- rescue LoadError
25
- # code_statistics not available (e.g., production without railties dev deps)
26
- end
27
- end
5
+ # Rails 7.2 runs `rails stats` as a rake task that never boots the app, so the
6
+ # railtie initializer does not fire there: install from here as well.
7
+ Railbow::StatsFormatter.install!
28
8
 
29
- if klass
30
- require_relative "../stats_formatter"
31
- klass.prepend(Railbow::StatsFormatter)
32
- end
33
- end
34
-
35
- # Rails 8.1 removed the built-in `stats` task. Re-define it so that
36
- # `rails stats` / `rake stats` keeps working with Railbow formatting.
37
- unless Rake::Task.task_defined?(:stats)
9
+ # Rails 8.1 dropped the `stats` rake task for the `rails stats` command; this
10
+ # keeps `rake stats` working. Gated on the version, not task_defined?: railtie
11
+ # tasks load before Rails' own, so on older Rails both would run.
12
+ if Rails.gem_version >= Gem::Version.new("8.1")
38
13
  desc "Report code statistics (lines / LOC / classes / methods / M/C / LOC/M)"
39
14
  task stats: :environment do
40
- klass = if defined?(Rails::CodeStatistics)
41
- Rails::CodeStatistics
42
- elsif defined?(CodeStatistics)
43
- CodeStatistics
44
- end
45
-
46
- unless klass
47
- abort "CodeStatistics is not available. Make sure railties is loaded."
48
- end
49
-
50
- stat_directories = [
51
- ["Controllers", "app/controllers"],
52
- ["Helpers", "app/helpers"],
53
- ["Jobs", "app/jobs"],
54
- ["Models", "app/models"],
55
- ["Mailers", "app/mailers"],
56
- ["Channels", "app/channels"],
57
- ["JavaScripts", "app/javascript"],
58
- ["Libraries", "lib"],
59
- ["APIs", "app/apis"],
60
- ["Controller tests", "test/controllers"],
61
- ["Helper tests", "test/helpers"],
62
- ["Job tests", "test/jobs"],
63
- ["Model tests", "test/models"],
64
- ["Mailer tests", "test/mailers"],
65
- ["Channel tests", "test/channels"],
66
- ["Integration tests", "test/integration"],
67
- ["System tests", "test/system"],
68
- ["Controller specs", "spec/controllers"],
69
- ["Helper specs", "spec/helpers"],
70
- ["Job specs", "spec/jobs"],
71
- ["Model specs", "spec/models"],
72
- ["Mailer specs", "spec/mailers"],
73
- ["Channel specs", "spec/channels"],
74
- ["Request specs", "spec/requests"],
75
- ["System specs", "spec/system"]
76
- ]
77
-
78
- root = Rails.root.to_s
79
- pairs = stat_directories
80
- .map { |name, dir| [name, File.join(root, dir)] }
15
+ stat_directories = Rails::CodeStatistics.directories
16
+ .map { |name, dir| [name, Rails.root.join(dir)] }
81
17
  .select { |_, dir| File.directory?(dir) }
82
18
 
83
- puts klass.new(*pairs)
19
+ Rails::CodeStatistics.new(*stat_directories).to_s
84
20
  end
85
21
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Railbow
4
- VERSION = "0.6.0"
4
+ VERSION = "0.7.0"
5
5
  end
data/lib/railbow.rb CHANGED
@@ -7,10 +7,10 @@ require_relative "railbow/config"
7
7
  module Railbow
8
8
  class Error < StandardError; end
9
9
 
10
- # Returns true when Railbow formatting should be disabled.
11
- # Checks for explicit opt-out, standard conventions, CI, and LLM agents.
12
- # RBW_PLAIN always wins; RBW_FORCE beats every auto-detection below it.
10
+ # True when railbow should leave the stock Rails output alone. RBW_PLAIN
11
+ # always wins; RBW_FORCE beats every auto-detection below it.
13
12
  def self.plain?
13
+ return true if failure
14
14
  return true if Params.plain?
15
15
  return false if Params.force?
16
16
  return true if ENV.key?("NO_COLOR")
@@ -18,7 +18,31 @@ module Railbow
18
18
  return true if ENV.key?("CI")
19
19
  !$stdout.tty?
20
20
  end
21
+
22
+ class << self
23
+ # The first error railbow's own rendering raised in this process. Once set,
24
+ # railbow stays plain so a broken formatter cannot fail twice.
25
+ attr_accessor :failure
26
+ end
27
+
28
+ # A railbow bug must never break the Rails command it decorates: an error
29
+ # falls back to the stock output and switches railbow off for the process.
30
+ # Errors Rails raises inside the fallback still propagate.
31
+ def self.guard(fallback)
32
+ yield
33
+ rescue => e
34
+ record_failure(e)
35
+ fallback.call
36
+ end
37
+
38
+ def self.record_failure(error)
39
+ return if failure
40
+
41
+ self.failure = error
42
+ frame = error.backtrace_locations&.find { |l| l.path.to_s.include?("/railbow/") }
43
+ where = frame ? " at #{frame.path[%r{lib/railbow/.*}] || frame.path}:#{frame.lineno}" : ""
44
+ warn "railbow: #{error.class}: #{error.message}#{where}; showing the default Rails output instead"
45
+ end
21
46
  end
22
47
 
23
- # Load Railtie if Rails is available
24
48
  require_relative "railbow/railtie" if defined?(Rails::Railtie)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: railbow
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eugene M
@@ -79,6 +79,7 @@ files:
79
79
  - lib/railbow.rb
80
80
  - lib/railbow/about_formatter.rb
81
81
  - lib/railbow/calendar.rb
82
+ - lib/railbow/clock.rb
82
83
  - lib/railbow/color_assigner.rb
83
84
  - lib/railbow/config.rb
84
85
  - lib/railbow/demo/fixtures.rb
@@ -119,7 +120,6 @@ homepage: https://github.com/amberpixels/railbow
119
120
  licenses:
120
121
  - MIT
121
122
  metadata:
122
- homepage_uri: https://github.com/amberpixels/railbow
123
123
  source_code_uri: https://github.com/amberpixels/railbow
124
124
  changelog_uri: https://github.com/amberpixels/railbow/blob/main/CHANGELOG.md
125
125
  bug_tracker_uri: https://github.com/amberpixels/railbow/issues