Skip to content

Commit 07fe07f

Browse files
committed
Update generate-api-docs.swift
1 parent 65c745b commit 07fe07f

File tree

1 file changed

+17
-74
lines changed

1 file changed

+17
-74
lines changed

generate-api-docs.swift

Lines changed: 17 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -39,84 +39,27 @@ let packages: [String: [String]] = [
3939
"apns": ["VaporAPNS"],
4040
]
4141

42-
try shell("rm", "-rf", "public/")
43-
let url = URL(fileURLWithPath: "index.html")
44-
var htmlString = try String(contentsOf: url)
45-
var optionsString = ""
46-
var allModules: [(package: String, module: String)] = []
42+
let htmlMenu = packages.values.flatMap { $0 }
43+
.sorted()
44+
.map { "<option value=\"\($0.lowercased())/documentation/\($0.lowercased())\">\($0)</option>" }
45+
.joined(separator: "\n")
4746

48-
for (package, modules) in packages {
49-
for module in modules {
50-
allModules.append((package: package, module: module))
51-
}
52-
}
53-
54-
let sortedModules = allModules.sorted { $0.module < $1.module }
55-
for object in sortedModules {
56-
let module = object.module
57-
optionsString += "<option value=\"/\(module.lowercased())/documentation/\(module.lowercased())\">\(module)</option>\n"
58-
}
59-
60-
htmlString = htmlString.replacingOccurrences(of: "{{Options}}", with: optionsString)
61-
62-
try shell("mkdir", "public")
63-
try htmlString.write(toFile: "public/index.html", atomically: true, encoding: .utf8)
64-
try shell("cp", "api-docs.png", "public/api-docs.png")
65-
try shell("cp", "error.html", "public/error.html")
66-
67-
// MARK: Functions
68-
@discardableResult
69-
func shell(_ args: String..., returnStdOut: Bool = false, stdIn: Pipe? = nil) throws -> Pipe {
70-
let task = Process()
71-
task.launchPath = "/usr/bin/env"
72-
task.arguments = args
73-
let pipe = Pipe()
74-
if returnStdOut {
75-
task.standardOutput = pipe
76-
}
77-
if let stdIn = stdIn {
78-
task.standardInput = stdIn
79-
}
80-
try task.run()
81-
task.waitUntilExit()
82-
guard task.terminationStatus == 0 else {
83-
throw ShellError(terminationStatus: task.terminationStatus)
84-
}
85-
return pipe
86-
}
47+
let publicDirUrl = URL(fileURLWithPath: "./public", isDirectory: true)
48+
try FileManager.default.removeItem(at: publicDirUrl)
49+
try FileManager.default.createDirectory(at: publicDirUrl, withIntermediateDirectories: true)
8750

88-
struct ShellError: Error {
89-
var terminationStatus: Int32
90-
}
51+
var htmlIndex = try String(contentsOf: URL(fileURLWithPath: "./index.html", isDirectory: false), encoding: .utf8)
52+
htmlIndex.replace("{{Options}}", with: "\(htmlMenu)\n", maxReplacements: 1)
9153

92-
extension Pipe {
93-
func string() throws -> String? {
94-
let data = try self.fileHandleForReading.readToEnd()!
95-
let result: String?
96-
if let string = String(
97-
data: data,
98-
encoding: String.Encoding.utf8
99-
) {
100-
result = string
101-
} else {
102-
result = nil
103-
}
104-
return result
105-
}
106-
}
54+
try htmlIndex.write(to: publicDirUrl.appendingPathComponent("index.html", isDirectory: false), atomically: true, encoding: .utf8)
55+
try FileManager.copyItem(at: URL(fileURLWithPath: "./api-docs.png", isDirectory: false), into: publicDirUrl)
56+
try FileManager.copyItem(at: URL(fileURLWithPath: "./error.html", isDirectory: false), into: publicDirUrl)
10757

10858
extension FileManager {
109-
func copyItemIfPossible(atPath: String, toPath: String) throws {
110-
var isDirectory: ObjCBool = false
111-
guard self.fileExists(
112-
atPath: toPath,
113-
isDirectory: &isDirectory
114-
) == false else {
115-
return
116-
}
117-
return try self.copyItem(
118-
atPath: atPath,
119-
toPath: toPath
120-
)
59+
func copyItem(at src: URL, into dst: URL) throws {
60+
assert(dst.hasDirectoryPath)
61+
62+
let dstItem = dst.appendingPathComponent(src.lastPathComponent, isDirectory: src.hasDirectoryPath)
63+
try self.copyItem(at: src, to: dstItem)
12164
}
12265
}

0 commit comments

Comments
 (0)