From 698d8d85b4bb66b1573811086e4eac1742f12435 Mon Sep 17 00:00:00 2001 From: Yonas Kolb Date: Tue, 31 Oct 2017 15:46:47 +0100 Subject: [PATCH] change target source from String to Source struct --- Sources/ProjectSpec/Source.swift | 47 +++++++++++++++++++ Sources/ProjectSpec/SpecValidation.swift | 2 +- Sources/ProjectSpec/Target.swift | 18 +++++-- Tests/XcodeGenKitTests/SpecLoadingTests.swift | 16 +++++++ 4 files changed, 78 insertions(+), 5 deletions(-) create mode 100644 Sources/ProjectSpec/Source.swift diff --git a/Sources/ProjectSpec/Source.swift b/Sources/ProjectSpec/Source.swift new file mode 100644 index 00000000..bba90497 --- /dev/null +++ b/Sources/ProjectSpec/Source.swift @@ -0,0 +1,47 @@ +// +// Source.swift +// ProjectSpec +// +// Created by Yonas Kolb on 31/10/17. +// + +import Foundation +import JSONUtilities + +public struct Source { + + public var path: String + + public init(path: String) { + self.path = path + } +} + +extension Source: ExpressibleByStringLiteral { + + public init(stringLiteral value: String) { + self = Source(path: value) + } + + public init(extendedGraphemeClusterLiteral value: String) { + self = Source(path: value) + } + + public init(unicodeScalarLiteral value: String) { + self = Source(path: value) + } +} + +extension Source: JSONObjectConvertible { + + public init(jsonDictionary: JSONDictionary) throws { + path = try jsonDictionary.json(atKeyPath: "path") + } +} + +extension Source: Equatable { + + public static func == (lhs: Source, rhs: Source) -> Bool { + return lhs.path == rhs.path + } +} diff --git a/Sources/ProjectSpec/SpecValidation.swift b/Sources/ProjectSpec/SpecValidation.swift index caa79e6a..b509bccc 100644 --- a/Sources/ProjectSpec/SpecValidation.swift +++ b/Sources/ProjectSpec/SpecValidation.swift @@ -69,7 +69,7 @@ extension ProjectSpec { } for source in target.sources { - let sourcePath = basePath + source + let sourcePath = basePath + source.path if !sourcePath.exists { errors.append(.invalidTargetSource(target: target.name, source: sourcePath.string)) } diff --git a/Sources/ProjectSpec/Target.swift b/Sources/ProjectSpec/Target.swift index e9943aa0..91402091 100644 --- a/Sources/ProjectSpec/Target.swift +++ b/Sources/ProjectSpec/Target.swift @@ -15,7 +15,7 @@ public struct Target { public var type: PBXProductType public var platform: Platform public var settings: Settings - public var sources: [String] + public var sources: [Source] public var dependencies: [Dependency] public var prebuildScripts: [BuildScript] public var postbuildScripts: [BuildScript] @@ -30,7 +30,7 @@ public struct Target { return name } - public init(name: String, type: PBXProductType, platform: Platform, settings: Settings = .empty, configFiles: [String: String] = [:], sources: [String] = [], dependencies: [Dependency] = [], prebuildScripts: [BuildScript] = [], postbuildScripts: [BuildScript] = [], scheme: TargetScheme? = nil) { + public init(name: String, type: PBXProductType, platform: Platform, settings: Settings = .empty, configFiles: [String: String] = [:], sources: [Source] = [], dependencies: [Dependency] = [], prebuildScripts: [BuildScript] = [], postbuildScripts: [BuildScript] = [], scheme: TargetScheme? = nil) { self.name = name self.type = type self.platform = platform @@ -179,9 +179,19 @@ extension Target: NamedJSONDictionaryConvertible { settings = jsonDictionary.json(atKeyPath: "settings") ?? .empty configFiles = jsonDictionary.json(atKeyPath: "configFiles") ?? [:] if let source: String = jsonDictionary.json(atKeyPath: "sources") { - sources = [source] + sources = [Source(path: source)] + } else if let array = jsonDictionary["sources"] as? [Any] { + sources = try array.flatMap { source in + if let string = source as? String { + return Source(path: string) + } else if let dictionary = source as? [String: Any] { + return try Source(jsonDictionary: dictionary) + } else { + return nil + } + } } else { - sources = jsonDictionary.json(atKeyPath: "sources") ?? [] + sources = [] } if jsonDictionary["dependencies"] == nil { dependencies = [] diff --git a/Tests/XcodeGenKitTests/SpecLoadingTests.swift b/Tests/XcodeGenKitTests/SpecLoadingTests.swift index b6a39a41..d4103c5f 100644 --- a/Tests/XcodeGenKitTests/SpecLoadingTests.swift +++ b/Tests/XcodeGenKitTests/SpecLoadingTests.swift @@ -68,6 +68,22 @@ func specLoadingTests() { try expectTargetError(target, .invalidDependency([invalid: "name"])) } + $0.it("parses sources") { + var targetDictionary1 = validTarget + targetDictionary1["sources"] = [ + "source1", + ["path": "source2"], + ] + var targetDictionary2 = validTarget + targetDictionary2["sources"] = "source3" + + let target1 = try Target(name: "test", jsonDictionary: targetDictionary1) + let target2 = try Target(name: "test", jsonDictionary: targetDictionary2) + + try expect(target1.sources) == [Source(path: "source1"), Source(path: "source2")] + try expect(target2.sources) == [Source(path: "source3")] + } + $0.it("parses target dependencies") { var targetDictionary = validTarget targetDictionary["dependencies"] = [