run_loop 2.5.3 → 2.5.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: df4249f36fc6ecabe9284a3065e9a46e03db5a5c
4
- data.tar.gz: 05ffd2992c349738d98fd054dac29423b969eab0
3
+ metadata.gz: 975468eda0e5cfb4d2432722651b202e8ec800a8
4
+ data.tar.gz: bf3695bf51d10dbaf295eb34d7b727da45e7996d
5
5
  SHA512:
6
- metadata.gz: fc3f2c96a9815ffe86ffc18afdc03aa3dc986d224b6c8255f54d4e409ce9164659508e648b7a327111f33d30904127634282c86a2469143b8598dd11384a3549
7
- data.tar.gz: 5a2c62019f91ffc015d7fae3fc256e918070598d0513a48f387f92ac7045efa604e70a772e304dc6e535bd7444cf18b193bce493ba5d9fb7624c2a706fbd0b4f
6
+ metadata.gz: bf3e3ab2e48f2754cb71d616e19f3a30d02d4d6749b1b471a0cf903318cd57b2d393badc75fc5d1664145755e9f584a2e0d898ea2cbfacda56bb6b590a0a1e33
7
+ data.tar.gz: 80b1520a789b6998204ff8137a80a26a0fbf9df37da7d7d978422a7562f16eaaca3acc4c727671f7d39f4ef57d48414617b666c279405d8cc8e89d0b7fd1c92a
@@ -275,79 +275,44 @@ version: #{version}
275
275
 
276
276
  # @!visibility private
277
277
  def simulator_root_dir
278
- @simulator_root_dir ||= lambda {
279
- return nil if physical_device?
280
- File.join(CORE_SIMULATOR_DEVICE_DIR, udid)
281
- }.call
278
+ return nil if physical_device?
279
+ @simulator_root_dir ||= File.join(CORE_SIMULATOR_DEVICE_DIR, udid)
282
280
  end
283
281
 
284
282
  # @!visibility private
285
283
  def simulator_accessibility_plist_path
286
- @simulator_accessibility_plist_path ||= lambda {
287
- return nil if physical_device?
288
- File.join(simulator_root_dir, 'data/Library/Preferences/com.apple.Accessibility.plist')
289
- }.call
284
+ return nil if physical_device?
285
+
286
+ directory = File.join(simulator_root_dir, "data", "Library", "Preferences")
287
+ pbuddy.ensure_plist(directory, "com.apple.Accessibility.plist")
290
288
  end
291
289
 
292
290
  # @!visibility private
293
291
  def simulator_preferences_plist_path
294
292
  return nil if physical_device?
295
293
 
296
-
297
294
  directory = File.join(simulator_root_dir, "data", "Library", "Preferences")
298
-
299
- if !File.exist?(directory)
300
- FileUtils.mkdir_p(directory)
301
- end
302
-
303
- plist = File.join(directory, "com.apple.Preferences.plist")
304
-
305
- if !File.exist?(plist)
306
- pbuddy.create_plist(plist)
307
- end
308
- plist
295
+ pbuddy.ensure_plist(directory, "com.apple.Preferences.plist")
309
296
  end
310
297
 
311
298
  # @!visibility private
312
299
  def simulator_log_file_path
313
- @simulator_log_file_path ||= lambda {
314
- return nil if physical_device?
315
- File.join(CORE_SIMULATOR_LOGS_DIR, udid, 'system.log')
316
- }.call
300
+ return nil if physical_device?
301
+ @simulator_log_file_path ||= File.join(CORE_SIMULATOR_LOGS_DIR, udid,
302
+ 'system.log')
317
303
  end
318
304
 
319
305
  # @!visibility private
320
306
  def simulator_device_plist
321
- @simulator_device_plist ||= lambda do
322
- return nil if physical_device?
323
- File.join(simulator_root_dir, 'device.plist')
324
- end.call
307
+ return nil if physical_device?
308
+ pbuddy.ensure_plist(simulator_root_dir, "device.plist")
325
309
  end
326
310
 
327
311
  # @!visibility private
328
- def simulator_global_preferences_path(timeout=10)
312
+ def simulator_global_preferences_path
329
313
  return nil if physical_device?
330
-
331
- path = File.join(simulator_root_dir,
332
- "data/Library/Preferences/.GlobalPreferences.plist")
333
-
334
- return path if File.exist?(path)
335
-
336
- start = Time.now
337
- while !File.exist?(path) && (start + timeout) < Time.now
338
- sleep(1.0)
339
- end
340
-
341
- return path if File.exist?(path)
342
-
343
- raise(RuntimeError, %Q[
344
- Timed out waiting for .GlobalPreferences.plist after #{Time.now - start} seconds.
345
-
346
- File does not exist at path:
347
-
348
- #{path}
349
-
350
- ])
314
+ directory = File.join(simulator_root_dir, "data", "Library", "Preferences")
315
+ pbuddy.ensure_plist(directory, ".GlobalPreferences.plist")
351
316
  end
