From fb7a56939afdc79ba8b9021e1ea1e08a8dcc12c4 Mon Sep 17 00:00:00 2001 From: Orta Therox Date: Mon, 6 Jul 2015 09:34:46 +0100 Subject: [PATCH 1/2] Add support for CocoaPods --- README.md | 6 ++-- samples/cocoapods/Podfile | 6 ++++ samples/cocoapods/main.swift | 4 +++ src/ErrorCodes.swift | 1 + src/Tools.swift | 54 ++++++++++++++++++++++++++++++++++-- src/main.swift | 18 ++++++++++-- 6 files changed, 80 insertions(+), 9 deletions(-) create mode 100644 samples/cocoapods/Podfile create mode 100644 samples/cocoapods/main.swift diff --git a/README.md b/README.md index a07f9ec..a68347f 100644 --- a/README.md +++ b/README.md @@ -11,12 +11,12 @@ Apous is a simple tool that allows for easier authoring of Swift scripts. Primary features: 1. Allow the breaking up of scripts into multiple files. - 2. Dependency management through [Carthage](https://github.com/Carthage/Carthage). + 2. Dependency management through [Carthage](https://github.com/Carthage/Carthage) or [CocoaPods](https://github.com/CocoaPods/CocoaPods/). # How it Works -Apous works by first checking for a `Cartfile` in your script's directory. If one is -present, then `carthage update` will be run. +Apous works by first checking for a `Cartfile` or `Podfile` in your script's directory. If one is +present, then `carthage update` or `pod install --ono-integrate` will be run. Next, all of your Swift files are combined into a single `.apous.swift` file that can then be run by the `swift` REPL. diff --git a/samples/cocoapods/Podfile b/samples/cocoapods/Podfile new file mode 100644 index 0000000..2c7b3cd --- /dev/null +++ b/samples/cocoapods/Podfile @@ -0,0 +1,6 @@ +platform :osx, '10.10' +use_frameworks! + +plugin 'cocoapods-rome' + +pod "Argo", :git => "https://github.com/thoughtbot/Argo", :branch => "td-swift-2" \ No newline at end of file diff --git a/samples/cocoapods/main.swift b/samples/cocoapods/main.swift new file mode 100644 index 0000000..073312b --- /dev/null +++ b/samples/cocoapods/main.swift @@ -0,0 +1,4 @@ +import Argo +import Runes + +print("dependencies import properly") \ No newline at end of file diff --git a/src/ErrorCodes.swift b/src/ErrorCodes.swift index 418d8f1..0f52989 100644 --- a/src/ErrorCodes.swift +++ b/src/ErrorCodes.swift @@ -10,5 +10,6 @@ enum ErrorCode: Int, ErrorType { case InvalidUsage = 1 case PathNotFound case CarthageNotInstalled + case CocoaPodsNotInstalled case SwiftNotInstalled } diff --git a/src/Tools.swift b/src/Tools.swift index c85fb91..9518c68 100644 --- a/src/Tools.swift +++ b/src/Tools.swift @@ -67,22 +67,70 @@ extension Tool { } - struct WhichTool : Tool { let printOutput = false let launchPath = "/usr/bin/which" } +struct CocoaPodsTool: Tool { + let launchPath: String + + init?() { + let which = WhichTool() + let result = which.run("pod") + if result.out.characters.count == 0 { return nil } + self.launchPath = result.out + } + + // HACK(owensd): I cannot figure out why this tool will not flush our to stdout in real-time, + // so forcing it to write to stdout for now. + func run(args: String...) -> (out: String, err: String, code: Int32) { + let output = NSFileHandle.fileHandleWithStandardOutput() + let error = NSFileHandle.fileHandleWithStandardError() + + var out = "" + var err = "" + + func stream(handle: NSFileHandle) -> String { + let data = handle.availableData + let str = NSString(data: data, encoding: NSUTF8StringEncoding) ?? "" + return str as String + } + + // NOTE(owensd): These don't work for stdout and stderr... + output.readabilityHandler = { out += stream($0) } + error.readabilityHandler = { err += stream($0) } + + let task = NSTask() + task.launchPath = self.launchPath + task.arguments = args + task.standardOutput = output + task.standardError = error + task.terminationHandler = { + ($0.standardOutput as? NSFileHandle)?.readabilityHandler = nil + ($0.standardError as? NSFileHandle)?.readabilityHandler = nil + } + + task.launch() + task.waitUntilExit() + + return ( + out: out.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet()), + err: err.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet()), + code: task.terminationStatus) + } +} + struct CarthageTool : Tool { let launchPath: String - + init?() { let which = WhichTool() let result = which.run("carthage") if result.out.characters.count == 0 { return nil } self.launchPath = result.out } - + // HACK(owensd): I cannot figure out why this tool will not flush our to stdout in real-time, // so forcing it to write to stdout for now. func run(args: String...) -> (out: String, err: String, code: Int32) { diff --git a/src/main.swift b/src/main.swift index 8ab56bd..14d8002 100644 --- a/src/main.swift +++ b/src/main.swift @@ -9,6 +9,8 @@ import Foundation let CartfileConfig = "Cartfile" +let PodfileConfig = "Podfile" + let ApousScriptFile = ".apous.swift" // TODO(owensd): Pull this from a proper versioning tool. @@ -46,11 +48,12 @@ func run() throws { } let path = try canonicalPath(scriptItem[0]) + let fileManager = NSFileManager.defaultManager() // The tools need to be run under the context of the script directory. - NSFileManager.defaultManager().changeCurrentDirectoryPath(path) + fileManager.changeCurrentDirectoryPath(path) - if NSFileManager.defaultManager().fileExistsAtPath(path.stringByAppendingPathComponent(CartfileConfig)) { + if fileManager.fileExistsAtPath(path.stringByAppendingPathComponent(CartfileConfig)) { guard let carthage = CarthageTool() else { print("Carthage does not seem to be installed or in your path.") exit(.CarthageNotInstalled) @@ -59,6 +62,15 @@ func run() throws { carthage.run("update") } + if fileManager.fileExistsAtPath(path.stringByAppendingPathComponent(PodfileConfig)) { + guard let pods = CocoaPodsTool() else { + print("CocoaPods does not seem to be installed or in your path.") + exit(.CocoaPodsNotInstalled) + } + + pods.run("install --no-integrate") + } + let files = filesAtPath(path) var script = "" @@ -74,7 +86,7 @@ func run() throws { exit(.SwiftNotInstalled) } - swift.run("-F", "Carthage/Build/Mac", scriptPath) + swift.run("-F", "Carthage/Build/Mac", "Rome", scriptPath) } try run() From 389493dc09f2e24ddd4519fe3ed9cf82b2183a25 Mon Sep 17 00:00:00 2001 From: Orta Therox Date: Mon, 6 Jul 2015 09:44:00 +0100 Subject: [PATCH 2/2] Update the gitignore for CocoaPods --- .gitignore | 1 + README.md | 2 +- samples/cocoapods/Podfile.lock | 23 +++++++++++++++++++++++ src/main.swift | 2 +- 4 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 samples/cocoapods/Podfile.lock diff --git a/.gitignore b/.gitignore index f2c828e..d697c7f 100644 --- a/.gitignore +++ b/.gitignore @@ -27,6 +27,7 @@ DerivedData # http://guides.cocoapods.org/using/using-cocoapods.html#should-i-ignore-the-pods-directory-in-source-control # Pods/ +Rome/ # Carthage # diff --git a/README.md b/README.md index a68347f..011a172 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ Primary features: # How it Works Apous works by first checking for a `Cartfile` or `Podfile` in your script's directory. If one is -present, then `carthage update` or `pod install --ono-integrate` will be run. +present, then `carthage update` or `pod install --no-integrate` will be run. Next, all of your Swift files are combined into a single `.apous.swift` file that can then be run by the `swift` REPL. diff --git a/samples/cocoapods/Podfile.lock b/samples/cocoapods/Podfile.lock new file mode 100644 index 0000000..ac23939 --- /dev/null +++ b/samples/cocoapods/Podfile.lock @@ -0,0 +1,23 @@ +PODS: + - Argo (1.0.3): + - Runes (>= 1.2.2) + - Runes (2.0.0) + +DEPENDENCIES: + - Argo (from `https://github.com/thoughtbot/Argo`, branch `td-swift-2`) + +EXTERNAL SOURCES: + Argo: + :branch: td-swift-2 + :git: https://github.com/thoughtbot/Argo + +CHECKOUT OPTIONS: + Argo: + :commit: 5b18ce0da13e3e6d8a3a5188cbf00bd8d5c86a0c + :git: https://github.com/thoughtbot/Argo + +SPEC CHECKSUMS: + Argo: 541f7b9167f264ddf9c6c5d75a87e8c68e29929d + Runes: 4fe81355f4620b76b02176222d264b33e60dba51 + +COCOAPODS: 0.38.0.beta.2 diff --git a/src/main.swift b/src/main.swift index 14d8002..c442525 100644 --- a/src/main.swift +++ b/src/main.swift @@ -68,7 +68,7 @@ func run() throws { exit(.CocoaPodsNotInstalled) } - pods.run("install --no-integrate") + pods.run("install", "--no-integrate") } let files = filesAtPath(path)