|
1 |
| -#if canImport(Darwin) |
2 |
| -@preconcurrency import Darwin |
3 |
| -#elseif canImport(Glibc) |
4 |
| -@preconcurrency import Glibc |
5 |
| -#endif |
6 | 1 |
|
7 | 2 | import ArgumentParser
|
8 |
| -import HTTPClient |
9 |
| -import NIOCore |
10 |
| -import NIOSSL |
11 |
| -import Symbols |
12 |
| -import SystemIO |
13 | 3 | import SymbolGraphBuilder
|
14 |
| -import UnidocClient |
15 | 4 |
|
16 |
| -struct Main |
17 |
| -{ |
18 |
| - @Option( |
19 |
| - name: [.customLong("swiftinit"), .customShort("S")], |
20 |
| - help: """ |
21 |
| - The API key for the Unidoc server running on swiftinit.org, \ |
22 |
| - also sets the host and port to the production values |
23 |
| - """) |
24 |
| - var authorizationSwiftinit:String? |
25 |
| - |
26 |
| - @Option( |
27 |
| - name: [.customLong("authorization"), .customShort("i")], |
28 |
| - help: "The API key for the Unidoc server") |
29 |
| - var authorization:String? |
30 |
| - |
31 |
| - @Option( |
32 |
| - name: [.customLong("host"), .customShort("h")], |
33 |
| - help: "The name of a host running a compatible instance of unidoc") |
34 |
| - var host:String = "localhost" |
35 |
| - |
36 |
| - @Option( |
37 |
| - name: [.customLong("port"), .customShort("p")], |
38 |
| - help: "The number of a port bound to a compatible instance of unidoc") |
39 |
| - var port:Int = 8443 |
40 |
| - |
41 |
| - @Option( |
42 |
| - name: [.customLong("swift-toolchain"), .customShort("u")], |
43 |
| - help: "The path to a Swift toolchain directory, usually ending in 'usr'", |
44 |
| - completion: .directory) |
45 |
| - var toolchain:FilePath.Directory = "/home/ubuntu/6.1.2/aarch64/usr" |
46 |
| - |
47 |
| - @Option( |
48 |
| - name: [.customLong("swift-sdk"), .customShort("k")], |
49 |
| - help: "The Swift SDK to use") |
50 |
| - var sdk:SSGC.AppleSDK? |
51 |
| - |
52 |
| - @Flag( |
53 |
| - name: [.customLong("pretty"), .customShort("o")], |
54 |
| - help: "Tell lib/SymbolGraphGen to pretty-print the JSON output, if possible") |
55 |
| - var pretty:Bool = false |
56 |
| - |
57 |
| - @Flag( |
58 |
| - name: [.customLong("init-stdlib")], |
59 |
| - help: "Generate and upload the standard library documentation") |
60 |
| - var initStandardLibrary:Bool = false |
61 |
| -} |
62 |
| -extension Main |
63 |
| -{ |
64 |
| - private mutating |
65 |
| - func normalize() |
66 |
| - { |
67 |
| - if let authorization:String = self.authorizationSwiftinit, |
68 |
| - case nil = self.authorization |
69 |
| - { |
70 |
| - self.authorization = authorization |
71 |
| - self.host = "swiftinit.org" |
72 |
| - self.port = 443 |
73 |
| - } |
74 |
| - |
75 |
| - #if os(macOS) |
76 |
| - |
77 |
| - // Guess the SDK if not specified. |
78 |
| - self.sdk = self.sdk ?? .macOS |
79 |
| - |
80 |
| - #endif |
81 |
| - } |
82 |
| - |
83 |
| - private |
84 |
| - var client:Unidoc.Client<HTTP.Client2> |
85 |
| - { |
86 |
| - get throws |
87 |
| - { |
88 |
| - var configuration:TLSConfiguration = .makeClientConfiguration() |
89 |
| - configuration.applicationProtocols = ["h2"] |
90 |
| - |
91 |
| - // If we are not using the default port, we are probably running locally. |
92 |
| - if self.port != 443 |
93 |
| - { |
94 |
| - configuration.certificateVerification = .none |
95 |
| - } |
96 |
| - |
97 |
| - return .init(authorization: self.authorization, |
98 |
| - pretty: self.pretty, |
99 |
| - http: .init(threads: .singleton, |
100 |
| - niossl: try .init(configuration: configuration), |
101 |
| - remote: self.host), |
102 |
| - port: self.port) |
103 |
| - } |
104 |
| - } |
105 |
| - |
106 |
| - private |
107 |
| - var triple:Symbol.Triple |
108 |
| - { |
109 |
| - get throws |
110 |
| - { |
111 |
| - let tools:SSGC.Toolchain.Paths = .init(swiftPM: nil, usr: self.toolchain) |
112 |
| - let splash:SSGC.Toolchain.Splash = try .init(running: tools.swiftCommand) |
113 |
| - return splash.triple |
114 |
| - } |
115 |
| - } |
116 |
| -} |
117 | 5 | @main
|
118 |
| -extension Main:AsyncParsableCommand |
| 6 | +struct Main:AsyncParsableCommand |
119 | 7 | {
|
120 | 8 | static var configuration:CommandConfiguration
|
121 | 9 | {
|
122 | 10 | .init(commandName: "unidocd",
|
123 |
| - subcommands: [SSGC.BuildCommand.self, SSGC.SlaveCommand.self]) |
124 |
| - } |
125 |
| - |
126 |
| - mutating |
127 |
| - func run() async throws |
128 |
| - { |
129 |
| - self.normalize() |
130 |
| - |
131 |
| - NIOSingletons.groupLoopCountSuggestion = 2 |
132 |
| - setlinebuf(stdout) |
133 |
| - |
134 |
| - let unidoc:Unidoc.Client<HTTP.Client2> = try self.client |
135 |
| - let triple:Symbol.Triple = try self.triple |
136 |
| - let cache:FilePath = "swiftpm" |
137 |
| - |
138 |
| - print("Connecting to \(self.host):\(self.port)...") |
139 |
| - |
140 |
| - if self.initStandardLibrary |
141 |
| - { |
142 |
| - let toolchain:Unidoc.Toolchain = .init(usr: self.toolchain, sdk: self.sdk) |
143 |
| - try await unidoc.buildAndUpload(local: nil, |
144 |
| - name: "swift", |
145 |
| - type: .package, |
146 |
| - with: toolchain) |
147 |
| - } |
148 |
| - |
149 |
| - while true |
150 |
| - { |
151 |
| - // Don’t run too hot if the network is down. |
152 |
| - async |
153 |
| - let cooldown:Void = try await Task.sleep(for: .seconds(5)) |
154 |
| - |
155 |
| - do |
156 |
| - { |
157 |
| - let labels:Unidoc.BuildLabels? = try await unidoc.connect |
158 |
| - { |
159 |
| - try await $0.subscribe(to: triple) |
160 |
| - } |
161 |
| - |
162 |
| - if let labels:Unidoc.BuildLabels |
163 |
| - { |
164 |
| - print(""" |
165 |
| - Building package '\(labels.package)' at '\(labels.ref)' \ |
166 |
| - (\(labels.coordinate)) |
167 |
| - """) |
168 |
| - |
169 |
| - let toolchain:Unidoc.Toolchain = .init(usr: self.toolchain, sdk: self.sdk) |
170 |
| - /// As this runs continuously, we should remove the build artifacts |
171 |
| - /// afterwards, to avoid filling up the disk. We must also remove the cloned |
172 |
| - /// repository, as it may experience name conflicts on long timescales. |
173 |
| - try await unidoc.buildAndUpload( |
174 |
| - labels: labels, |
175 |
| - remove: true, |
176 |
| - with: toolchain, |
177 |
| - cache: cache) |
178 |
| - } |
179 |
| - else |
180 |
| - { |
181 |
| - print("Heartbeat received; no packages to build.") |
182 |
| - } |
183 |
| - } |
184 |
| - catch let error |
185 |
| - { |
186 |
| - print("Error: \(error)") |
187 |
| - } |
188 |
| - |
189 |
| - try await cooldown |
190 |
| - } |
| 11 | + subcommands: [UpCommand.self, SSGC.BuildCommand.self, SSGC.SlaveCommand.self]) |
191 | 12 | }
|
192 | 13 | }
|
0 commit comments