-
-
Notifications
You must be signed in to change notification settings - Fork 76
Expand file tree
/
Copy pathUtilities.swift
More file actions
85 lines (71 loc) · 2.26 KB
/
Copy pathUtilities.swift
File metadata and controls
85 lines (71 loc) · 2.26 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
extension String {
public var capitalizedFirstLetter: String {
guard !isEmpty else { return self }
return prefix(1).uppercased() + dropFirst()
}
}
public enum BridgeJSGeneratedFile {
/// The magic comment to skip processing by BridgeJS.
public static let skipLine = "// bridge-js: skip"
public static func hasSkipComment(_ content: String) -> Bool {
content.starts(with: skipLine + "\n")
}
public static var swiftHeader: String {
// The generated Swift file itself should not be processed by BridgeJS again.
"""
\(skipLine)
// swift-format-ignore-file
// NOTICE: This is auto-generated code by BridgeJS from JavaScriptKit,
// DO NOT EDIT.
//
// To update this file, just rebuild your project or run
// `swift package bridge-js`.
"""
}
public static func swiftImports(_ moduleNames: [String]) -> String {
moduleNames.map { "@_spi(BridgeJS) import \($0)" }.joined(separator: "\n")
}
}
/// A printer for code fragments.
public final class CodeFragmentPrinter {
public private(set) var lines: [String] = []
private var indentLevel: Int = 0
public init(header: String = "") {
self.lines.append(contentsOf: header.split(separator: "\n").map { String($0) })
}
public func nextLine() {
lines.append("")
}
public func write<S: StringProtocol>(_ line: S) {
if line.isEmpty {
// Empty lines should not have trailing spaces
lines.append("")
return
}
lines.append(String(repeating: " ", count: indentLevel * 4) + String(line))
}
public func write(lines: [String]) {
for line in lines {
write(line)
}
}
public func write(contentsOf printer: CodeFragmentPrinter) {
self.write(lines: printer.lines)
}
public func write(multilineString: String) {
for line in multilineString.split(separator: "\n") {
write(line)
}
}
public func indent() {
indentLevel += 1
}
public func unindent() {
indentLevel -= 1
}
public func indent(_ body: () throws -> Void) rethrows {
indentLevel += 1
try body()
indentLevel -= 1
}
}