Skip to content

Commit e52fd84

Browse files
committed
Pretty up generate-package-api-docs.swift a bit
1 parent e203a01 commit e52fd84

File tree

1 file changed

+27
-8
lines changed

1 file changed

+27
-8
lines changed

generate-package-api-docs.swift

+27-8
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ let moduleList = CommandLine.arguments[2]
1010

1111
let modules = moduleList.components(separatedBy: ",")
1212

13-
let currentDirectoryUrl = URL(fileURLWithPath: FileManager.default.currentDirectoryPath, isDirectory: true)
14-
let publicDirectoryUrl = currentDirectoryUrl.appendingPathComponent("public", isDirectory: true)
13+
let publicDirectoryUrl = URL.currentDirectory().appending(component: "public/")
1514

1615
try run()
1716

@@ -34,7 +33,7 @@ func run() throws {
3433
}
3534

3635
func ensurePluginAvailable() throws {
37-
let manifestUrl = currentDirectoryUrl.appendingPathComponent("Package.swift", isDirectory: false)
36+
let manifestUrl = URL.currentDirectory().appending(component: "Package.swift")
3837
var manifestContents = try String(contentsOf: manifestUrl, encoding: .utf8)
3938
if !manifestContents.contains(".package(url: \"https://github.com/apple/swift-docc-plugin") {
4039
// This is freely admitted to be quick and dirty. When SE-0301 gets into a release, we can use that.
@@ -54,7 +53,7 @@ func ensurePluginAvailable() throws {
5453
func generateDocs(module: String) throws {
5554
print("🔎 Finding DocC catalog")
5655
let doccCatalogs = try FileManager.default.contentsOfDirectory(
57-
at: currentDirectoryUrl.appendingPathComponent("Sources", isDirectory: true).appendingPathComponent(module, isDirectory: true),
56+
at: URL.currentDirectory().appending(components: "Sources", "\(module)/"),
5857
includingPropertiesForKeys: nil,
5958
options: [.skipsSubdirectoryDescendants]
6059
).filter { $0.hasDirectoryPath && $0.pathExtension == "docc" }
@@ -72,8 +71,8 @@ func generateDocs(module: String) throws {
7271
print("📐 Copying theme")
7372
do {
7473
try FileManager.default.copyItemIfExistsWithoutOverwrite(
75-
at: currentDirectoryUrl.appendingPathComponent("theme-settings.json", isDirectory: false),
76-
to: doccCatalogUrl.appendingPathComponent("theme-settings.json", isDirectory: false)
74+
at: URL.currentDirectory().appending(component: "theme-settings.json"),
75+
to: doccCatalogUrl.appending(component: "theme-settings.json")
7776
)
7877
} catch CocoaError.fileReadNoSuchFile, CocoaError.fileWriteFileExists {
7978
// ignore
@@ -92,7 +91,7 @@ func generateDocs(module: String) throws {
9291
"--fallback-bundle-version", "1.0.0",
9392
"--transform-for-static-hosting",
9493
"--hosting-base-path", "/\(module.lowercased())",
95-
"--output-path", publicDirectoryUrl.appendingPathComponent(module.lowercased(), isDirectory: true).path,
94+
"--output-path", publicDirectoryUrl.appending(component: "\(module.lowercased())/").path,
9695
])
9796
}
9897

@@ -111,7 +110,7 @@ func shell(_ args: [String]) throws {
111110
}
112111

113112
// Run the command:
114-
let task = try Process.run(URL(fileURLWithPath: "/usr/bin/env", isDirectory: false), arguments: args)
113+
let task = try Process.run(URL(filePath: "/usr/bin/env"), arguments: args)
115114
task.waitUntilExit()
116115
guard task.terminationStatus == 0 else {
117116
throw ShellError(terminationStatus: task.terminationStatus)
@@ -141,3 +140,23 @@ extension FileManager {
141140
}
142141
}
143142
}
143+
144+
#if !canImport(Darwin)
145+
extension URL {
146+
public enum DirectoryHint: Equatable { case isDirectory, notDirectory, inferFromPath }
147+
static func isDirFlag(_ path: some StringProtocol, _ hint: DirectoryHint) -> Bool {
148+
hint == .inferFromPath ? path.last == "/" : hint == .isDirectory
149+
}
150+
public init(filePath: String, directoryHint hint: DirectoryHint = .inferFromPath, relativeTo base: URL? = nil) {
151+
self = URL(fileURLWithPath: filePath, isDirectory: Self.isDirFlag(path, hint), relativeTo: base)
152+
}
153+
public func appending(component: some StringProtocol, directoryHint hint: DirectoryHint = .inferFromPath) -> URL {
154+
self.appendingPathComponent(component, isDirectory: Self.isDirFlag(component, hint))
155+
}
156+
public func appending(components: (some StringProtocol)..., directoryHint hint: DirectoryHint = .inferFromPath) -> URL {
157+
components.dropLast().reduce(self) { $0.appending(component: $1, directoryHint: .isDirectory) }
158+
.appending(component: components.last!, directoryHint: hint)
159+
}
160+
public static func currentDirectory() -> URL { .init(filePath: FileManager.default.currentDirectoryPath, directoryHint: .isDirectory) }
161+
}
162+
#endif

0 commit comments

Comments
 (0)