-
Notifications
You must be signed in to change notification settings - Fork 226
/
Doc.swift
66 lines (61 loc) · 2.55 KB
/
Doc.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import ArgumentParser
import Foundation
import SourceKittenFramework
extension SourceKitten {
struct Doc: ParsableCommand {
static let configuration = CommandConfiguration(abstract: "Print Swift or Objective-C docs as JSON")
@Flag(help: "Only document one file")
var singleFile: Bool = false
@Option(help: "Name of Swift module to document (can't be used with `--single-file`)")
var moduleName: String = ""
@Flag(help: "Document a Swift Package Manager module")
var spm: Bool = false
@Flag(help: "Document Objective-C headers instead of Swift code")
var objc: Bool = false
@Argument(help: "Arguments passed to `xcodebuild` or `swift build`")
var arguments: [String] = []
mutating func run() throws {
let moduleName = self.moduleName.isEmpty ? nil : self.moduleName
if spm {
if let docs = Module(spmArguments: arguments, spmName: moduleName)?.docs {
print(docs)
return
}
throw SourceKittenError.docFailed
} else if objc {
#if os(Linux)
fatalError("unsupported")
#else
if arguments.isEmpty {
throw SourceKittenError.invalidArgument(
description: "at least 5 arguments are required when using `--objc`"
)
}
let translationUnit = ClangTranslationUnit(headerFiles: [arguments[0]],
compilerArguments: Array(arguments.dropFirst(1)))
print(translationUnit)
return
#endif
} else if singleFile {
if arguments.isEmpty {
throw SourceKittenError.invalidArgument(
description: "at least 5 arguments are required when using `--single-file`"
)
}
let sourcekitdArguments = Array(arguments.dropFirst(1))
if let file = File(path: arguments[0]),
let docs = SwiftDocs(file: file, arguments: sourcekitdArguments) {
print(docs)
return
}
throw SourceKittenError.readFailed(path: arguments[0])
}
let module = Module(xcodeBuildArguments: arguments, name: moduleName)
if let docs = module?.docs {
print(docs)
return
}
throw SourceKittenError.docFailed
}
}
}