Skip to content

Instantly share code, notes, and snippets.

@garmoshka-mo
Last active September 14, 2021 09:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save garmoshka-mo/3e8afff09fd89e4ca8bf1802f4937aa4 to your computer and use it in GitHub Desktop.
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
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