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

Commit

Permalink
Merge pull request #4 from orta/master
Browse files Browse the repository at this point in the history
Add support for CocoaPods.
  • Loading branch information
owensd committed Jul 6, 2015
2 parents f833cac + 389493d commit a372e8a
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
#
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 --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.
Expand Down
6 changes: 6 additions & 0 deletions samples/cocoapods/Podfile
Original file line number Diff line number Diff line change
@@ -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"
23 changes: 23 additions & 0 deletions samples/cocoapods/Podfile.lock
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions samples/cocoapods/main.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import Argo
import Runes

print("dependencies import properly")
1 change: 1 addition & 0 deletions src/ErrorCodes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ enum ErrorCode: Int, ErrorType {
case InvalidUsage = 1
case PathNotFound
case CarthageNotInstalled
case CocoaPodsNotInstalled
case SwiftNotInstalled
}
54 changes: 51 additions & 3 deletions src/Tools.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
18 changes: 15 additions & 3 deletions src/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
Expand All @@ -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 = ""
Expand All @@ -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()
Expand Down

0 comments on commit a372e8a

Please sign in to comment.