Skip to content

Commit ce886e5

Browse files
Transform DocC archives for static hosting by default (#121) (#136)
Updates `docc convert` to transform for static hosting by default. This keeps the original `--transform-for-static-hosting` flag for backwards compatibility and adds a new `--no-transform-for-static-hosting` flag to explicitly opt-out. Resolves rdar://91173450.
1 parent 5e5614e commit ce886e5

File tree

3 files changed

+120
-6
lines changed

3 files changed

+120
-6
lines changed

Sources/SwiftDocC/Infrastructure/Diagnostics/Diagnostic.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,15 @@ public struct Diagnostic: DescribedError {
6666
/// the second *X* while adding a note on the first *X* to note that it was the first occurrence.
6767
public var notes = [DiagnosticNote]()
6868

69-
public init(source: URL?, severity: DiagnosticSeverity, range: SourceRange?, identifier: String, summary: String, explanation: String? = nil, notes: [DiagnosticNote] = []) {
69+
public init(
70+
source: URL? = nil,
71+
severity: DiagnosticSeverity,
72+
range: SourceRange? = nil,
73+
identifier: String,
74+
summary: String,
75+
explanation: String? = nil,
76+
notes: [DiagnosticNote] = []
77+
) {
7078
self.source = source
7179
self.severity = severity
7280
self.range = range

Sources/SwiftDocCUtilities/ArgumentParsing/Subcommands/Convert.swift

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -238,9 +238,17 @@ extension Docc {
238238
return outputURL
239239
}
240240

241-
/// Defaults to false
242-
@Flag(help: "Produce a Swift-DocC Archive that supports a static hosting environment.")
243-
public var transformForStaticHosting = false
241+
/// A Boolean value that is true if the DocC archive produced by this conversion
242+
/// will support static hosting environments.
243+
///
244+
/// This value defaults to true but can be explicitly disabled with the
245+
/// `--no-transform-for-static-hosting` flag.
246+
@Flag(
247+
inversion: .prefixedNo,
248+
exclusivity: .exclusive,
249+
help: "Produce a DocC archive that supports static hosting environments."
250+
)
251+
public var transformForStaticHosting = true
244252

245253
/// A user-provided relative path to be used in the archived output
246254
@Option(
@@ -251,6 +259,10 @@ extension Docc {
251259
)
252260
var hostingBasePath: String?
253261

262+
/// The file handle that should be used for emitting warnings during argument validation.
263+
///
264+
/// Provided as a static variable to allow for redirecting output in unit tests.
265+
static var _errorLogHandle: LogHandle = .standardError
254266

255267
// MARK: - Property Validation
256268

@@ -288,9 +300,29 @@ extension Docc {
288300
}
289301

290302
} else {
291-
throw TemplateOption.missingHTMLTemplateError(
292-
path: templateOption.defaultTemplateURL.path
303+
let invalidOrMissingTemplateDiagnostic = Diagnostic(
304+
severity: .warning,
305+
identifier: "org.swift.docc.MissingHTMLTemplate",
306+
summary: "Invalid or missing HTML template directory",
307+
explanation: """
308+
Invalid or missing HTML template directory, relative to the docc \
309+
executable, at: '\(templateOption.defaultTemplateURL.path)'.
310+
Set the '\(TemplateOption.environmentVariableKey)' environment variable \
311+
to use a custom HTML template.
312+
313+
Conversion will continue, but the produced DocC archive will not be \
314+
compatible with static hosting environments.
315+
316+
Pass the '--no-transform-for-static-hosting' flag to silence this warning.
317+
"""
318+
)
319+
320+
print(
321+
invalidOrMissingTemplateDiagnostic.localizedDescription,
322+
to: &Self._errorLogHandle
293323
)
324+
325+
transformForStaticHosting = false
294326
}
295327
}
296328

Tests/SwiftDocCUtilitiesTests/ArgumentParsing/ConvertSubcommandTests.swift

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ class ConvertSubcommandTests: XCTestCase {
2020
private let testTemplateURL = Bundle.module.url(
2121
forResource: "Test Template", withExtension: nil, subdirectory: "Test Resources")!
2222

23+
override func setUp() {
24+
// By default, send all warnings to `.none` instead of filling the
25+
// test console output with unrelated messages.
26+
Docc.Convert._errorLogHandle = .none
27+
}
28+
2329
func testOptionsValidation() throws {
2430
// create source bundle directory
2531
let sourceURL = try createTemporaryDirectory(named: "documentation")
@@ -363,4 +369,72 @@ class ConvertSubcommandTests: XCTestCase {
363369
XCTAssertTrue(commandWithFlag.experimentalEnableCustomTemplates)
364370
XCTAssertTrue(actionWithFlag.experimentalEnableCustomTemplates)
365371
}
372+
373+
func testTransformForStaticHostingFlagWithoutHTMLTemplate() throws {
374+
unsetenv(TemplateOption.environmentVariableKey)
375+
376+
// Since there's no custom template set (and relative HTML template lookup isn't
377+
// supported in the test harness), we expect `transformForStaticHosting` to
378+
// be false in every possible scenario of the flag, even when explicitly requested.
379+
380+
do {
381+
let convertOptions = try Docc.Convert.parse([
382+
testBundleURL.path,
383+
])
384+
385+
XCTAssertFalse(convertOptions.transformForStaticHosting)
386+
}
387+
388+
do {
389+
let convertOptions = try Docc.Convert.parse([
390+
testBundleURL.path,
391+
"--transform-for-static-hosting",
392+
])
393+
394+
XCTAssertFalse(convertOptions.transformForStaticHosting)
395+
}
396+
397+
do {
398+
let convertOptions = try Docc.Convert.parse([
399+
testBundleURL.path,
400+
"--no-transform-for-static-hosting",
401+
])
402+
403+
XCTAssertFalse(convertOptions.transformForStaticHosting)
404+
}
405+
}
406+
407+
func testTransformForStaticHostingFlagWithHTMLTemplate() throws {
408+
setenv(TemplateOption.environmentVariableKey, testTemplateURL.path, 1)
409+
410+
// Since we've provided an HTML template, we expect `transformForStaticHosting`
411+
// to be true by default, and when explicitly requested. It should only be false
412+
// when `--no-transform-for-static-hosting` is passed.
413+
414+
do {
415+
let convertOptions = try Docc.Convert.parse([
416+
testBundleURL.path,
417+
])
418+
419+
XCTAssertTrue(convertOptions.transformForStaticHosting)
420+
}
421+
422+
do {
423+
let convertOptions = try Docc.Convert.parse([
424+
testBundleURL.path,
425+
"--transform-for-static-hosting",
426+
])
427+
428+
XCTAssertTrue(convertOptions.transformForStaticHosting)
429+
}
430+
431+
do {
432+
let convertOptions = try Docc.Convert.parse([
433+
testBundleURL.path,
434+
"--no-transform-for-static-hosting",
435+
])
436+
437+
XCTAssertFalse(convertOptions.transformForStaticHosting)
438+
}
439+
}
366440
}

0 commit comments

Comments
 (0)