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 5 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
143 changes: 21 additions & 122 deletions Launcher/Classes/ViewControllers/TasksViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,37 +25,21 @@ class TasksViewController: NSViewController {
var deviceListIsEmpty = false
var buildItemIsDisabled = false
@objc dynamic var isRunning = false
var outputPipeConsole: Pipe?
var outputPipeTestRun: Pipe?
var buildTask: Process!
var buildTask1: Process!
var buildTask2: Process!
var buildProcess:Process!
var killProcessesProcess:Process!
var sendToIRBSessionProcess:Process!
var simulatorProcess:Process!
var generalIRBSessionTask:Process!
var createIRBSessionTask:Process!
let env = ProcessInfo.processInfo.environment as [String: String]
let applicationStateHandler = ApplicationStateHandler()
let tagsController = TagsController()
let fileManager = FileManager.default
var devices: [String] = [""]
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe either remove the : [String] or make the initial array empty ([])

var simulators: [String] = [""]
var file = ""
var timer: Timer!
var pathToCalabashFolder = ""

override func viewDidAppear() {
super.viewDidAppear()
let filePath2 = "/tmp/allout.txt"
let filePath3 = "/tmp/phys_dev.txt"
let filePath4 = "/tmp/tag_list.txt"

try? fileManager.removeItem(atPath: filePath2)
try? fileManager.removeItem(atPath: filePath3)
try? fileManager.removeItem(atPath: filePath4)

textField.backgroundColor = .darkAquamarine
textField.textColor = .white
let placeholderText = NSMutableAttributedString(string: "Console Input (Beta)")
Expand Down Expand Up @@ -112,8 +96,6 @@ class TasksViewController: NSViewController {
tagPicker.completes = true
killIrbSession()
runGeneralIrbSession()
getSimulators()

setupTagSelection()

if let simulatorRadioButtonState = applicationStateHandler.simulatorRadioButtonState,
Expand All @@ -124,49 +106,7 @@ class TasksViewController: NSViewController {

disableBuildItems()

let filePath2 = "/tmp/allout.txt"
let filePath3 = "/tmp/phys_dev.txt"

if let bStreamReader = StreamReader(path: filePath2) {
defer {
bStreamReader.close()
}
while var line = bStreamReader.nextLine() {
line = line.trimmingCharacters(in: .whitespaces)
phoneComboBox.addItem(withTitle: line)
}
}


simulators = phoneComboBox.itemTitles

phoneComboBox.removeAllItems()

if let bStreamReader = StreamReader(path: filePath3) {
defer {
bStreamReader.close()
}
while var line = bStreamReader.nextLine() {
line = line.trimmingCharacters(in: .whitespaces)
phoneComboBox.addItem(withTitle: line)
}
}

devices = phoneComboBox.itemTitles

phoneComboBox.removeAllItems()

if simulator_radio.state == .on {
languagePopUpButton.isEnabled = true
phoneComboBox.addItems(withTitles: simulators)
} else {
get_device.isEnabled = true
languagePopUpButton.isEnabled = false
phoneComboBox.addItems(withTitles: devices)
}

phoneComboBox.selectItem(at: 0)

getSimulators()
selectDeviceIfAvailable(prefixed: "iPhone 7(")

if phoneComboBox.selectedItem == nil {
Expand Down Expand Up @@ -255,24 +195,6 @@ class TasksViewController: NSViewController {

getSimulators()

phoneComboBox.removeAllItems()

let filePath6 = "/tmp/phys_dev.txt"
let filePath7 = "/tmp/allout.txt"

if let bStreamReader = StreamReader(path: filePath6) {
defer {
bStreamReader.close()
}
while var line = bStreamReader.nextLine() {
line = line.trimmingCharacters(in: .whitespaces)
phoneComboBox.addItem(withTitle: line)
}
}

try? fileManager.removeItem(atPath: filePath6)
try? fileManager.removeItem(atPath: filePath7)

if phoneComboBox.selectedItem == nil {
deviceListIsEmpty = true
phoneComboBox.highlight(true)
Expand All @@ -285,19 +207,13 @@ class TasksViewController: NSViewController {

spinner.stopAnimation(self)
progressBar.stopAnimation(self)

devices = self.phoneComboBox.itemTitles
}

@IBAction func simulator_radio(_ sender: Any) {
phys_radio.state = .off
get_device.isEnabled = false
languagePopUpButton.isEnabled = true

phoneComboBox.removeAllItems()

phoneComboBox.addItems(withTitles: simulators)


if let phoneName = applicationStateHandler.phoneName,
phoneName != "\(Constants.Strings.noDevicesConnected) \(Constants.Strings.pluginDevice)" {
phoneComboBox.selectItem(withTitle: phoneName)
Expand Down Expand Up @@ -512,41 +428,23 @@ class TasksViewController: NSViewController {
get_device.isEnabled = false
isRunning = true

deviceCollector.simulators(completion: {
DispatchQueue.global(qos: .background).async { [weak self] in
guard let strongSelf = self else { return }
if let launchPath = Constants.FilePaths.Bash.simulators {
let outputStream = CommandsCore.CommandTextOutputStream()
outputStream.textHandler = { text in
DispatchQueue.main.async {
strongSelf.buildButton.isEnabled = true
strongSelf.spinner.stopAnimation(strongSelf)
strongSelf.get_device.isEnabled = true
strongSelf.progressBar.stopAnimation(strongSelf)
}
strongSelf.isRunning = false
}
}) { output in
DispatchQueue.global(qos: .background).async { [weak self] in
guard let strongSelf = self else { return }
let previousOutput = strongSelf.textView.string
if !output.isEmpty {
let nextOutput = "\(previousOutput)\n\(output)"
strongSelf.textView.string = nextOutput

let range = NSRange(location: nextOutput.count, length: 0)
strongSelf.textView.scrollRangeToVisible(range)
let filderedText = text.components(separatedBy: "\n").filter { !$0.isEmpty }
self.phoneComboBox.addItems(withTitles: filderedText)
}
}
let commands = CommandsCore.CommandExecutor()
commands.executeCommand(at: launchPath, 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.

[]?

}

DispatchQueue.global(qos: .background).async { [weak self] in
guard let strongSelf = self else { return }
DispatchQueue.main.async {
strongSelf.buildButton.isEnabled = true
strongSelf.get_device.isEnabled = true
strongSelf.spinner.stopAnimation(strongSelf)
strongSelf.progressBar.stopAnimation(strongSelf)
}
strongSelf.isRunning = false
}
buildButton.isEnabled = true
spinner.stopAnimation(self)
get_device.isEnabled = true
progressBar.stopAnimation(self)
isRunning = false
}

func killIrbSession() {
Expand Down Expand Up @@ -585,13 +483,14 @@ class TasksViewController: NSViewController {
}
}
let commands = CommandsCore.CommandExecutor()
Copy link
Contributor

Choose a reason for hiding this comment

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

Unrelated, let's create a ticket for this: we should remove the runScript from the VC.

Copy link
Contributor

Choose a reason for hiding this comment

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

I've created #32 and #33

commands.executeCommand(at: launchPath, arguments: arguments, outputStream: outputStream)
DispatchQueue.global(qos: .background).async {
commands.executeCommand(at: launchPath, arguments: arguments, outputStream: outputStream)
self.buildButton.isEnabled = true
self.spinner.stopAnimation(self)
self.progressBar.stopAnimation(self)
self.isRunning = false
}
}

buildButton.isEnabled = true
spinner.stopAnimation(self)
progressBar.stopAnimation(self)
isRunning = false
}

func runGeneralIrbSession() {
Expand Down
9 changes: 1 addition & 8 deletions Scripts/bash/get_sim_list.command
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
#!/bin/bash --login

xcrun instruments -s devices | grep -E "Simulator" | sed '/Known Devices:/d' | sed '/nb/d' | sed '/Apple Watch -/d' | sed '/Apple TV/d' | sort -nr -t/ -k2,2>/tmp/allout.txt 2>&1
xcrun instruments -s devices | sed '/Simulator/d' | sed '/Known Devices:/d' | sed '/nb/d' | sed '/Apple Watch -/d' | sed '/Apple TV/d'>/tmp/phys_dev.txt 2>&1
xcrun instruments -s devices | grep -E "Simulator" | grep -E 'iPhone|iPad' | sed '/Watch/d' | sort -nr -t/ -k2,2


if xcrun instruments -s devices | sed '/Simulator/d' | sed '/Known Devices:/d' | sed '/nb/d' | sed '/Apple Watch/d' | grep -q '(null)'; then
echo "Please unlock your iPhone and tap on 'Trust' button"
fi

exit
1 change: 0 additions & 1 deletion Scripts/bash/get_tags.command
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,4 @@ file=${1}/Gemfile
if [ -e "$file" ]; then
dir=$(find ${1} -maxdepth 6 -type d -name 'features' -print -quit)
grep -R "@" ${dir} | tr -s " " "\012" | sed -n -e '/^@/p' | tr -d @ | sort -u
exit
fi