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

JP Simard

March 02, 2016
Tweet

More Decks by JP Simard

Other Decks in Programming

Transcript

  1. 2

  2. 4

  3. 9

  4. 14

  5. 15

  6. 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
  7. 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
  8. THINGS YOU'D EXPECT TO WORK > dynamic keyword > Casting

    > Foundation > Grand Central Dispatch > Auto-importing of frameworks 24
  9. 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
  10. #IFS EVERYWHERE #if SWIFT_PACKAGE import SomeModuleOtherwiseAvailable #endif #if os(Linux) //

    some arcane hack #else // something more reasonable #endif 26
  11. ! 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
  12. 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
  13. 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
  14. 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
  15. 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
  16. 38