352
317
 
353
318
  # @!visibility private
@@ -474,25 +439,14 @@ File does not exist at path:
474
439
 
475
440
  global_plist = simulator_global_preferences_path
476
441
 
477
- cmd = [
478
- "/usr/libexec/PlistBuddy",
479
- "-c",
480
- "Add :AppleLanguages:0 string '#{lang_code}'",
481
- global_plist
482
- ]
483
-
484
- # RunLoop::PlistBuddy cannot add items to arrays.
485
- hash = run_shell_command(cmd, {:log_cmd => true})
486
-
487
- if hash[:exit_status] != 0
442
+ begin
443
+ pbuddy.unshift_array("AppleLanguages", "string", lang_code,
444
+ global_plist)
445
+ rescue RuntimeError => e
488
446
  raise RuntimeError, %Q[
489
- Could not update the Simulator languages because this command:
490
-
491
- #{cmd.join(" ")}
492
-
493
- failed with this output:
447
+ Could not update the Simulator languages.
494
448
 
495
- #{hash[:out]}
449
+ #{e.message}
496
450
 
497
451
  ]
498
452
  end
@@ -1,3 +1,4 @@
1
+
1
2
  module RunLoop
2
3
  # A class for reading and writing property list values.
3
4
  #
@@ -6,6 +7,11 @@ module RunLoop
6
7
  # is problematic for our purposes.
7
8
  class PlistBuddy
8
9
 
10
+ require "fileutils"
11
+ require "run_loop/shell"
12
+
13
+ include RunLoop::Shell
14
+
9
15
  # Reads key from file and returns the result.
10
16
  # @param [String] key the key to inspect (may not be nil or empty)
11
17
  # @param [String] file the plist to read
@@ -18,11 +24,11 @@ module RunLoop
18
24
  raise(ArgumentError, "key '#{key}' must not be nil or empty")
19
25
  end
20
26
  cmd = build_plist_cmd(:print, {:key => key}, file)
21
- res = execute_plist_cmd(cmd, opts)
22
- if res == "Print: Entry, \":#{key}\", Does Not Exist"
27
+ success, output = execute_plist_cmd(cmd, file, opts)
28
+ if !success
23
29
  nil
24
30
  else
25
- res
31
+ output
26
32
  end
27
33
  end
28
34
 
@@ -64,8 +70,16 @@ module RunLoop
64
70
  cmd = build_plist_cmd(:add, cmd_args, file)
65
71
  end
66
72
 
67
- res = execute_plist_cmd(cmd, merged)
68
- res == ''
73
+ success, output = execute_plist_cmd(cmd, file, merged)
74
+ if !success
75
+ raise RuntimeError, %Q[
76
+ Encountered an error performing operation on plist:
77
+
78
+ #{plist_buddy} -c "#{cmd}" #{file}
79
+ => #{output}
80
+ ]
81
+ end
82
+ success
69
83
  end
70
84
 
71
85
  # Creates an new empty plist at `path`.
@@ -85,6 +99,78 @@ module RunLoop
85
99
  path
86
100
  end
87
101
 
102
+ # Ensures a plist exists at path by creating necessary directories and
103
+ # creating an empty plist if none exists.
104
+ def ensure_plist(directory, name)
105
+ FileUtils.mkdir_p(directory) if !File.exist?(directory)
106
+
107
+ plist = File.join(directory, name)
108
+
109
+ create_plist(plist) if !File.exists?(plist)
110
+
111
+ plist
112
+ end
113
+
114
+ # Sends an arbitrary command (-c) to PlistBuddy.
115
+ #
116
+ # This class does not handle setting data, date, dictionary, or array
117
+ # or manipulating elements in existing array or dictionary types. This
118
+ # method is an attempt to bridge this gap.
119
+ #
120
+ # When setting/adding bool, real, integer, string values, use #plist_set.
121
+ #
122
+ # For reading values, use #plist_read.
123
+ #
124
+ # @param [String] cmd The command passed to PlistBuddy with -c
125
+ # @param [String] file Path the plist file
126
+ # @param [Hash] opts options for controlling execution
127
+ # @option opts [Boolean] :verbose (false) controls log level
128
+ # @raise RuntimeError when running the command fails.
129
+ # @return Boolean, String Success and the output of running the command.
130
+ def run_command(cmd, file, opts={})
131
+ success, output = execute_plist_cmd(cmd, file, opts)
132
+ if !success
133
+ raise RuntimeError, %Q[
134
+ Encountered an error performing operation on plist:
135
+
136
+ #{plist_buddy} -c "#{cmd}" #{file}
137
+ => #{output}
138
+ ]
139
+ end
140
+ return success, output
141
+ end
142
+
143
+ # Add value to the head of an array type.
144
+ #
145
+ # @param [String] key The plist key
146
+ # @param [String] type any allowed plist type
147
+ # @param [Object] value the value to add
148
+ # @param [String] path the plist path
149
+ # @param [Hash] opts options for controlling execution
150
+ # @option opts [Boolean] :verbose (false) controls log level
151
+ # @raise RuntimeError when running the command fails.
152
+ # @raise RuntimeError if attempt to push value onto non-array container.
153
+ def unshift_array(key, type, value, path, opts={})
154
+ if !plist_key_exists?(key, path)
155
+ run_command("Add :#{key} array", path, opts)
156
+ else
157
+ key_type = plist_read(key, path).split(" ")[0]
158
+ if key_type != "Array"
159
+ raise RuntimeError, %Q[
160
+ Could not push #{value} onto array:
161
+ Expected: key #{key} be of type Array
162
+ Found: had type #{key_type}
163
+
164
+ in plist:
165
+
166
+ #{path}
167
+ ]
168
+ end
169
+ end
170
+
171
+ run_command("Add :#{key}:0 #{type} #{value}", path, opts)
172
+ end
173
+
88
174
  private
