|
1 | 1 | import ArgumentParser
|
2 | 2 | import Foundation
|
| 3 | +import SwiftDoc |
| 4 | +import SwiftMarkup |
3 | 5 | import SwiftSemantics
|
4 | 6 | import struct SwiftSemantics.Protocol
|
5 |
| -import SwiftMarkup |
6 |
| -import SwiftDoc |
7 | 7 |
|
8 | 8 | extension SwiftDoc {
|
9 |
| - struct Generate: ParsableCommand { |
10 |
| - enum Format: String, ExpressibleByArgument { |
11 |
| - case commonmark |
12 |
| - case html |
13 |
| - } |
| 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 |
14 | 32 |
|
15 |
| - struct Options: ParsableArguments { |
16 |
| - @Argument(help: "One or more paths to Swift files") |
17 |
| - var inputs: [String] |
| 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) |
18 | 49 |
|
19 |
| - @Option(name: [.long, .customShort("n")], |
20 |
| - help: "The name of the module") |
21 |
| - var moduleName: String |
| 50 | + do { |
| 51 | + let format = options.format |
22 | 52 |
|
23 |
| - @Option(name: .shortAndLong, |
24 |
| - default: ".build/documentation", |
25 |
| - help: "The path for generated output") |
26 |
| - var output: String |
| 53 | + var pages: [String: Page] = [:] |
27 | 54 |
|
28 |
| - @Option(name: .shortAndLong, |
29 |
| - default: .commonmark, |
30 |
| - help: "The output format") |
31 |
| - var format: Format |
| 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 | + } |
| 69 | + } |
32 | 70 |
|
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 |
| 71 | + for (name, symbols) in globals { |
| 72 | + pages[path(for: name)] = GlobalPage(module: module, name: name, symbols: symbols) |
37 | 73 | }
|
38 | 74 |
|
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 |
| - } |
69 |
| - } |
70 |
| - |
71 |
| - for (name, symbols) in globals { |
72 |
| - pages[path(for: name)] = GlobalPage(module: module, name: name, symbols: symbols) |
73 |
| - } |
74 |
| - |
75 |
| - guard !pages.isEmpty else { return } |
76 |
| - |
77 |
| - if pages.count == 1, let page = pages.first?.value { |
78 |
| - let filename: String |
79 |
| - switch format { |
80 |
| - case .commonmark: |
81 |
| - filename = "Home.md" |
82 |
| - case .html: |
83 |
| - filename = "index.html" |
84 |
| - } |
85 |
| - |
86 |
| - let url = outputDirectoryURL.appendingPathComponent(filename) |
87 |
| - try page.write(to: url, format: format, baseURL: options.baseURL) |
88 |
| - } else { |
89 |
| - switch format { |
90 |
| - case .commonmark: |
91 |
| - pages["Home"] = HomePage(module: module) |
92 |
| - pages["_Sidebar"] = SidebarPage(module: module) |
93 |
| - pages["_Footer"] = FooterPage() |
94 |
| - case .html: |
95 |
| - pages["Home"] = HomePage(module: module) |
96 |
| - } |
97 |
| - |
98 |
| - try pages.map { $0 }.parallelForEach { |
99 |
| - let filename: String |
100 |
| - switch format { |
101 |
| - case .commonmark: |
102 |
| - filename = "\($0.key).md" |
103 |
| - case .html where $0.key == "Home": |
104 |
| - filename = "index.html" |
105 |
| - case .html: |
106 |
| - filename = "\($0.key)/index.html" |
107 |
| - } |
108 |
| - |
109 |
| - let url = outputDirectoryURL.appendingPathComponent(filename) |
110 |
| - try $0.value.write(to: url, format: format, baseURL: options.baseURL) |
111 |
| - } |
112 |
| - } |
113 |
| - |
114 |
| - if case .html = format { |
115 |
| - let cssData = try fetchRemoteCSS() |
116 |
| - let cssURL = outputDirectoryURL.appendingPathComponent("all.css") |
117 |
| - try writeFile(cssData, to: cssURL) |
118 |
| - } |
119 |
| - |
120 |
| - |
121 |
| - } catch { |
122 |
| - logger.error("\(error)") |
| 75 | + guard !pages.isEmpty else { return } |
| 76 | + |
| 77 | + if pages.count == 1, let page = pages.first?.value { |
| 78 | + let filename: String |
| 79 | + switch format { |
| 80 | + case .commonmark: |
| 81 | + filename = "Home.md" |
| 82 | + case .html: |
| 83 | + filename = "index.html" |
| 84 | + } |
| 85 | + |
| 86 | + let url = outputDirectoryURL.appendingPathComponent(filename) |
| 87 | + try page.write(to: url, format: format, baseURL: options.baseURL) |
| 88 | + } else { |
| 89 | + switch format { |
| 90 | + case .commonmark: |
| 91 | + pages["Home"] = HomePage(module: module) |
| 92 | + pages["_Sidebar"] = SidebarPage(module: module) |
| 93 | + pages["_Footer"] = FooterPage() |
| 94 | + case .html: |
| 95 | + pages["Home"] = HomePage(module: module) |
| 96 | + } |
| 97 | + |
| 98 | + try pages.map { $0 }.parallelForEach { |
| 99 | + let filename: String |
| 100 | + switch format { |
| 101 | + case .commonmark: |
| 102 | + filename = "\($0.key).md" |
| 103 | + case .html where $0.key == "Home": |
| 104 | + filename = "index.html" |
| 105 | + case .html: |
| 106 | + filename = "\($0.key)/index.html" |
123 | 107 | }
|
| 108 | + |
| 109 | + let url = outputDirectoryURL.appendingPathComponent(filename) |
| 110 | + try $0.value.write(to: url, format: format, baseURL: options.baseURL) |
| 111 | + } |
124 | 112 | }
|
| 113 | + |
| 114 | + if case .html = format { |
| 115 | + let cssData = try fetchRemoteCSS() |
| 116 | + let cssURL = outputDirectoryURL.appendingPathComponent("all.css") |
| 117 | + try writeFile(cssData, to: cssURL) |
| 118 | + } |
| 119 | + |
| 120 | + } catch { |
| 121 | + logger.error("\(error)") |
| 122 | + } |
125 | 123 | }
|
| 124 | + } |
126 | 125 | }
|
127 | 126 |
|
128 | 127 | func fetchRemoteCSS() throws -> Data {
|
129 |
| - let url = URL(string: "https://raw.githubusercontent.com/SwiftDocOrg/swift-doc/master/Resources/all.css")! |
130 |
| - return try Data(contentsOf: url) |
| 128 | + let url = URL(string: "https://raw.githubusercontent.com/SwiftDocOrg/swift-doc/master/Resources/all.min.css")! |
| 129 | + return try Data(contentsOf: url) |
131 | 130 | }
|
0 commit comments