Skip to content

Commit

Permalink
Add support to relative paths SourceRepository checkoutPath argument
Browse files Browse the repository at this point in the history
  • Loading branch information
Anthony-Eid committed May 30, 2024
1 parent 037f0bc commit be84a97
Showing 1 changed file with 20 additions and 11 deletions.
31 changes: 20 additions & 11 deletions Sources/SwiftDocC/SourceRepository/SourceRepository.swift
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

/// 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

0 comments on commit be84a97

Please sign in to comment.