Files
i2p.i2p/launchers/macosx/I2PLauncher/subprocesses/TaskPipeline.swift
meeh 7615b9236b Adding all new code, removed a lot obsolete code and fixed import paths etc.
Mac OS X launcher:
* UI built on Swift
  * Why?
    * Apple seems to on purpose make it harder to get into Objective-C these days
    * Swift is compiled to native code, but has easiness of Javascript in programming
    * Perfect for the OS X UI, many guides & tutorials as well
* "Backend" in Objective-C++ / C++14
  * Why?
    * Originally written in Objective-C / C++14 with C++17 backports
    * Only for backend because of the time the development takes
    *

Short summary of features:

* Java
  * It can detect java from:
    * JAVA_HOME environment variable
    * "Internet Plug-Ins" Apple stuff
    * By the /usr/libexec/java_home binary helper
  * It can unpack a new version of I2P
    * Unpacks to ~/Library/I2P
    * Can check currently unpacked version in ~/Library/I2P via i2p.jar's "net.i2p.CoreVersion"

  * User Interface (a popover, see https://youtu.be/k8L3lQ5rUq0 for example of this concept)
    * Router control tab view
      * It can start the router
      * It can stop the router
      * It can detect already running router, then avoid fireing up one
      * It can show basic information about the router state & version
    * Log view tab (not yet done)
  * While left-click triggers popover, right-click draws a minimal context menu
2018-09-18 15:36:38 +00:00

114 lines
3.5 KiB
Swift

//
// TaskPipeline.swift
// I2PLauncher
//
// Created by Mikal Villa on 17/09/2018.
// Copyright © 2018 The I2P Project. All rights reserved.
//
import Foundation
// Extend the stdlib with the extension bellow
extension Process {
/// Launches a task, captures any objective-c exception and relaunches it as Swift error
public func launchCapturingExceptions() throws {
if let exception = AppleStuffExecuteWithPossibleExceptionInBlock({
self.launch()
}) {
let reason = exception.reason ?? "unknown error"
throw SubprocessError.Error(status: -1, message: reason)
}
}
}
/// A pipeline of tasks, connected in a cascade pattern with pipes
public struct TaskPipeline {
/// List of tasks in the pipeline
let tasks: [Process]
/// Output pipe
let outputPipe: Pipe?
/// Whether the pipeline should capture output to stdErr and stdOut
let captureOutput : Bool
/// Adds a task to the head of the pipeline, that is, the task will provide the input
/// for the first task currently on the head of the pipeline
func addToHead(task: Process) -> TaskPipeline {
guard let firstTask = tasks.first else {
fatalError("Expecting at least one task")
}
let inoutPipe = Pipe()
firstTask.standardInput = inoutPipe
task.standardOutput = inoutPipe
var errorPipe : Pipe?
if self.captureOutput {
errorPipe = Pipe()
task.standardError = errorPipe
}
return TaskPipeline(tasks: [task] + self.tasks, outputPipe: self.outputPipe, captureOutput: self.captureOutput)
}
/// Start all tasks in the pipeline, then wait for them to complete
/// - returns: the return status of the last process in the pipe, or nil if there was an error
func run() -> ExecutionResult? {
let runTasks = launchAndReturnNotFailedTasks()
if runTasks.count != self.tasks.count {
// dropped a task? it's because it failed to start, so error
return nil
}
runTasks.forEach { $0.waitUntilExit() }
// exit status
let exitStatuses = runTasks.map { $0.terminationStatus }
guard captureOutput else {
return ExecutionResult(pipelineStatuses: exitStatuses)
}
// output
let errorOutput = runTasks.map { task -> String in
guard let errorPipe = task.standardError as? Pipe else { return "" }
let readData = errorPipe.fileHandleForReading.readDataToEndOfFile()
return String(data: readData, encoding: String.Encoding.utf8)!
}
let output = String(data: self.outputPipe!.fileHandleForReading.readDataToEndOfFile(), encoding: String.Encoding.utf8)!
return ExecutionResult(pipelineStatuses: exitStatuses, pipelineErrors: errorOutput, output: output)
}
/// Run all tasks and return the tasks that did not fail to launch
private func launchAndReturnNotFailedTasks() -> [Process] {
return self.tasks.flatMap { task -> Process? in
do {
try task.launchCapturingExceptions()
return task
} catch {
return nil
}
}
}
init(task: Process, captureOutput: Bool) {
self.tasks = [task]
self.captureOutput = captureOutput
if captureOutput {
self.outputPipe = Pipe()
task.standardOutput = self.outputPipe
let errorPipe = Pipe()
task.standardError = errorPipe
} else {
self.outputPipe = nil
}
}
private init(tasks: [Process], outputPipe: Pipe?, captureOutput: Bool) {
self.tasks = tasks
self.outputPipe = outputPipe
self.captureOutput = captureOutput
}
}