diff --git a/lib/wicked_pdf.rb b/lib/wicked_pdf.rb index 290e5cb1..393fba2a 100644 --- a/lib/wicked_pdf.rb +++ b/lib/wicked_pdf.rb @@ -29,8 +29,10 @@ require 'wicked_pdf/railtie' require 'wicked_pdf/tempfile' require 'wicked_pdf/middleware' +require 'wicked_pdf/progress' class WickedPdf + include Progress DEFAULT_BINARY_VERSION = Gem::Version.new('0.9.9') BINARY_VERSION_WITHOUT_DASHES = Gem::Version.new('0.12.0') EXE_NAME = 'wkhtmltopdf'.freeze @@ -106,32 +108,6 @@ def in_development_mode? RAILS_ENV == 'development' if defined?(RAILS_ENV) end - def track_progress?(options) - options[:progress] && !on_windows? - end - - def invoke_with_progress(command, options) - output = [] - begin - PTY.spawn(command.join(' ')) do |stdout, _stdin, pid| - begin - stdout.sync - stdout.each_line("\r") do |line| - output << line.chomp - options[:progress].call(line) if options[:progress] - end - rescue Errno::EIO # child process is terminated, this is expected behaviour - ensure - ::Process.wait pid - end - end - rescue PTY::ChildExited - puts 'The child process exited!' - end - err = output.join('\n') - raise "#{command} failed (exitstatus 0). Output was: #{err}" unless $CHILD_STATUS && $CHILD_STATUS.exitstatus.zero? - end - def on_windows? RbConfig::CONFIG['target_os'] =~ /mswin|mingw/ end diff --git a/lib/wicked_pdf/progress.rb b/lib/wicked_pdf/progress.rb new file mode 100644 index 00000000..0cc8d3f1 --- /dev/null +++ b/lib/wicked_pdf/progress.rb @@ -0,0 +1,30 @@ +class WickedPdf + module Progress + def track_progress?(options) + options[:progress] && !on_windows? + end + + def invoke_with_progress(command, options) + output = [] + begin + PTY.spawn(command.join(' ')) do |stdout, _stdin, pid| + begin + stdout.sync + stdout.each_line("\r") do |line| + output << line.chomp + options[:progress].call(line) if options[:progress] + end + rescue Errno::EIO # rubocop:disable Lint/HandleExceptions + # child process is terminated, this is expected behaviour + ensure + ::Process.wait pid + end + end + rescue PTY::ChildExited + puts 'The child process exited!' + end + err = output.join('\n') + raise "#{command} failed (exitstatus 0). Output was: #{err}" unless $CHILD_STATUS && $CHILD_STATUS.exitstatus.zero? + end + end +end