@@ -50,9 +50,9 @@ public struct VFSOverlay: Encodable {
50
50
case contents
51
51
}
52
52
53
- private let contents : [ File ]
53
+ private let contents : [ Resource ]
54
54
55
- public init ( name: String , contents: [ File ] ) {
55
+ public init ( name: String , contents: [ Resource ] ) {
56
56
self . contents = contents
57
57
super. init ( name: name, type: " directory " )
58
58
}
@@ -89,3 +89,44 @@ public struct VFSOverlay: Encodable {
89
89
try JSONEncoder . makeWithDefaults ( prettified: false ) . encode ( path: path, fileSystem: fileSystem, self )
90
90
}
91
91
}
92
+
93
+ public extension VFSOverlay {
94
+ /// Returns a tree of `VFSOverlay` resources for a given directory in the form of an array. Each item
95
+ /// in this array will be a resource (either file or directory) from the top most level of the given directory.
96
+ /// - Parameters:
97
+ /// - directoryPath: The directory to recursively search for resources in.
98
+ /// - fileSystem: The file system to search.
99
+ /// - Returns: An array of `VFSOverlay.Resource`s from the given directory.
100
+ /// - Throws: An error if the given path is a not a directory.
101
+ /// - Note: This API will recursively scan all subpaths of the given path.
102
+ static func overlayResources(
103
+ directoryPath: AbsolutePath ,
104
+ fileSystem: FileSystem
105
+ ) throws -> [ VFSOverlay . Resource ] {
106
+ return
107
+ // Absolute path to each resource in the directory.
108
+ try fileSystem. getDirectoryContents ( directoryPath) . map ( directoryPath. appending ( component: ) )
109
+ // Map each path to a corresponding VFSOverlay, recursing for directories.
110
+ . compactMap { resourcePath in
111
+ if fileSystem. isDirectory ( resourcePath) {
112
+ return VFSOverlay . Directory (
113
+ name: resourcePath. basename,
114
+ contents:
115
+ try overlayResources (
116
+ directoryPath: resourcePath,
117
+ fileSystem: fileSystem
118
+ )
119
+ )
120
+ } else if fileSystem. isFile ( resourcePath) {
121
+ return VFSOverlay . File (
122
+ name: resourcePath. basename,
123
+ externalContents: resourcePath. pathString
124
+ )
125
+ } else {
126
+ // This case is not expected to be reached as a resource
127
+ // should be either a file or directory.
128
+ return nil
129
+ }
130
+ }
131
+ }
132
+ }
0 commit comments