|
1 | 1 | /*
|
2 | 2 | This source file is part of the Swift.org open source project
|
3 | 3 |
|
4 |
| - Copyright (c) 2021 Apple Inc. and the Swift project authors |
| 4 | + Copyright (c) 2021-2024 Apple Inc. and the Swift project authors |
5 | 5 | Licensed under Apache License v2.0 with Runtime Library Exception
|
6 | 6 |
|
7 | 7 | See https://swift.org/LICENSE.txt for license information
|
|
11 | 11 | import Foundation
|
12 | 12 |
|
13 | 13 | 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) { |
21 | 16 | 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 |
53 | 25 | }
|
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 |
74 | 28 | }
|
| 29 | + let markupFileURL = Array(files.keys) |
75 | 30 |
|
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 |
81 | 37 | }
|
82 | 38 |
|
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 | + ) |
99 | 51 | }
|
100 | 52 |
|
| 53 | + private static func makeURL() -> URL { |
| 54 | + URL(string: "docc-service:/\(UUID().uuidString)")! |
| 55 | + } |
101 | 56 | }
|
0 commit comments