Slide 1

Slide 1 text

Making developer tools with Swift Pol Piella Abadia Senior iOS Developer, BBC iPlayer iOS Dev UK · Aberystwyth, Wales 10:20 Great Hall

Slide 2

Slide 2 text

Senior software engineer based in Manchester Hi! I’m Pol

Slide 3

Slide 3 text

Content creation Apps I am working on Public speaking

Slide 4

Slide 4 text

Content creation Apps I am working on Public speaking

Slide 5

Slide 5 text

Content creation Apps I am working on Public speaking

Slide 6

Slide 6 text

Swift & Developer Tools?

Slide 7

Slide 7 text

Swift & Developer Tools!

Slide 8

Slide 8 text

You know Swift!

Slide 9

Slide 9 text

You (and your team) know Swift!

Slide 10

Slide 10 text

Focus on the system and NOT the language!

Slide 11

Slide 11 text

You (and your audience) know Swift!

Slide 12

Slide 12 text

Can you build it with Swift?

Slide 13

Slide 13 text

#1 A simple CLI script?

Slide 14

Slide 14 text

feature/MYTICKET-123_name-of-your-branch

Slide 15

Slide 15 text

feature/MYTICKET-123_name-of-your-branch Author: Pol Piella Abadia Date: Sun Aug 20 14:03:41 2023 +0100 My awesome feature TICKET-123 Branch Commit

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

# ! /usr/bin/env swift import Foundation let commitMessageFile = CommandLine.arguments[1] guard let data = FileManager.default.contents(atPath: commitMessageFile), let commitMessage = String(data: data, encoding: .utf8) else { exit(1) } let gitBranchName = shell("git rev-parse -- abbrev-ref HEAD") .trimmingCharacters(in: .newlines) let regex = try! NSRegularExpression(pattern: #"(\w+-\d+)"#, options: .anchorsMatchLines) let stringRange = NSRange(location: 0, length: gitBranchName.utf16.count) guard let match = regex.firstMatch(in: gitBranchName, range: stringRange) else { exit(0) } let range = match.range(at: 1) guard !NSEqualRanges(range, NSMakeRange(NSNotFound, 0)) else { exit(1) } let ticketNumber = (gitBranchName as NSString) .substring(with: range) .trimmingCharacters(in: .newlines) if !commitMessage.contains(ticketNumber) { do { try "\(commitMessage.trimmingCharacters(in:.newlines))\n\n\(ticketNumber)" .write(toFile: commitMessageFile, atomically: true, encoding: .utf8) } catch { print("Could not write to file \(commitMessageFile)") } }

Slide 19

Slide 19 text

# ! /usr/bin/env swift import Foundation let commitMessageFile = CommandLine.arguments[1] guard let data = FileManager.default.contents(atPath: commitMessageFile), let commitMessage = String(data: data, encoding: .utf8) else { exit(1) } let gitBranchName = shell("git rev-parse -- abbrev-ref HEAD") .trimmingCharacters(in: .newlines) let regex = try! NSRegularExpression(pattern: #"(\w+-\d+)"#, options: .anchorsMatchLines) let stringRange = NSRange(location: 0, length: gitBranchName.utf16.count) guard let match = regex.firstMatch(in: gitBranchName, range: stringRange) else { exit(0) } let range = match.range(at: 1) guard !NSEqualRanges(range, NSMakeRange(NSNotFound, 0)) else { exit(1) } let ticketNumber = (gitBranchName as NSString) .substring(with: range) .trimmingCharacters(in: .newlines) if !commitMessage.contains(ticketNumber) { do { try "\(commitMessage.trimmingCharacters(in:.newlines))\n\n\(ticketNumber)" .write(toFile: commitMessageFile, atomically: true, encoding: .utf8) } catch { print("Could not write to file \(commitMessageFile)") } }

Slide 20

Slide 20 text

chmod +x file_where_hook_code_is

Slide 21

Slide 21 text

Not so fast…

Slide 22

Slide 22 text

Yes and no… https://github.com/mxcl/swift-sh

Slide 23

Slide 23 text

