XcodeGen/Sources/ProjectSpec/SpecLoader.swift
Craig Siemens 0500db212d
Handle imports with same relative path (#1262)
* Added test for when includes contain relative paths with the same name.

* Updated mergedDictionary to handle includes with the same file path.

* Removed the need to multiple places to pass in cachedSpecFiles.

* Converts the projectRoot into a absolute path before loading a project.

* Updated changelog.
2023-01-16 17:57:43 +11:00

74 lines
2.2 KiB
Swift

import Foundation
import JSONUtilities
import PathKit
import XcodeProj
import Yams
import Version
public class SpecLoader {
var project: Project!
public private(set) var projectDictionary: [String: Any]?
let version: Version
public init(version: Version) {
self.version = version
}
public func loadProject(path: Path, projectRoot: Path? = nil, variables: [String: String] = [:]) throws -> Project {
let projectRoot = projectRoot?.absolute()
let spec = try SpecFile(path: path, projectRoot: projectRoot, variables: variables)
let resolvedDictionary = spec.resolvedDictionary()
let project = try Project(basePath: projectRoot ?? spec.basePath, jsonDictionary: resolvedDictionary)
self.project = project
projectDictionary = resolvedDictionary
return project
}
public func validateProjectDictionaryWarnings() throws {
try projectDictionary?.validateWarnings()
}
public func generateCacheFile() throws -> CacheFile? {
guard let projectDictionary = projectDictionary,
let project = project else {
return nil
}
return try CacheFile(
version: version,
projectDictionary: projectDictionary,
project: project
)
}
}
private extension Dictionary where Key == String, Value: Any {
func validateWarnings() throws {
let errors: [SpecValidationError.ValidationError] = []
if !errors.isEmpty {
throw SpecValidationError(errors: errors)
}
}
func hasValueContaining(_ needle: String) -> Bool {
values.contains { value in
switch value {
case let dictionary as JSONDictionary:
return dictionary.hasValueContaining(needle)
case let string as String:
return string.contains(needle)
case let array as [JSONDictionary]:
return array.contains { $0.hasValueContaining(needle) }
case let array as [String]:
return array.contains { $0.contains(needle) }
default:
return false
}
}
}
}