|
23 | 23 |
|
24 | 24 |
|
25 | 25 | ## Changelogs
|
26 |
| -- [Swift 4.2 CHANGELOG.md](core/swift42Action/CHANGELOG.md) |
27 | 26 | - [Swift 5.1 CHANGELOG.md](core/swift51Action/CHANGELOG.md)
|
28 | 27 | - [Swift 5.3 CHANGELOG.md](core/swift53Action/CHANGELOG.md)
|
29 | 28 | - [Swift 5.4 CHANGELOG.md](core/swift54Action/CHANGELOG.md)
|
@@ -148,270 +147,3 @@ zip - -r * | docker run -i openwhisk/action-swift-v5.1 -compile main >../action.
|
148 | 147 | ```
|
149 | 148 |
|
150 | 149 | For more build examples see [here](./examples/)
|
151 |
| - |
152 |
| -## Swift 4.x support |
153 |
| - |
154 |
| -Some examples of using Codable In and Out |
155 |
| -### Codable style function signature |
156 |
| -Create file `helloCodableAsync.swift` |
157 |
| -```swift |
158 |
| -// Domain model/entity |
159 |
| -struct Employee: Codable { |
160 |
| - let id: Int? |
161 |
| - let name: String? |
162 |
| -} |
163 |
| -// codable main function |
164 |
| -func main(input: Employee, respondWith: (Employee?, Error?) -> Void) -> Void { |
165 |
| - // For simplicity, just passing same Employee instance forward |
166 |
| - respondWith(input, nil) |
167 |
| -} |
168 |
| -``` |
169 |
| -``` |
170 |
| -wsk action update helloCodableAsync helloCodableAsync.swift swift:4.2 |
171 |
| -``` |
172 |
| -``` |
173 |
| -ok: updated action helloCodableAsync |
174 |
| -``` |
175 |
| -``` |
176 |
| -wsk action invoke helloCodableAsync -r -p id 42 -p name Carlos |
177 |
| -``` |
178 |
| -```json |
179 |
| -{ |
180 |
| - "id": 42, |
181 |
| - "name": "Carlos" |
182 |
| -} |
183 |
| -``` |
184 |
| - |
185 |
| -### Codable Error Handling |
186 |
| -Create file `helloCodableAsync.swift` |
187 |
| -```swift |
188 |
| -struct Employee: Codable { |
189 |
| - let id: Int? |
190 |
| - let name: String? |
191 |
| -} |
192 |
| -enum VendingMachineError: Error { |
193 |
| - case invalidSelection |
194 |
| - case insufficientFunds(coinsNeeded: Int) |
195 |
| - case outOfStock |
196 |
| -} |
197 |
| -func main(input: Employee, respondWith: (Employee?, Error?) -> Void) -> Void { |
198 |
| - // Return real error |
199 |
| - do{ |
200 |
| - throw VendingMachineError.insufficientFunds(coinsNeeded: 5) |
201 |
| - } catch { |
202 |
| - respondWith(nil, error) |
203 |
| - } |
204 |
| -} |
205 |
| -``` |
206 |
| -``` |
207 |
| -wsk action update helloCodableError helloCodableError.swift swift:4.2 |
208 |
| -``` |
209 |
| -``` |
210 |
| -ok: updated action helloCodableError |
211 |
| -``` |
212 |
| -``` |
213 |
| -wsk action invoke helloCodableError -b -p id 42 -p name Carlos |
214 |
| -``` |
215 |
| -```json |
216 |
| -{ |
217 |
| - "name": "helloCodableError", |
218 |
| - "response": { |
219 |
| - "result": { |
220 |
| - "error": "insufficientFunds(5)" |
221 |
| - }, |
222 |
| - "status": "application error", |
223 |
| - "success": false |
224 |
| - } |
225 |
| -} |
226 |
| -``` |
227 |
| - |
228 |
| -## Packaging an action as a Swift executable using Swift 4.x |
229 |
| - |
230 |
| -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. |
231 |
| - |
232 |
| -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. |
233 |
| - |
234 |
| -### Compiling Swift 4.2 |
235 |
| - |
236 |
| -### Compiling Swift 4.2 single file |
237 |
| - |
238 |
| -Use the docker container and pass the single source file as stdin. |
239 |
| -Pass the name of the method to the flag `-compile` |
240 |
| -``` |
241 |
| -docker run -i openwhisk/action-swift-v4.2 -compile main <main.swift >../action.zip |
242 |
| -``` |
243 |
| - |
244 |
| -### Compiling Swift 4.2 multiple files with dependencies |
245 |
| -Use the docker container and pass a zip archive containing a `Package.swift` and source files a main source file in the location `Sources/main.swift`. |
246 |
| -``` |
247 |
| -zip - -r * | docker run -i openwhisk/action-swift-v4.2 -compile main >../action.zip |
248 |
| -``` |
249 |
| - |
250 |
| -For more build examples see [here](./examples/) |
251 |
| - |
252 |
| -### Building the Swift4 Image |
253 |
| -``` |
254 |
| -./gradlew core:swift42Action:distDocker |
255 |
| -``` |
256 |
| -This will produce the image `whisk/action-swift-v4.2` |
257 |
| - |
258 |
| -Build and Push image |
259 |
| -``` |
260 |
| -docker login |
261 |
| -./gradlew core:swift42Action:distDocker -PdockerImagePrefix=$prefix-user -PdockerRegistry=docker.io |
262 |
| -``` |
263 |
| - |
264 |
| - |
265 |
| -## Codable Support with Swift 4.x |
266 |
| - |
267 |
| -Some examples of using Codable In and Out |
268 |
| - |
269 |
| -### Codable style function signature |
270 |
| -Create file `helloCodableAsync.swift` |
271 |
| -```swift |
272 |
| -// Domain model/entity |
273 |
| -struct Employee: Codable { |
274 |
| - let id: Int |
275 |
| - let name: String |
276 |
| -} |
277 |
| -// codable main function |
278 |
| -func main(input: Employee, respondWith: (Employee?, Error?) -> Void) -> Void { |
279 |
| - // For simplicity, just passing same Employee instance forward |
280 |
| - respondWith(input, nil) |
281 |
| -} |
282 |
| -``` |
283 |
| -``` |
284 |
| -wsk action update helloCodableAsync helloCodableAsync.swift swift:4.2 |
285 |
| -``` |
286 |
| -``` |
287 |
| -ok: updated action helloCodableAsync |
288 |
| -``` |
289 |
| -``` |
290 |
| -wsk action invoke helloCodableAsync -r -p id 42 -p name Carlos |
291 |
| -``` |
292 |
| -```json |
293 |
| -{ |
294 |
| - "id": 42, |
295 |
| - "name": "Carlos" |
296 |
| -} |
297 |
| -``` |
298 |
| - |
299 |
| -### Codable Error Handling |
300 |
| -Create file `helloCodableAsync.swift` |
301 |
| -```swift |
302 |
| -struct Employee: Codable { |
303 |
| - let id: Int |
304 |
| - let name: String |
305 |
| -} |
306 |
| -enum VendingMachineError: Error { |
307 |
| - case invalidSelection |
308 |
| - case insufficientFunds(coinsNeeded: Int) |
309 |
| - case outOfStock |
310 |
| -} |
311 |
| -func main(input: Employee, respondWith: (Employee?, Error?) -> Void) -> Void { |
312 |
| - // Return real error |
313 |
| - do{ |
314 |
| - throw VendingMachineError.insufficientFunds(coinsNeeded: 5) |
315 |
| - } catch { |
316 |
| - respondWith(nil, error) |
317 |
| - } |
318 |
| -} |
319 |
| -``` |
320 |
| -``` |
321 |
| -wsk action update helloCodableError helloCodableError.swift swift:4.2 |
322 |
| -``` |
323 |
| -``` |
324 |
| -ok: updated action helloCodableError |
325 |
| -``` |
326 |
| -``` |
327 |
| -wsk action invoke helloCodableError -b -p id 42 -p name Carlos |
328 |
| -``` |
329 |
| -```json |
330 |
| -{ |
331 |
| - "name": "helloCodableError", |
332 |
| - "response": { |
333 |
| - "result": { |
334 |
| - "error": "insufficientFunds(5)" |
335 |
| - }, |
336 |
| - "status": "application error", |
337 |
| - "success": false |
338 |
| - } |
339 |
| -} |
340 |
| -``` |
341 |
| - |
342 |
| -### Using Swift 4.2 |
343 |
| -To use as a docker action |
344 |
| -``` |
345 |
| -wsk action update myAction myAction.swift --docker openwhisk/action-swift-v4.2:1.0.7 |
346 |
| -``` |
347 |
| -This works on any deployment of Apache OpenWhisk |
348 |
| - |
349 |
| -### To use on deployment that contains the runtime as a kind |
350 |
| -To use as a kind action |
351 |
| -``` |
352 |
| -wsk action update myAction myAction.swift --kind swift:4.2 |
353 |
| -``` |
354 |
| - |
355 |
| -## Local development |
356 |
| -``` |
357 |
| -./gradlew core:swift42Action:distDocker |
358 |
| -``` |
359 |
| -This will produce the image `whisk/action-swift-v4.2` |
360 |
| - |
361 |
| -Build and Push image |
362 |
| -``` |
363 |
| -docker login |
364 |
| -./gradlew core:swift42Action:distDocker -PdockerImagePrefix=$prefix-user -PdockerRegistry=docker.io |
365 |
| -``` |
366 |
| - |
367 |
| -Deploy OpenWhisk using ansible environment that contains the kind `swift:4.2` |
368 |
| -Assuming you have OpenWhisk already deploy locally and `OPENWHISK_HOME` pointing to root directory of OpenWhisk core repository. |
369 |
| - |
370 |
| -Set `ROOTDIR` to the root directory of this repository. |
371 |
| - |
372 |
| -Redeploy OpenWhisk |
373 |
| -``` |
374 |
| -cd $OPENWHISK_HOME/ansible |
375 |
| -ANSIBLE_CMD="ansible-playbook -i ${ROOTDIR}/ansible/environments/local" |
376 |
| -$ANSIBLE_CMD setup.yml |
377 |
| -$ANSIBLE_CMD couchdb.yml |
378 |
| -$ANSIBLE_CMD initdb.yml |
379 |
| -$ANSIBLE_CMD wipe.yml |
380 |
| -$ANSIBLE_CMD openwhisk.yml |
381 |
| -``` |
382 |
| - |
383 |
| -Or you can use `wskdev` and create a soft link to the target ansible environment, for example: |
384 |
| -``` |
385 |
| -ln -s ${ROOTDIR}/ansible/environments/local ${OPENWHISK_HOME}/ansible/environments/local-swift |
386 |
| -wskdev fresh -t local-swift |
387 |
| -``` |
388 |
| - |
389 |
| -### Testing |
390 |
| -Install dependencies from the root directory on $OPENWHISK_HOME repository |
391 |
| -``` |
392 |
| -./gradlew :common:scala:install :core:controller:install :core:invoker:install :tests:install |
393 |
| -``` |
394 |
| - |
395 |
| -Using gradle to run all tests |
396 |
| -``` |
397 |
| -./gradlew :tests:test |
398 |
| -``` |
399 |
| -Using gradle to run some tests |
400 |
| -``` |
401 |
| -./gradlew :tests:test --tests *ActionContainerTests* |
402 |
| -``` |
403 |
| -Using IntelliJ: |
404 |
| -- Import project as gradle project. |
405 |
| -- Make sure the working directory is root of the project/repo |
406 |
| - |
407 |
| -#### Using container image to test |
408 |
| -To use as docker action push to your own Docker Hub account |
409 |
| -``` |
410 |
| -docker tag whisk/action-swift-v4.2 $user_prefix/action-swift-v4.2 |
411 |
| -docker push $user_prefix/action-swift-v4.2 |
412 |
| -``` |
413 |
| -Then create the action using your image from Docker Hub |
414 |
| -``` |
415 |
| -wsk action update myAction myAction.swift --docker $user_prefix/action-swift-v4.2 |
416 |
| -``` |
417 |
| -The `$user_prefix` is usually your Docker Hub user id. |
0 commit comments