-
Notifications
You must be signed in to change notification settings - Fork 143
Add Static Hosting Support #44
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
ethan-kusters
merged 8 commits into
swiftlang:main
from
macdevnet:Add-Static-Hosting-Support
Dec 9, 2021
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
5d4a4ea
Add Static Hosting Support
macdevnet 2bc5a86
Fixes for PR#44
macdevnet 8a5d6cd
Merge branch 'main' into Add-Static-Hosting-Support
ethan-kusters dfe24b4
Merge branch 'main' into Add-Static-Hosting-Support
ethan-kusters 9b054eb
Fix test failures on linux
ethan-kusters 2d3b228
Make TemplateOption error messsages more consistent
ethan-kusters 60120e4
Make 'StaticHostableTransformer' a struct
ethan-kusters 6087d4f
Merge branch 'main' into Add-Static-Hosting-Support
ethan-kusters 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
114 changes: 114 additions & 0 deletions
114
Sources/SwiftDocCUtilities/Action/Actions/TransformForStaticHostingAction.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,114 @@ | ||
/* | ||
This source file is part of the Swift.org open source project | ||
|
||
Copyright (c) 2021 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 | ||
import SwiftDocC | ||
|
||
/// An action that emits a static hostable website from a DocC Archive. | ||
struct TransformForStaticHostingAction: Action { | ||
|
||
let rootURL: URL | ||
let outputURL: URL | ||
let hostingBasePath: String? | ||
let outputIsExternal: Bool | ||
let htmlTemplateDirectory: URL | ||
|
||
let fileManager: FileManagerProtocol | ||
|
||
var diagnosticEngine: DiagnosticEngine | ||
|
||
/// Initializes the action with the given validated options, creates or uses the given action workspace & context. | ||
init(documentationBundleURL: URL, | ||
outputURL:URL?, | ||
hostingBasePath: String?, | ||
htmlTemplateDirectory: URL, | ||
fileManager: FileManagerProtocol = FileManager.default, | ||
diagnosticEngine: DiagnosticEngine = .init()) throws | ||
{ | ||
// Initialize the action context. | ||
self.rootURL = documentationBundleURL | ||
self.outputURL = outputURL ?? documentationBundleURL | ||
self.outputIsExternal = outputURL != nil | ||
self.hostingBasePath = hostingBasePath | ||
self.htmlTemplateDirectory = htmlTemplateDirectory | ||
self.fileManager = fileManager | ||
self.diagnosticEngine = diagnosticEngine | ||
self.diagnosticEngine.add(DiagnosticConsoleWriter(formattingOptions: [])) | ||
} | ||
|
||
/// Converts each eligible file from the source archive and | ||
/// saves the results in the given output folder. | ||
mutating func perform(logHandle: LogHandle) throws -> ActionResult { | ||
try emit() | ||
return ActionResult(didEncounterError: false, outputs: [outputURL]) | ||
} | ||
|
||
mutating private func emit() throws { | ||
|
||
|
||
// If the emit is to create the static hostable content outside of the source archive | ||
// then the output folder needs to be set up and the archive data copied | ||
// to the new folder. | ||
if outputIsExternal { | ||
|
||
try setupOutputDirectory(outputURL: outputURL) | ||
|
||
// Copy the appropriate folders from the archive. | ||
// We will copy individual items from the folder rather then just copy the folder | ||
// as we want to preserve anything intentionally left in the output URL by `setupOutputDirectory` | ||
for sourceItem in try fileManager.contentsOfDirectory(at: rootURL, includingPropertiesForKeys: [], options:[.skipsHiddenFiles]) { | ||
let targetItem = outputURL.appendingPathComponent(sourceItem.lastPathComponent) | ||
try fileManager.copyItem(at: sourceItem, to: targetItem) | ||
} | ||
} | ||
|
||
// Copy the HTML template to the output folder. | ||
var excludedFiles = [HTMLTemplate.templateFileName.rawValue] | ||
|
||
if outputIsExternal { | ||
excludedFiles.append(HTMLTemplate.indexFileName.rawValue) | ||
} | ||
|
||
for content in try fileManager.contentsOfDirectory(atPath: htmlTemplateDirectory.path) { | ||
|
||
guard !excludedFiles.contains(content) else { continue } | ||
|
||
let source = htmlTemplateDirectory.appendingPathComponent(content) | ||
let target = outputURL.appendingPathComponent(content) | ||
if fileManager.fileExists(atPath: target.path){ | ||
try fileManager.removeItem(at: target) | ||
} | ||
try fileManager.copyItem(at: source, to: target) | ||
} | ||
|
||
// Transform the indexHTML if needed. | ||
let indexHTMLData = try StaticHostableTransformer.transformHTMLTemplate(htmlTemplate: htmlTemplateDirectory, hostingBasePath: hostingBasePath) | ||
|
||
// Create a StaticHostableTransformer targeted at the archive data folder | ||
let dataProvider = try LocalFileSystemDataProvider(rootURL: rootURL.appendingPathComponent(NodeURLGenerator.Path.dataFolderName)) | ||
let transformer = StaticHostableTransformer(dataProvider: dataProvider, fileManager: fileManager, outputURL: outputURL, indexHTMLData: indexHTMLData) | ||
try transformer.transform() | ||
|
||
} | ||
|
||
/// Create output directory or empty its contents if it already exists. | ||
private func setupOutputDirectory(outputURL: URL) throws { | ||
|
||
var isDirectory: ObjCBool = false | ||
if fileManager.fileExists(atPath: outputURL.path, isDirectory: &isDirectory), isDirectory.boolValue { | ||
let contents = try fileManager.contentsOfDirectory(at: outputURL, includingPropertiesForKeys: [], options: [.skipsHiddenFiles]) | ||
for content in contents { | ||
try fileManager.removeItem(at: content) | ||
} | ||
} else { | ||
try fileManager.createDirectory(at: outputURL, withIntermediateDirectories: false, attributes: [:]) | ||
} | ||
} | ||
} |
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
34 changes: 34 additions & 0 deletions
34
...umentParsing/ActionExtensions/TransformForStaticHostingAction+CommandInitialization.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,34 @@ | ||
/* | ||
This source file is part of the Swift.org open source project | ||
|
||
Copyright (c) 2021 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 | ||
import ArgumentParser | ||
|
||
|
||
extension TransformForStaticHostingAction { | ||
/// Initializes ``TransformForStaticHostingAction`` from the options in the ``TransformForStaticHosting`` command. | ||
/// - Parameters: | ||
/// - cmd: The emit command this `TransformForStaticHostingAction` will be based on. | ||
init(fromCommand cmd: Docc.ProcessArchive.TransformForStaticHosting, withFallbackTemplate fallbackTemplateURL: URL? = nil) throws { | ||
// Initialize the `TransformForStaticHostingAction` from the options provided by the `EmitStaticHostable` command | ||
|
||
guard let htmlTemplateFolder = cmd.templateOption.templateURL ?? fallbackTemplateURL else { | ||
throw TemplateOption.missingHTMLTemplateError( | ||
path: cmd.templateOption.defaultTemplateURL.path | ||
) | ||
} | ||
|
||
try self.init( | ||
documentationBundleURL: cmd.documentationArchive.urlOrFallback, | ||
outputURL: cmd.outputURL, | ||
hostingBasePath: cmd.hostingBasePath, | ||
htmlTemplateDirectory: htmlTemplateFolder ) | ||
} | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure I understand this behavior. Why are we modifying the
index.html
iftransformForStaticHosting
is not set?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We still transform the template if
hostingBasePath
has been set to allow the non static version of the archive to be hosted somewhere other than root.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change was based on some feedback we got from Konrad in the forums: https://forums.swift.org/t/support-hosting-docc-archives-in-static-hosting-environments/53572/7.
The
hosting-base-path
parameter isn't specific to thetransform-for-static-hosting
work we're doing so it makes sense to support it here as well.