SwiftScriptRunner/README.md

114 lines
2.7 KiB
Markdown
Raw Permalink Normal View History

2015-10-06 19:59:21 +08:00
# SwiftScriptRunner
2015-10-06 20:15:00 +08:00
[![CI Status](http://img.shields.io/travis/mgrebenets/SwiftScriptRunner.svg?style=flat)](https://travis-ci.org/mgrebenets/SwiftScriptRunner)
2015-10-06 19:59:21 +08:00
[![Version](https://img.shields.io/cocoapods/v/SwiftScriptRunner.svg?style=flat)](http://cocoapods.org/pods/SwiftScriptRunner)
[![License](https://img.shields.io/cocoapods/l/SwiftScriptRunner.svg?style=flat)](http://cocoapods.org/pods/SwiftScriptRunner)
[![Platform](https://img.shields.io/cocoapods/p/SwiftScriptRunner.svg?style=flat)](http://cocoapods.org/pods/SwiftScriptRunner)
2015-10-10 14:42:39 +08:00
A simple utility for writing Swift scripts with async callbacks to 3rd party frameworks.
2015-10-10 14:13:30 +08:00
2015-10-10 14:42:39 +08:00
See [this blog post](http://mgrebenets.github.io/swift/2015/10/08/async-swift-scripting/) for more details.
2015-10-10 14:13:30 +08:00
2015-10-06 19:59:21 +08:00
## Requirements
2018-03-02 10:59:19 +08:00
- Xcode 9.2
- cocoapods gem version 1.4.0
- [cocoapods-rome](https://github.com/neonichu/Rome) gem version 0.8.0
- carthage version 0.28.0
2015-10-10 14:13:30 +08:00
2015-10-06 19:59:21 +08:00
## Installation
2015-10-10 14:13:30 +08:00
SwiftScriptRunner is available through [CocoaPods Rome](https://github.com/neonichu/Rome) and [Carthage](https://github.com/Carthage/Carthage).
2015-10-06 20:15:00 +08:00
### CocoaPods Rome
2015-10-10 14:13:30 +08:00
Make sure you have [cocoapods-rome](https://github.com/neonichu/Rome) Ruby gem installed.
2015-10-06 20:15:00 +08:00
2018-03-02 10:59:19 +08:00
Add the following line to your `Podfile`:
2015-10-06 19:59:21 +08:00
```ruby
2018-03-02 10:14:00 +08:00
platform :osx, "10.10"
2015-10-10 14:13:30 +08:00
use_frameworks!
2018-03-02 10:14:00 +08:00
plugin "cocoapods-rome"
2015-10-10 14:13:30 +08:00
2018-03-02 10:59:19 +08:00
pod "SwiftScriptRunner", "~> 1.0.1"
2015-10-06 19:59:21 +08:00
```
2015-10-06 20:15:00 +08:00
And run
2015-10-10 14:13:30 +08:00
2015-10-06 20:15:00 +08:00
```bash
2018-03-02 10:59:19 +08:00
pod install
2015-10-06 20:15:00 +08:00
```
### Carthage
2018-03-02 10:59:19 +08:00
Add the following line to your `Cartfile`:
2015-10-06 20:15:00 +08:00
```ruby
2018-03-02 10:59:19 +08:00
github "mgrebenets/SwiftScriptRunner", ~> 1.0.1
2015-10-06 20:15:00 +08:00
```
And run
```bash
2015-10-10 14:13:30 +08:00
carthage update --platform mac
2015-10-06 20:15:00 +08:00
```
2018-03-02 10:59:19 +08:00
### Swift Package Manager
_TODO:_
2015-10-06 20:15:00 +08:00
## Usage
2018-03-02 10:59:19 +08:00
Example of asynchronous network request using [Alamofire](https://github.com/Alamofire/Alamofire).
2015-10-10 14:13:30 +08:00
```swift
2018-03-02 10:59:19 +08:00
// Shebangs for using with different dependency managers.
//
// - Carthage:
// #!/usr/bin/env xcrun swift -F Carthage/Build/Mac
// - CocoaPods Rome:
// #!/usr/bin/env xcrun swift -F Rome
// - Swift Package Manager:
// Example.swift
2015-10-10 14:13:30 +08:00
import Alamofire
import SwiftScriptRunner
var runner = SwiftScriptRunner()
runner.lock() // Lock
2015-10-06 20:15:00 +08:00
2018-03-02 10:59:19 +08:00
Alamofire.request("http://httpbin.org/get")
.responseJSON { response in
print("Successful response:")
print(response) // Result of response serialization
runner.unlock() // Unlock
}
2015-10-10 14:13:30 +08:00
runner.wait() // Wait
```
Now you can run it.
```bash
# When using CocoaPods
2018-03-02 10:59:19 +08:00
swift -F Rome Example.swift
2015-10-10 14:13:30 +08:00
# When using Carthage
2018-03-02 10:59:19 +08:00
swift -F Carthage/Build/Mac Example.swift
# When -F option is part of shebang and Example.swift is executable
./Example.swift
2015-10-10 14:13:30 +08:00
```
2015-10-06 20:15:00 +08:00
2018-03-02 10:59:19 +08:00
See `Examples` folder for details.
2015-10-06 19:59:21 +08:00
## Author
2015-10-10 14:13:30 +08:00
Maksym Grebenets, mgrebenets@gmail.com, [@mgrebenets](https://twitter.com/mgrebenets)
2015-10-06 19:59:21 +08:00
## License
SwiftScriptRunner is available under the MIT license. See the LICENSE file for more info.