Skip to content

Commit 54be294

Browse files
committed
1 parent e52fd84 commit 54be294

File tree

1 file changed

+20
-11
lines changed

1 file changed

+20
-11
lines changed

generate-package-api-docs.swift

+20-11
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,10 @@ func generateDocs(module: String) throws {
6969
print("🗂️ Using DocC catalog \(doccCatalogUrl.lastPathComponent)")
7070

7171
print("📐 Copying theme")
72-
do {
73-
try FileManager.default.copyItemIfExistsWithoutOverwrite(
74-
at: URL.currentDirectory().appending(component: "theme-settings.json"),
75-
to: doccCatalogUrl.appending(component: "theme-settings.json")
76-
)
77-
} catch CocoaError.fileReadNoSuchFile, CocoaError.fileWriteFileExists {
78-
// ignore
79-
}
72+
try FileManager.default.copyItemIfExistsWithoutOverwrite(
73+
at: URL.currentDirectory().appending(component: "theme-settings.json"),
74+
to: doccCatalogUrl.appending(component: "theme-settings.json")
75+
)
8076

8177
print("📝 Generating docs")
8278
try shell([
@@ -125,22 +121,35 @@ extension FileManager {
125121
func removeItemIfExists(at url: URL) throws {
126122
do {
127123
try self.removeItem(at: url)
128-
} catch let error as NSError where error.domain == CocoaError.errorDomain && error.code == CocoaError.fileNoSuchFile.rawValue {
124+
} catch let error as NSError where error.isCocoaError(.fileNoSuchFile) {
129125
// ignore
130126
}
131127
}
132128

133129
func copyItemIfExistsWithoutOverwrite(at src: URL, to dst: URL) throws {
134130
do {
131+
// https://github.com/apple/swift-corelibs-foundation/pull/4808
132+
#if !canImport(Darwin)
133+
do {
134+
_ = try dst.checkResourceIsReachable()
135+
throw NSError(domain: CocoaError.errorDomain, code: CocoaError.fileWriteFileExists.rawValue)
136+
} catch let error as NSError where error.isCocoaError(.fileReadNoSuchFile) {}
137+
#endif
135138
try self.copyItem(at: src, to: dst)
136-
} catch let error as NSError where error.domain == CocoaError.errorDomain && error.code == CocoaError.fileReadNoSuchFile.rawValue {
139+
} catch let error as NSError where error.isCocoaError(.fileReadNoSuchFile) {
137140
// ignore
138-
} catch let error as NSError where error.domain == CocoaError.errorDomain && error.code == CocoaError.fileWriteFileExists.rawValue {
141+
} catch let error as NSError where error.isCocoaError(.fileWriteFileExists) {
139142
// ignore
140143
}
141144
}
142145
}
143146

147+
extension NSError {
148+
func isCocoaError(_ code: CocoaError.Code) -> Bool {
149+
self.domain == CocoaError.errorDomain && self.code == code.rawValue
150+
}
151+
}
152+
144153
#if !canImport(Darwin)
145154
extension URL {
146155
public enum DirectoryHint: Equatable { case isDirectory, notDirectory, inferFromPath }

0 commit comments

Comments
 (0)