Class: Macinbox::Task

Inherits:
Object
  • Object
show all
Defined in:
lib/macinbox/task.rb

Class Method Summary collapse

Class Method Details

.backtick(cmd) ⇒ Object



77
78
79
80
# File 'lib/macinbox/task.rb', line 77

def self.backtick(cmd)
  Logger.info "Running command: #{Shellwords.join(cmd)}" if $verbose
  IO.popen(cmd).read.chomp
end


37
38
39
# File 'lib/macinbox/task.rb', line 37

def self.print_progress_bar(io, activity, percent_done)
  io.print TTY::Line::CLEAR + TTY::Color::GREEN + progress_bar(activity, percent_done) + TTY::Color::RESET if io.isatty
end

.progress_bar(activity, percent_done) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/macinbox/task.rb', line 21

def self.progress_bar(activity, percent_done)
  @spinner ||= Enumerator.new { |e| loop { e.yield '|'; e.yield '/'; e.yield '-'; e.yield '\\' } }
  columns = STDOUT.winsize[1] - 8
  header = activity + ": " + percent_done.round(0).to_s + "% done "
  bar = ""
  if percent_done.round(0).to_i < 100
    bar_available_size = columns - header.size - 2
    bar_size = (percent_done * bar_available_size / 100.0).to_i
    bar_remainder = bar_available_size - bar_size
    bar_full = "#" * bar_size
    bar_empty = @spinner.next + " " * (bar_remainder-1) rescue ""
    bar = "[" + bar_full + bar_empty + "]"
  end
  header + bar
end

.run(cmd) ⇒ Object



11
12
13
14
# File 'lib/macinbox/task.rb', line 11

def self.run(cmd)
  Logger.info "Running command: #{Shellwords.join(cmd)}" if $verbose
  system(*cmd) or raise Macinbox::Error.new("#{cmd.slice(0)} failed with non-zero exit code: #{$? >> 8}")
end

.run_as_sudo_user(cmd) ⇒ Object



16
17
18
19
# File 'lib/macinbox/task.rb', line 16

def self.run_as_sudo_user(cmd)
  Logger.info "Running command: sudo -u #{ENV["SUDO_USER"]} #{Shellwords.join(cmd)}" if $verbose
  system "sudo", "-u", ENV["SUDO_USER"], *cmd or raise Macinbox::Error.new("#{cmd.slice(0)} failed with non-zero exit code: #{$?.to_i}")
end

.run_with_input(cmd) ⇒ Object



82
83
84
85
86
87
88
# File 'lib/macinbox/task.rb', line 82

def self.run_with_input(cmd)
  Logger.info "Running command: #{Shellwords.join(cmd)}" if $verbose
  IO.popen(cmd, "w") do |pipe|
    yield pipe
  end
  $? == 0 or raise Macinbox::Error.new("#{cmd.slice(0)} failed with non-zero exit code: #{$?.to_i}")
end

.run_with_progress(activity, cmd, opts = {}) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/macinbox/task.rb', line 41

def self.run_with_progress(activity, cmd, opts={})
  STDERR.print TTY::Cursor::INVISIBLE
  print_progress_bar(STDERR, activity, 0.0)
  Logger.info "Running command: #{Shellwords.join(cmd)}" if $verbose
  IO.popen cmd, opts do |pipe|
    pipe.each_line do |line|
      percent = yield line
      print_progress_bar(STDERR, activity, percent) if percent
    end
  end
  STDERR.puts TTY::Cursor::NORMAL
end

.write_file_to_io_with_progress(source, destination) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/macinbox/task.rb', line 54

def self.write_file_to_io_with_progress(source, destination)
  activity = Logger.prefix + File.basename(source)
  eof = false
  bytes_written = 0
  total_size = File.size(source)
  last_percent_done = -1
  STDERR.print TTY::Cursor::INVISIBLE
  print_progress_bar(STDERR, activity, 0.0)
  File.open(source) do |file|
    until eof
      begin
        bytes_written += destination.write(file.readpartial(1024*1024))
        percent_done = ((bytes_written.to_f / total_size.to_f) * 100).round(1)
        last_percent_done = percent_done
        print_progress_bar(STDERR, activity, percent_done)
      rescue EOFError
        eof = true
      end
    end
  end
  STDERR.puts TTY::Cursor::NORMAL
end