Skip to content

Commit e4e1d09

Browse files
authored
Pass documentation context its only bundle during initialization (#1057)
* Add ability to initialize a context with a given bundle * Update test to verify bundle discovery options in convert command rather than convert action * Remove unused parameters on DataAssetManager * Minor refinements to bundle loading test helpers * Stop using DocumentationConverter in ConvertService * Update InputsProvider to also return the DataProvider * Update InputsProvider to raise an error when it can't create inputs * Remove `throws` for `renderNode(for:)` that doesn't raise any errors * Stop using DocumentationConverter in ConvertAction * Update tests that relied on ConvertAction doing validation when run instead of when initialized * Support initializing TestFileSystem with CopyOfFolder * Update emit curation action to pass initialize context with bundle * Use top-level protocol to remain compatible with Swift 5.9 * Make `ConvertActionConverter.convert(...)` synchronous for now * Remove outdated comment about optional indexer * Add comment about code that can be simplified after 6.2 is released. * Add assertion about correct identifier use to new provider code paths
1 parent 8e0c017 commit e4e1d09

29 files changed

+894
-722
lines changed

Sources/SwiftDocC/Converter/DocumentationContextConverter.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public class DocumentationContextConverter {
8484
/// - Parameters:
8585
/// - node: The documentation node to convert.
8686
/// - Returns: The render node representation of the documentation node.
87-
public func renderNode(for node: DocumentationNode) throws -> RenderNode? {
87+
public func renderNode(for node: DocumentationNode) -> RenderNode? {
8888
guard !node.isVirtual else {
8989
return nil
9090
}
@@ -104,6 +104,6 @@ public class DocumentationContextConverter {
104104

105105
@available(*, deprecated, renamed: "renderNode(for:)", message: "Use 'renderNode(for:)' instead. This deprecated API will be removed after 6.1 is released")
106106
public func renderNode(for node: DocumentationNode, at source: URL?) throws -> RenderNode? {
107-
return try self.renderNode(for: node)
107+
self.renderNode(for: node)
108108
}
109109
}
Lines changed: 35 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
This source file is part of the Swift.org open source project
33

4-
Copyright (c) 2021 Apple Inc. and the Swift project authors
4+
Copyright (c) 2021-2024 Apple Inc. and the Swift project authors
55
Licensed under Apache License v2.0 with Runtime Library Exception
66

77
See https://swift.org/LICENSE.txt for license information
@@ -11,91 +11,46 @@
1111
import Foundation
1212

1313
extension ConvertService {
14-
/// Data provider for a conversion service.
15-
///
16-
/// This data provider accepts in-memory documentation and assigns unique URLs for each document.
17-
struct InMemoryContentDataProvider: DocumentationWorkspaceDataProvider {
18-
var identifier: String = UUID().uuidString
19-
var bundles: [DocumentationBundle] = []
20-
14+
/// Creates a bundle and an associated in-memory data provider from the information of a given convert request
15+
static func makeBundleAndInMemoryDataProvider(_ request: ConvertRequest) -> (bundle: DocumentationBundle, provider: InMemoryDataProvider) {
2116
var files: [URL: Data] = [:]
22-
23-
mutating func registerBundle(
24-
info: DocumentationBundle.Info,
25-
symbolGraphs: [Data],
26-
markupFiles: [Data],
27-
tutorialFiles: [Data],
28-
miscResourceURLs: [URL]
29-
) {
30-
let symbolGraphURLs = symbolGraphs.map { registerFile(contents: $0, pathExtension: nil) }
31-
let markupFileURLs = markupFiles.map { markupFile in
32-
registerFile(
33-
contents: markupFile,
34-
pathExtension:
35-
DocumentationBundleFileTypes.referenceFileExtension
36-
)
37-
} + tutorialFiles.map { tutorialFile in
38-
registerFile(
39-
contents: tutorialFile,
40-
pathExtension:
41-
DocumentationBundleFileTypes.tutorialFileExtension
42-
)
43-
}
44-
45-
bundles.append(
46-
DocumentationBundle(
47-
info: info,
48-
symbolGraphURLs: symbolGraphURLs,
49-
markupURLs: markupFileURLs,
50-
miscResourceURLs: miscResourceURLs
51-
)
52-
)
17+
files.reserveCapacity(
18+
request.symbolGraphs.count
19+
+ request.markupFiles.count
20+
+ request.tutorialFiles.count
21+
+ request.miscResourceURLs.count
22+
)
23+
for markupFile in request.markupFiles {
24+
files[makeURL().appendingPathExtension(DocumentationBundleFileTypes.referenceFileExtension)] = markupFile
5325
}
54-
55-
private mutating func registerFile(contents: Data, pathExtension: String?) -> URL {
56-
let url = Self.createURL(pathExtension: pathExtension)
57-
files[url] = contents
58-
return url
59-
}
60-
61-
/// Creates a unique URL for a resource.
62-
///
63-
/// The URL this function generates for a resource is not derived from the resource itself, because it doesn't need to be. The
64-
/// ``DocumentationWorkspaceDataProvider`` model revolves around retrieving resources by their URL. In our use
65-
/// case, our resources are not file URLs so we generate a URL for each resource.
66-
static private func createURL(pathExtension: String? = nil) -> URL {
67-
var url = URL(string: "docc-service:/\(UUID().uuidString)")!
68-
69-
if let pathExtension {
70-
url.appendPathExtension(pathExtension)
71-
}
72-
73-
return url
26+
for tutorialFile in request.tutorialFiles {
27+
files[makeURL().appendingPathExtension(DocumentationBundleFileTypes.tutorialFileExtension)] = tutorialFile
7428
}
29+
let markupFileURL = Array(files.keys)
7530

76-
func contentsOfURL(_ url: URL) throws -> Data {
77-
guard let contents = files[url] else {
78-
throw Error.unknownURL(url: url)
79-
}
80-
return contents
31+
var symbolGraphURLs: [URL] = []
32+
symbolGraphURLs.reserveCapacity(request.symbolGraphs.count)
33+
for symbolGraph in request.symbolGraphs {
34+
let url = makeURL()
35+
symbolGraphURLs.append(url)
36+
files[url] = symbolGraph
8137
}
8238

83-
func bundles(options: BundleDiscoveryOptions) throws -> [DocumentationBundle] {
84-
return bundles
85-
}
86-
87-
enum Error: DescribedError {
88-
case unknownURL(url: URL)
89-
90-
var errorDescription: String {
91-
switch self {
92-
case .unknownURL(let url):
93-
return """
94-
Unable to retrieve contents of file at \(url.absoluteString.singleQuoted).
95-
"""
96-
}
97-
}
98-
}
39+
return (
40+
DocumentationBundle(
41+
info: request.bundleInfo,
42+
symbolGraphURLs: symbolGraphURLs,
43+
markupURLs: markupFileURL,
44+
miscResourceURLs: request.miscResourceURLs
45+
),
46+
InMemoryDataProvider(
47+
files: files,
48+
fallbackFileManager: FileManager.default
49+
)
50+
)
9951
}
10052

53+
private static func makeURL() -> URL {
54+
URL(string: "docc-service:/\(UUID().uuidString)")!
55+
}
10156
}

Sources/SwiftDocC/DocumentationService/Convert/ConvertService+OutputConsumer.swift

Lines changed: 0 additions & 43 deletions
This file was deleted.

0 commit comments

Comments
 (0)