Slide 1

Slide 1 text

SWIFT SCRIPTS: ZERO TO HERO FEDERICO ZANETELLO Μ£Μ£Μ£Μ£Μ£ fivestars.blog β€’ @zntfdr

Slide 2

Slide 2 text

WHY?

Slide 3

Slide 3 text

GETTING STARTED

Slide 4

Slide 4 text

hello

Slide 5

Slide 5 text

CREATING AN EXECUTABLE $ mkdir hello $ cd hello $ swift package init --type executable

Slide 6

Slide 6 text

THE PACKAGE STRUCTURE β”œβ”€β”€ .gitignore β”œβ”€β”€ Package.swift β”œβ”€β”€ README.md β”œβ”€β”€ Sources β”‚ └── hello β”‚ └── main.swift └── Tests β”œβ”€β”€ helloTests β”‚ β”œβ”€β”€ helloTests.swift β”‚ └── XCTestManifests.swift └── LinuxMain.swift

Slide 7

Slide 7 text

PACKAGE.SWIFT // swift-tools-version:5.2 import PackageDescription let package = Package( name: "hello", dependencies: [ ], targets: [ .target( name: "hello", dependencies: []), .testTarget( name: "helloTests", dependencies: ["hello"]), ] )

Slide 8

Slide 8 text

THE PACKAGE STRUCTURE β”œβ”€β”€ .gitignore β”œβ”€β”€ Package.swift β”œβ”€β”€ README.md β”œβ”€β”€ Sources β”‚ └── hello β”‚ └── main.swift └── Tests β”œβ”€β”€ helloTests β”‚ β”œβ”€β”€ helloTests.swift β”‚ └── XCTestManifests.swift └── LinuxMain.swift

Slide 9

Slide 9 text

MAIN.SWIFT print("Hello, world!")

Slide 10

Slide 10 text

BUILD RUN TEST $ swift build hello $ swift run hello $ swift test

Slide 11

Slide 11 text

BUILD RUN TEST $ swift build hello $ swift run hello $ swift test ..OR USE XCODE

Slide 12

Slide 12 text

WE'RE DONE! !

Slide 13

Slide 13 text

GUI -> TUI

Slide 14

Slide 14 text

LAUNCH ARGUMENTS import Foundation // The first argument is the script execution path. let arguments = CommandLine.arguments.dropFirst() if let name = arguments.first { print("Hello \(name)") } else { exit(EXIT_FAILURE) } $ swift run hello Federico > Hello Federico

Slide 15

Slide 15 text

INTERACTIVE import Foundation print("What's your name?") guard let name = readLine(), !name.isEmpty else { exit(EXIT_FAILURE) } print("Hello \(name)") $ swift run hello > What's your name? > Federico > Hello Federico

Slide 16

Slide 16 text

ENVIRONMENT VARIABLES import Foundation let processInfo = ProcessInfo.processInfo let environment = processInfo.environment if let name = environment["MYNAME"] { print("Hello \(name)") } else { exit(EXIT_FAILURE) } $ MYNAME=Federico swift run > Hello Federico

Slide 17

Slide 17 text

PIPELINE MESSAGES import Foundation let standardInput: FileHandle = .standardInput let data = standardInput.availableData if let inputString = String( data: data, encoding: .utf8 ) { print("Hello \(inputString)") } else { exit(EXIT_FAILURE) } $ printf Federico | swift run hello > Hello Federico

Slide 18

Slide 18 text

ARGUMENT PARSER

Slide 19

Slide 19 text

ADDING A DEPENDENCY let package = Package( name: "hello", dependencies: [ .package(url: "https://github.com/apple/swift-argument-parser.git", from: "0.0.1"), ], targets: [ .target( name: "hello", dependencies: [ .product(name: "ArgumentParser", package: "swift-argument-parser") ] ), .testTarget( name: "helloTests", dependencies: ["hello"]), ] )

Slide 20

Slide 20 text

ARGUMENTPARSER import ArgumentParser struct Hello: ParsableCommand { @Argument(help: "Specify your name.") var name: String func run() throws { print("Hello \(name)") } } Hello.main() $ swift run hello Federico > Hello Federico $ swift run hello > Error: Missing expected argument '' > Usage: hello $ swift run hello --help > USAGE: hello > > OPTIONS: > Specify your name. > -h, --help Show help information.

Slide 21

Slide 21 text

SwiftToolsSupport's TSCUTILITY & TSCBASIC

Slide 22

Slide 22 text

ADDING A DEPENDENCY 2 let package = Package( name: "hello", dependencies: [ .package(url: "https://github.com/apple/swift-tools-support-core.git", from: "0.0.1"), ], targets: [ .target( name: "hello", dependencies: [ .product(name: "SwiftToolsSupport", package: "swift-tools-support-core") ] ), .testTarget( name: "helloTests", dependencies: ["hello"]), ] )

Slide 23

Slide 23 text

PROGRESS STATE import Foundation import TSCBasic import TSCUtility let animation = PercentProgressAnimation( stream: stdoutStream, header: "Loading Greeting") for i in 0..<100 { let second: Double = 1_000_000 usleep(UInt32(second * 0.05)) animation.update(step: i, total: 100, text: "Almost there...") } animation.complete(success: true) print("Hello UIKonf! ! ")

Slide 24

Slide 24 text

RELEASE

Slide 25

Slide 25 text

USE THE SCRIPT ANYWHERE $ swift build -c release $ cp .build/release/hello /usr/local/bin/hello $ hello

Slide 26

Slide 26 text

WE'RE DONE! *REALLY

Slide 27

Slide 27 text

LINKS github.com/apple/swift-package-manager github.com/apple/swift-argument-parser github.com/apple/swift-tools-support-core fivestars.blog/ultimate-guide-swift-executables.html github.com/zntfdr/talks

Slide 28

Slide 28 text

SWIFT SCRIPTS: ZERO TO HERO FEDERICO ZANETELLO Μ£Μ£Μ£Μ£Μ£ fivestars.blog β€’ @zntfdr