paulcastro commented on a change in pull request #1: Swift 4 support URL: https://github.com/apache/incubator-openwhisk-runtime-swift/pull/1#discussion_r163047637
########## File path: README.md ########## @@ -2,10 +2,158 @@ [](https://travis-ci.org/apache/incubator-openwhisk-runtime-swift) -### Give it a try today +### Simple swift action hello.swift +The traditional support for dictionary still works: +```swift +func main(args: [String:Any]) -> [String:Any] { + if let name = args["name"] as? String { + return [ "greeting" : "Hello \(name)!" ] + } else { + return [ "greeting" : "Hello swif4!" ] + } +} +``` + +### Packaging an action as a Swift executable using Swift 4 + +When you create an OpenWhisk Swift action with a Swift source file, it has to be compiled into a binary before the action is run. Once done, subsequent calls to the action are much faster until the container holding your action is purged. This delay is known as the cold-start delay. + +To avoid the cold-start delay, you can compile your Swift file into a binary and then upload to OpenWhisk in a zip file. As you need the OpenWhisk scaffolding, the easiest way to create the binary is to build it within the same environment as it will be run in. These are the steps: + +- Run an interactive Swift action container. + ``` + docker run --rm -it -v "$(pwd):/owexec" openwhisk/action-swift-v4 bash + ``` + This puts you in a bash shell within the Docker container. + +- Copy the source code and prepare to build it. + ``` + cp /owexec/hello.swift /swift4Action/spm-build/Sources/Action/main.swift + ``` + ``` + cat /swift4Action/epilogue.swift >> /swift4Action/spm-build/Sources/Action/main.swift + ``` + ``` + echo '_run_main(mainFunction:main)' >> /swift4Action/spm-build/Sources/Action/main.swift + ``` + Copy any additional source files to `/swift4Action/spm-build/Sources/Action/` + + +- (Optional) Create the `Package.swift` file to add dependencies. +```swift +// swift-tools-version:4.0 +// The swift-tools-version declares the minimum version of Swift required to build this package. + +import PackageDescription + +let package = Package( + name: "Action", + products: [ + .executable( + name: "Action", + targets: ["Action"] + ) + ], + dependencies: [ + .package(url: "https://github.com/IBM-Swift/SwiftyRequest.git", .upToNextMajor(from: "1.0.0")) + ], + targets: [ + .target( + name: "Action", + dependencies: ["SwiftyRequest"], + path: "." + ) + +``` + As you can see this example adds `SwiftyRequest` dependencies. + + Notice that now with swift:4 is no longer required to include `CCurl`, `Kitura-net` and `SwiftyJSON` in your own `Package.swift`. + You are free now to use no dependencies, or add the combination that you want with the versions you want. + +- Copy Package.swift to spm-build directory + ``` + cp /owexec/Package.swift /swift4Action/spm-build/Package.swift + ``` + +- Change to the spm-build directory. + ``` + cd /swift4Action/spm-build + ``` + +- Compile your Swift Action. + ``` + swift build -c release + ``` + +- Create the zip archive. + ``` + zip /owexec/hello.zip .build/release/Action + ``` + +- Exit the Docker container. + ``` + exit + ``` + + This has created hello.zip in the same directory as hello.swift. + +- Upload it to OpenWhisk with the action name helloSwifty: + ``` + wsk action update helloSwiftly hello.zip openwhisk/action-swift-v4 + ``` + +- To check how much faster it is, run + ``` + wsk action invoke helloSwiftly --blocking + ``` + +### Migrating from Swift 3 to Swift 4 + +### Helper compile.sh helper script +When compiling and packaging your swift 4 now there are a couple of differences Review comment: Typo: `couple of differences.` ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services