# ! /usr/bin/env swift-sh import Foundation let commitMessageFile = CommandLine.arguments[1] guard let data = FileManager.default.contents(atPath: commitMessageFile), let commitMessage = String(data: data, encoding: .utf8) else { exit(1) } let gitBranchName = shell("git rev-parse -- abbrev-ref HEAD") .trimmingCharacters(in: .newlines) let regex = try! NSRegularExpression(pattern: #"(\w+-\d+)"#, options: .anchorsMatchLines) let stringRange = NSRange(location: 0, length: gitBranchName.utf16.count) guard let match = regex.firstMatch(in: gitBranchName, range: stringRange) else { exit(0) } let range = match.range(at: 1) guard !NSEqualRanges(range, NSMakeRange(NSNotFound, 0)) else { exit(1) } let ticketNumber = (gitBranchName as NSString) .substring(with: range) .trimmingCharacters(in: .newlines) if !commitMessage.contains(ticketNumber) { do { try "\(commitMessage.trimmingCharacters(in:.newlines))\n\n\(ticketNumber)" .write(toFile: commitMessageFile, atomically: true, encoding: .utf8) } catch { print("Could not write to file \(commitMessageFile)") } }

Slide 24

Slide 24 text

# ! /usr/bin/env swift-sh import Foundation import SourceKittenFramework // @jpsim ~> 0.34.1 let commitMessageFile = CommandLine.arguments[1] guard let data = FileManager.default.contents(atPath: commitMessageFile), let commitMessage = String(data: data, encoding: .utf8) else { exit(1) } let gitBranchName = shell("git rev-parse -- abbrev-ref HEAD") .trimmingCharacters(in: .newlines) let regex = try! NSRegularExpression(pattern: #"(\w+-\d+)"#, options: .anchorsMatchLines) let stringRange = NSRange(location: 0, length: gitBranchName.utf16.count) guard let match = regex.firstMatch(in: gitBranchName, range: stringRange) else { exit(0) } let range = match.range(at: 1) guard !NSEqualRanges(range, NSMakeRange(NSNotFound, 0)) else { exit(1) } let ticketNumber = (gitBranchName as NSString) .substring(with: range) .trimmingCharacters(in: .newlines) if !commitMessage.contains(ticketNumber) { do { try "\(commitMessage.trimmingCharacters(in:.newlines))\n\n\(ticketNumber)" .write(toFile: commitMessageFile, atomically: true, encoding: .utf8) } catch { print("Could not write to file \(commitMessageFile)") } }

Slide 25

Slide 25 text

Future- proofing and scaling

Slide 26

Slide 26 text

swift package init -- type executable

Slide 27

Slide 27 text

// swift-tools-version: 5.9 import PackageDescription let package = Package( name: "GitHookExecutable", dependencies: [ ], targets: [ .executableTarget( name: "GitHookExecutable", dependencies: [ ] ), ] )

Slide 28

Slide 28 text

// swift-tools-version: 5.9 import PackageDescription let package = Package( name: "GitHookExecutable", dependencies: [ .package(url: "https: // github.com/apple/swift-argument-parser.git", exact: "1.2.3") ], targets: [ .executableTarget( name: "GitHookExecutable", dependencies: [ .product(name: "ArgumentParser", package: "swift-argument-parser") ] ), ] )

Slide 29

Slide 29 text

import ArgumentParser @main struct GitHookExecutable: ParsableCommand { @Argument(help: "The path to the commit message file") var commitMessagePath: String func run() throws { let commitMessage = try String(contentsOfFile: commitMessagePath) // . .. } }

Slide 30

Slide 30 text

Learn more about it! https://www.youtube.com/watch?v=yyOXynCoagE&t=611s

Slide 31

Slide 31 text

#2 Running a build script?

Slide 32

Slide 32 text

No content

Slide 33

Slide 33 text

brew install swiftlint pod install

Slide 34

Slide 34 text

What about Swift Packages?

Slide 35

Slide 35 text

Swift 5.6 to the rescue! https://github.com/apple/swift-evolution/blob/main/proposals/0303-swiftpm-extensible-build-tools.md

Slide 36

Slide 36 text

// swift-tools-version:5.7 import PackageDescription let package = Package( name: "SwiftLint", platforms: [.macOS(.v12)], products: [ .plugin(name: "SwiftLintPlugin", targets: ["SwiftLintPlugin"]) ], dependencies: [ / / .. . ], targets: [ .plugin( name: "SwiftLintPlugin", capability: .buildTool(), dependencies: [ .target(name: "SwiftLintBinary", condition: .when(platforms: [.macOS])), .target(name: "swiftlint", condition: .when(platforms: [.linux])) ] ), .executableTarget( name: "swiftlint", dependencies: [ .product(name: "ArgumentParser", package: "swift-argument-parser"), "CollectionConcurrencyKit", "SwiftLintFramework", "SwiftyTextTable", ] ), .binaryTarget( name: "SwiftLintBinary", url: "https: // github.com/realm/SwiftLint/releases/download/0.52.4/SwiftLintBinary-macos.artifactbundle.zip", checksum: "8a8095e6235a07d00f34a9e500e7568b359f6f66a249f36d12cd846017a8c6f5" ) ] ) https://github.com/realm/SwiftLint/blob/main/Package.swift

Slide 37

Slide 37 text

// swift-tools-version:5.7 import PackageDescription let package = Package( name: "SwiftLint", platforms: [.macOS(.v12)], products: [ .plugin(name: "SwiftLintPlugin", targets: ["SwiftLintPlugin"]) ], dependencies: [ / / .. . ], targets: [ .plugin( name: "SwiftLintPlugin", capability: .buildTool(), dependencies: [ .target(name: "SwiftLintBinary", condition: .when(platforms: [.macOS])), .target(name: "swiftlint", condition: .when(platforms: [.linux])) ] ), .executableTarget( name: "swiftlint", dependencies: [ .product(name: "ArgumentParser", package: "swift-argument-parser"), "CollectionConcurrencyKit", "SwiftLintFramework", "SwiftyTextTable", ] ), .binaryTarget( name: "SwiftLintBinary", url: "https: // github.com/realm/SwiftLint/releases/download/0.52.4/SwiftLintBinary-macos.artifactbundle.zip", checksum: "8a8095e6235a07d00f34a9e500e7568b359f6f66a249f36d12cd846017a8c6f5" ) ] ) https://github.com/realm/SwiftLint/blob/main/Package.swift

Slide 38

Slide 38 text

// swift-tools-version:5.7 import PackageDescription let package = Package( name: "SwiftLint", platforms: [.macOS(.v12)], products: [ .plugin(name: "SwiftLintPlugin", targets: ["SwiftLintPlugin"]) ], dependencies: [ / / .. . ], targets: [ .plugin( name: "SwiftLintPlugin", capability: .buildTool(), dependencies: [ .target(name: "SwiftLintBinary", condition: .when(platforms: [.macOS])), .target(name: "swiftlint", condition: .when(platforms: [.linux])) ] ), .executableTarget( name: "swiftlint", dependencies: [ .product(name: "ArgumentParser", package: "swift-argument-parser"), "CollectionConcurrencyKit", "SwiftLintFramework", "SwiftyTextTable", ] ), .binaryTarget( name: "SwiftLintBinary", url: "https: // github.com/realm/SwiftLint/releases/download/0.52.4/SwiftLintBinary-macos.artifactbundle.zip", checksum: "8a8095e6235a07d00f34a9e500e7568b359f6f66a249f36d12cd846017a8c6f5" ) ] ) https://github.com/realm/SwiftLint/blob/main/Package.swift

Slide 39

Slide 39 text

// swift-tools-version:5.7 import PackageDescription let package = Package( name: "SwiftLint", platforms: [.macOS(.v12)], products: [ .plugin(name: "SwiftLintPlugin", targets: ["SwiftLintPlugin"]) ], dependencies: [ / / .. . ], targets: [ .plugin( name: "SwiftLintPlugin", capability: .buildTool(), dependencies: [ .target(name: "SwiftLintBinary", condition: .when(platforms: [.macOS])), .target(name: "swiftlint", condition: .when(platforms: [.linux])) ] ), .executableTarget( name: "swiftlint", dependencies: [ .product(name: "ArgumentParser", package: "swift-argument-parser"), "CollectionConcurrencyKit", "SwiftLintFramework", "SwiftyTextTable", ] ), .binaryTarget( name: "SwiftLintBinary", url: "https: // github.com/realm/SwiftLint/releases/download/0.52.4/SwiftLintBinary-macos.artifactbundle.zip", checksum: "8a8095e6235a07d00f34a9e500e7568b359f6f66a249f36d12cd846017a8c6f5" ) ] ) https://github.com/realm/SwiftLint/blob/main/Package.swift

Slide 40

Slide 40 text

// swift-tools-version:5.7 import PackageDescription let package = Package( name: "SwiftLint", platforms: [.macOS(.v12)], products: [ .plugin(name: "SwiftLintPlugin", targets: ["SwiftLintPlugin"]) ], dependencies: [ / / .. . ], targets: [ .plugin( name: "SwiftLintPlugin", capability: .buildTool(), dependencies: [ .target(name: "SwiftLintBinary", condition: .when(platforms: [.macOS])), .target(name: "swiftlint", condition: .when(platforms: [.linux])) ] ), .executableTarget( name: "swiftlint", dependencies: [ .product(name: "ArgumentParser", package: "swift-argument-parser"), "CollectionConcurrencyKit", "SwiftLintFramework", "SwiftyTextTable", ] ), .binaryTarget( name: "SwiftLintBinary", url: "https: // github.com/realm/SwiftLint/releases/download/0.52.4/SwiftLintBinary-macos.artifactbundle.zip", checksum: "8a8095e6235a07d00f34a9e500e7568b359f6f66a249f36d12cd846017a8c6f5" ) ] ) https://github.com/realm/SwiftLint/blob/main/Package.swift

Slide 41

Slide 41 text

import Foundation import PackagePlugin @main struct SwiftLintPlugin: BuildToolPlugin { func createBuildCommands(context: PluginContext, target: Target) async throws - > [Command] { guard let sourceTarget = target as? SourceModuleTarget else { return [] } return createBuildCommands( inputFiles: sourceTarget.sourceFiles(withSuffix: "swift").map(\.path), packageDirectory: context.package.directory, workingDirectory: context.pluginWorkDirectory, tool: try context.tool(named: "swiftlint") ) } private func createBuildCommands( inputFiles: [Path], packageDirectory: Path, workingDirectory: Path, tool: PluginContext.Tool ) -> [Command] { // . .. return [ .prebuildCommand( displayName: "SwiftLint", executable: tool.path, arguments: arguments, outputFilesDirectory: outputFilesDirectory ) ] } } https://github.com/realm/SwiftLint/blob/main/Plugins/SwiftLintPlugin/SwiftLintPlugin.swift

Slide 42

Slide 42 text

import Foundation import PackagePlugin @main struct SwiftLintPlugin: BuildToolPlugin { func createBuildCommands(context: PluginContext, target: Target) async throws - > [Command] { guard let sourceTarget = target as? SourceModuleTarget else { return [] } return createBuildCommands( inputFiles: sourceTarget.sourceFiles(withSuffix: "swift").map(\.path), packageDirectory: context.package.directory, workingDirectory: context.pluginWorkDirectory, tool: try context.tool(named: "swiftlint") ) } private func createBuildCommands( inputFiles: [Path], packageDirectory: Path, workingDirectory: Path, tool: PluginContext.Tool ) -> [Command] { // . .. return [ .prebuildCommand( displayName: "SwiftLint", executable: tool.path, arguments: arguments, outputFilesDirectory: outputFilesDirectory ) ] } } https://github.com/realm/SwiftLint/blob/main/Plugins/SwiftLintPlugin/SwiftLintPlugin.swift

Slide 43

Slide 43 text

import Foundation import PackagePlugin @main struct SwiftLintPlugin: BuildToolPlugin { func createBuildCommands(context: PluginContext, target: Target) async throws - > [Command] { guard let sourceTarget = target as? SourceModuleTarget else { return [] } return createBuildCommands( inputFiles: sourceTarget.sourceFiles(withSuffix: "swift").map(\.path), packageDirectory: context.package.directory, workingDirectory: context.pluginWorkDirectory, tool: try context.tool(named: "swiftlint") ) } private func createBuildCommands( inputFiles: [Path], packageDirectory: Path, workingDirectory: Path, tool: PluginContext.Tool ) -> [Command] { // . .. return [ .prebuildCommand( displayName: "SwiftLint", executable: tool.path, arguments: arguments, outputFilesDirectory: outputFilesDirectory ) ] } } https://github.com/realm/SwiftLint/blob/main/Plugins/SwiftLintPlugin/SwiftLintPlugin.swift

Slide 44

Slide 44 text

import Foundation import PackagePlugin @main struct SwiftLintPlugin: BuildToolPlugin { func createBuildCommands(context: PluginContext, target: Target) async throws - > [Command] { guard let sourceTarget = target as? SourceModuleTarget else { return [] } return createBuildCommands( inputFiles: sourceTarget.sourceFiles(withSuffix: "swift").map(\.path), packageDirectory: context.package.directory, workingDirectory: context.pluginWorkDirectory, tool: try context.tool(named: "swiftlint") ) } private func createBuildCommands( inputFiles: [Path], packageDirectory: Path, workingDirectory: Path, tool: PluginContext.Tool ) -> [Command] { // . .. return [ .prebuildCommand( displayName: "SwiftLint", executable: tool.path, arguments: arguments, outputFilesDirectory: outputFilesDirectory ) ] } } https://github.com/realm/SwiftLint/blob/main/Plugins/SwiftLintPlugin/SwiftLintPlugin.swift

Slide 45

Slide 45 text

// swift-tools-version: 5.9 import PackageDescription let package = Package( name: "DemoSwiftPackage", products: [ .library( name: "DemoSwiftPackage", targets: ["DemoSwiftPackage"] ), ], dependencies: [ .package(url: "https: // github.com/realm/SwiftLint.git", exact: "0.52.4") ], targets: [ .target( name: "DemoSwiftPackage", plugins: [ .plugin(name: "SwiftLintPlugin", package: "SwiftLint") ] ) ] )

Slide 46

Slide 46 text

// swift-tools-version: 5.9 import PackageDescription let package = Package( name: "DemoSwiftPackage", products: [ .library( name: "DemoSwiftPackage", targets: ["DemoSwiftPackage"] ), ], dependencies: [ .package(url: "https: // github.com/realm/SwiftLint.git", exact: "0.52.4") ], targets: [ .target( name: "DemoSwiftPackage", plugins: [ .plugin(name: "SwiftLintPlugin", package: "SwiftLint") ] ) ] )

Slide 47

Slide 47 text

What about Xcode projects?

Slide 48

Slide 48 text

import Foundation import PackagePlugin #if canImport(XcodeProjectPlugin) import XcodeProjectPlugin extension SwiftLintPlugin: XcodeBuildToolPlugin { func createBuildCommands(context: XcodePluginContext, target: XcodeTarget) throws - > [Command] { let inputFilePaths = target.inputFiles .filter { $0.type == .source && $0.path.extension == "swift" } .map(\.path) return createBuildCommands( inputFiles: inputFilePaths, packageDirectory: context.xcodeProject.directory, workingDirectory: context.pluginWorkDirectory, tool: try context.tool(named: "swiftlint") ) } } #endif https://github.com/realm/SwiftLint/blob/main/Plugins/SwiftLintPlugin/SwiftLintPlugin.swift

Slide 49

Slide 49 text

No content

Slide 50

Slide 50 text

No content

Slide 51

Slide 51 text

Manually running a build script?

Slide 52

Slide 52 text

import UIKit public protocol Camera { func start() func stop() func capture(_ completion: @escaping (UIImage?) -> Void) func rotate() }

Slide 53

Slide 53 text

import UIKit // Protocol to be matched protocol AutoMockable {} public protocol Camera: AutoMockable { func start() func stop() func capture(_ completion: @escaping (UIImage?) -> Void) func rotate() }

Slide 54

Slide 54 text

./sourcery -- config ".sourcery.yml"

Slide 55

Slide 55 text

// Generated using Sourcery 1.8.2 — https: // github.com/krzysztofzablocki/Sourcery // DO NOT EDIT // swiftlint:disable line_length // swiftlint:disable variable_name import Foundation #if os(iOS) || os(tvOS) || os(watchOS) import UIKit #elseif os(OSX) import AppKit #endif class CameraMock: Camera { // MARK: - start var startCallsCount = 0 var startCalled: Bool { return startCallsCount > 0 } var startClosure: (() -> Void)? func start() { startCallsCount += 1 startClosure?() } //

Slide 56

Slide 56 text

What if…

Slide 57

Slide 57 text

/ / swift-tools-version:5.6 import PackageDescription import Foundation let package = Package( name: "Sourcery", platforms: [ .macOS(.v12) ], products: [ .plugin(name: "SourceryCommandPlugin", targets: ["SourceryCommandPlugin"]) ], dependencies: [], targets: [ .executableTarget( name: "SourceryExecutable", dependencies: ["SourceryLib"], path: "SourceryExecutable", exclude: [ "Info.plist" ] ), .plugin( name: "SourceryCommandPlugin", capability: .command( intent: .custom( verb: "sourcery-command", description: "Sourcery command plugin for code generation" ), permissions: [ .writeToPackageDirectory(reason: "Need permission to write generated files to package directory") ] ), dependencies: ["SourceryExecutable"] ) ] ) https://github.com/krzysztofzablocki/Sourcery/blob/master/Package.swift

Slide 58

Slide 58 text

/ / swift-tools-version:5.6 import PackageDescription import Foundation let package = Package( name: "Sourcery", platforms: [ .macOS(.v12) ], products: [ .plugin(name: "SourceryCommandPlugin", targets: ["SourceryCommandPlugin"]) ], dependencies: [], targets: [ .executableTarget( name: "SourceryExecutable", dependencies: ["SourceryLib"], path: "SourceryExecutable", exclude: [ "Info.plist" ] ), .plugin( name: "SourceryCommandPlugin", capability: .command( intent: .custom( verb: "sourcery-command", description: "Sourcery command plugin for code generation" ), permissions: [ .writeToPackageDirectory(reason: "Need permission to write generated files to package directory") ] ), dependencies: ["SourceryExecutable"] ) ] ) https://github.com/krzysztofzablocki/Sourcery/blob/master/Package.swift

Slide 59

Slide 59 text

/ / swift-tools-version:5.6 import PackageDescription import Foundation let package = Package( name: "Sourcery", platforms: [ .macOS(.v12) ], products: [ .plugin(name: "SourceryCommandPlugin", targets: ["SourceryCommandPlugin"]) ], dependencies: [], targets: [ .executableTarget( name: "SourceryExecutable", dependencies: ["SourceryLib"], path: "SourceryExecutable", exclude: [ "Info.plist" ] ), .plugin( name: "SourceryCommandPlugin", capability: .command( intent: .custom( verb: "sourcery-command", description: "Sourcery command plugin for code generation" ), permissions: [ .writeToPackageDirectory(reason: "Need permission to write generated files to package directory") ] ), dependencies: ["SourceryExecutable"] ) ] ) https://github.com/krzysztofzablocki/Sourcery/blob/master/Package.swift

Slide 60

Slide 60 text

import PackagePlugin import Foundation @main struct SourceryCommandPlugin: CommandPlugin { func performCommand(context: PluginContext, arguments: [String]) async throws { / / Run one per target for target in context.package.targets { let configFilePath = target.directory.appending(subpath: ".sourcery.yml").string let sourcery = try context.tool(named: "SourceryExecutable").path.string guard FileManager.default.fileExists(atPath: configFilePath) else { Diagnostics.warning("⚠ Could not find `.sourcery.yml` for target \(target.name)") continue } let sourceryURL = URL(fileURLWithPath: sourcery) let process = Process() process.executableURL = sourceryURL process.arguments = [ " - - config", configFilePath, " - - cacheBasePath", context.pluginWorkDirectory.string ] try process.run() process.waitUntilExit() let gracefulExit = process.terminationReason = = .exit && process.terminationStatus == 0 if !gracefulExit { throw "🛑 The plugin execution failed with reason: \(process.terminationReason.rawValue) and status: \(process.terminationStatus) " } } } } https://github.com/krzysztofzablocki/Sourcery/blob/master/Plugins/SourceryCommandPlugin/SourceryCommandPlugin.swift

Slide 61

Slide 61 text

// swift-tools-version: 5.9 import PackageDescription let package = Package( name: "GitHookExecutable", dependencies: [ .package(url: "https: // github.com/apple/swift-argument-parser.git", exact: "1.2.3") ], targets: [ .executableTarget( name: "GitHookExecutable", dependencies: [ .product(name: "ArgumentParser", package: "swift-argument-parser") ] ), ] )

Slide 62

Slide 62 text

// swift-tools-version: 5.9 import PackageDescription let package = Package( name: "GitHookExecutable", dependencies: [ .package(url: "https: // github.com/apple/swift-argument-parser.git", exact: "1.2.3"), .package(url: "https: // github.com/krzysztofzablocki/Sourcery.git", exact: "2.0.3") ], targets: [ .executableTarget( name: "GitHookExecutable", dependencies: [ .product(name: "ArgumentParser", package: "swift-argument-parser") ] ), ] )

Slide 63

Slide 63 text

swift package sourcery-command

Slide 64

Slide 64 text

No content

Slide 65

Slide 65 text

What about Xcode projects?

Slide 66

Slide 66 text

#if canImport(XcodeProjectPlugin) import XcodeProjectPlugin extension SourceryCommandPlugin: XcodeCommandPlugin { func performCommand(context: XcodePluginContext, arguments: [String]) throws { for target in context.xcodeProject.targets { guard let configFilePath = target .inputFiles .filter({ $0.path.lastComponent == ".sourcery.yml" }) .first? .path .string else { Diagnostics.warning("⚠ Could not find `.sourcery.yml` in Xcode's input file list") return } let sourcery = try context.tool(named: "SourceryExecutable").path.string try run(sourcery, withConfig: configFilePath, cacheBasePath: context.pluginWorkDirectory.string) } } } #endif https://github.com/krzysztofzablocki/Sourcery/blob/master/Plugins/SourceryCommandPlugin/SourceryCommandPlugin.swift

Slide 67

Slide 67 text

No content

Slide 68

Slide 68 text

No content

Slide 69

Slide 69 text

#3 Back-End service?

Slide 70

Slide 70 text

No content

Slide 71

Slide 71 text

No content

Slide 72

Slide 72 text

How does this work?

Slide 73

Slide 73 text

Ok… but what are web hooks?

Slide 74

Slide 74 text

issue_comment webhook https://docs.github.com/en/webhooks/webhook-events-and-payloads#issue_comment

Slide 75

Slide 75 text

Creating a service with Swift!

Slide 76

Slide 76 text

No content

Slide 77

Slide 77 text

No content

Slide 78

Slide 78 text

https://github.com/swift-server/swift-aws-lambda-runtime

Slide 79

Slide 79 text

It’s just an executable…

Slide 80

Slide 80 text

import AWSLambdaRuntime import AWSLambdaEvents @main struct UploadToTestFlightWebhook: SimpleLambdaHandler { let snakeCaseDecoder: JSONDecoder init() { let decoder = JSONDecoder() decoder.keyDecodingStrategy = .convertFromSnakeCase snakeCaseDecoder = decoder } func handle(_ request: APIGatewayV2Request, context: LambdaContext) async throws -> APIGatewayV2Response { guard let body = request.body, let bodyData = body.data(using: .utf8), let request = try? snakeCaseDecoder.decode(GithubWebhookData.self, from: bodyData) else { return .init(statusCode: .badRequest, body: "Could not parse the request content ... ") } guard request.action == "created", request.comment.body.lowercased() == "upload to testflight" else { return .init(statusCode: .ok, body: "Not handling the event ... ") } // ... } }

Slide 81

Slide 81 text

… that runs on a different platform

Slide 82

Slide 82 text

#! /bin/bash product=Webhook docker run \ -- rm \ -- volume "$(pwd)/:/src" \ -- workdir "/src/" \ swift:5.8-amazonlinux2 \ swift build -- product $product -c release -Xswiftc -static-stdlib target=.build/lambda/$product rm -rf "$target" mkdir -p "$target" cp ".build/release/$product" "$target/" cd "$target" ln -s "$product" "bootstrap" zip -- symlinks lambda.zip * https://fabianfett.dev/getting-started-with-swift-aws-lambda-runtime

Slide 83

Slide 83 text

#! /bin/bash swift package \ -- disable-sandbox archive \ -- swift-version 5.8 \ -- output-path . \ -- products Webhook

Slide 84

Slide 84 text

#! /bin/bash swift package \ -- disable-sandbox archive \ -- swift-version 5.8 \ -- output-path . \ -- products Webhook

Slide 85

Slide 85 text

It’s getting better…

Slide 86

Slide 86 text

You still need to upload it…

Slide 87

Slide 87 text

No content

Slide 88

Slide 88 text

service: upload-to-testflight-webhook frameworkVersion: "3" configValidationMode: warn package: artifact: build/Products/Webhook.zip provider: name: aws region: eu-west-1 httpApi: payload: "2.0" cors: true runtime: provided.al2 architecture: arm64 functions: webhook: handler: webhook memorySize: 256 description: "[${sls:stage}] Upload" events: - httpApi: path: /upload method: post Serverless framework https://www.serverless.com

Slide 89

Slide 89 text

name: Deploy the serverless lambda to AWS on: push: branches: - main jobs: deploy: runs-on: ubuntu-latest container: image: swift:5.8-amazonlinux2 steps: - uses: actions/checkout@v3 - name: Configure AWS Credentials uses: aws-actions/configure-aws-credentials@v2 with: aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} aws-region: eu-west-2 - name: Install zip run: | yum install -y zip - name: Install awscli run: | yum install -y awscli - name: Archive the lambda run: | ./package.sh Webhook - name: Deploy to AWS run: | aws lambda update-function-code \ - - function-name webhook \ - - zip-file fileb: // .build/lambda/Webhook/Webhook.zip CI/CD workflow https://www.polpiella.dev/automatic-deployment-of-swift-aws-lambdas-on-ci-cd

Slide 90

Slide 90 text

This is jut the tip of the iceberg…

Slide 91

Slide 91 text

Even slide decks! https://github.com/joshdholtz/DeckUI

Slide 92

Slide 92 text

Any questions?

Slide 93

Slide 93 text

Any questions? I have stickers too…

Slide 94

Slide 94 text

Resources - Plugins https://www.polpiella.dev/code-generation-using-swift-package-plugins https://www.polpiella.dev/sourcery-swift-package-command-plugin https://www.polpiella.dev/load-custom-fonts-with-no-code-using-swift-package-plugins https://www.polpiella.dev/an-early-look-at-swift-extensible-build-tools https://theswiftdev.com/beginners-guide-to-swift-package-manager-command-plugins/ https://www.polpiella.dev/binary-targets-in-modern-swift-packages https://www.polpiella.dev/network-requests-in-swift-package-plugins

Slide 95

Slide 95 text

https://www.polpiella.dev/automating-swift-package-releases-with-github-actions https://www.polpiella.dev/collecting-gihub-actions-workflow-metrics-with-swift https://www.polpiella.dev/scripting-in-swift-git-hooks https://github.com/artemnovichkov/Swift-For-Scripting https://blog.eidinger.info/swift-e-runs-code-directly-from-the-command-line https://nshipster.com/swift-sh/ Resources - CLI + Scripts

Slide 96

Slide 96 text

https://www.areweserveryet.org/ https://www.polpiella.dev/github-webhooks-and-xcode-cloud https://www.polpiella.dev/swift-async-await-in-aws-lambdas https://www.polpiella.dev/scheduled-swift-aws-lambdas https://www.polpiella.dev/automatic-deployment-of-swift-aws-lambdas-on-ci-cd https://opticalaberration.com/2021/12/proxy-server.html Resources - ServerSide Swift https://www.serversideswift.info https://theswiftdev.com