Last active
September 14, 2021 09:24
-
-
Save garmoshka-mo/3e8afff09fd89e4ca8bf1802f4937aa4 to your computer and use it in GitHub Desktop.
Fix rake tasks to accept arguments in normal bash way; Condense huge backtraces for exceptions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Rake::Application.class_eval do | |
def parse_task_string(_) | |
args = ARGV.clone | |
return args.shift, | |
args.reject{|_| _ =~ /^(\w+)=(.*)$/} # clean ENV variables | |
.join(' ').presence | |
end | |
def display_exception_backtrace(ex) | |
if options.backtrace | |
backtrace = ex.backtrace | |
else | |
backtrace = Rake::Backtrace.collapse(ex.backtrace) | |
end | |
trace condense_backtrace(backtrace).join("\n") | |
end | |
def condense_backtrace(bt) | |
lines = ENV['TRACE_LINES']&.to_i || 30 | |
if bt.count > 50 | |
new_bt = bt[0..lines/2] + | |
["...#{bt.count - lines} lines hidden..."] + | |
bt[-lines/2..-1] | |
bt.clear | |
bt.concat new_bt | |
end | |
bt | |
end | |
end | |
Rake::Task.class_eval do | |
alias initialize_origin_for_arguments_patch initialize | |
def initialize(*args) | |
initialize_origin_for_arguments_patch *args | |
# Unwrap arguments list to single argument | |
def @actions.each | |
super do |action| | |
yield(Proc.new do |task, args| | |
action.call(task, args.first) | |
end) | |
end | |
end | |
end | |
def invoke(*args) | |
invoke_with_call_chain(args, Rake::InvocationChain::EMPTY) | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment