Skip to content

Pass documentation context its only bundle during initialization #1057

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 22 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
c499ace
Add ability to initialize a context with a given bundle
d-ronnqvist Sep 30, 2024
a6da113
Update test to verify bundle discovery options in convert command rat…
d-ronnqvist Oct 11, 2024
a7965ec
Remove unused parameters on DataAssetManager
d-ronnqvist Oct 11, 2024
dfd680f
Minor refinements to bundle loading test helpers
d-ronnqvist Oct 11, 2024
67743b5
Stop using DocumentationConverter in ConvertService
d-ronnqvist Oct 11, 2024
e36f229
Update InputsProvider to also return the DataProvider
d-ronnqvist Oct 11, 2024
3772742
Update InputsProvider to raise an error when it can't create inputs
d-ronnqvist Oct 11, 2024
9404c62
Remove `throws` for `renderNode(for:)` that doesn't raise any errors
d-ronnqvist Oct 14, 2024
207f394
Stop using DocumentationConverter in ConvertAction
d-ronnqvist Oct 14, 2024
59f3161
Update tests that relied on ConvertAction doing validation when run i…
d-ronnqvist Oct 14, 2024
efbbb50
Support initializing TestFileSystem with CopyOfFolder
d-ronnqvist Oct 14, 2024
0c64600
Update emit curation action to pass initialize context with bundle
d-ronnqvist Oct 14, 2024
609d19b
Use top-level protocol to remain compatible with Swift 5.9
d-ronnqvist Oct 14, 2024
a5bd4ca
Merge branch 'main' into init-context-with-bundle
d-ronnqvist Oct 14, 2024
1046daa
Make `ConvertActionConverter.convert(...)` synchronous for now
d-ronnqvist Oct 15, 2024
545b1d9
Merge branch 'main' into init-context-with-bundle
d-ronnqvist Oct 21, 2024
8c830de
Merge branch 'main' into init-context-with-bundle
d-ronnqvist Oct 21, 2024
59ae3cb
Merge branch 'main' into init-context-with-bundle
d-ronnqvist Oct 23, 2024
93609f9
Remove outdated comment about optional indexer
d-ronnqvist Oct 23, 2024
c08b41f
Merge branch 'main' into init-context-with-bundle
d-ronnqvist Oct 24, 2024
bf1d51c
Add comment about code that can be simplified after 6.2 is released.
d-ronnqvist Oct 24, 2024
9606561
Add assertion about correct identifier use to new provider code paths
d-ronnqvist Oct 24, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public class DocumentationContextConverter {
/// - Parameters:
/// - node: The documentation node to convert.
/// - Returns: The render node representation of the documentation node.
public func renderNode(for node: DocumentationNode) throws -> RenderNode? {
public func renderNode(for node: DocumentationNode) -> RenderNode? {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't need to throw? Was the Swift compiler returning a warning here all along?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It wasn't. It's allowed for a function to declare itself as raising errors without ever doing so in the implementation.

I was surprised that this wasn't a warning.

guard !node.isVirtual else {
return nil
}
Expand All @@ -104,6 +104,6 @@ public class DocumentationContextConverter {

@available(*, deprecated, renamed: "renderNode(for:)", message: "Use 'renderNode(for:)' instead. This deprecated API will be removed after 6.1 is released")
public func renderNode(for node: DocumentationNode, at source: URL?) throws -> RenderNode? {
return try self.renderNode(for: node)
self.renderNode(for: node)
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
This source file is part of the Swift.org open source project

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

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

extension ConvertService {
/// Data provider for a conversion service.
///
/// This data provider accepts in-memory documentation and assigns unique URLs for each document.
struct InMemoryContentDataProvider: DocumentationWorkspaceDataProvider {
var identifier: String = UUID().uuidString
var bundles: [DocumentationBundle] = []

/// Creates a bundle and an associated in-memory data provider from the information of a given convert request
static func makeBundleAndInMemoryDataProvider(_ request: ConvertRequest) -> (bundle: DocumentationBundle, provider: InMemoryDataProvider) {
var files: [URL: Data] = [:]

mutating func registerBundle(
info: DocumentationBundle.Info,
symbolGraphs: [Data],
markupFiles: [Data],
tutorialFiles: [Data],
miscResourceURLs: [URL]
) {
let symbolGraphURLs = symbolGraphs.map { registerFile(contents: $0, pathExtension: nil) }
let markupFileURLs = markupFiles.map { markupFile in
registerFile(
contents: markupFile,
pathExtension:
DocumentationBundleFileTypes.referenceFileExtension
)
} + tutorialFiles.map { tutorialFile in
registerFile(
contents: tutorialFile,
pathExtension:
DocumentationBundleFileTypes.tutorialFileExtension
)
}

bundles.append(
DocumentationBundle(
info: info,
symbolGraphURLs: symbolGraphURLs,
markupURLs: markupFileURLs,
miscResourceURLs: miscResourceURLs
)
)
files.reserveCapacity(
request.symbolGraphs.count
+ request.markupFiles.count
+ request.tutorialFiles.count
+ request.miscResourceURLs.count
)
for markupFile in request.markupFiles {
files[makeURL().appendingPathExtension(DocumentationBundleFileTypes.referenceFileExtension)] = markupFile
}

private mutating func registerFile(contents: Data, pathExtension: String?) -> URL {
let url = Self.createURL(pathExtension: pathExtension)
files[url] = contents
return url
}

/// Creates a unique URL for a resource.
///
/// The URL this function generates for a resource is not derived from the resource itself, because it doesn't need to be. The
/// ``DocumentationWorkspaceDataProvider`` model revolves around retrieving resources by their URL. In our use
/// case, our resources are not file URLs so we generate a URL for each resource.
static private func createURL(pathExtension: String? = nil) -> URL {
var url = URL(string: "docc-service:/\(UUID().uuidString)")!

if let pathExtension {
url.appendPathExtension(pathExtension)
}

return url
for tutorialFile in request.tutorialFiles {
files[makeURL().appendingPathExtension(DocumentationBundleFileTypes.tutorialFileExtension)] = tutorialFile
}
let markupFileURL = Array(files.keys)

func contentsOfURL(_ url: URL) throws -> Data {
guard let contents = files[url] else {
throw Error.unknownURL(url: url)
}
return contents
var symbolGraphURLs: [URL] = []
symbolGraphURLs.reserveCapacity(request.symbolGraphs.count)
for symbolGraph in request.symbolGraphs {
let url = makeURL()
symbolGraphURLs.append(url)
files[url] = symbolGraph
}

func bundles(options: BundleDiscoveryOptions) throws -> [DocumentationBundle] {
return bundles
}

enum Error: DescribedError {
case unknownURL(url: URL)

var errorDescription: String {
switch self {
case .unknownURL(let url):
return """
Unable to retrieve contents of file at \(url.absoluteString.singleQuoted).
"""
}
}
}
return (
DocumentationBundle(
info: request.bundleInfo,
symbolGraphURLs: symbolGraphURLs,
markupURLs: markupFileURL,
miscResourceURLs: request.miscResourceURLs
),
InMemoryDataProvider(
files: files,
fallbackFileManager: FileManager.default
)
)
}

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

This file was deleted.

Loading