Skip to content

Commit 75cebb9

Browse files
authored
Merge pull request #2 from minuscorp/module-interface
Instant module interface generation
2 parents d7ea6a3 + c8091ab commit 75cebb9

File tree

10 files changed

+6874
-2499
lines changed

10 files changed

+6874
-2499
lines changed

Examples/Commandant.swift

Lines changed: 312 additions & 77 deletions
Large diffs are not rendered by default.

Examples/Mini.swift

Lines changed: 398 additions & 231 deletions
Large diffs are not rendered by default.

Examples/SourceKittenFramework.swift

Lines changed: 1233 additions & 324 deletions
Large diffs are not rendered by default.

Examples/SwiftFormat.swift

Lines changed: 1182 additions & 0 deletions
Large diffs are not rendered by default.

Examples/SwiftLintFramework.swift

Lines changed: 2418 additions & 1457 deletions
Large diffs are not rendered by default.

Examples/Yams.swift

Lines changed: 1301 additions & 393 deletions
Large diffs are not rendered by default.

Sources/ModuleInterface/Classes/AccessLevel.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,10 @@ enum AccessLevel: String, CaseIterable {
5858

5959
extension AccessLevel: Comparable, Equatable {
6060
static func < (lhs: AccessLevel, rhs: AccessLevel) -> Bool {
61-
return lhs.priority < rhs.priority
61+
lhs.priority < rhs.priority
6262
}
6363

6464
static func == (lhs: AccessLevel, rhs: AccessLevel) -> Bool {
65-
return lhs.priority == rhs.priority
65+
lhs.priority == rhs.priority
6666
}
6767
}

Sources/ModuleInterface/Classes/SwiftDoc.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ typealias SwiftDoc = [String: Any]
2121

2222
extension Dictionary where Key == String, Value == Any {
2323
func get<T>(_ key: SwiftDocKey) -> T? {
24-
return self[key.rawValue] as? T
24+
self[key.rawValue] as? T
2525
}
2626

2727
var accessLevel: AccessLevel {

Sources/ModuleInterface/Commands/Clean.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ struct CleanCommandOptions: OptionsProtocol {
2424
let moduleName: String
2525

2626
static func evaluate(_ mode: CommandMode) -> Result<CleanCommandOptions, CommandantError<ModuleInterface.Error>> {
27-
return curry(self.init)
27+
curry(self.init)
2828
<*> mode <| Option(key: "output-folder", defaultValue: ModuleInterface.defaultOutputPath, usage: "Output directory (defaults to \(ModuleInterface.defaultOutputPath)).")
2929
<*> mode <| Argument(usage: "The module's interface name to clean.")
3030
}

Sources/ModuleInterface/Commands/Generate.swift

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -82,43 +82,56 @@ struct GenerateCommand: CommandProtocol {
8282
}
8383
}
8484

85-
private func parseSPMModule(moduleName: String) throws -> [SwiftDocs] {
86-
guard let docs = Module(spmName: moduleName)?.docs else {
85+
private func interfaceForModule(_ module: String, compilerArguments: [String]) throws -> [String: SourceKitRepresentable] {
86+
try Request.customRequest(request: [
87+
"key.request": UID("source.request.editor.open.interface"),
88+
"key.name": NSUUID().uuidString,
89+
"key.compilerargs": compilerArguments,
90+
"key.modulename": "\(module)",
91+
]).send()
92+
}
93+
94+
private func parseSPMModule(moduleName: String) throws -> [String: SourceKitRepresentable] {
95+
guard let module = Module(spmName: moduleName) else {
8796
let message = "Error: Failed to generate documentation for SPM module '\(moduleName)'."
8897
throw ModuleInterface.Error.default(message)
8998
}
90-
return docs
99+
return try interfaceForModule(module.name, compilerArguments: module.compilerArguments)
91100
}
92101

93-
private func parseSwiftModule(moduleName: String, args: [String], path: String) throws -> [SwiftDocs] {
94-
guard let docs = Module(xcodeBuildArguments: args, name: moduleName, inPath: path)?.docs else {
102+
private func parseSwiftModule(moduleName: String, args: [String], path: String) throws -> [String: SourceKitRepresentable] {
103+
guard let module = Module(xcodeBuildArguments: args, name: moduleName, inPath: path) else {
95104
let message = "Error: Failed to generate documentation for module '\(moduleName)'."
96105
throw ModuleInterface.Error.default(message)
97106
}
98-
return docs
107+
return try interfaceForModule(module.name, compilerArguments: module.compilerArguments)
99108
}
100109

101-
private func parseXcodeProject(args: [String], inputPath: String) throws -> [SwiftDocs] {
102-
guard let docs = Module(xcodeBuildArguments: args, name: nil, inPath: inputPath)?.docs else {
110+
private func parseXcodeProject(args: [String], inputPath: String) throws -> [String: SourceKitRepresentable] {
111+
guard let module = Module(xcodeBuildArguments: args, name: nil, inPath: inputPath) else {
103112
throw ModuleInterface.Error.default("Error: Failed to generate documentation.")
104113
}
105-
return docs
114+
return try interfaceForModule(module.name, compilerArguments: module.compilerArguments)
106115
}
107116

108-
private func generateDocumentation(docs: [SwiftDocs], options: GenerateCommandOptions, module: String) throws {
117+
private func generateDocumentation(docs: [String: SourceKitRepresentable], options: GenerateCommandOptions, module: String) throws {
109118
if options.clean {
110119
try CleanCommand.removeModuleInterface(path: options.outputFolder, module: module)
111120
}
112-
let output = process(docs: docs, options: options)
121+
guard let sourcetext = docs["key.sourcetext"] as? String else {
122+
let message = "Unable to parse module named \(module)"
123+
throw ModuleInterface.Error.default(message)
124+
}
125+
let output = sourcetext
113126
try createDirectory(path: options.outputFolder)
114127
try write(to: options.outputFolder, module: module, documentation: output)
115128
}
116129

117-
private func write(to path: String, module: String, documentation: [String]) throws {
130+
private func write(to path: String, module: String, documentation: String) throws {
118131
fputs("Generating Module Interface...\n".green, stdout)
119132
let fileName = module.isEmpty ? "Unknown" : module
120133
let fileExtension = ".swift"
121-
let contents = documentation.joined(separator: "\n")
134+
let contents = documentation
122135
try contents.write(toFile: path + "/" + fileName + fileExtension, atomically: true, encoding: .utf8)
123136
fputs("Fomatting...\n".green, stdout)
124137
let formatted = try format(contents)

0 commit comments

Comments
 (0)