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

Creating a Swift Library

Jeff
March 03, 2016

Creating a Swift Library

Talk about all the infrastructure required to creating a cross-platform swift library.

Note: these slides may make more sense in the context of the presentation. Videos may be published in the next few weeks.

Supplementary Material:

- https://github.com/jeffh/Snorlax is the sample library that integrates with as many platforms that Swift currently supports.
- https://github.com/jeffh/SnorlaxSamples has some sample integrations of all the package managers.

Try! Swift 2016 Talk

Jeff

March 03, 2016
Tweet

More Decks by Jeff

Other Decks in Technology

Transcript

  1. import Foundation /// Sleeps for a random time interval public

    func rest() { let interval = NSTimeInterval(random() % 2) NSThread.sleepForTimeInterval(interval) }
  2. import XCTest import Snorlax import Foundation class SnorlaxTests: XCTestCase {

    func testRest() { let expectation = expectationWithDescription("Resting") dispatch_async(dispatch_get_main_queue()) { rest() expectation.fulfill() } waitForExpectationsWithTimeout(5, handler: nil) } }
  3. #!/bin/bash # ./test script set -e # stop when a

    test fails xcodebuild test -scheme Snorlax-OSX -destination "platform=OS X" xcodebuild test -scheme Snorlax-iOS -destination "platform=iOS Simulator,name=iPhone 6,OS=9.2" xcodebuild test -scheme Snorlax-tvOS -destination "platform=tvOS Simulator,name=Apple TV 1080p,OS=9.1"
  4. // LinuxMain.swift import XCTest @testable import Snorlaxtest // This is

    the entry point for tests on Linux XCTMain([ SnorlaxTests(), ])
  5. // ... SnorlaxTests.swift ... class SnorlaxTests: XCTestCase, XCTestCaseProvider { var

    allTests: [(String, () throws -> Void)] { return [("testRest", testRest)] } func testRest() { let start = NSDate() rest() let end = NSDate() let duration = end.timeIntervalSinceDate(start) XCTAssertGreaterThanOrEqual(duration, 0) XCTAssertLessThan(duration, 2) } }
  6. // ... SnorlaxTests.swift ... class SnorlaxTests: XCTestCase, XCTestCaseProvider { var

    allTests: [(String, () throws -> Void)] { return [("testRest", testRest)] } func testRest() { let start = NSDate() rest() let end = NSDate() let duration = end.timeIntervalSinceDate(start) XCTAssertGreaterThanOrEqual(duration, 0) XCTAssertLessThan(duration, 2) } }
  7. #if os(OSX) || os(iOS) || os(watchOS) || os(tvOS) public protocol

    XCTestCaseProvider { var allTests: [(String, () throws -> Void)] { get } } #endif class SnorlaxTests: XCTestCase, XCTestCaseProvider { ... }
  8. # .travis.yml osx_image: xcode7.2 language: generic matrix: include: - os:

    osx env: TYPE=xcode - os: osx env: TYPE=spm - os: linux dist: trusty sudo: required env: TYPE=spm # Install SwiftEnv install: if [[ "$TYPE" == "spm" ]]; then eval "$(curl -sL https://…)"; fi script: ./test $TYPE
  9. TEXT CUTTING A RELEASE ▸ Update version in Podspec (commit

    and push it) ▸ Tag new version: git tag v0.1.2 ▸ Push tag: git push origin v0.1.2 ▸ Push podspec: pod trunk push Snorlax.podspec ▸ Create a release from a tag in Github ▸ Announce!