This repository has been archived by the owner on Nov 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 282
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #245 from CocoaPods/dan-new-podfile
Adds support for running `pod init` on an Xcode Project
- Loading branch information
Showing
6 changed files
with
126 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import Foundation | ||
|
||
public enum CPPodfileInitErrors: ErrorType { | ||
case CommandError(String) | ||
case NSURLError | ||
|
||
var message: String { | ||
switch self { | ||
case .CommandError(let s): return s | ||
case .NSURLError: return "NSURL unexpectedly nil" | ||
} | ||
} | ||
} | ||
|
||
public class CPPodfileInitController: NSObject, CPCLITaskDelegate { | ||
private var task: CPCLITask! | ||
private let completionHandler: (NSURL?, CPPodfileInitErrors?) -> () | ||
private let output = NSMutableAttributedString() | ||
private let projectURL: NSURL | ||
|
||
init(xcodeprojURL: NSURL, completionHandler: (podfileURL: NSURL?, error: CPPodfileInitErrors?) -> ()) { | ||
self.completionHandler = completionHandler | ||
self.projectURL = xcodeprojURL | ||
|
||
super.init() | ||
let task = CPCLITask(workingDirectory: xcodeprojURL.URLByDeletingLastPathComponent!.path, | ||
command: "init", | ||
delegate: self, | ||
qualityOfService: .UserInitiated) | ||
self.task = task | ||
|
||
self.task.run() | ||
} | ||
|
||
public func task(task: CPCLITask!, didUpdateOutputContents updatedOutput: NSAttributedString!) { | ||
output.appendAttributedString(updatedOutput) | ||
} | ||
|
||
public func taskCompleted(task: CPCLITask!) { | ||
guard task.finishedSuccessfully() else { | ||
self.callbackWithError(CPPodfileInitErrors.CommandError(self.output.string)) | ||
return | ||
} | ||
|
||
guard let podfileURL = projectURL.URLByDeletingLastPathComponent?.URLByAppendingPathComponent("Podfile") where NSFileManager().fileExistsAtPath(podfileURL.path ?? "") | ||
else { | ||
self.callbackWithError(CPPodfileInitErrors.NSURLError) | ||
return | ||
} | ||
|
||
callbackWithSuccess(podfileURL) | ||
} | ||
|
||
private func callbackWithError(error: CPPodfileInitErrors) { | ||
dispatch_async(dispatch_get_main_queue()) { | ||
self.completionHandler(nil, error) | ||
} | ||
} | ||
|
||
private func callbackWithSuccess(url: NSURL) { | ||
dispatch_async(dispatch_get_main_queue()) { | ||
self.completionHandler(url, nil) | ||
} | ||
} | ||
} |