Skip to content

Commit b515899

Browse files
committed
Add ability to use relative paths with convert --checkout-path argument
1 parent 68273e2 commit b515899

File tree

2 files changed

+27
-21
lines changed

2 files changed

+27
-21
lines changed

Sources/SwiftDocC/SourceRepository/SourceRepository.swift

+15-9
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ import Foundation
1414
public struct SourceRepository {
1515
/// The path at which the repository is cloned locally.
1616
public var checkoutPath: String
17-
17+
1818
/// The base URL where the service hosts the repository's contents.
1919
public var sourceServiceBaseURL: URL
20-
20+
2121
/// A function that formats a line number to be included in a URL.
2222
public var formatLineNumber: (Int) -> String
23-
23+
2424
/// Creates a source code repository.
2525
/// - Parameters:
2626
/// - checkoutPath: The path at which the repository is checked out locally and from which its symbol graphs were generated.
@@ -31,11 +31,17 @@ public struct SourceRepository {
3131
sourceServiceBaseURL: URL,
3232
formatLineNumber: @escaping (Int) -> String
3333
) {
34+
if checkoutPath.starts(with: "/") {
35+
self.checkoutPath = checkoutPath
36+
} else {
37+
self.checkoutPath = "\(FileManager.default.currentDirectoryPath)/\(checkoutPath)"
38+
}
39+
3440
self.checkoutPath = checkoutPath
3541
self.sourceServiceBaseURL = sourceServiceBaseURL
3642
self.formatLineNumber = formatLineNumber
3743
}
38-
44+
3945
/// Formats a local source file URL to a URL hosted by the remote source code service.
4046
/// - Parameters:
4147
/// - sourceFileURL: The location of the source file on disk.
@@ -45,7 +51,7 @@ public struct SourceRepository {
4551
guard sourceFileURL.path.hasPrefix(checkoutPath) else {
4652
return nil
4753
}
48-
54+
4955
let path = sourceFileURL.path.dropFirst(checkoutPath.count).removingLeadingSlash
5056
return sourceServiceBaseURL
5157
.appendingPathComponent(path)
@@ -65,7 +71,7 @@ public extension SourceRepository {
6571
formatLineNumber: { line in "L\(line)" }
6672
)
6773
}
68-
74+
6975
/// Creates a source repository hosted by the GitLab service.
7076
/// - Parameters:
7177
/// - checkoutPath: The path of the local checkout.
@@ -77,7 +83,7 @@ public extension SourceRepository {
7783
formatLineNumber: { line in "L\(line)" }
7884
)
7985
}
80-
86+
8187
/// Creates a source repository hosted by the BitBucket service.
8288
/// - Parameters:
8389
/// - checkoutPath: The path of the local checkout.
@@ -89,7 +95,7 @@ public extension SourceRepository {
8995
formatLineNumber: { line in "lines-\(line)" }
9096
)
9197
}
92-
98+
9399
/// Creates a source repository hosted by the device's filesystem.
94100
///
95101
/// Use this source repository to format `doc-source-file://` links to files on the
@@ -98,7 +104,7 @@ public extension SourceRepository {
98104
/// This source repository uses a custom scheme to offer more control local source file navigation.
99105
static func localFilesystem() -> SourceRepository {
100106
SourceRepository(
101-
checkoutPath: "",
107+
checkoutPath: "/",
102108
// 2 slashes to specify an empty authority/host component and 1 slash to specify a base path at the root.
103109
sourceServiceBaseURL: URL(string: "doc-source-file:///")!,
104110
formatLineNumber: { line in "L\(line)" }

Tests/SwiftDocCUtilitiesTests/ArgumentParsing/ConvertSubcommandSourceRepositoryTests.swift

+12-12
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ class ConvertSubcommandSourceRepositoryTests: XCTestCase {
2020
withExtension: "docc",
2121
subdirectory: "Test Bundles"
2222
)!
23-
23+
2424
private let testTemplateURL = Bundle.module.url(
2525
forResource: "Test Template",
2626
withExtension: nil,
2727
subdirectory: "Test Resources"
2828
)!
29-
29+
3030
func testSourceRepositoryAllArgumentsSpecified() throws {
3131
for sourceService in ["github", "gitlab", "bitbucket"] {
3232
try assertSourceRepositoryArguments(
@@ -39,7 +39,7 @@ class ConvertSubcommandSourceRepositoryTests: XCTestCase {
3939
}
4040
}
4141
}
42-
42+
4343
func testDoesNotSetSourceRepositoryIfBothCheckoutPathAndsourceServiceBaseURLArgumentsAreMissing() throws {
4444
try assertSourceRepositoryArguments(
4545
checkoutPath: nil,
@@ -49,7 +49,7 @@ class ConvertSubcommandSourceRepositoryTests: XCTestCase {
4949
XCTAssertNil(action.sourceRepository)
5050
}
5151
}
52-
52+
5353
func testThrowsValidationErrorWhenSourceServiceIsSpecifiedButNotSourceServiceBaseURL() throws {
5454
XCTAssertThrowsError(
5555
try assertSourceRepositoryArguments(
@@ -67,7 +67,7 @@ class ConvertSubcommandSourceRepositoryTests: XCTestCase {
6767
)
6868
}
6969
}
70-
70+
7171
func testThrowsValidationErrorWhenSourceServiceBaseURLIsSpecifiedButNotSourceService() throws {
7272
XCTAssertThrowsError(
7373
try assertSourceRepositoryArguments(
@@ -85,7 +85,7 @@ class ConvertSubcommandSourceRepositoryTests: XCTestCase {
8585
)
8686
}
8787
}
88-
88+
8989
func testThrowsValidationErrorWhenSourceServiceBaseURLIsInvalid() throws {
9090
XCTAssertThrowsError(
9191
try assertSourceRepositoryArguments(
@@ -100,7 +100,7 @@ class ConvertSubcommandSourceRepositoryTests: XCTestCase {
100100
)
101101
}
102102
}
103-
103+
104104
func testThrowsValidationErrorWhenCheckoutPathIsNotSpecified() throws {
105105
XCTAssertThrowsError(
106106
try assertSourceRepositoryArguments(
@@ -118,7 +118,7 @@ class ConvertSubcommandSourceRepositoryTests: XCTestCase {
118118
)
119119
}
120120
}
121-
121+
122122
func testThrowsValidationErrorWhenSourceServiceIsInvalid() throws {
123123
XCTAssertThrowsError(
124124
try assertSourceRepositoryArguments(
@@ -133,15 +133,15 @@ class ConvertSubcommandSourceRepositoryTests: XCTestCase {
133133
)
134134
}
135135
}
136-
136+
137137
private func assertSourceRepositoryArguments(
138138
checkoutPath: String?,
139139
sourceService: String?,
140140
sourceServiceBaseURL: String?,
141141
assertion: ((ConvertAction) throws -> Void)? = nil
142142
) throws {
143143
SetEnvironmentVariable(TemplateOption.environmentVariableKey, testTemplateURL.path)
144-
144+
145145
var arguments: [String] = [testBundleURL.path]
146146
if let checkoutPath {
147147
arguments.append(contentsOf: ["--checkout-path", checkoutPath])
@@ -152,9 +152,9 @@ class ConvertSubcommandSourceRepositoryTests: XCTestCase {
152152
if let sourceServiceBaseURL {
153153
arguments.append(contentsOf: ["--source-service-base-url", sourceServiceBaseURL])
154154
}
155-
155+
156156
let convertOptions = try Docc.Convert.parse(arguments)
157-
157+
158158
let result = try ConvertAction(fromConvertCommand: convertOptions)
159159
try assertion?(result)
160160
}

0 commit comments

Comments
 (0)