@@ -15,7 +15,7 @@ public class SnippetExtractor {
15
15
16
16
enum SymbolGraphExtractionResult {
17
17
case packageDoesNotProduceSnippets
18
- case packageContainsSnippets( symbolGraphDirectory : URL )
18
+ case packageContainsSnippets( symbolGraphFile : URL )
19
19
}
20
20
21
21
private let snippetTool : URL
@@ -65,6 +65,27 @@ public class SnippetExtractor {
65
65
var _fileExists : ( _ path: String ) -> Bool = { path in
66
66
return FileManager . default. fileExists ( atPath: path)
67
67
}
68
+
69
+ /// Returns all of the `.swift` files under a directory recursively.
70
+ ///
71
+ /// Provided for testing.
72
+ var _findSnippetFilesInDirectory : ( _ directory: URL ) -> [ String ] = { directory -> [ String ] in
73
+ guard let snippetEnumerator = FileManager . default. enumerator ( at: directory,
74
+ includingPropertiesForKeys: nil ,
75
+ options: [ . skipsHiddenFiles] ) else {
76
+ return [ ]
77
+
78
+ }
79
+ var snippetInputFiles = [ String] ( )
80
+ for case let potentialSnippetURL as URL in snippetEnumerator {
81
+ guard potentialSnippetURL. pathExtension. lowercased ( ) == " swift " else {
82
+ continue
83
+ }
84
+ snippetInputFiles. append ( potentialSnippetURL. path)
85
+ }
86
+
87
+ return snippetInputFiles
88
+ }
68
89
69
90
/// Generate snippets for the given package.
70
91
///
@@ -79,16 +100,15 @@ public class SnippetExtractor {
79
100
/// The snippet extractor will look for a `Snippets` subdirectory
80
101
/// within this directory.
81
102
///
82
- /// - Returns: A URL for the directory containing the generated snippets or nil if
83
- /// no snippets were produced.
103
+ /// - Returns: A URL for the output file of the generated snippets symbol graph JSON file.
84
104
public func generateSnippets(
85
105
for packageIdentifier: PackageIdentifier ,
86
106
packageDisplayName: String ,
87
107
packageDirectory: URL
88
108
) throws -> URL ? {
89
109
switch snippetSymbolGraphExtractionResults [ packageIdentifier] {
90
- case . packageContainsSnippets( symbolGraphDirectory : let symbolGraphDirectory ) :
91
- return symbolGraphDirectory
110
+ case . packageContainsSnippets( symbolGraphFile : let symbolGraphFile ) :
111
+ return symbolGraphFile
92
112
case . packageDoesNotProduceSnippets:
93
113
return nil
94
114
case . none:
@@ -100,26 +120,34 @@ public class SnippetExtractor {
100
120
snippetSymbolGraphExtractionResults [ packageIdentifier] = . packageDoesNotProduceSnippets
101
121
return nil
102
122
}
123
+
124
+ let snippetInputFiles = _findSnippetFilesInDirectory ( snippetsDirectory)
125
+
126
+ guard !snippetInputFiles. isEmpty else {
127
+ snippetSymbolGraphExtractionResults [ packageIdentifier] = . packageDoesNotProduceSnippets
128
+ return nil
129
+ }
103
130
104
131
let outputDirectory = snippetsOutputDirectory (
105
132
in: workingDirectory,
106
133
packageIdentifier: packageIdentifier,
107
134
packageDisplayName: packageDisplayName
108
135
)
136
+
137
+ let outputFile = outputDirectory. appendingPathComponent ( " \( packageDisplayName) -snippets.symbols.json " )
109
138
110
139
let process = Process ( )
111
140
process. executableURL = snippetTool
112
141
process. arguments = [
113
- snippetsDirectory. path,
114
- outputDirectory. path,
115
- packageDisplayName,
116
- ]
117
-
142
+ " --output " , outputFile. path,
143
+ " --module-name " , packageDisplayName,
144
+ ] + snippetInputFiles
145
+
118
146
try _runProcess ( process)
119
147
120
- if _fileExists ( outputDirectory . path) {
121
- snippetSymbolGraphExtractionResults [ packageIdentifier] = . packageContainsSnippets( symbolGraphDirectory : outputDirectory )
122
- return outputDirectory
148
+ if _fileExists ( outputFile . path) {
149
+ snippetSymbolGraphExtractionResults [ packageIdentifier] = . packageContainsSnippets( symbolGraphFile : outputFile )
150
+ return outputFile
123
151
} else {
124
152
snippetSymbolGraphExtractionResults [ packageIdentifier] = . packageDoesNotProduceSnippets
125
153
return nil
0 commit comments