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

The Swift Open Source Tour

Sponsored · Ship Features Fearlessly Turn features on and off without deploys. Used by thousands of Ruby developers.
Avatar for David Hart David Hart
February 14, 2019

The Swift Open Source Tour

Did you know Apple released a Swift library that allows you to parse and modify Swift code? Were you aware that parts of Xcode are Open Source? You heard about SwiftNIO, the event-driven networking framework, but never tried it out? Let David take you through a pragmatic tour of those projects and explain how to take advantage of each of them in your day-to-day work.

Avatar for David Hart

David Hart

February 14, 2019
Tweet

More Decks by David Hart

Other Decks in Programming

Transcript

  1. @inlinable public func reduce<Result>( _ initialResult: Result, _ next: (Result,

    Element) throws -> Result ) rethrows -> Result { var accumulator = initialResult for element in self { accumulator = try next(accumulator, element) } return accumulator } SE-0193 Standard Library
  2. GYB

  3. %{ ... }% % ... ${ ... } % end

    Multiline code Single-line control flow Control flow end Substitution GYB
  4. %{ aliases = { 'ibu': 'internationalBitteringUnits', 'abv': 'alcoholByVolume', 'ebc': 'europeanBreweryConvention',

    'srm': 'standardReferenceMethod' } }% extension Beer { % for property, alias in aliases.items(): var ${property}: Int { get { return ${alias} } set { ${alias} = newValue } } % end } GYB
  5. $ brew install nshipster/formulae/gyb for file in **/*.gyb do done

    gyb —line-directive '' \ -o "${file%.gyb}" \ “$file" done Install Xcode Build Script GYB
  6. // swift-tools-version:4.2 import PackageDescription let package = Package( name: "iBrew",

    products: [], dependencies: [ .package( url: "https://github.com/realm/SwiftLint.git", .exact(“0.30.1")) ], targets: [] ) Swift Package Manager
  7. Swift Evolution Proposal Accepted Rejected Pitch Introduction: December 2015 Language:

    Markdown https://github.com/apple/swift-evolution Review
  8. Swift Protobuf syntax = "proto3"; message Beer { int64 id

    = 1; string name = 2; string brewer = 3; int32 abv = 4; }
  9. var beer = Beer.with { $0.id = 1070 $0.name =

    "Tempête" $0.brewer = "Docteur Gab's" $0.abv = 8 } let data = try beer.serializedData() beer = try Beer(serializedData: data) let jsonData = try beer.jsonUTF8Data() beer = try Beer(jsonUTF8Data: jsonData) Swift Protobuf
  10. { "repository": "Git", "url": "https://github.com/ibrew/beer-kit.git", "path": "BeerKit", "branch": "master", "maintainer":

    "[email protected]", "compatibility": [ { "version": "4.2", "commit": "195cd8cde2bb717242b3081f9c367ccd0a2f0121" } ], "platforms": ["Darwin"], "actions": [ { "action": "BuildXcodeWorkspaceScheme", "workspace": "BeerKit.xcworkspace", "scheme": "BeerKit", "destination": "generic/platform=iOS", "configuration": "Release" } ] } Compatibility Test Suite
  11. Swift Syntax Spaces, Newlines, Comments Trivia Tokens if, {, },

    45, ”Hello” A token owns all of the trivia following it up to the next token or newline A token owns all of the trivia preceding it that isn’t owned by another token
  12. Swift Syntax let url = URL(fileURLWithPath: CommandLine.arguments[1]) var syntax =

    try SyntaxTreeParser.parse(url) syntax = IfFormatter().visit(sourceSyntax) try syntax.description.write(to: url, atomically: true, encoding: .utf8)
  13. class IfFormatter: SyntaxRewriter { open override func visit(_ node: IfStmtSyntax)

    -> StmtSyntax { var node = node if node.ifKeyword.trailingTrivia != .spaces(1) { node = node.withIfKeyword( node.ifKeyword.withTrailingTrivia(.spaces(1))) } if node.conditions.trailingTrivia != .spaces(1) { node = node.withConditions( node.conditions.withTrailingTrivia(.spaces(1))) } if node.body.leftBrace.leadingTrivia?.count != 0 { node = node.withBody( node.body.withLeftBrace( node.body.leftBrace.withoutLeadingTrivia())) } if node.body.leftBrace.trailingTrivia?.count != 0 { node = node.withBody( node.body.withLeftBrace( node.body.leftBrace.withoutTrailingTrivia())) } return node } }
  14. Swift Docker Image $ docker-compose exec swift test —-parallel $

    docker-compose exec swift run Build & Test Build & Run
  15. Follow up $ Come talk to me ! Swift Blog

    https://swift.org/blog # Swift Community Podcast https://www.swiftcommunitypodcast.org " Swift Weekly Brief https://swiftweekly.github.io Swift Forums https://forums.swift.org