Add a PathContaining protocol for expanding paths to the source file's directory

This commit is contained in:
Ell Neal 2019-01-13 18:18:48 +00:00
parent a2684a96a4
commit f9df768ec0
No known key found for this signature in database
GPG Key ID: 0AAD6C98BA484C61

View File

@ -11,3 +11,47 @@ extension Project {
try self.init(spec: template, basePath: basePath)
}
}
protocol PathContaining {
associatedtype JSONSourceType
static func expandPaths(for source: JSONSourceType, relativeTo path: Path) -> JSONSourceType
}
extension PathContaining {
static func expandStringPaths(from source: JSONDictionary, forKey key: String, relativeTo path: Path) -> JSONDictionary {
var result = source
if let source = result[key] as? String {
result[key] = (path + source).string
} else if let source = result[key] as? [String] {
result[key] = source.map { (path + $0).string }
} else if let source = result[key] as? [String: String] {
result[key] = source.mapValues { (path + $0).string }
}
return result
}
static func expandChildPaths<T: PathContaining>(from source: JSONDictionary, forKey key: String, relativeTo path: Path, type: T.Type) -> JSONDictionary {
var result = source
if let source = result[key] as? T.JSONSourceType {
result[key] = T.expandPaths(for: source, relativeTo: path)
} else if let source = result[key] as? [T.JSONSourceType] {
result[key] = source.map { T.expandPaths(for: $0, relativeTo: path) }
} else if let source = result[key] as? [String: T.JSONSourceType] {
result[key] = source.mapValues { T.expandPaths(for: $0, relativeTo: path) }
}
return result
}
static func expandChildPaths<T: PathContaining>(from source: JSONDictionary, forPotentialKeys keys: [String], relativeTo path: Path, type: T.Type) -> JSONDictionary {
var result = source
for key in keys {
result = expandChildPaths(from: result, forKey: key, relativeTo: path, type: type)
}
return result
}
}