9
9
*/
10
10
11
11
import Foundation
12
+ import ArgumentParser
12
13
13
14
/// A remote repository that hosts source code.
14
15
public struct SourceRepository {
15
16
/// The path at which the repository is cloned locally.
16
17
public var checkoutPath : String
17
-
18
+
18
19
/// The base URL where the service hosts the repository's contents.
19
20
public var sourceServiceBaseURL : URL
20
-
21
+
21
22
/// A function that formats a line number to be included in a URL.
22
23
public var formatLineNumber : ( Int ) -> String
23
-
24
+
24
25
/// Creates a source code repository.
25
26
/// - Parameters:
26
27
/// - checkoutPath: The path at which the repository is checked out locally and from which its symbol graphs were generated.
27
28
/// - sourceServiceBaseURL: The base URL where the service hosts the repository's contents.
28
29
/// - formatLineNumber: A function that formats a line number to be included in a URL.
29
- public init (
30
+ public init (
30
31
checkoutPath: String ,
31
32
sourceServiceBaseURL: URL ,
32
33
formatLineNumber: @escaping ( Int ) -> String
33
34
) {
34
- self . checkoutPath = checkoutPath
35
+
36
+
37
+ // guard FileManager.default.directoryExists(atPath: checkoutPath) else {
38
+ // throw ValidationError("User provided checkout-path argument {checkoutPath} is invalid.")
39
+ // }
40
+ let absoluteCheckoutPath = URL ( fileURLWithPath: checkoutPath) . absoluteString
41
+ let startIndex = absoluteCheckoutPath. index ( absoluteCheckoutPath. startIndex, offsetBy: 7 )
42
+
43
+ self . checkoutPath = String ( absoluteCheckoutPath [ startIndex... ] )
35
44
self . sourceServiceBaseURL = sourceServiceBaseURL
36
45
self . formatLineNumber = formatLineNumber
37
46
}
38
-
47
+
39
48
/// Formats a local source file URL to a URL hosted by the remote source code service.
40
49
/// - Parameters:
41
50
/// - sourceFileURL: The location of the source file on disk.
@@ -45,7 +54,7 @@ public struct SourceRepository {
45
54
guard sourceFileURL. path. hasPrefix ( checkoutPath) else {
46
55
return nil
47
56
}
48
-
57
+
49
58
let path = sourceFileURL. path. dropFirst ( checkoutPath. count) . removingLeadingSlash
50
59
return sourceServiceBaseURL
51
60
. appendingPathComponent ( path)
@@ -65,7 +74,7 @@ public extension SourceRepository {
65
74
formatLineNumber: { line in " L \( line) " }
66
75
)
67
76
}
68
-
77
+
69
78
/// Creates a source repository hosted by the GitLab service.
70
79
/// - Parameters:
71
80
/// - checkoutPath: The path of the local checkout.
@@ -77,7 +86,7 @@ public extension SourceRepository {
77
86
formatLineNumber: { line in " L \( line) " }
78
87
)
79
88
}
80
-
89
+
81
90
/// Creates a source repository hosted by the BitBucket service.
82
91
/// - Parameters:
83
92
/// - checkoutPath: The path of the local checkout.
@@ -89,7 +98,7 @@ public extension SourceRepository {
89
98
formatLineNumber: { line in " lines- \( line) " }
90
99
)
91
100
}
92
-
101
+
93
102
/// Creates a source repository hosted by the device's filesystem.
94
103
///
95
104
/// Use this source repository to format `doc-source-file://` links to files on the
@@ -98,7 +107,7 @@ public extension SourceRepository {
98
107
/// This source repository uses a custom scheme to offer more control local source file navigation.
99
108
static func localFilesystem( ) -> SourceRepository {
100
109
SourceRepository (
101
- checkoutPath: " " ,
110
+ checkoutPath: " / " ,
102
111
// 2 slashes to specify an empty authority/host component and 1 slash to specify a base path at the root.
103
112
sourceServiceBaseURL: URL ( string: " doc-source-file:/// " ) !,
104
113
formatLineNumber: { line in " L \( line) " }
0 commit comments