Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add support to relative paths SourceRepository checkoutPath argument
  • Loading branch information
Anthony-Eid committed May 30, 2024
commit be84a9798dc0c0f32687dcbc255f54f413495999
31 changes: 20 additions & 11 deletions Sources/SwiftDocC/SourceRepository/SourceRepository.swift
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a significant amount of whitespace-only changes in all these files which adds noise to the PR. Please remove all those.

Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,42 @@
*/

import Foundation
import ArgumentParser

/// A remote repository that hosts source code.
public struct SourceRepository {
/// The path at which the repository is cloned locally.
public var checkoutPath: String
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this was defined as a URL then the issue with absolute and relative paths would already be handled by the URL(fileURLWithPath:) initializer.


/// The base URL where the service hosts the repository's contents.
public var sourceServiceBaseURL: URL

/// A function that formats a line number to be included in a URL.
public var formatLineNumber: (Int) -> String

/// Creates a source code repository.
/// - Parameters:
/// - checkoutPath: The path at which the repository is checked out locally and from which its symbol graphs were generated.
/// - sourceServiceBaseURL: The base URL where the service hosts the repository's contents.
/// - formatLineNumber: A function that formats a line number to be included in a URL.
public init(
public init (
checkoutPath: String,
sourceServiceBaseURL: URL,
formatLineNumber: @escaping (Int) -> String
) {
self.checkoutPath = checkoutPath


// guard FileManager.default.directoryExists(atPath: checkoutPath) else {
// throw ValidationError("User provided checkout-path argument {checkoutPath} is invalid.")
// }
let absoluteCheckoutPath = URL(fileURLWithPath: checkoutPath).absoluteString
let startIndex = absoluteCheckoutPath.index(absoluteCheckoutPath.startIndex, offsetBy: 7)

self.checkoutPath = String(absoluteCheckoutPath[startIndex...])
self.sourceServiceBaseURL = sourceServiceBaseURL
self.formatLineNumber = formatLineNumber
}

/// Formats a local source file URL to a URL hosted by the remote source code service.
/// - Parameters:
/// - sourceFileURL: The location of the source file on disk.
Expand All @@ -45,7 +54,7 @@ public struct SourceRepository {
guard sourceFileURL.path.hasPrefix(checkoutPath) else {
return nil
}

let path = sourceFileURL.path.dropFirst(checkoutPath.count).removingLeadingSlash
return sourceServiceBaseURL
.appendingPathComponent(path)
Expand All @@ -65,7 +74,7 @@ public extension SourceRepository {
formatLineNumber: { line in "L\(line)" }
)
}

/// Creates a source repository hosted by the GitLab service.
/// - Parameters:
/// - checkoutPath: The path of the local checkout.
Expand All @@ -77,7 +86,7 @@ public extension SourceRepository {
formatLineNumber: { line in "L\(line)" }
)
}

/// Creates a source repository hosted by the BitBucket service.
/// - Parameters:
/// - checkoutPath: The path of the local checkout.
Expand All @@ -89,7 +98,7 @@ public extension SourceRepository {
formatLineNumber: { line in "lines-\(line)" }
)
}

/// Creates a source repository hosted by the device's filesystem.
///
/// Use this source repository to format `doc-source-file://` links to files on the
Expand All @@ -98,7 +107,7 @@ public extension SourceRepository {
/// This source repository uses a custom scheme to offer more control local source file navigation.
static func localFilesystem() -> SourceRepository {
SourceRepository(
checkoutPath: "",
checkoutPath: "/",
// 2 slashes to specify an empty authority/host component and 1 slash to specify a base path at the root.
sourceServiceBaseURL: URL(string: "doc-source-file:///")!,
formatLineNumber: { line in "L\(line)" }
Expand Down