Skip to content

Commit

Permalink
Merge pull request #31 from JoeSSS/simplification
Browse files Browse the repository at this point in the history
Simplify get simulator command + reduce launch time
  • Loading branch information
JoeSSS authored Oct 23, 2017
2 parents ddc3fd1 + 2286a26 commit 80150ff
Show file tree
Hide file tree
Showing 7 changed files with 151 additions and 402 deletions.
2 changes: 1 addition & 1 deletion Launcher/Classes/Controllers/TagsController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class TagsController {
if let launchPath = Constants.FilePaths.Bash.tags {
let outputStream = CommandsCore.CommandTextOutputStream()
outputStream.textHandler = {text in
tags.append(contentsOf: text.components(separatedBy: "\n"))
tags.append(contentsOf: text.components(separatedBy: "\n").filter { !$0.isEmpty })
}

let commands = CommandsCore.CommandExecutor()
Expand Down
45 changes: 0 additions & 45 deletions Launcher/Classes/DeviceCollector.swift
Original file line number Diff line number Diff line change
@@ -1,51 +1,6 @@
import Foundation

class DeviceCollector {
var getSimTask15: Process!
var outputPipe: Pipe!
var outputText = ""

func simulators(completion: @escaping () -> (), output: @escaping (String) -> ()) {
let taskQueue = DispatchQueue.global(qos: .background)

taskQueue.sync {

let path = Constants.FilePaths.Bash.simulators
self.getSimTask15 = Process()
self.getSimTask15.launchPath = path

self.getSimTask15.terminationHandler = { task in
completion()
}

captureStandardOutputAndRouteToTextView(self.getSimTask15, completionHandler: output)

self.getSimTask15.launch()
self.getSimTask15.waitUntilExit()
}
}

func captureStandardOutputAndRouteToTextView(_ task: Process, completionHandler handler: @escaping (String) -> ()) {

outputPipe = Pipe()
task.standardOutput = outputPipe

outputPipe.fileHandleForReading.waitForDataInBackgroundAndNotify()

NotificationCenter.default.addObserver(forName: .NSFileHandleDataAvailable, object: outputPipe.fileHandleForReading , queue: nil) { notification in
let output = self.outputPipe.fileHandleForReading.availableData
let outputString = String(data: output, encoding: .utf8) ?? ""

if !outputString.isEmpty {
DispatchQueue.main.async {
handler(outputString)
}
}
// FIXME: Call a completion handler with an error here?
}
self.outputPipe.fileHandleForReading.waitForDataInBackgroundAndNotify()
}

func getDeviceUDID(device : String) -> String {
// Gets simulator UDID by parcing value between square brackets
let regex = "\\[(.*?)\\]"
Expand Down
81 changes: 19 additions & 62 deletions Launcher/Classes/ViewControllers/InspectorViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,8 @@ import CommandsCore
class InspectorViewController: NSViewController, NSTableViewDataSource {
let commands = CommandsCore.CommandExecutor()
let applicationStateHandler = ApplicationStateHandler()
var runDeviceTask:Process!
var runDeviceTask1:Process!
var runDeviceTask2:Process!
var buildTaskNew558:Process!
var textViewPrinter: TextViewPrinter!
@objc dynamic var isRunning = false
var outputPipe:Pipe!
let defaults = UserDefaults.standard
let env = ProcessInfo.processInfo.environment as [String: String]
let fileManager = FileManager.default
var uiElements: [String] = []
var parentCollection: [String] = []
Expand Down Expand Up @@ -218,28 +212,15 @@ class InspectorViewController: NSViewController, NSTableViewDataSource {
}

func getElementsByOffset(_ arguments:[String]) {
let taskQueue6 = DispatchQueue.global(qos: .background)
taskQueue6.async {
let path = Constants.FilePaths.Bash.elementsByOffset
self.runDeviceTask = Process()
self.runDeviceTask.launchPath = path
self.runDeviceTask.arguments = arguments
self.runDeviceTask.launch()
}
commands.executeCommand(at: Constants.FilePaths.Bash.elementsByOffset ?? "", arguments: arguments)
}

func getElements() {
let taskQueue7 = DispatchQueue.global(qos: .background)

taskQueue7.async {
let path = Constants.FilePaths.Bash.elements
self.runDeviceTask1 = Process()
self.runDeviceTask1.launchPath = path
if let filePath = self.applicationStateHandler.filePath {
self.runDeviceTask1.arguments = [filePath.absoluteString]
}
self.runDeviceTask1.launch()
var arguments: [String] = []
if let filePath = self.applicationStateHandler.filePath {
arguments = [filePath.absoluteString]
}
commands.executeCommand(at: Constants.FilePaths.Bash.elements ?? "", arguments: arguments)
}


Expand All @@ -250,18 +231,22 @@ class InspectorViewController: NSViewController, NSTableViewDataSource {

func startDevice() {
try? fileManager.removeItem(atPath: "/tmp/screenshot_0.png")
let taskQueue8 = DispatchQueue.global(qos: .background)

taskQueue8.async {
let path = Constants.FilePaths.Bash.startDevice
self.runDeviceTask2 = Process()
self.runDeviceTask2.launchPath = path
if let launchPath = Constants.FilePaths.Bash.startDevice {
let outputStream = CommandsCore.CommandTextOutputStream()
outputStream.textHandler = { text in
guard !text.isEmpty else { return }
DispatchQueue.main.async {
self.textViewPrinter.printToTextView(text)
}
}
var arguments: [String] = []
if let phoneUDID = self.applicationStateHandler.phoneUDID {
self.runDeviceTask2.arguments = [phoneUDID]
arguments = [phoneUDID]
}
DispatchQueue.global(qos: .background).async {
self.commands.executeCommand(at: launchPath, arguments: arguments, outputStream: outputStream)
}
self.captureStandardOutputAndRouteToTextView(self.runDeviceTask2)
self.runDeviceTask2.launch()
self.runDeviceTask2.waitUntilExit()
}

self.waitingForFile(fileName: "/tmp/screenshot_0.png", numberOfRetries: 9999) {
Expand All @@ -274,34 +259,6 @@ class InspectorViewController: NSViewController, NSTableViewDataSource {
}
}

func captureStandardOutputAndRouteToTextView(_ task:Process) {

outputPipe = Pipe()
task.standardOutput = outputPipe

outputPipe.fileHandleForReading.waitForDataInBackgroundAndNotify()

NotificationCenter.default.addObserver(forName: .NSFileHandleDataAvailable, object: outputPipe.fileHandleForReading, queue: nil) { notification in

let output = self.outputPipe.fileHandleForReading.availableData
let outputString = String(data: output, encoding: .utf8) ?? ""

DispatchQueue.main.async {
let previousOutput = self.outputText.string

if !outputString.isEmpty {

let nextOutput = previousOutput + "\n" + outputString
self.outputText.string = nextOutput

let range = NSRange(location: nextOutput.count, length: 0)
self.outputText.scrollRangeToVisible(range)
}
}
self.outputPipe.fileHandleForReading.waitForDataInBackgroundAndNotify()
}
}

@objc func getScreenProcsLoop() {
syncScreen()
waitingForFile(fileName: "/tmp/screenshot_0.png", numberOfRetries: 50, enableSpinner: false) {
Expand Down
Loading

0 comments on commit 80150ff

Please sign in to comment.