csantanapr commented on a change in pull request #2369: document how to add swift dependencies URL: https://github.com/apache/incubator-openwhisk/pull/2369#discussion_r124877472
########## File path: docs/actions.md ########## @@ -636,49 +636,69 @@ When you create an OpenWhisk Swift action with a Swift source file, it has to be 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/swift3action bash -``` - This puts you in a bash shell within the Docker container. Execute the following commands within it: - -- Install zip for convenience, to package the binary ``` - apt-get install -y zip + docker run --rm -it -v "$(pwd):/owexec" openwhisk/action-swift-v3.1.1 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 /swift3Action/spm-build/main.swift ``` - ``` cat /swift3Action/epilogue.swift >> /swift3Action/spm-build/main.swift ``` - ``` echo '_run_main(mainFunction:main)' >> /swift3Action/spm-build/main.swift ``` + Copy any additional source files to `/swift3Action/spm-build/` -- Build and link +- (Optional) Create Package.swift + If you need to add dependencies create a Package.swift file like the following: + ```swift + import PackageDescription + + let package = Package( + name: "Action", + dependencies: [ + .Package(url: "https://github.com/apple/example-package-deckofplayingcards.git", majorVersion: 3), + .Package(url: "https://github.com/IBM-Swift/CCurl.git", "0.2.3"), + .Package(url: "https://github.com/IBM-Swift/Kitura-net.git", "1.7.10"), + .Package(url: "https://github.com/IBM-Swift/SwiftyJSON.git", "15.0.1"), + .Package(url: "https://github.com/watson-developer-cloud/swift-sdk.git", "0.16.0") + ] + ) + ``` + As you can see this example adds `swift-watson-sdk` and `example-package-deckofplayingcards` dependencies. + Notice that `CCurl`, `Kitura-net` and `SwiftyJSON.git` are the minimum required dependencies to include. Review comment: If they follow the instructions as is it would be required. The resulting `main.swift` file will have depend on `WhiskJsonUtils.jsonDataToDictionary` And there are two swift files `_Whisk.swift` and `_WhiskJSONUtils.swift` in the directory that get compiled that depend on those two libraries. For CCurl, the problem is that we currently need to pinned to that version, so putting explicit version before Kitura-net works around the problem we have. I will use your suggested text. ``` root@e37cec41703a:/# ls -l /swift3Action/ -rw-r--r-- 1 root root 10842 Jun 29 03:48 _Whisk.swift -rw-r--r-- 1 root root 5313 Jun 29 03:48 _WhiskJSONUtils.swift -rw-r--r-- 1 root root 0 Jun 29 03:49 main.swift ``` ``` root@9d6c5f81bf9f:/# cat /swift3Action/spm-build/main.swift #if os(Linux) import Glibc srandom(UInt32(clock())) #endif import DeckOfPlayingCards func main(args: [String:Any]) -> [String:Any] { let numberOfCards = 10 var deck = Deck.standard52CardDeck() deck.shuffle() if let name = args["name"] as? String { return [ "greeting" : "Hello \(name)!" ] } else { return [ "greeting" : "Hello stranger!" ] } } import Foundation #if os(Linux) import Glibc #endif func _whisk_json2dict(txt: String) -> [String:Any]? { if let data = txt.data(using: String.Encoding.utf8, allowLossyConversion: true) { do { return WhiskJsonUtils.jsonDataToDictionary(jsonData: data) } catch { return nil } } return nil } func _run_main(mainFunction: ([String: Any]) -> [String: Any]) -> Void { let env = ProcessInfo.processInfo.environment let inputStr: String = env["WHISK_INPUT"] ?? "{}" if let parsed = _whisk_json2dict(txt: inputStr) { let result = mainFunction(parsed) if result is [String:Any] { do { if let respString = WhiskJsonUtils.dictionaryToJsonString(jsonDict: result) { print("\(respString)") } else { print("Error converting \(result) to JSON string") #if os(Linux) fputs("Error converting \(result) to JSON string", stderr) #endif } } catch { print("Error serializing response \(error)") #if os(Linux) fputs("Error serializing response \(error)", stderr) #endif } } else { print("Cannot serialize response: \(result)") #if os(Linux) fputs("Cannot serialize response: \(result)", stderr) #endif } } else { print("Error: couldn't parse JSON input.") #if os(Linux) fputs("Error: couldn't parse JSON input.", stderr) #endif } } _run_main(mainFunction:main) ``` ---------------------------------------------------------------- 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
