forked from mxcl/swift-sh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.swift
43 lines (32 loc) · 1.17 KB
/
main.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import Foundation
import Shwifty
//TODO
// should we update packages? maybe in background when running scripts
guard ProcessInfo.processInfo.arguments.count == 2 else {
fputs("Insufficient arguments", stderr)
exit(1)
}
let scriptPath = ProcessInfo.processInfo.arguments[1]
let name = Path.root.join(scriptPath).basename(dropExtension: true)
guard let reader = StreamReader(path: scriptPath) else {
fputs("Could not open \(scriptPath)", stderr)
exit(2)
}
var tee = [String]()
var deps = [(String, Constraint)]()
// We are not a thorough parser, and that would be inefficient.
// Since any line starting with import that is not in a comment
// must be at file-scope or it is invalid Swift we just look
// for that
//TODO if we are inside a comment block, know that, and wait for
// end of comment block.
for (index, line) in reader.enumerated() {
if index == 0, line.hasPrefix("#!") { continue }
let trimmed = line.trimmingCharacters(in: .whitespaces)
if trimmed.hasPrefix("import"), let parse = Shwifty.parse(trimmed) {
deps.append(parse)
}
tee.append(line)
}
let script = Script(name: name, contents: tee, dependencies: deps)
try script.run()