Module: Pindo::Plistbuddyexecutable

Defined in:
lib/pindo/base/plistbuddyexecutable.rb

Defined Under Namespace

Classes: Plistbuddyindenter

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.plistbuddy_execute_command(executable, command, raise_on_failure = true) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/pindo/base/plistbuddyexecutable.rb', line 16

def self.plistbuddy_execute_command(executable, command, raise_on_failure = true)
  bin = '/usr/libexec/PlistBuddy'

  command = command.map(&:to_s)
  full_command = "#{bin} #{command.join(' ')}"

  
  puts "$ #{full_command}" 
  stdout = Plistbuddyindenter.new(STDOUT)
  stderr = Plistbuddyindenter.new(STDERR)


  status = plistbuddy_popen3(bin, command, stdout, stderr)
  stdout = stdout.join
  stderr = stderr.join
  output = stdout + stderr
  unless status.success?
    if raise_on_failure
      raise  Informative, "#{full_command}\n\n#{output}"
    else
      puts "[!] Failed: #{full_command}"
    end
  end

  output
end

.plistbuddy_popen3(bin, command, stdout, stderr) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/pindo/base/plistbuddyexecutable.rb', line 45

def self.plistbuddy_popen3(bin, command, stdout, stderr)
  puts "plistbuddy_popen3 ++++1"
  puts bin
  require 'open3'
  Open3.popen3(bin, *command) do |i, o, e, t|
    plistbuddy_reader(o, stdout)
    plistbuddy_reader(e, stderr)
    i.close

    status = t.value

    o.flush
    e.flush
    sleep(0.01)

    status
  end
end

.plistbuddy_reader(input, output) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/pindo/base/plistbuddyexecutable.rb', line 64

def self.plistbuddy_reader(input, output)
  Thread.new do
    buf = ''
    begin
      loop do
        buf << input.readpartial(4096)
        loop do
          string, separator, buf = buf.partition(/[\r\n]/)
          if separator.empty?
            buf = string
            break
          end
          output << (string << separator)
        end
      end
    rescue EOFError
      output << (buf << $/) unless buf.empty?
    end
  end
end

Instance Method Details

#plistbuddyexecutable(name) ⇒ Object



5
6
7
8
9
10
11
12
13
# File 'lib/pindo/base/plistbuddyexecutable.rb', line 5

def plistbuddyexecutable(name)
  define_method(name) do |*command|
    Plistbuddyexecutable.plistbuddy_execute_command(name, Array(command).flatten, false)
  end

  define_method(name.to_s + '!') do |*command|
    Plistbuddyexecutable.plistbuddy_execute_command(name, Array(command).flatten, true)
  end
end