Skip to content

Commit

Permalink
Fix groups (#144)
Browse files Browse the repository at this point in the history
* failing createIntermediateDirectories test

* refactor sources

* add reference generator tests
  • Loading branch information
yonaskolb authored Nov 12, 2017
1 parent 975cc8b commit a2c4191
Show file tree
Hide file tree
Showing 46 changed files with 691 additions and 483 deletions.
10 changes: 5 additions & 5 deletions Sources/ProjectSpec/Target.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public struct Target {
public var type: PBXProductType
public var platform: Platform
public var settings: Settings
public var sources: [Source]
public var sources: [TargetSource]
public var dependencies: [Dependency]
public var prebuildScripts: [BuildScript]
public var postbuildScripts: [BuildScript]
Expand All @@ -30,7 +30,7 @@ public struct Target {
return name
}

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) {
public init(name: String, type: PBXProductType, platform: Platform, settings: Settings = .empty, configFiles: [String: String] = [:], sources: [TargetSource] = [], dependencies: [Dependency] = [], prebuildScripts: [BuildScript] = [], postbuildScripts: [BuildScript] = [], scheme: TargetScheme? = nil) {
self.name = name
self.type = type
self.platform = platform
Expand Down Expand Up @@ -179,13 +179,13 @@ extension Target: NamedJSONDictionaryConvertible {
settings = jsonDictionary.json(atKeyPath: "settings") ?? .empty
configFiles = jsonDictionary.json(atKeyPath: "configFiles") ?? [:]
if let source: String = jsonDictionary.json(atKeyPath: "sources") {
sources = [Source(path: source)]
sources = [TargetSource(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)
return TargetSource(path: string)
} else if let dictionary = source as? [String: Any] {
return try Source(jsonDictionary: dictionary)
return try TargetSource(jsonDictionary: dictionary)
} else {
return nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import Foundation
import JSONUtilities
import PathKit

public struct Source {
public struct TargetSource {

public var path: String
public var compilerFlags: [String]
Expand All @@ -22,22 +22,22 @@ public struct Source {
}
}

extension Source: ExpressibleByStringLiteral {
extension TargetSource: ExpressibleByStringLiteral {

public init(stringLiteral value: String) {
self = Source(path: value)
self = TargetSource(path: value)
}

public init(extendedGraphemeClusterLiteral value: String) {
self = Source(path: value)
self = TargetSource(path: value)
}

public init(unicodeScalarLiteral value: String) {
self = Source(path: value)
self = TargetSource(path: value)
}
}

extension Source: JSONObjectConvertible {
extension TargetSource: JSONObjectConvertible {

public init(jsonDictionary: JSONDictionary) throws {
path = try jsonDictionary.json(atKeyPath: "path")
Expand All @@ -51,16 +51,16 @@ extension Source: JSONObjectConvertible {
}
}

extension Source: Equatable {
extension TargetSource: Equatable {

public static func == (lhs: Source, rhs: Source) -> Bool {
public static func == (lhs: TargetSource, rhs: TargetSource) -> Bool {
return lhs.path == rhs.path
&& lhs.compilerFlags == rhs.compilerFlags
&& lhs.excludes == rhs.excludes
}
}

extension Source: Hashable {
extension TargetSource: Hashable {
public var hashValue: Int {
return path.hashValue
^ compilerFlags.joined(separator: ":").hashValue
Expand Down
399 changes: 81 additions & 318 deletions Sources/XcodeGenKit/PBXProjGenerator.swift

Large diffs are not rendered by default.

38 changes: 38 additions & 0 deletions Sources/XcodeGenKit/ReferenceGenerator.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//
// ReferenceGenerator.swift
// XcodeGenKit
//
// Created by Yonas Kolb on 11/11/17.
//

import Foundation
import xcproj

public class ReferenceGenerator {

private var references: Set<String> = []

public init() {

}

public func generate<T: PBXObject>(_ element: T.Type, _ id: String) -> String {
var uuid: String = ""
var counter: UInt = 0
let className: String = String(describing: T.self)
.replacingOccurrences(of: "PBX", with: "")
.replacingOccurrences(of: "XC", with: "")
let classAcronym = String(className.filter { String($0).lowercased() != String($0) })
let stringID = String(abs(id.hashValue).description.prefix(10 - classAcronym.count))
repeat {
counter += 1
uuid = "\(classAcronym)\(stringID)\(String(format: "%02d", counter))"
} while (references.contains(uuid))
references.insert(uuid)
return uuid
}

public func clear() {
references.removeAll()
}
}
Loading

0 comments on commit a2c4191

Please sign in to comment.