-
Notifications
You must be signed in to change notification settings - Fork 56
Add initial experimental support for combined documentation for multiple targets #84
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
d-ronnqvist
merged 17 commits into
swiftlang:main
from
d-ronnqvist:multi-target-documentation
Aug 9, 2024
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
5d7ff6d
Add a minimal build graph for documentation tasks
d-ronnqvist 0284c97
Build documentation for targets in reverse dependency order
d-ronnqvist 8b8d89d
Fix unrelated warning about a deprecated (renamed) DocC flag
d-ronnqvist 0f7d4be
Combine nested conditionals into one if-statement
d-ronnqvist b623ee6
Decode the supported features for a given DocC executable
d-ronnqvist 043ba3b
List all the generated documentation archvies
d-ronnqvist 7f7f636
Add flag to enable combined documentation support
d-ronnqvist fd4f39b
Warn if the DocC executable doesn't support combined documentation
d-ronnqvist 350ee6d
Update integration tests to more explicitly check for archive paths i…
d-ronnqvist 18f912b
Update check-source to include 2024 as a supported year
d-ronnqvist 5371735
Merge branch 'main' into multi-target-documentation
d-ronnqvist 8f7ed4d
Merge branch 'main' into multi-target-documentation
d-ronnqvist 6d761ec
Merge branch 'main' into multi-target-documentation
d-ronnqvist 7ecba95
Address code review feedback:
d-ronnqvist fe2779b
Merge branch 'multi-target-documentation' of github.com:d-ronnqvist/s…
d-ronnqvist 5b4c75d
Add a type to encapsulate performing work for each build graph item
d-ronnqvist 7f9aa55
Remove extra blank line before license comment which cause a false-po…
d-ronnqvist File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 112 additions & 0 deletions
112
Sources/SwiftDocCPluginUtilities/BuildGraph/DocumentationBuildGraph.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
// This source file is part of the Swift.org open source project | ||
// | ||
// Copyright (c) 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 | ||
// See https://swift.org/CONTRIBUTORS.txt for Swift project authors | ||
|
||
import Foundation | ||
|
||
/// A target that can have a documentation task in the build graph | ||
protocol DocumentationBuildGraphTarget { | ||
typealias ID = String | ||
/// The unique identifier of this target | ||
var id: ID { get } | ||
/// The unique identifiers of this target's direct dependencies (non-transitive). | ||
var dependencyIDs: [ID] { get } | ||
} | ||
|
||
/// A build graph of documentation tasks. | ||
struct DocumentationBuildGraph<Target: DocumentationBuildGraphTarget> { | ||
fileprivate typealias ID = Target.ID | ||
/// All the documentation tasks | ||
let tasks: [Task] | ||
|
||
/// Creates a new documentation build graph for a series of targets with dependencies. | ||
init(targets: some Sequence<Target>) { | ||
// Create tasks | ||
let taskLookup: [ID: Task] = targets.reduce(into: [:]) { acc, target in | ||
acc[target.id] = Task(target: target) | ||
} | ||
// Add dependency information to each task | ||
for task in taskLookup.values { | ||
task.dependencies = task.target.dependencyIDs.compactMap { taskLookup[$0] } | ||
} | ||
|
||
tasks = Array(taskLookup.values) | ||
} | ||
|
||
/// Creates a list of dependent operations to perform the given work for each task in the build graph. | ||
/// | ||
/// You can add these operations to an `OperationQueue` to perform them in dependency order | ||
/// (dependencies before dependents). The queue can run these operations concurrently. | ||
/// | ||
/// - Parameter work: The work to perform for each task in the build graph. | ||
/// - Returns: A list of dependent operations that performs `work` for each documentation task task. | ||
func makeOperations(performing work: @escaping (Task) -> Void) -> [Operation] { | ||
var builder = OperationBuilder(work: work) | ||
for task in tasks { | ||
builder.buildOperationHierarchy(for: task) | ||
} | ||
|
||
return Array(builder.operationsByID.values) | ||
} | ||
} | ||
|
||
extension DocumentationBuildGraph { | ||
/// A documentation task in the build graph | ||
final class Task { | ||
/// The target to build documentation for | ||
let target: Target | ||
/// The unique identifier of the task | ||
fileprivate var id: ID { target.id } | ||
/// The other documentation tasks that this task depends on. | ||
fileprivate(set) var dependencies: [Task] | ||
|
||
init(target: Target) { | ||
self.target = target | ||
self.dependencies = [] | ||
} | ||
} | ||
} | ||
|
||
extension DocumentationBuildGraph { | ||
/// A type that builds a hierarchy of dependent operations | ||
private struct OperationBuilder { | ||
/// The work that each operation should perform | ||
let work: (Task) -> Void | ||
/// A lookup of operations by their ID | ||
private(set) var operationsByID: [ID: Operation] = [:] | ||
sofiaromorales marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/// Adds new dependent operations to the builder. | ||
/// | ||
/// You can access the created dependent operations using `operationsByID.values`. | ||
mutating func buildOperationHierarchy(for task: Task) { | ||
let operation = makeOperation(for: task) | ||
for dependency in task.dependencies { | ||
let hasAlreadyVisitedTask = operationsByID[dependency.id] != nil | ||
|
||
let dependentOperation = makeOperation(for: dependency) | ||
operation.addDependency(dependentOperation) | ||
|
||
if !hasAlreadyVisitedTask { | ||
buildOperationHierarchy(for: dependency) | ||
} | ||
} | ||
} | ||
|
||
/// Returns the existing operation for the given task or creates a new operation if the builder didn't already have an operation for this task. | ||
private mutating func makeOperation(for task: Task) -> Operation { | ||
if let existing = operationsByID[task.id] { | ||
return existing | ||
} | ||
// Copy the closure and the target into a block operation object | ||
let new = BlockOperation { [work, task] in | ||
work(task) | ||
} | ||
operationsByID[task.id] = new | ||
return new | ||
} | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
Sources/SwiftDocCPluginUtilities/BuildGraph/DocumentationBuildGraphRunner.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// This source file is part of the Swift.org open source project | ||
// | ||
// Copyright (c) 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 | ||
// See https://swift.org/CONTRIBUTORS.txt for Swift project authors | ||
|
||
|
||
import Foundation | ||
|
||
/// A type that runs tasks for each target in a build graph in dependency order. | ||
struct DocumentationBuildGraphRunner<Target: DocumentationBuildGraphTarget> { | ||
|
||
let buildGraph: DocumentationBuildGraph<Target> | ||
|
||
typealias Work<Result> = (DocumentationBuildGraph<Target>.Task) throws -> Result | ||
|
||
func perform<Result>(_ work: @escaping Work<Result>) throws -> [Result] { | ||
// Create a serial queue to perform each documentation build task | ||
let queue = OperationQueue() | ||
queue.maxConcurrentOperationCount = 1 | ||
|
||
// Operations can't raise errors. Instead we catch the error from 'performBuildTask(_:)' | ||
// and cancel the remaining tasks. | ||
let resultLock = NSLock() | ||
var caughtError: Error? | ||
var results: [Result] = [] | ||
|
||
let operations = buildGraph.makeOperations { [work] task in | ||
do { | ||
let result = try work(task) | ||
resultLock.withLock { | ||
results.append(result) | ||
} | ||
} catch { | ||
resultLock.withLock { | ||
caughtError = error | ||
queue.cancelAllOperations() | ||
} | ||
} | ||
} | ||
|
||
// Run all the documentation build tasks in dependency order (dependencies before dependents). | ||
queue.addOperations(operations, waitUntilFinished: true) | ||
|
||
// If any of the build tasks raised an error. Re-throw that error. | ||
if let caughtError { | ||
throw caughtError | ||
} | ||
|
||
return results | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.