Swift Script

今天在 FB 湊巧看到有人 PO Swift Script 這篇。

稍微玩了一下~~。

使用 Swift Script

方法1

xcrun swift hello.swift

方法2

chmod +x hello.swift

hello.swift
1
2
#!/usr/bin/env xcrun swift
print("hello")

./hello.swift

搭配 Carthage

cartfile
1
github "jdhealy/PrettyColors"

carthage update

swift hello.swift -F FRAMEWORK_LOACTION

其他 Swift CLI Framework

當天 Talk Repo


Swift for CLI tools

介紹蠻仔細的簡報

Compile & run

1
2
3
swiftc hello.swift -o hello
chomd +x hello
./hello

Caveats

arg

Process.arguments

Status Code

exit(0) // 0

Option parsing

IO

Spawning

git status | slug | say

Reading / Writing
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import IO

STDIN.eachLine { line in
print("printing: \(line)")
}

file.eachLine { line in
print("printing: \(line)")
}

readLines(stdin) { line in
print("printing: \(line)")
}

readLines(file) { line in
print("printing: \(line)")
}
File
1
2
3
File.join(Dir.home. "code") // ~/code

File.expand_path("~/code/file.rb") // /Users/supermarin/code/file.rb
File Structure
1
2
3
4
└── slug
├── bin/
├── lib/
└── test/
Test

swift test


Swift - Reading and Writing Files(stdio(read), String extension(split method, strip))

Shebang (Unix) wiki