-
Notifications
You must be signed in to change notification settings - Fork 6
/
TasksViewController.swift
559 lines (482 loc) · 21.8 KB
/
TasksViewController.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
import AppKit
import CommandsCore
class TasksViewController: NSViewController {
@IBOutlet var textView: NSTextView!
@IBOutlet var spinner: NSProgressIndicator!
@IBOutlet var buildButton: NSButton!
@IBOutlet var debugCheckbox: NSButton!
@IBOutlet var phoneComboBox: NSPopUpButton!
@IBOutlet var languagePopUpButton: NSPopUpButton!
@IBOutlet var buildPicker: NSPopUpButtonCell!
@IBOutlet var tagPicker: NSComboBox!
@IBOutlet var simulatorRadioButton: NSButton!
@IBOutlet var physicalDeviceRadioButton: NSButton!
@IBOutlet var getDeviceButton: NSButtonCell!
@IBOutlet var cautionImage: NSImageView!
@IBOutlet var cautionBuildImage: NSImageView!
@IBOutlet weak var textField: NSTextField!
@IBOutlet weak var progressBar: NSProgressIndicator!
@IBOutlet weak var downloadButton: NSButton!
@IBOutlet weak var switchLanguageButton: NSButton!
@IBOutlet weak var downloadCheckbox: NSButton!
@IBOutlet weak var installButton: NSButton!
@IBOutlet weak var eraseSimulatorButton: NSButton!
let localization = Localization()
let deviceCollector = DeviceCollector()
let plistHandler = PlistHandler()
var textViewPrinter: TextViewPrinter!
@objc dynamic var isRunning = false
let applicationStateHandler = ApplicationStateHandler()
let tagsController = TagsController()
var devices: [String] = []
var timer: Timer!
var calabashFolderPath = ""
var linkInfo: [String] = []
var commandExecutor: CommandExecutor?
var isDeviceListEmpty: Bool {
return phoneComboBox.numberOfItems == 0
}
override func viewDidAppear() {
super.viewDidAppear()
textField.backgroundColor = .darkGray
textField.textColor = .white
let placeholderText = NSMutableAttributedString(string: "Console Input (Beta)".localized)
placeholderText.setAttributes([.foregroundColor: NSColor.lightGray], range: NSRange(location: 0, length: "Console Input (Beta)".count))
textField.placeholderAttributedString = placeholderText
timer = .scheduledTimer(timeInterval: 40, target: self, selector: #selector(cleanupOutput), userInfo: nil, repeats: true);
populateBuildPicker()
handleRadioButtons(willGetDevice: false)
}
override func viewDidLoad() {
super.viewDidLoad()
textViewPrinter = TextViewPrinter(textView: textView)
let languageValues = Language.all.map { $0.rawValue }
languagePopUpButton.addItems(withTitles: languageValues)
if let filePath = applicationStateHandler.filePath {
calabashFolderPath = filePath.absoluteString.replacingOccurrences(of: "file://", with: "")
} else if let controller = storyboard?.instantiateController(withIdentifier: .settingsWindow) as? NSViewController {
presentViewControllerAsModalWindow(controller)
}
killProcessScreenshot()
textView.backgroundColor = .darkAquamarine
textView.textColor = .white
buildPicker.autoenablesItems = false
cautionBuildImage.isHidden = true
cautionImage.isHidden = true
if #available(OSX 10.12.2, *) {
textField.isAutomaticTextCompletionEnabled = true
}
tagPicker.completes = true
quitIrbSession()
runGeneralIrbSession()
DispatchQueue.global(qos: .background).async {
self.setupTagSelection()
if let tag = self.applicationStateHandler.tag {
DispatchQueue.main.async {
self.tagPicker.selectItem(withObjectValue: tag)
}
}
}
if applicationStateHandler.physicalRadioButtonState {
getDevices(ofType: .physical)
} else {
DispatchQueue.global(qos: .background).async {
self.getDevices(ofType: .simulator)
}
}
if let language = applicationStateHandler.language {
languagePopUpButton.selectItem(withTitle: language)
}
if let debugState = applicationStateHandler.debugState {
debugCheckbox.state = NSControl.StateValue(rawValue: debugState)
}
if let downloadCheckboxState = applicationStateHandler.downloadCheckbox {
downloadCheckbox.stringValue = downloadCheckboxState
}
}
private func setupTagSelection() {
tagsController.tags(in: calabashFolderPath).forEach { tag in
DispatchQueue.main.async {
self.tagPicker.addItem(withObjectValue: tag)
}
}
}
func killProcessScreenshot() {
CommandExecutor(launchPath: Constants.FilePaths.Bash.killProcess ?? "", arguments: []).execute()
}
@IBAction func clickDownloadButton(_ sender: Any) {
guard let url = URL(string: plistHandler.readKeys(forKey: Constants.Keys.linkInfo)[buildPicker.indexOfSelectedItem]) else { return }
applicationStateHandler.downloadCheckbox = downloadCheckbox.stringValue
DispatchQueue.global(qos: .background).async {
DispatchQueue.main.async {
self.spinner.startAnimation(self)
self.downloadButton.isEnabled = false
}
CommandsController().downloadApp(from: url, textView: self.textView)
DispatchQueue.main.async {
self.spinner.stopAnimation(self)
self.downloadButton.isEnabled = true
}
}
}
@IBAction func clickInstallButton(_ sender: Any) {
DispatchQueue.global(qos: .background).async {
DispatchQueue.main.async {
self.spinner.startAnimation(self)
self.installButton.isEnabled = false
}
let device: String
if self.applicationStateHandler.physicalRadioButtonState {
device = "physical"
} else {
device = "simulator"
}
CommandsController().installApp(textView: self.textView, deviceType: device)
DispatchQueue.main.async {
self.spinner.stopAnimation(self)
self.installButton.isEnabled = true
}
}
}
@IBAction func clickEraseSimulatorButton(_ sender: Any) {
DispatchQueue.global(qos: .background).async {
DispatchQueue.main.async {
self.spinner.startAnimation(self)
self.installButton.isEnabled = false
}
CommandsController().eraseSimulator(textView: self.textView)
DispatchQueue.main.async {
self.spinner.stopAnimation(self)
self.installButton.isEnabled = true
}
}
}
@IBAction func buildPicker(_ sender: Any) {
applicationStateHandler.buildName = buildPicker.titleOfSelectedItem
let elementsState = buildPicker.titleOfSelectedItem != Constants.Strings.useLocalBuild
downloadButton.isEnabled = elementsState
downloadCheckbox.isEnabled = elementsState
}
@IBAction func clearBufferButton(_ sender: Any) {
textView.string = ""
}
@IBAction func settingsButton(_ sender: Any) {
guard let controller = storyboard?.instantiateController(withIdentifier: .settingsWindow) as? NSViewController else { return }
presentViewControllerAsSheet(controller)
}
@IBAction func simulator_radio(_ sender: Any) {
applicationStateHandler.physicalRadioButtonState = false
phoneComboBox.removeAllItems()
handleRadioButtons()
if let phoneName = applicationStateHandler.phoneName,
phoneName != "\(Constants.Strings.noDevicesConnected) \(Constants.Strings.pluginDevice)" {
phoneComboBox.selectItem(withTitle: phoneName)
}
if phoneComboBox.selectedItem == nil {
phoneComboBox.highlight(true)
cautionImage.isHidden = false
let noConnectedSimulators = "\(Constants.Strings.noSimulatorsConnected) \(Constants.Strings.installSimulator)"
phoneComboBox.addItem(withTitle: noConnectedSimulators)
let previousOutput = textView.string
textView.string = "\(previousOutput)\n\(noConnectedSimulators)"
} else {
cautionImage.isHidden = true
}
}
@IBAction func phys_radio(_ sender: Any) {
applicationStateHandler.physicalRadioButtonState = true
phoneComboBox.removeAllItems()
handleRadioButtons()
if phoneComboBox.selectedItem == nil {
phoneComboBox.highlight(true)
cautionImage.isHidden = false
phoneComboBox.addItem(withTitle: "\(Constants.Strings.noDevicesConnected) \(Constants.Strings.pluginDevice)")
getDeviceButton.isEnabled = false
eraseSimulatorButton.isEnabled = true
} else {
getDeviceButton.isEnabled = true
eraseSimulatorButton.isEnabled = false
cautionImage.isHidden = true
}
}
@IBAction func clickTagPicker(_ sender: Any) {
if !tagPicker.stringValue.isEmpty {
applicationStateHandler.tag = tagPicker.stringValue
}
}
@IBAction func startTask(_ sender:AnyObject) {
applicationStateHandler.downloadCheckbox = downloadCheckbox.stringValue
if downloadCheckbox.state == .on, downloadCheckbox.isEnabled {
guard let url = URL(string: plistHandler.readKeys(forKey: Constants.Keys.linkInfo)[buildPicker.indexOfSelectedItem]) else { return }
DispatchQueue.global(qos: .background).async {
CommandsController().downloadApp(from: url, textView: self.textView)
self.runScript()
}
} else {
DispatchQueue.global(qos: .background).async {
self.runScript()
}
}
}
@IBAction func textField(_ sender: Any) {
defer { textField.stringValue = "" }
guard let launchPath = Constants.FilePaths.Bash.sendToIRB else { return }
let outputStream = CommandTextOutputStream()
outputStream.textHandler = { text in
guard !text.isEmpty else { return }
DispatchQueue.main.async {
self.textViewPrinter.printToTextView(text)
}
}
let argument = textField.stringValue
DispatchQueue.global(qos: .background).async {
CommandExecutor(launchPath: launchPath, arguments: [argument], outputStream: outputStream).execute()
}
}
@IBAction func toggleDebug(_ sender: NSButton) {
applicationStateHandler.debugState = sender.state.rawValue
}
@IBAction func stopTask(_ sender:AnyObject) {
guard let testRunProcess = commandExecutor else { return }
testRunProcess.terminate()
commandExecutor = nil
buildButton.isEnabled = true
spinner.stopAnimation(self)
progressBar.stopAnimation(self)
}
@IBAction func clickPhoneChooser(_ sender: Any) {
if !applicationStateHandler.physicalRadioButtonState {
applicationStateHandler.phoneName = phoneComboBox.titleOfSelectedItem
}
applicationStateHandler.phoneUDID = deviceCollector.getDeviceUDID(device: phoneComboBox.itemTitle(at: phoneComboBox.indexOfSelectedItem))
}
@IBAction func languageSwitchButton(_ sender: Any) {
localization.changeDefaultLocale(language: languagePopUpButton.title)
}
@IBAction func languagePopUp(_ sender: Any) {
applicationStateHandler.language = languagePopUpButton.title
guard
let controller = storyboard?.instantiateController(withIdentifier: .languageSettings) as? NSViewController,
languagePopUpButton.title == Language.other.rawValue else { return }
presentViewControllerAsModalWindow(controller)
}
@objc func cleanupOutput() {
let maxCharacters = 40_000
let characterCount = textView.string.count
if characterCount > maxCharacters {
textView.textStorage?.deleteCharacters(in: NSRange(location: 1, length: characterCount - maxCharacters))
}
}
func getDevicesCommand(ofType type: Constants.DeviceType) {
let path: String?
switch type {
case .simulator:
path = Constants.FilePaths.Bash.simulators
case .physical:
path = Constants.FilePaths.Bash.physicalDevices
}
guard let launchPath = path else { return }
let outputStream = CommandTextOutputStream()
outputStream.textHandler = { text in
let filteredText = text.components(separatedBy: "\n")
.filter { RegexHandler().matches(for: "\\(([^()])*\\) \\[(.*?)\\]", in: $0).isEmpty == false }
guard !filteredText.isEmpty else { return }
DispatchQueue.main.async {
self.phoneComboBox.addItems(withTitles: filteredText)
}
}
CommandExecutor(launchPath: launchPath, arguments: [], outputStream: outputStream).execute()
}
func emptyDeviceHandler() {
if isDeviceListEmpty {
phoneComboBox.highlight(true)
cautionImage.isHidden = false
let noConnectedDevices = "\(Constants.Strings.noDevicesConnected) \(Constants.Strings.installSimulatorOrPluginDevice)"
phoneComboBox.addItem(withTitle: noConnectedDevices)
let previousOutput = textView.string
textView.string = "\(previousOutput)\n\(noConnectedDevices)"
getDeviceButton.isEnabled = false
eraseSimulatorButton.isEnabled = true
} else {
cautionImage.isHidden = true
}
}
func getDevices(ofType type: Constants.DeviceType) {
DispatchQueue.main.async {
self.isRunning = true
}
switch type {
case .simulator:
getDevicesCommand(ofType: .simulator)
case .physical:
getDevicesCommand(ofType: .physical)
}
DispatchQueue.main.async {
if let phoneName = self.applicationStateHandler.phoneName,
self.phoneComboBox.itemTitles.contains(phoneName),
type == .simulator
{
self.phoneComboBox.selectItem(withTitle: phoneName)
} else {
self.phoneComboBox.selectItem(at: 0)
}
if type == .simulator {
self.applicationStateHandler.phoneName = self.phoneComboBox.titleOfSelectedItem
}
self.applicationStateHandler.phoneUDID = self.deviceCollector.getDeviceUDID(device: self.phoneComboBox.titleOfSelectedItem ?? "")
self.buildButton.isEnabled = true
self.isRunning = false
self.emptyDeviceHandler()
}
}
func handleRadioButtons(willGetDevice: Bool = true) {
if applicationStateHandler.physicalRadioButtonState {
simulatorRadioButton.state = .off
physicalDeviceRadioButton.state = .on
getDeviceButton.isEnabled = true
eraseSimulatorButton.isEnabled = false
languagePopUpButton.isEnabled = false
switchLanguageButton.isEnabled = false
if willGetDevice {
spinner.startAnimation(self)
progressBar.startAnimation(self)
getDevices(ofType: .physical)
spinner.stopAnimation(self)
progressBar.stopAnimation(self)
}
guard
let itemTitle = phoneComboBox.titleOfSelectedItem,
itemTitle.contains("(null)"),
let controller = storyboard?.instantiateController(withIdentifier: .deviceUnlock) as? NSViewController else { return }
presentViewControllerAsModalWindow(controller)
} else {
simulatorRadioButton.state = .on
physicalDeviceRadioButton.state = .off
getDeviceButton.isEnabled = false
eraseSimulatorButton.isEnabled = true
languagePopUpButton.isEnabled = true
switchLanguageButton.isEnabled = true
if willGetDevice {
spinner.startAnimation(self)
progressBar.startAnimation(self)
getDevices(ofType: .simulator)
spinner.stopAnimation(self)
progressBar.stopAnimation(self)
}
}
}
func populateBuildPicker() {
buildPicker.removeAllItems()
linkInfo = plistHandler.readValues(forKey: Constants.Keys.linkInfo)
buildPicker.addItems(withTitles: linkInfo)
buildPicker.addItem(withTitle: Constants.Strings.useLocalBuild)
if let buildName = applicationStateHandler.buildName, buildPicker.itemTitles.contains(buildName) {
buildPicker.selectItem(withTitle: buildName)
} else {
buildPicker.selectItem(withTitle: Constants.Strings.useLocalBuild)
}
let elementsState = buildPicker.titleOfSelectedItem != Constants.Strings.useLocalBuild
downloadButton.isEnabled = elementsState
downloadCheckbox.isEnabled = elementsState
}
func quitIrbSession() {
CommandExecutor(launchPath: Constants.FilePaths.Bash.quitIRBSession ?? "", arguments: []).execute()
}
func runScript() {
DispatchQueue.main.async {
if self.isDeviceListEmpty {
self.phoneComboBox.highlight(true)
self.cautionImage.isHidden = false
let previousOutput = self.textView.string
self.textView.string = "\(previousOutput)\n\(Constants.Strings.noDevicesConnected) \(Constants.Strings.installSimulatorOrPluginDevice)"
return
} else {
self.cautionImage.isHidden = true
}
}
var arguments: [String] = []
DispatchQueue.main.sync {
if self.debugCheckbox.state == .on {
arguments.append("DEBUG=1")
} else {
arguments.append("DEBUG=0")
}
}
arguments.append("DEVICE_TARGET=\(applicationStateHandler.phoneUDID ?? "")")
arguments.append(calabashFolderPath)
if let cucumberProfile = plistHandler.readValues(forKey: Constants.Keys.cucumberProfileInfo).first, !cucumberProfile.isEmpty {
arguments.append("-p \(cucumberProfile)")
}
DispatchQueue.main.sync {
// We still need an arugment to be passed, otherwise bash variable order will be spoiled
if !self.tagPicker.stringValue.isEmpty {
arguments.append("--t @\(self.tagPicker.stringValue)")
} else {
arguments.append("")
}
}
if let additionalRunParameter = plistHandler.readValues(forKey: Constants.Keys.additionalFieldInfo).first, !additionalRunParameter.isEmpty {
arguments.append("export \(additionalRunParameter)")
} else {
arguments.append("")
}
let commandToExecute = plistHandler.readValues(forKey: Constants.Keys.commandFieldInfo).first ?? ""
arguments.append(commandToExecute)
DispatchQueue.main.sync {
if let deviceIP = self.applicationStateHandler.deviceIP,
let bundleID = self.applicationStateHandler.bundleID,
self.physicalDeviceRadioButton.state == .on,
!deviceIP.isEmpty {
arguments.append("export DEVICE_ENDPOINT=http://\(deviceIP):\(Constants.CalabashData.port)")
arguments.append("export BUNDLE_ID=\(bundleID)")
} else if self.physicalDeviceRadioButton.state == .on {
self.textViewPrinter.printToTextView(Constants.Strings.wrongDeviceSetup)
self.textViewPrinter.printToTextView("\n")
return
}
}
DispatchQueue.main.async {
self.buildButton.isEnabled = false
self.isRunning = true
self.spinner.startAnimation(self)
self.progressBar.startAnimation(self)
}
if let launchPath = Constants.FilePaths.Bash.buildScript {
let outputStream = CommandTextOutputStream()
outputStream.textHandler = { text in
DispatchQueue.main.async {
self.textViewPrinter.printToTextView(text)
}
}
self.commandExecutor = CommandExecutor(launchPath: launchPath, arguments: arguments, outputStream: outputStream)
self.commandExecutor?.execute()
DispatchQueue.main.async {
self.buildButton.isEnabled = true
self.spinner.stopAnimation(self)
self.progressBar.stopAnimation(self)
self.isRunning = false
}
}
}
func runGeneralIrbSession() {
self.spinner.startAnimation(self)
self.progressBar.startAnimation(self)
guard let launchPath = Constants.FilePaths.Bash.createIRBSession else { return }
let outputStream = CommandTextOutputStream()
outputStream.textHandler = { text in
DispatchQueue.main.async {
self.textViewPrinter.printToTextView(text)
self.spinner.stopAnimation(self)
self.progressBar.stopAnimation(self)
}
}
var arguments: [String] = []
arguments.append(calabashFolderPath)
if let helpersPath = Constants.FilePaths.Ruby.helpers {
arguments.append(helpersPath)
}
DispatchQueue.global(qos: .background).async {
CommandExecutor(launchPath: launchPath, arguments: arguments, outputStream: outputStream).execute()
}
}
}