Skip to content

Commit

Permalink
Extract progress tracking code to separate module
Browse files Browse the repository at this point in the history
  • Loading branch information
corincerami committed Mar 12, 2019
1 parent 4ccff8a commit a4a51c1
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 26 deletions.
28 changes: 2 additions & 26 deletions lib/wicked_pdf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
30 changes: 30 additions & 0 deletions lib/wicked_pdf/progress.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit a4a51c1

Please sign in to comment.