@@ -4,130 +4,60 @@ import SwiftDoc
4
4
import SwiftMarkup
5
5
import SwiftSemantics
6
6
import struct SwiftSemantics. Protocol
7
+ import SQLite
7
8
8
9
extension SwiftDoc {
9
- struct Generate : ParsableCommand {
10
- enum Format : String , ExpressibleByArgument {
11
- case commonmark
12
- case html
13
- }
14
-
15
- struct Options : ParsableArguments {
16
- @Argument ( help: " One or more paths to Swift files " )
17
- var inputs : [ String ]
18
-
19
- @Option ( name: [ . long, . customShort( " n " ) ] ,
20
- help: " The name of the module " )
21
- var moduleName : String
22
-
23
- @Option ( name: . shortAndLong,
24
- default: " .build/documentation " ,
25
- help: " The path for generated output " )
26
- var output : String
27
-
28
- @Option ( name: . shortAndLong,
29
- default: . commonmark,
30
- help: " The output format " )
31
- var format : Format
32
-
33
- @Option ( name: . customLong( " base-url " ) ,
34
- default: " / " ,
35
- help: " The base URL used for all relative URLs in generated documents. " )
36
- var baseURL : String
37
- }
38
-
39
- static var configuration = CommandConfiguration ( abstract: " Generates Swift documentation " )
40
-
41
- @OptionGroup ( )
42
- var options : Options
43
-
44
- func run( ) throws {
45
- let module = try Module ( name: options. moduleName, paths: options. inputs)
46
-
47
- let outputDirectoryURL = URL ( fileURLWithPath: options. output)
48
- try fileManager. createDirectory ( at: outputDirectoryURL, withIntermediateDirectories: true , attributes: fileAttributes)
49
-
50
- do {
51
- let format = options. format
52
-
53
- var pages : [ String : Page ] = [ : ]
54
-
55
- var globals : [ String : [ Symbol ] ] = [ : ]
56
- for symbol in module. interface. topLevelSymbols. filter ( { $0. isPublic } ) {
57
- switch symbol. api {
58
- case is Class , is Enumeration , is Structure , is Protocol :
59
- pages [ path ( for: symbol) ] = TypePage ( module: module, symbol: symbol)
60
- case let `typealias` as Typealias :
61
- pages [ path ( for: `typealias`. name) ] = TypealiasPage ( module: module, symbol: symbol)
62
- case let function as Function where !function. isOperator:
63
- globals [ function. name, default: [ ] ] += [ symbol]
64
- case let variable as Variable :
65
- globals [ variable. name, default: [ ] ] += [ symbol]
66
- default :
67
- continue
68
- }
10
+ struct Generate : ParsableCommand {
11
+ enum Format : String , ExpressibleByArgument {
12
+ case commonmark
13
+ case html
14
+ case docset
69
15
}
70
16
71
- for (name , symbols ) in globals {
72
- pages [ path ( for : name ) ] = GlobalPage ( module : module , name : name , symbols : symbols )
73
- }
17
+ struct Options : ParsableArguments {
18
+ @ Argument ( help : " One or more paths to Swift files " )
19
+ var inputs : [ String ]
74
20
75
- guard !pages. isEmpty else {
76
- logger. warning ( " No public API symbols were found at the specified path. No output was written. " )
77
- return
78
- }
21
+ @Option ( name: [ . long, . customShort( " n " ) ] ,
22
+ help: " The name of the module " )
23
+ var moduleName : String
79
24
80
- if pages. count == 1 , let page = pages. first? . value {
81
- let filename : String
82
- switch format {
83
- case . commonmark:
84
- filename = " Home.md "
85
- case . html:
86
- filename = " index.html "
87
- }
25
+ @Option ( name: . shortAndLong,
26
+ default: " .build/documentation " ,
27
+ help: " The path for generated output " )
28
+ var output : String
88
29
89
- let url = outputDirectoryURL. appendingPathComponent ( filename)
90
- try page. write ( to: url, format: format, baseURL: options. baseURL)
91
- } else {
92
- switch format {
93
- case . commonmark:
94
- pages [ " Home " ] = HomePage ( module: module)
95
- pages [ " _Sidebar " ] = SidebarPage ( module: module)
96
- pages [ " _Footer " ] = FooterPage ( )
97
- case . html:
98
- pages [ " Home " ] = HomePage ( module: module)
99
- }
30
+ @Option ( name: . shortAndLong,
31
+ default: . commonmark,
32
+ help: " The output format " )
33
+ var format : Format
100
34
101
- try pages. map { $0 } . parallelForEach {
102
- let filename : String
103
- switch format {
104
- case . commonmark:
105
- filename = " \( $0. key) .md "
106
- case . html where $0. key == " Home " :
107
- filename = " index.html "
108
- case . html:
109
- filename = " \( $0. key) /index.html "
110
- }
111
-
112
- let url = outputDirectoryURL. appendingPathComponent ( filename)
113
- try $0. value. write ( to: url, format: format, baseURL: options. baseURL)
114
- }
35
+ @Option ( name: . customLong( " base-url " ) ,
36
+ default: " / " ,
37
+ help: " The base URL used for all relative URLs in generated documents. " )
38
+ var baseURL : String
115
39
}
116
40
117
- if case . html = format {
118
- let cssData = try fetchRemoteCSS ( )
119
- let cssURL = outputDirectoryURL. appendingPathComponent ( " all.css " )
120
- try writeFile ( cssData, to: cssURL)
41
+ static var configuration = CommandConfiguration ( abstract: " Generates Swift documentation " )
42
+
43
+ @OptionGroup ( )
44
+ var options : Options
45
+
46
+ func run( ) throws {
47
+ do {
48
+ let module = try Module ( name: options. moduleName, paths: options. inputs)
49
+
50
+ switch options. format {
51
+ case . commonmark:
52
+ try CommonMarkGenerator . generate ( for: module, with: options)
53
+ case . html:
54
+ try HTMLGenerator . generate ( for: module, with: options)
55
+ case . docset:
56
+ try DocSetGenerator . generate ( for: module, with: options)
57
+ }
58
+ } catch {
59
+ logger. error ( " \( error) " )
60
+ }
121
61
}
122
-
123
- } catch {
124
- logger. error ( " \( error) " )
125
- }
126
62
}
127
- }
128
- }
129
-
130
- func fetchRemoteCSS( ) throws -> Data {
131
- let url = URL ( string: " https://raw.githubusercontent.com/SwiftDocOrg/swift-doc/master/Resources/all.min.css " ) !
132
- return try Data ( contentsOf: url)
133
63
}
0 commit comments