diff --git a/mas-cli.xcodeproj/project.pbxproj b/mas-cli.xcodeproj/project.pbxproj index f03976a..c84b6e0 100644 --- a/mas-cli.xcodeproj/project.pbxproj +++ b/mas-cli.xcodeproj/project.pbxproj @@ -15,6 +15,8 @@ 319FDBA6ED6443A912B9A65F /* ResultType.swift in Sources */ = {isa = PBXBuildFile; fileRef = 580DF177A1A8B1DF500CADA7 /* ResultType.swift */; }; 345960DE661C85EB2609263C /* ArgumentType.swift in Sources */ = {isa = PBXBuildFile; fileRef = 62F77E7DD6274E0371AF5CA6 /* ArgumentType.swift */; }; 3A45897E98247F74ED6D51E2 /* Curry.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9AE1408CEE41BD4CD64491E3 /* Curry.swift */; }; + 693A98991CBFFA760004D3B4 /* Search.swift in Sources */ = {isa = PBXBuildFile; fileRef = 693A98981CBFFA760004D3B4 /* Search.swift */; }; + 693A989B1CBFFAAA0004D3B4 /* NSURLSession+Synchronous.swift in Sources */ = {isa = PBXBuildFile; fileRef = 693A989A1CBFFAAA0004D3B4 /* NSURLSession+Synchronous.swift */; }; AD0785BC0EC6BBF4ED560DCC /* ArgumentParser.swift in Sources */ = {isa = PBXBuildFile; fileRef = EA9D96DDBBCCCC5944160ABE /* ArgumentParser.swift */; }; EBD6B44FDF65E0253153629F /* HelpCommand.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8FDC2B8063EC231E28353D23 /* HelpCommand.swift */; }; ED031A7C1B5127C00097692E /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = ED031A7B1B5127C00097692E /* main.swift */; }; @@ -53,6 +55,8 @@ 326E4D331CCD66ADFE19CE39 /* Command.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; name = Command.swift; path = Seeds/Commandant/Sources/Commandant/Command.swift; sourceTree = ""; }; 580DF177A1A8B1DF500CADA7 /* ResultType.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; name = ResultType.swift; path = Seeds/Result/Result/ResultType.swift; sourceTree = ""; }; 62F77E7DD6274E0371AF5CA6 /* ArgumentType.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; name = ArgumentType.swift; path = Seeds/Commandant/Sources/Commandant/ArgumentType.swift; sourceTree = ""; }; + 693A98981CBFFA760004D3B4 /* Search.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Search.swift; sourceTree = ""; }; + 693A989A1CBFFAAA0004D3B4 /* NSURLSession+Synchronous.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "NSURLSession+Synchronous.swift"; sourceTree = ""; }; 8FDC2B8063EC231E28353D23 /* HelpCommand.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; name = HelpCommand.swift; path = Seeds/Commandant/Sources/Commandant/HelpCommand.swift; sourceTree = ""; }; 9257C5FABA335E5F060CB7F7 /* Result.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; name = Result.swift; path = Seeds/Result/Result/Result.swift; sourceTree = ""; }; 9AE1408CEE41BD4CD64491E3 /* Curry.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; name = Curry.swift; path = Seeds/Curry/Source/Curry.swift; sourceTree = ""; }; @@ -183,6 +187,7 @@ children = ( ED0F238E1B87A54700AE40CD /* AppStore */, ED0F23801B87524700AE40CD /* Commands */, + 693A989A1CBFFAAA0004D3B4 /* NSURLSession+Synchronous.swift */, ED0F238C1B8756E600AE40CD /* Error.swift */, ED031A7B1B5127C00097692E /* main.swift */, EDEAA12C1B51CF8000F2FC3F /* mas-cli-Bridging-Header.h */, @@ -198,6 +203,7 @@ ED0F237E1B87522400AE40CD /* Install.swift */, ED0F23821B87533A00AE40CD /* List.swift */, ED0F23841B87536A00AE40CD /* Outdated.swift */, + 693A98981CBFFA760004D3B4 /* Search.swift */, EDC90B641C70045E0019E396 /* SignIn.swift */, EDE296521C700F4300554778 /* SignOut.swift */, EDD3B3621C34709400B56B88 /* Upgrade.swift */, @@ -342,29 +348,31 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( - ED0F23871B87537200AE40CD /* Account.swift in Sources */, - F184B6B7CD9C013CACDED0FB /* Argument.swift in Sources */, - AD0785BC0EC6BBF4ED560DCC /* ArgumentParser.swift in Sources */, - 345960DE661C85EB2609263C /* ArgumentType.swift in Sources */, - 0C47E694564FCB59996690DD /* Command.swift in Sources */, + 319FDBA6ED6443A912B9A65F /* ResultType.swift in Sources */, + 0EBF5CDD379D7462C3389536 /* Result.swift in Sources */, 3A45897E98247F74ED6D51E2 /* Curry.swift in Sources */, - ED0F238B1B87569C00AE40CD /* Downloader.swift in Sources */, - ED0F238D1B8756E600AE40CD /* Error.swift in Sources */, - 3053D11E74A22A4C5A6BE833 /* Errors.swift in Sources */, + 15E27926A580EABEB1B218EF /* Switch.swift in Sources */, + 693A989B1CBFFAAA0004D3B4 /* NSURLSession+Synchronous.swift in Sources */, + 30EA893640B02CCF679F9C57 /* Option.swift in Sources */, EBD6B44FDF65E0253153629F /* HelpCommand.swift in Sources */, + 3053D11E74A22A4C5A6BE833 /* Errors.swift in Sources */, + 0C47E694564FCB59996690DD /* Command.swift in Sources */, + 345960DE661C85EB2609263C /* ArgumentType.swift in Sources */, + AD0785BC0EC6BBF4ED560DCC /* ArgumentParser.swift in Sources */, + F184B6B7CD9C013CACDED0FB /* Argument.swift in Sources */, + ED0F23871B87537200AE40CD /* Account.swift in Sources */, + ED0F238B1B87569C00AE40CD /* Downloader.swift in Sources */, + 693A98991CBFFA760004D3B4 /* Search.swift in Sources */, + ED0F238D1B8756E600AE40CD /* Error.swift in Sources */, ED0F237F1B87522400AE40CD /* Install.swift in Sources */, ED0F23901B87A56F00AE40CD /* ISStoreAccount.swift in Sources */, ED0F23831B87533A00AE40CD /* List.swift in Sources */, ED031A7C1B5127C00097692E /* main.swift in Sources */, - 30EA893640B02CCF679F9C57 /* Option.swift in Sources */, ED0F23851B87536A00AE40CD /* Outdated.swift in Sources */, ED0F23891B87543D00AE40CD /* PurchaseDownloadObserver.swift in Sources */, - 0EBF5CDD379D7462C3389536 /* Result.swift in Sources */, - 319FDBA6ED6443A912B9A65F /* ResultType.swift in Sources */, EDC90B651C70045E0019E396 /* SignIn.swift in Sources */, EDE296531C700F4300554778 /* SignOut.swift in Sources */, EDA3BE521B8B84AF00C18D70 /* SSPurchase.swift in Sources */, - 15E27926A580EABEB1B218EF /* Switch.swift in Sources */, EDD3B3631C34709400B56B88 /* Upgrade.swift in Sources */, EDB6CE8C1BAEC3D400648B4D /* Version.swift in Sources */, ); diff --git a/mas-cli/Commands/Search.swift b/mas-cli/Commands/Search.swift new file mode 100644 index 0000000..0858129 --- /dev/null +++ b/mas-cli/Commands/Search.swift @@ -0,0 +1,54 @@ +// +// Search.swift +// mas-cli +// +// Created by Michael Schneider on 4/14/16. +// Copyright © 2016 Andrew Naylor. All rights reserved. +// + +struct SearchCommand: CommandType { + typealias Options = SearchOptions + let verb = "search" + let function = "Search for apps from the Mac App Store" + + func run(options: Options) -> Result<(), MASError> { + let searchRequest = NSURLRequest(URL: NSURL(string: searchURLString(options.appName))!) + + guard let searchData = NSURLSession.requestSynchronousData(searchRequest), + let searchJsonString = try? NSJSONSerialization.JSONObjectWithData(searchData, options: []) as! Dictionary else { + return .Failure(MASError(code:.SearchError)) + } + + guard let resultCount = searchJsonString["resultCount"] as? Int where resultCount > 0, + let results = searchJsonString["results"] as? Array> else { + print("No apps found") + return .Failure(MASError(code:.NoSearchResultsFound)) + } + + for result in results { + if let appName = result["trackName"] as? String, + appId = result["trackId"] as? Int { + print("\(String(appId)) \(appName)") + } + } + + return .Success(()) + } + + func searchURLString(appName: String) -> String { + return "https://itunes.apple.com/search?entity=macSoftware&term=\(appName)&attribute=allTrackTerm" + } +} + +struct SearchOptions: OptionsType { + let appName: String + + static func create(appName: String) -> SearchOptions { + return SearchOptions(appName: appName) + } + + static func evaluate(m: CommandMode) -> Result> { + return create + <*> m <| Argument(usage: "the app name to search") + } +} diff --git a/mas-cli/Error.swift b/mas-cli/Error.swift index 85c9f1a..1440d4c 100644 --- a/mas-cli/Error.swift +++ b/mas-cli/Error.swift @@ -19,6 +19,8 @@ public enum MASErrorCode: Int { case DownloadFailed case SignInError case AlreadySignedIn + case SearchError + case NoSearchResultsFound var exitCode: Int32 { return Int32(self.rawValue) diff --git a/mas-cli/NSURLSession+Synchronous.swift b/mas-cli/NSURLSession+Synchronous.swift new file mode 100644 index 0000000..81f7d49 --- /dev/null +++ b/mas-cli/NSURLSession+Synchronous.swift @@ -0,0 +1,51 @@ +// +// NSURLSession+Synchronous.swift +// mas-cli +// +// Created by Michael Schneider on 4/14/16. +// Copyright © 2016 Andrew Naylor. All rights reserved. +// + +import Foundation + +/// NSURLSession synchronous behavior +/// Particularly for playground sessions that need to run sequentially +public extension NSURLSession { + + /// Return data from synchronous URL request + public static func requestSynchronousData(request: NSURLRequest) -> NSData? { + var data: NSData? = nil + let semaphore: dispatch_semaphore_t = dispatch_semaphore_create(0) + let task = NSURLSession.sharedSession().dataTaskWithRequest(request, completionHandler: { + taskData, _, error -> () in + data = taskData + if data == nil, let error = error {print(error)} + dispatch_semaphore_signal(semaphore); + }) + task.resume() + dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER) + return data + } + + /// Return data synchronous from specified endpoint + public static func requestSynchronousDataWithURLString(requestString: String) -> NSData? { + guard let url = NSURL(string:requestString) else {return nil} + let request = NSURLRequest(URL: url) + return NSURLSession.requestSynchronousData(request) + } + + /// Return JSON synchronous from URL request + public static func requestSynchronousJSON(request: NSURLRequest) -> AnyObject? { + guard let data = NSURLSession.requestSynchronousData(request) else {return nil} + return try? NSJSONSerialization.JSONObjectWithData(data, options: []) + } + + /// Return JSON synchronous from specified endpoint + public static func requestSynchronousJSONWithURLString(requestString: String) -> AnyObject? { + guard let url = NSURL(string: requestString) else {return nil} + let request = NSMutableURLRequest(URL:url) + request.HTTPMethod = "GET" + request.addValue("application/json", forHTTPHeaderField: "Content-Type") + return NSURLSession.requestSynchronousJSON(request) + } +} diff --git a/mas-cli/main.swift b/mas-cli/main.swift index 98113f2..4b1faf4 100644 --- a/mas-cli/main.swift +++ b/mas-cli/main.swift @@ -17,6 +17,7 @@ public struct StderrOutputStream: OutputStreamType { let registry = CommandRegistry() let helpCommand = HelpCommand(registry: registry) registry.register(AccountCommand()) +registry.register(SearchCommand()) registry.register(InstallCommand()) registry.register(ListCommand()) registry.register(OutdatedCommand())