Skip to content

Commit 142c29b

Browse files
committed
[PackageLoading] Move PackageBuilder to FileSystem
1 parent f649350 commit 142c29b

File tree

1 file changed

+28
-16
lines changed

1 file changed

+28
-16
lines changed

Sources/PackageLoading/PackageBuilder.swift

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -133,13 +133,17 @@ public struct PackageBuilder {
133133
/// The path of the package.
134134
private let packagePath: AbsolutePath
135135

136+
/// The filesystem package builder will runs on.
137+
private let fileSystem: FileSystem
138+
136139
/// Create a builder for the given manifest and package `path`.
137140
///
138141
/// - Parameters:
139142
/// - path: The root path of the package.
140-
public init(manifest: Manifest, path: AbsolutePath) {
143+
public init(manifest: Manifest, path: AbsolutePath, fileSystem: FileSystem = localFileSystem) {
141144
self.manifest = manifest
142145
self.packagePath = path
146+
self.fileSystem = fileSystem
143147
}
144148

145149
/// Build a new package following the conventions.
@@ -168,9 +172,10 @@ public struct PackageBuilder {
168172
if path.basename.hasPrefix(".") { return false }
169173
if path == manifest.path { return false }
170174
if excludedPaths.contains(path) { return false }
171-
if !path.asString.isFile { return false }
172-
guard let ext = path.asString.fileExt else { return false }
173-
return validExtensions.contains(ext)
175+
if !fileSystem.isFile(path) { return false }
176+
guard let ext = path.suffix else { return false }
177+
// FIXME: Change this when path gets extension property.
178+
return validExtensions.contains(String(ext.utf8.dropFirst()))
174179
}
175180

176181
private func shouldConsiderDirectory(_ path: AbsolutePath) -> Bool {
@@ -182,7 +187,7 @@ public struct PackageBuilder {
182187
if base.hasPrefix(".") { return false } // eg .git
183188
if excludedPaths.contains(path) { return false }
184189
if path == packagesDirectory { return false }
185-
if !path.asString.isDirectory { return false }
190+
if !fileSystem.isDirectory(path) { return false }
186191
return true
187192
}
188193

@@ -198,12 +203,17 @@ public struct PackageBuilder {
198203
guard let pkgConfig = manifest.package.pkgConfig else { return nil }
199204
return RelativePath(pkgConfig)
200205
}
201-
206+
207+
/// Returns path to all the items in a directory.
208+
func directoryContents(_ path: AbsolutePath) throws -> [AbsolutePath] {
209+
return try fileSystem.getDirectoryContents(path).map { path.appending(component: $0) }
210+
}
211+
202212
func sourceRoot() throws -> AbsolutePath {
203-
let viableRoots = try walk(packagePath, recursively: false).filter { entry in
213+
let viableRoots = try directoryContents(packagePath).filter { entry in
204214
switch entry.basename.lowercased() {
205215
case "sources", "source", "src", "srcs":
206-
return entry.asString.isDirectory && !excludedPaths.contains(entry)
216+
return fileSystem.isDirectory(entry) && !excludedPaths.contains(entry)
207217
default:
208218
return false
209219
}
@@ -223,7 +233,7 @@ public struct PackageBuilder {
223233
/// Collects the modules which are defined by a package.
224234
private func constructModules() throws -> [Module] {
225235
let moduleMapPath = packagePath.appending("module.modulemap")
226-
if moduleMapPath.asString.isFile {
236+
if fileSystem.isFile(moduleMapPath) {
227237
let sources = Sources(paths: [moduleMapPath], root: packagePath)
228238
return [try CModule(name: manifest.name, sources: sources, path: packagePath, pkgConfig: pkgConfigPath, providers: manifest.package.providers)]
229239
}
@@ -235,16 +245,16 @@ public struct PackageBuilder {
235245
let srcroot = try sourceRoot()
236246

237247
if srcroot != packagePath {
238-
let invalidRootFiles = try walk(packagePath, recursively: false).filter(isValidSource)
248+
let invalidRootFiles = try directoryContents(packagePath).filter(isValidSource)
239249
guard invalidRootFiles.isEmpty else {
240250
throw ModuleError.invalidLayout(.invalidLayout(invalidRootFiles.map{ $0.asString }))
241251
}
242252
}
243253

244-
let maybeModules = try walk(srcroot, recursively: false).filter(shouldConsiderDirectory)
254+
let maybeModules = try directoryContents(srcroot).filter(shouldConsiderDirectory)
245255

246256
if maybeModules.count == 1 && maybeModules[0] != srcroot {
247-
let invalidModuleFiles = try walk(srcroot, recursively: false).filter(isValidSource)
257+
let invalidModuleFiles = try directoryContents(srcroot).filter(isValidSource)
248258
guard invalidModuleFiles.isEmpty else {
249259
throw ModuleError.invalidLayout(.invalidLayout(invalidModuleFiles.map{ $0.asString }))
250260
}
@@ -313,7 +323,7 @@ public struct PackageBuilder {
313323
}
314324

315325
private func modulify(_ path: AbsolutePath, name: String, isTest: Bool) throws -> Module {
316-
let walked = try walk(path, recursing: shouldConsiderDirectory).map{ $0 }
326+
let walked = try walk(path, fileSystem: fileSystem, recursing: shouldConsiderDirectory).map{ $0 }
317327

318328
let cSources = walked.filter{ isValidSource($0, validExtensions: SupportedLanguageExtension.cFamilyExtensions) }
319329
let swiftSources = walked.filter{ isValidSource($0, validExtensions: SupportedLanguageExtension.swiftExtensions) }
@@ -400,11 +410,13 @@ public struct PackageBuilder {
400410
private func constructTestModules(modules: [Module]) throws -> [Module] {
401411
let testsPath = packagePath.appending("Tests")
402412

403-
// Don't try to walk Tests if it is in excludes.
404-
guard testsPath.asString.isDirectory && !excludedPaths.contains(testsPath) else { return [] }
413+
// Don't try to walk Tests if it is in excludes or doesn't exists.
414+
guard fileSystem.isDirectory(testsPath) && !excludedPaths.contains(testsPath) else {
415+
return []
416+
}
405417

406418
// Create the test modules
407-
let testModules = try walk(testsPath, recursively: false).filter(shouldConsiderDirectory).flatMap { dir in
419+
let testModules = try directoryContents(testsPath).filter(shouldConsiderDirectory).flatMap { dir in
408420
return [try modulify(dir, name: dir.basename, isTest: true)]
409421
}
410422

0 commit comments

Comments
 (0)