Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Practical Cross-Platform Swift

Practical Cross-Platform Swift

In this talk, we'll go over some practical recommendations for building cross-platform Swift code, from development environment to testing and continuous integration.

This presentation was given at try! Swift in Tokyo on March 2nd, 2016: http://www.tryswiftconf.com/en

3a0ae72b2f6bdc4476f1fcb63396e717?s=128

JP Simard

March 02, 2016
Tweet

Transcript

  1. CROSS-PLATFORM SWIFT JP SIMARD – @SIMJP – TRY! SWIFT –

    TOKYO – MARCH 2, 2016 1
  2. 2

  3. Jazzy & SwiftLint 3

  4. 4

  5. WE'LL GO OVER... > Development Environment > Swift Package Manager

    > Testing > Continuous Integration 5
  6. EXACTLY 2 PLATFORMS 6

  7. EXACTLY 3 PLATFORMS 7

  8. EXACTLY 3-ISH PLATFORMS 8

  9. 9

  10. EXPECTATION 10

  11. EXPECTATION 11

  12. EXPECTATION 12

  13. REALITY 13

  14. 14

  15. 15

  16. ! APPLE'S DOING A GREAT JOB 16

  17. EARLY DAYS 17

  18. DEVELOPING ALL FROM ONE PLACE > Xcode > Xcode+Toolchain >

    Docker+CLI+Editor 18
  19. XCODE 19

  20. XCODE+TOOLCHAIN 20

  21. DOCKER+CLI+EDITOR $ brew install docker docker-machine $ docker-machine create --driver

    virtualbox default $ eval $(docker-machine env default) $ docker pull swiftdocker/swift $ docker run -it -v `pwd`:/project swiftdocker/swift bash --- root@445ab1838149 $ cd /project root@445ab1838149 $ swift build & swift test 21
  22. SWIFT PACKAGE MANAGER The Swift Package Manager is a tool

    for managing the distribution of Swift code. It’s integrated with the Swift build system to automate the process of downloading, compiling, and linking dependencies. 22
  23. USE SPM EVEN FOR SMALL OR PRIVATE PROJECTS! 23

  24. THINGS YOU'D EXPECT TO WORK > dynamic keyword > Casting

    > Foundation > Grand Central Dispatch > Auto-importing of frameworks 24
  25. CASTING HAS NEVER BEEN SO DIFFICULT... public func materialize<T>(@autoclosure f:

    () throws -> T) -> Result<T, NSError> { do { return .Success(try f()) - } catch { - return .Failure(error as NSError) + } catch let error as NSError { + return .Failure(error) } } From porting Result to Linux: https://github.com/antitypical/Result/pull/135 25
  26. #IFS EVERYWHERE #if SWIFT_PACKAGE import SomeModuleOtherwiseAvailable #endif #if os(Linux) //

    some arcane hack #else // something more reasonable #endif 26
  27. TESTING 27

  28. ! REDISCOVERING XCTEST ! import PackageDescription let package = Package(

    name: "MyPackage", targets: [ Target(name: "MyPackage"), Target(name: "MyPackageTests", // build tests as a regular target... dependencies: [.Target(name: "MyPackage")]), // ...that depend on the main one ], dependencies: [ #if !os(Linux) // XCTest is distributed with Swift releases on Linux .Package( // no version tags at apple/swift-corelibs-xctest, so fork it url: "https://github.com/username/swift-corelibs-xctest.git", majorVersion: 0 ), #endif ] ) 28
  29. SPM TESTING Package.swift # can be empty in simplest configuration

    Sources/ MyPackage/ file.swift Tests/ LinuxMain.swift # needs `@testable import MyPackagetest` & `XCTMain()` MyPackage/ # *must* be named after package being tested test.swift # can `import XCTest` and `@testable import MyPackage` 29
  30. MUCH SIMPLIFICATION 30

  31. CONTINUOUS INTEGRATION 31

  32. EMBRACE THE MATRIX 32

  33. TRAVIS CI 33

  34. TRAVIS CONFIGURATION matrix: include: - env: JOB=OSX_Xcode - env: JOB=OSX_SPM

    - env: JOB=Linux 34
  35. TRAVIS XCODE script: xcodebuild test env: JOB=OSX_Xcode os: osx osx_image:

    xcode7.2 language: objective-c before_install: pod install / carthage update / etc. 35
  36. TRAVIS OS X SPM script: - swift build - .build/Debug/MyUnitTests

    env: JOB=OSX_SPM os: osx osx_image: xcode7.2 language: objective-c before_install: - export SWIFT_VERSION=swift-DEVELOPMENT-SNAPSHOT-2016-02-25-a - curl -O https://swift.org/builds/development/xcode/$(SWIFT_VERSION)/$(SWIFT_VERSION)-osx.pkg - sudo installer -pkg $(SWIFT_VERSION)-osx.pkg -target / - export PATH=/Library/Developer/Toolchains/$(SWIFT_VERSION).xctoolchain/usr/bin:"${PATH}" 36
  37. TRAVIS LINUX script: - swift build - .build/Debug/MyUnitTests env: JOB=Linux

    dist: trusty sudo: required language: generic before_install: - DIR="$(pwd)" - cd .. - export SWIFT_VERSION=swift-DEVELOPMENT-SNAPSHOT-2016-02-25-a - wget https://swift.org/builds/development/ubuntu1404/$SWIFT_VERSION/$SWIFT_VERSION-ubuntu14.04.tar.gz - tar xzf $SWIFT_VERSION-ubuntu14.04.tar.gz - export PATH="${PWD}/${SWIFT_VERSION}-ubuntu14.04/usr/bin:${PATH}" - cd "$DIR" 37
  38. 38

  39. EARLY DAYS 39

  40. Ϳ͜Θ͘Π͢;͜ THANK YOU! JP SIMARD – @SIMJP – TRY! SWIFT

    – TOKYO – MARCH 2, 2016 40