-
Notifications
You must be signed in to change notification settings - Fork 199
Expand file tree
/
Copy pathModule.swift
More file actions
166 lines (150 loc) · 5.64 KB
/
Copy pathModule.swift
File metadata and controls
166 lines (150 loc) · 5.64 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import Foundation
import FileKit
final class Module {
static var overriddenOutput: String?
let name: String
let imports: [String]
let testableImports: [String]
let sources: [Path]?
let exclude: [String]
let regex: String?
let output: String
let filenameFormat: String?
let options: Options
let xcodeproj: Xcodeproj?
init(name: String, output: String?, configurationPath: Path, dto: DTO) throws {
guard Module.overriddenOutput != nil || output != nil || dto.output != nil else {
throw ModuleInitError.missingOutput(name: name)
}
self.name = name
self.imports = dto.imports?.map(\.trimmed) ?? []
self.testableImports = dto.testableImports?.map(\.trimmed) ?? []
self.sources = dto.sources?.map(\.trimmed).map { source in
Path(source, expandingTilde: true).relative(to: configurationPath.parent)
}
self.exclude = dto.exclude?.map(\.trimmed) ?? []
self.regex = dto.regex
self.output = (Module.overriddenOutput ?? dto.output ?? output)!
self.filenameFormat = dto.filenameFormat
self.options = Options(
glob: dto.options?.glob ?? true,
keepDocumentation: dto.options?.keepDocumentation ?? true,
enableInheritance: dto.options?.enableInheritance ?? true,
protocolsOnly: dto.options?.protocolsOnly ?? false,
omitHeaders: dto.options?.omitHeaders ?? false,
filePrefix: dto.options?.filePrefix?.joined(separator: "\n").appending("\n") ?? "",
fileSuffix: dto.options?.fileSuffix?.joined(separator: "\n").appending("\n") ?? ""
)
if let xcodeproj = dto.xcodeproj {
if let target = xcodeproj.target {
self.xcodeproj = Xcodeproj(
path: xcodeproj.path.map {
Path($0, expandingTilde: true).relative(to: configurationPath.parent)
} ?? configurationPath.parent,
target: target
)
} else {
throw ModuleInitError.missingXcodeprojTarget
}
} else {
xcodeproj = nil
}
if let filenameFormat, !filenameFormat.contains("{}") {
throw ModuleInitError.wrongFilenameFormat
}
}
struct Options {
let glob: Bool
let keepDocumentation: Bool
let enableInheritance: Bool
let protocolsOnly: Bool
let omitHeaders: Bool
let filePrefix: String
let fileSuffix: String
}
struct Xcodeproj {
let path: Path
let target: String
}
enum ModuleInitError: Error, CustomStringConvertible {
case missingOutput(name: String)
case wrongFilenameFormat
case missingXcodeprojTarget
var description: String {
switch self {
case .missingOutput(let moduleName):
return "Missing `output` parameter in module \(moduleName). Either define a global `output` or add `output` to the module."
case .wrongFilenameFormat:
return "Filename format must contain \"{}\" to denote where to put the base filename."
case .missingXcodeprojTarget:
return "xcodeproj.target is required"
}
}
}
}
extension Module {
struct DTO: Decodable {
let imports: [String]?
let testableImports: [String]?
let sources: [String]?
let exclude: [String]?
let regex: String?
let output: String?
let filenameFormat: String?
let options: Options?
let xcodeproj: Xcodeproj?
struct Options: Decodable {
let glob: Bool?
let keepDocumentation: Bool?
let enableInheritance: Bool?
let protocolsOnly: Bool?
let omitHeaders: Bool?
let filePrefix: [String]?
let fileSuffix: [String]?
}
struct Xcodeproj: Decodable {
let path: String?
let target: String?
}
}
}
extension Module: CustomDebugStringConvertible {
var debugDescription: String {
[
"imports:\(imports.map { "\n\t-\($0.bold)" }.joined(separator: ", "))",
"testable imports:\(testableImports.map { "\n\t-\($0.bold)" }.joined(separator: ", "))",
sources.map { "sources:\($0.map { "\n\t-\($0.rawValue.bold)" }.joined())" },
"excluded types:\(exclude.map { "\n\t-\($0.bold)" }.joined())",
regex.map { "regex: \($0.bold)" },
"output: \(output.bold)",
filenameFormat.map { "filename format: \($0.bold)" },
"options:\(options.debugDescription.components(separatedBy: "\n").map { "\n\t- \($0)" }.joined())",
xcodeproj.map { "xcodeproj:\($0.debugDescription.components(separatedBy: "\n").map { "\n\t- \($0)" }.joined())" },
]
.compactMap { $0 }
.joined(separator: "\n")
}
}
extension Module.Options: CustomDebugStringConvertible {
var debugDescription: String {
[
"glob: \(String(glob).bold)",
"keep documentation: \(String(keepDocumentation).bold)",
"enable inheritance: \(String(enableInheritance).bold)",
"protocols only: \(String(protocolsOnly).bold)",
"omit headers: \(String(omitHeaders).bold)",
]
.compactMap { $0 }
.joined(separator: "\n")
}
}
extension Module.Xcodeproj: CustomDebugStringConvertible {
var debugDescription: String {
[
"path: \(path.rawValue.bold)",
"target: \(target.bold)",
]
.compactMap { $0 }
.joined(separator: "\n")
}
}