Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify get simulator command + reduce launch time #31

Merged
merged 11 commits into from
Oct 23, 2017
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 })
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@q231950 Not sure why it is needed. Sometimes we get a lot of empty strings after actual result.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess somewhere in the script an empty line is generated.

}

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
82 changes: 20 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,23 @@ 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
if !text.isEmpty {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe make this guard !text.isEmpty else { return } to prevent some indentation?

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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are some methods like running tests or this one that takes a lot of time to continue. If I do it in the main thread, it will freeze the whole APP, but we actually can use the APP while these commands are executing in background

}
self.captureStandardOutputAndRouteToTextView(self.runDeviceTask2)
self.runDeviceTask2.launch()
self.runDeviceTask2.waitUntilExit()
}

self.waitingForFile(fileName: "/tmp/screenshot_0.png", numberOfRetries: 9999) {
Expand All @@ -274,34 +260,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