1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
|
import Foundation
/// A unit of WIT package managing a collection of WIT source files
public final class PackageUnit: Hashable, CustomStringConvertible {
public let packageName: PackageNameSyntax
public let sourceFiles: [SyntaxNode<SourceFileSyntax>]
init(packageName: PackageNameSyntax, sourceFiles: [SyntaxNode<SourceFileSyntax>]) {
self.packageName = packageName
self.sourceFiles = sourceFiles
}
public var description: String {
"PackageUnit(\(packageName))"
}
public static func == (lhs: PackageUnit, rhs: PackageUnit) -> Bool {
lhs === rhs
}
public func hash(into hasher: inout Hasher) {
hasher.combine(packageName.name.text)
}
}
/// A collection of WIT packages.
///
/// Responsible to find a package that satisfies the given requirement.
public final class PackageResolver: Hashable {
private(set) var packages: [PackageUnit] = []
/// Create a new package resolver.
public init() {}
/// Register a package to this resolver, creating a new package from the given source files.
///
/// - Returns: A newly created package from the given source files.
public func register(packageSources: [SyntaxNode<SourceFileSyntax>]) throws -> PackageUnit {
var packageBuilder = PackageBuilder()
for sourceFile in packageSources {
try packageBuilder.append(sourceFile)
}
let package = try packageBuilder.build()
register(packageUnit: package)
return package
}
/// Register the given package to this resolver.
public func register(packageUnit: PackageUnit) {
packages.append(packageUnit)
}
func findPackage(
namespace: String,
package: String,
version: Version?
) -> PackageUnit? {
for pkg in self.packages {
let found = Self.satisfyRequirement(
pkg: pkg,
namespace: namespace,
packageName: package,
version: version
)
if found { return pkg }
}
return nil
}
private static func satisfyRequirement(
pkg: PackageUnit,
namespace: String,
packageName: String,
version: Version?
) -> Bool {
guard pkg.packageName.namespace.text == namespace,
pkg.packageName.name.text == packageName
else { return false }
// If package user specify version, check package version
if let version {
if let candidateVersion = pkg.packageName.version {
return candidateVersion.isCompatible(with: version)
}
// If candidate does not have a version specification, reject.
return false
}
return true
}
public static func == (lhs: PackageResolver, rhs: PackageResolver) -> Bool {
lhs === rhs
}
public func hash(into hasher: inout Hasher) {
hasher.combine(ObjectIdentifier(self))
}
}
extension Version {
/// Whether this version satisfies the given requirement.
fileprivate func isCompatible(with requirement: Version) -> Bool {
// Currently the same pre-release and build metadata are required
// for compatibility with other WIT tools.
return major == requirement.major && minor == requirement.minor && patch == requirement.patch && prerelease == requirement.prerelease && buildMetadata == requirement.buildMetadata
}
}
// - MARK: Directory structure convention
/// A type to interact with files and directories required to load packages.
public protocol PackageFileLoader {
/// A type that represents a file path in this loader.
associatedtype FilePath: CustomStringConvertible
/// Returns a list of WIT file paths contained in the given package directory.
func packageFiles(in packageDirectory: FilePath) throws -> [FilePath]
/// Returns text contents of a file at the given file path.
func contentsOfWITFile(at filePath: FilePath) throws -> String
/// Returns a list of directory paths contained in the given package directory.
/// Typically, returns directory entries in `deps` directory under the package directory.
func dependencyDirectories(from packageDirectory: FilePath) throws -> [FilePath]
}
extension PackageResolver {
/// Parses a WIT package at the given directory path and its dependency packages.
///
/// - Parameters:
/// - directory: A WIT package directory containing `*.wit` files and optionally `deps` directory.
/// - loader: A file loader used to load package contents.
/// - Returns: A pair of the main package parsed from the given directory directly and package
/// resolver containing a set of packages including dependencies.
public static func parse<Loader: PackageFileLoader>(
directory: Loader.FilePath, loader: Loader
) throws -> (mainPackage: PackageUnit, packageResolver: PackageResolver) {
let packageResolver = PackageResolver()
let mainPackage = try PackageUnit.parse(directory: directory, loader: loader)
packageResolver.register(packageUnit: mainPackage)
for dependency in try loader.dependencyDirectories(from: directory) {
let depPackage = try PackageUnit.parse(directory: dependency, loader: loader)
packageResolver.register(packageUnit: depPackage)
}
return (mainPackage, packageResolver)
}
}
extension PackageUnit {
/// Parses a WIT package at the given directory path.
///
/// - Parameters:
/// - directory: A WIT package directory containing `*.wit` files.
/// - loader: A file loader used to load package contents.
/// - Returns: A package parsed from the given directory.
public static func parse<Loader: PackageFileLoader>(
directory: Loader.FilePath, loader: Loader
) throws -> PackageUnit {
var packageBuilder = PackageBuilder()
for filePath in try loader.packageFiles(in: directory) {
try packageBuilder.append(
SourceFileSyntax.parse(
filePath: filePath,
loader: loader
)
)
}
return try packageBuilder.build()
}
}
extension SourceFileSyntax {
/// Parses a WIT file at the given file path.
///
/// - Parameters:
/// - filePath: A WIT file path.
/// - loader: A file loader used to load package contents.
/// - Returns: A parsed WIT source file representation.
public static func parse<Loader: PackageFileLoader>(
filePath: Loader.FilePath, loader: Loader
) throws -> SyntaxNode<SourceFileSyntax> {
let contents = try loader.contentsOfWITFile(at: filePath)
return try SourceFileSyntax.parse(contents, fileName: filePath.description)
}
/// Parses the given WIT source
///
/// - Parameters:
/// - contents: A WIT source contents
/// - fileName: A file name used for diagnostics
/// - Returns: A parsed WIT source file representation.
public static func parse(_ contents: String, fileName: String) throws -> SyntaxNode<SourceFileSyntax> {
var lexer = Lexer(cursor: Lexer.Cursor(input: contents))
return try SourceFileSyntax.parse(lexer: &lexer, fileName: fileName)
}
}
#if !os(WASI)
/// A ``PackageFileLoader`` adapter for local file system.
public struct LocalFileLoader: PackageFileLoader {
public typealias FilePath = String
let fileManager: FileManager
public init(fileManager: FileManager = .default) {
self.fileManager = fileManager
}
enum Error: Swift.Error {
case failedToLoadFile(FilePath)
}
private func isDirectory(filePath: String) -> Bool {
var isDirectory: ObjCBool = false
let exists = fileManager.fileExists(atPath: filePath, isDirectory: &isDirectory)
return exists && isDirectory.boolValue
}
public func contentsOfWITFile(at filePath: String) throws -> String {
guard let bytes = fileManager.contents(atPath: filePath) else {
throw Error.failedToLoadFile(filePath)
}
return String(decoding: bytes, as: UTF8.self)
}
public func packageFiles(in packageDirectory: String) throws -> [String] {
let dirURL = URL(fileURLWithPath: packageDirectory)
return try fileManager.contentsOfDirectory(atPath: packageDirectory).filter { fileName in
return fileName.hasSuffix(".wit")
&& {
let filePath = dirURL.appendingPathComponent(fileName)
return !isDirectory(filePath: filePath.path)
}()
}
.map { dirURL.appendingPathComponent($0).path }
}
public func dependencyDirectories(from packageDirectory: String) throws -> [String] {
let dirURL = URL(fileURLWithPath: packageDirectory)
let depsDir = dirURL.appendingPathComponent("deps")
guard isDirectory(filePath: depsDir.path) else { return [] }
return try fileManager.contentsOfDirectory(atPath: depsDir.path)
}
}
#endif
|