Skip to content

Commit f8be787

Browse files
committed
Add host: parameter to PreviewAction.init
Now that `PreviewServer` requires an explicit host name, we should require one here too instead of hardcoding `localhost`.
1 parent 2a1539a commit f8be787

File tree

3 files changed

+13
-3
lines changed

3 files changed

+13
-3
lines changed

Sources/SwiftDocCUtilities/Action/Actions/PreviewAction.swift

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ public final class PreviewAction: Action, RecreatingContext {
5353

5454
var logHandle = LogHandle.standardOutput
5555

56+
let host: String
5657
let port: Int
5758

5859
var convertAction: ConvertAction
@@ -75,6 +76,7 @@ public final class PreviewAction: Action, RecreatingContext {
7576
/// Creates a new preview action from the given parameters.
7677
///
7778
/// - Parameters:
79+
/// - host: The host name used by the preview server.
7880
/// - port: The port number used by the preview server.
7981
/// - convertAction: The action used to convert the documentation bundle before preview.
8082
/// On macOS, this action will be reused to convert documentation each time the source is modified.
@@ -84,6 +86,7 @@ public final class PreviewAction: Action, RecreatingContext {
8486
/// is performed.
8587
/// - Throws: If an error is encountered while initializing the documentation context.
8688
public init(
89+
host: String,
8790
port: Int,
8891
createConvertAction: @escaping () throws -> ConvertAction,
8992
workspace: DocumentationWorkspace = DocumentationWorkspace(),
@@ -95,6 +98,7 @@ public final class PreviewAction: Action, RecreatingContext {
9598
}
9699

97100
// Initialize the action context.
101+
self.host = host
98102
self.port = port
99103
self.createConvertAction = createConvertAction
100104
self.convertAction = try createConvertAction()
@@ -108,13 +112,14 @@ public final class PreviewAction: Action, RecreatingContext {
108112
@available(*, deprecated, message: "TLS support has been removed.")
109113
public convenience init(
110114
tlsCertificateKey: URL?, tlsCertificateChain: URL?, serverUsername: String?,
111-
serverPassword: String?, port: Int,
115+
serverPassword: String?, host: String, port: Int,
112116
createConvertAction: @escaping () throws -> ConvertAction,
113117
workspace: DocumentationWorkspace = DocumentationWorkspace(),
114118
context: DocumentationContext? = nil,
115119
printTemplatePath: Bool = true) throws
116120
{
117121
try self.init(
122+
host: host,
118123
port: port,
119124
createConvertAction: createConvertAction,
120125
workspace: workspace,
@@ -163,13 +168,13 @@ public final class PreviewAction: Action, RecreatingContext {
163168
// Preview the output and monitor the source bundle for changes.
164169
do {
165170
print(String(repeating: "=", count: 40), to: &logHandle)
166-
if let previewURL = URL(string: "http://localhost:\(port)") {
171+
if let previewURL = URL(string: "http://\(host):\(port)") {
167172
print("Starting Local Preview Server", to: &logHandle)
168173
printPreviewAddresses(base: previewURL)
169174
print(String(repeating: "=", count: 40), to: &logHandle)
170175
}
171176

172-
let to: PreviewServer.Bind = bindServerToSocketPath.map { .socket(path: $0) } ?? .localhost(host: "localhost", port: port)
177+
let to: PreviewServer.Bind = bindServerToSocketPath.map { .socket(path: $0) } ?? .localhost(host: host, port: port)
173178
servers[serverIdentifier] = try PreviewServer(contentURL: convertAction.targetDirectory, bindTo: to, logHandle: &logHandle)
174179

175180
// When the user stops docc - stop the preview server first before exiting.

Sources/SwiftDocCUtilities/ArgumentParsing/ActionExtensions/PreviewAction+CommandInitialization.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ extension PreviewAction {
2424
{
2525
// Initialize the `PreviewAction` from the options provided by the `Preview` command
2626
try self.init(
27+
host: "localhost",
2728
port: previewOptions.port,
2829
createConvertAction: {
2930
try ConvertAction(

Tests/SwiftDocCUtilitiesTests/PreviewActionIntegrationTests.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ class PreviewActionIntegrationTests: XCTestCase {
103103
// tlsCertificateChain: nil,
104104
// serverUsername: nil,
105105
// serverPassword: nil,
106+
// host: "localhost",
106107
// port: 8080, // We ignore this value when we set the `bindServerToSocketPath` property below.
107108
// createConvertAction: createConvertAction) else {
108109
// XCTFail("Could not create preview action from parameters")
@@ -301,6 +302,7 @@ class PreviewActionIntegrationTests: XCTestCase {
301302
}
302303

303304
guard let preview = try? PreviewAction(
305+
host: "localhost",
304306
port: bindPort,
305307
createConvertAction: createConvertAction) else {
306308
XCTFail("Could not create preview action from parameters", file: file, line: line)
@@ -375,6 +377,7 @@ class PreviewActionIntegrationTests: XCTestCase {
375377
}
376378

377379
guard let preview = try? PreviewAction(
380+
host: "localhost",
378381
port: 0, // Use port 0 to pick a random free port number
379382
createConvertAction: createConvertAction) else {
380383
XCTFail("Could not create preview action from parameters")
@@ -449,6 +452,7 @@ class PreviewActionIntegrationTests: XCTestCase {
449452
}
450453

451454
guard let preview = try? PreviewAction(
455+
host: "localhost",
452456
port: 8080, // We ignore this value when we set the `bindServerToSocketPath` property below.
453457
createConvertAction: createConvertAction) else {
454458
XCTFail("Could not create preview action from parameters")

0 commit comments

Comments
 (0)