Skip to content
This repository has been archived by the owner on Dec 13, 2018. It is now read-only.

Commit

Permalink
I had to comment out the NSTask/sdtdout redirection code as it didn't…
Browse files Browse the repository at this point in the history
… work reliably. Falling back to the previous behavior, which is not ideal, but at least it's robust. I'll look at this for v0.3.0.

The `filesAtPath()` needed to filter out the Carthage and CocoaPods folders and not try to build that content into the script tool... might be a good thing.
  • Loading branch information
owensd committed Jul 7, 2015
1 parent 62e3756 commit 4203823
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 25 deletions.
2 changes: 2 additions & 0 deletions apous.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
7D3AC3451B49F99B0068CC83 /* Utils.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Utils.swift; sourceTree = "<group>"; };
7D3AC3471B49FE170068CC83 /* ErrorCodes.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ErrorCodes.swift; sourceTree = "<group>"; };
7D3AC3491B4A37BC0068CC83 /* Tools.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Tools.swift; sourceTree = "<group>"; };
7DC546B91B4C55640070A858 /* samples */ = {isa = PBXFileReference; lastKnownFileType = folder; path = samples; sourceTree = SOURCE_ROOT; };
7DF997431B4B33A200E90F56 /* VersionInfo.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = VersionInfo.swift; sourceTree = "<group>"; };
7DF9974F1B4B356700E90F56 /* version.sh */ = {isa = PBXFileReference; lastKnownFileType = text.script.sh; path = version.sh; sourceTree = "<group>"; };
/* End PBXFileReference section */
Expand Down Expand Up @@ -127,6 +128,7 @@
7D0F00921B4AF32F003B6EF0 /* test */ = {
isa = PBXGroup;
children = (
7DC546B91B4C55640070A858 /* samples */,
7D0F00931B4AF32F003B6EF0 /* Samples.swift */,
7D0F00951B4AF32F003B6EF0 /* Info.plist */,
);
Expand Down
2 changes: 1 addition & 1 deletion samples/carthage/main.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Argo
import Runes

print("dependencies import properly")
print("dependencies imported properly")
2 changes: 1 addition & 1 deletion samples/cocoapods/main.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Argo
import Runes

print("dependencies import properly")
print("dependencies imported properly")
47 changes: 29 additions & 18 deletions src/Tools.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,28 @@ typealias Tool = (args: String...) throws -> TaskResult
/// by `output` and `error`.
func runTask(launchPath: String, args: [String] = [], outputToStandardOut: Bool = true) throws -> TaskResult
{
// Ok, so stdout sucks the big one. If your NSTask actually does any redirection to another
// tool that then outputs to stdout, that is going to be buffered and will only come back in
// chunks.

var master: Int32 = 0
var slave: Int32 = 0
if openpty(&master, &slave, nil, nil, nil) == -1 {
throw ErrorCode.PTYCreationFailed
}
defer {
close(master)
close(slave)
}

let output = NSFileHandle(fileDescriptor: master)

// This is the buffered output that will be returned.
var out = ""


// It turns out this code is not robust; it does not seem to always get all of the stream data.
// BUG #12 - https://github.com/owensd/apous/issues/12
//
// // Ok, so stdout sucks the big one. If your NSTask actually does any redirection to another
// // tool that then outputs to stdout, that is going to be buffered and will only come back in
// // chunks.
//
// var master: Int32 = 0
// var slave: Int32 = 0
// if openpty(&master, &slave, nil, nil, nil) == -1 {
// throw ErrorCode.PTYCreationFailed
// }
// defer {
// close(master)
// close(slave)
// }
//
// let output = NSFileHandle(fileDescriptor: master)

func stream(handle: NSFileHandle) -> String {
let data = handle.availableData
let str = NSString(data: data, encoding: NSUTF8StringEncoding) as? String ?? ""
Expand Down Expand Up @@ -85,8 +88,10 @@ func runTask(launchPath: String, args: [String] = [], outputToStandardOut: Bool

return stripped
}

let output = NSPipe()

output.readabilityHandler = { out += stream($0) }
output.fileHandleForReading.readabilityHandler = { out += stream($0) }

let task = NSTask()
task.launchPath = try canonicalPath(launchPath)
Expand Down Expand Up @@ -122,11 +127,17 @@ extension tools {

static func pod(args: String...) throws -> TaskResult {
guard let path = try launchPathForTool("pod") else { throw ErrorCode.CocoaPodsNotInstalled }

let info = args.reduce("") { $0 + $1 + " " }
print("Running pod \(info)")
return try runTask(path, args: args)
}

static func carthage(args: String...) throws -> TaskResult {
guard let path = try launchPathForTool("carthage") else { throw ErrorCode.CarthageNotInstalled }

let info = args.reduce("") { $0 + $1 + " " }
print("Running carthage \(info)")
return try runTask(path, args: args)
}

Expand Down
7 changes: 4 additions & 3 deletions src/Utils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ func filesAtPath(path: String) -> [String] {
}()

return items
.filter() { $0.pathExtension == "swift" }
.filter() {
let root = $0.pathComponents[0]
return $0.pathExtension == "swift" && (root != "Carthage" && root != "Rome" && root != "Pods")
}
.map() { path.stringByAppendingPathComponent($0) }
}


4 changes: 2 additions & 2 deletions test/Samples.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ class SamplesTest : XCTestCase {
}

func testMulti() {
let output = "foo: 2\r\nbar: 1"
let output = "foo: 2\nbar: 1"
validateSampleToolOutput("multi", output: output)
}

func testNested() {
let output = "Testing Nested Directories\r\nabspath: abspath!\r\nbasename: basename!"
let output = "Testing Nested Directories\nabspath: abspath!\nbasename: basename!"
validateSampleToolOutput("nested", output: output)
}
}

0 comments on commit 4203823

Please sign in to comment.