89
175
 
90
176
  # returns the path to the PlistBuddy executable
@@ -101,24 +187,17 @@ module RunLoop
101
187
  # @return [Boolean,String] `true` if command was successful. If :print'ing
102
188
  # the result, the value of the key. If there is an error, the output of
103
189
  # stderr.
104
- def execute_plist_cmd(cmd, opts={})
105
- default_opts = {:verbose => false}
190
+ def execute_plist_cmd(cmd, file, opts={})
191
+ default_opts = {:verbose => false }
106
192
  merged = default_opts.merge(opts)
107
193
 
108
- puts cmd if merged[:verbose]
194
+ merged[:log_cmd] = merged[:verbose]
109
195
 
110
- res = nil
111
- # noinspection RubyUnusedLocalVariable
112
- Open3.popen3(cmd) do |stdin, stdout, stderr, wait_thr|
113
- err = stderr.read
114
- std = stdout.read
115
- if not err.nil? and err != ''
116
- res = err.chomp
117
- else
118
- res = std.chomp
119
- end
120
- end
121
- res
196
+ args = [plist_buddy, "-c", cmd, file]
197
+
198
+ hash = run_shell_command(args, merged)
199
+
200
+ return hash[:exit_status] == 0, hash[:out]
122
201
  end
123
202
 
124
203
  # Composes a PlistBuddy command that can be executed as a shell command.
@@ -161,13 +240,13 @@ module RunLoop
161
240
  unless key
162
241
  raise(ArgumentError, ':key is a required key for :add command')
163
242
  end
164
- cmd_part = "\"Add :#{key} #{value_type} #{value}\""
243
+ cmd_part = "Add :#{key} #{value_type} #{value}"
165
244
  when :print
166
245
  key = args_hash[:key]
167
246
  unless key
168
247
  raise(ArgumentError, ':key is a required key for :print command')
169
248
  end
170
- cmd_part = "\"Print :#{key}\""
249
+ cmd_part = "Print :#{key}"
171
250
  when :set
172
251
  value = args_hash[:value]
173
252
  unless value
@@ -177,13 +256,13 @@ module RunLoop
177
256
  unless key
178
257
  raise(ArgumentError, ':key is a required key for :set command')
179
258
  end
180
- cmd_part = "\"Set :#{key} #{value}\""
259
+ cmd_part = "Set :#{key} #{value}"
181
260
  else
182
261
  cmds = [:add, :print, :set]
183
262
  raise(ArgumentError, "expected '#{type}' to be one of '#{cmds}'")
184
263
  end
185
264
 
186
- "#{plist_buddy} -c #{cmd_part} \"#{file}\""
265
+ cmd_part
187
266
  end
188
267
  end
189
268
  end
@@ -24,7 +24,7 @@ module RunLoop
24
24
  "Shutdown" => 1,
25
25
  "Shutting Down" => 2,
26
26
  "Booted" => 3,
27
- "Plist Missing" => -1
27
+ "Plist Missing Key" => -1
28
28
  }.freeze
29
29
 
30
30
  # @!visibility private
@@ -125,10 +125,11 @@ module RunLoop
125
125
  # @!visibility private
126
126
  def simulator_state_as_int(device)
127
127
  plist = device.simulator_device_plist
128
- if File.exist?(plist)
128
+
129
+ if pbuddy.plist_key_exists?("state", plist)
129
130
  pbuddy.plist_read("state", plist).to_i
130
131
  else
131
- SIM_STATES["Plist Missing"]
132
+ SIM_STATES["Plist Missing Key"]
132
133
  end
133
134
  end
134
135
 
@@ -1,5 +1,5 @@
1
1
  module RunLoop
2
- VERSION = "2.5.3"
2
+ VERSION = "2.5.4"
3
3
 
4
4
  # A model of a software release version that can be used to compare two versions.
5
5
  #
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: run_loop
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.5.3
4
+ version: 2.5.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Karl Krukow
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2017-08-30 00:00:00.000000000 Z
12
+ date: 2017-08-31 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: json