Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions Xcode/Xcode/Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@

import Foundation

func += <KeyType, ValueType> (inout left: Dictionary<KeyType, ValueType>, right: Dictionary<KeyType, ValueType>) {
func += <KeyType, ValueType> ( left: inout Dictionary<KeyType, ValueType>, right: Dictionary<KeyType, ValueType>) {
for (k, v) in right {
left.updateValue(v, forKey: k)
}
}

extension SequenceType {
func ofType<T>(type: T.Type) -> [T] {
extension Sequence {
func ofType<T>(_ type: T.Type) -> [T] {
return self.flatMap { $0 as? T }
}

func any(pred: Generator.Element -> Bool) -> Bool {
func any(pred: (Iterator.Element) -> Bool) -> Bool {
for elem in self {
if pred(elem) {
return true
Expand All @@ -29,8 +29,8 @@ extension SequenceType {
return false
}

func groupBy<Key: Hashable>(keySelector: Generator.Element -> Key) -> [Key : [Generator.Element]] {
var groupedBy = Dictionary<Key, [Generator.Element]>()
func groupBy<Key: Hashable>(_ keySelector: (Iterator.Element) -> Key) -> [Key : [Iterator.Element]] {
var groupedBy = Dictionary<Key, [Iterator.Element]>()

for element in self {
let key = keySelector(element)
Expand All @@ -44,7 +44,7 @@ extension SequenceType {
return groupedBy
}

func sortBy<U: Comparable>(keySelector: Generator.Element -> U) -> [Generator.Element] {
return self.sort { keySelector($0) < keySelector($1) }
func sortBy<U: Comparable>(_ keySelector: (Iterator.Element) -> U) -> [Iterator.Element] {
return self.sorted { keySelector($0) < keySelector($1) }
}
}
}
14 changes: 7 additions & 7 deletions Xcode/Xcode/PBXObject.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public /* abstract */ class PBXObject {
self.allObjects = allObjects
}

func bool(key: String) -> Bool? {
func bool(_ key: String) -> Bool? {
guard let string = dict[key] as? String else { return nil }

switch string {
Expand All @@ -36,15 +36,15 @@ public /* abstract */ class PBXObject {
}
}

func string(key: String) -> String? {
func string(_ key: String) -> String? {
return dict[key] as? String
}

func strings(key: String) -> [String]? {
func strings(_ key: String) -> [String]? {
return dict[key] as? [String]
}

func object<T : PBXObject>(key: String) -> T? {
func object<T : PBXObject>(_ key: String) -> T? {
guard let objectKey = dict[key] as? String else {
return nil
}
Expand All @@ -53,12 +53,12 @@ public /* abstract */ class PBXObject {
return obj
}

func object<T : PBXObject>(key: String) -> T {
func object<T : PBXObject>(_ key: String) -> T {
let objectKey = dict[key] as! String
return allObjects.object(objectKey)
}

func objects<T : PBXObject>(key: String) -> [T] {
func objects<T : PBXObject>(_ key: String) -> [T] {
let objectKeys = dict[key] as! [String]
return objectKeys.map(allObjects.object)
}
Expand Down Expand Up @@ -164,7 +164,7 @@ public class PBXGroup : PBXReference {

// convenience accessors
public lazy var subGroups: [PBXGroup] = self.children.ofType(PBXGroup.self)
public lazy var fileRefs: [PBXFileReference] = self.children.ofType(PBXFileReference)
public lazy var fileRefs: [PBXFileReference] = self.children.ofType(PBXFileReference.self)
}

public class PBXVariantGroup : PBXGroup {
Expand Down
73 changes: 37 additions & 36 deletions Xcode/Xcode/Serialization.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,48 +10,49 @@ import Foundation

extension XCProjectFile {

public func writeToXcodeproj(xcodeprojURL url: NSURL, format: NSPropertyListFormat? = nil) throws -> Bool {
public func writeToXcodeproj(xcodeprojURL url: URL, format: PropertyListSerialization.PropertyListFormat? = nil) throws -> Bool {

try NSFileManager.defaultManager().createDirectoryAtURL(url, withIntermediateDirectories: true, attributes: nil)
try FileManager.default.createDirectory(at: url, withIntermediateDirectories: true, attributes: nil)

let name = try XCProjectFile.projectName(url)
guard let path = url.URLByAppendingPathComponent("project.pbxproj", isDirectory: false) else {
let name = try XCProjectFile.projectName(url: url)
guard let path = try? url.appendingPathComponent("project.pbxproj", isDirectory: false) else {
throw ProjectFileError.MissingPbxproj
}

let serializer = Serializer(projectName: name, projectFile: self)
let plformat = format ?? self.format

if plformat == NSPropertyListFormat.OpenStepFormat {
try serializer.openStepSerialization.writeToURL(path, atomically: true, encoding: NSUTF8StringEncoding)
if plformat == PropertyListSerialization.PropertyListFormat.openStep {
try serializer.openStepSerialization.write(to: path, atomically: true, encoding: String.Encoding.utf8)
return true
}
else {
let data = try NSPropertyListSerialization.dataWithPropertyList(dict, format: plformat, options: 0)
return data.writeToURL(path, atomically: true)
let data = try PropertyListSerialization.data(fromPropertyList: dict, format: plformat, options: 0)
try data.write(to: path, options: [.atomic])
return true
}
}

public func serialize(projectName: String) throws -> NSData {

let serializer = Serializer(projectName: projectName, projectFile: self)

if format == NSPropertyListFormat.OpenStepFormat {
return serializer.openStepSerialization.dataUsingEncoding(NSUTF8StringEncoding)!
if format == PropertyListSerialization.PropertyListFormat.openStep {
return serializer.openStepSerialization.data(using: String.Encoding.utf8)!
}
else {
return try NSPropertyListSerialization.dataWithPropertyList(dict, format: format, options: 0)
return try PropertyListSerialization.data(fromPropertyList: dict, format: format, options: 0)
}
}
}

let nonescapeRegex = try! NSRegularExpression(pattern: "^[a-z0-9_\\.\\/]+$", options: NSRegularExpressionOptions.CaseInsensitive)
let nonescapeRegex = try! RegularExpression(pattern: "^[a-z0-9_\\.\\/]+$", options: [.caseInsensitive])
let specialRegexes = [
"\\\\": try! NSRegularExpression(pattern: "\\\\", options: []),
"\\\"": try! NSRegularExpression(pattern: "\"", options: []),
"\\n": try! NSRegularExpression(pattern: "\\n", options: []),
"\\r": try! NSRegularExpression(pattern: "\\r", options: []),
"\\t": try! NSRegularExpression(pattern: "\\t", options: []),
"\\\\": try! RegularExpression(pattern: "\\\\", options: []),
"\\\"": try! RegularExpression(pattern: "\"", options: []),
"\\n": try! RegularExpression(pattern: "\\n", options: []),
"\\r": try! RegularExpression(pattern: "\\r", options: []),
"\\t": try! RegularExpression(pattern: "\\t", options: []),
]

internal class Serializer {
Expand All @@ -75,7 +76,7 @@ internal class Serializer {

lazy var buildPhaseByFileId: [String: PBXBuildPhase] = {

let buildPhases = self.projectFile.allObjects.dict.values.ofType(PBXBuildPhase)
let buildPhases = self.projectFile.allObjects.dict.values.ofType(PBXBuildPhase.self)

var dict: [String: PBXBuildPhase] = [:]
for buildPhase in buildPhases {
Expand All @@ -93,7 +94,7 @@ internal class Serializer {
"{",
]

for key in projectFile.dict.keys.sort() {
for key in projectFile.dict.keys.sorted() {
let val: AnyObject = projectFile.dict[key]!

if key == "objects" {
Expand All @@ -112,14 +113,14 @@ internal class Serializer {

let multiline = isa != "PBXBuildFile" && isa != "PBXFileReference"

let parts = rows(isa, objKey: object.id, multiline: multiline, dict: object.dict)
let parts = rows(type: isa, objKey: object.id, multiline: multiline, dict: object.dict)
if multiline {
for ln in parts {
lines.append("\t\t" + ln)
}
}
else {
lines.append("\t\t" + parts.joinWithSeparator(""))
lines.append("\t\t" + parts.joined(separator: ""))
}
}

Expand All @@ -134,18 +135,18 @@ internal class Serializer {
}

let row = "\(key) = \(val)\(comment);"
for line in row.componentsSeparatedByString("\n") {
for line in row.components(separatedBy: "\n") {
lines.append("\t\(line)")
}
}
}

lines.append("}\n")

return lines.joinWithSeparator("\n")
return lines.joined(separator: "\n")
}

func comment(key: String) -> String? {
func comment(_ key: String) -> String? {
if key == projectFile.project.id {
return "Project object"
}
Expand Down Expand Up @@ -205,24 +206,24 @@ internal class Serializer {
return nil
}

func valStr(val: String) -> String {
func valStr(_ val: String) -> String {

var str = val
for (replacement, regex) in specialRegexes {
let range = NSRange(location: 0, length: str.utf16.count)
let template = NSRegularExpression.escapedTemplateForString(replacement)
str = regex.stringByReplacingMatchesInString(str, options: [], range: range, withTemplate: template)
let template = RegularExpression.escapedTemplate(for: replacement)
str = regex.stringByReplacingMatches(in: str, options: [], range: range, withTemplate: template)
}

let range = NSRange(location: 0, length: str.utf16.count)
if let _ = nonescapeRegex.firstMatchInString(str, options: [], range: range) {
if let _ = nonescapeRegex.firstMatch(in: str, options: [], range: range) {
return str
}

return "\"\(str)\""
}

func objval(key: String, val: AnyObject, multiline: Bool) -> [String] {
func objval(_ key: String, val: AnyObject, multiline: Bool) -> [String] {
var parts: [String] = []
let keyStr = valStr(key)

Expand All @@ -247,7 +248,7 @@ internal class Serializer {
parts.append(");")
}
else {
parts.append(ps.map { $0 + " "}.joinWithSeparator("") + "); ")
parts.append(ps.map { $0 + " "}.joined(separator: "") + "); ")
}

}
Expand All @@ -259,7 +260,7 @@ internal class Serializer {
parts.append("\t{")
}

for valKey in valObj.keys.sort() {
for valKey in valObj.keys.sorted() {
let valVal: AnyObject = valObj[valKey]!
let ps = objval(valKey, val: valVal, multiline: multiline)

Expand All @@ -269,7 +270,7 @@ internal class Serializer {
}
}
else {
parts.append("\t" + ps.joinWithSeparator("") + "}; ")
parts.append("\t" + ps.joined(separator: "") + "}; ")
}
}

Expand All @@ -284,7 +285,7 @@ internal class Serializer {
else if let valObj = val as? JsonObject {
parts.append("\(keyStr) = {")

for valKey in valObj.keys.sort() {
for valKey in valObj.keys.sorted() {
let valVal: AnyObject = valObj[valKey]!
let ps = objval(valKey, val: valVal, multiline: multiline)

Expand All @@ -294,7 +295,7 @@ internal class Serializer {
}
}
else {
parts.append(ps.joinWithSeparator("") + "}; ")
parts.append(ps.joined(separator: "") + "}; ")
}
}

Expand Down Expand Up @@ -336,7 +337,7 @@ internal class Serializer {
parts.append("isa = \(type); ")
}

for key in dict.keys.sort() {
for key in dict.keys.sorted() {
if key == "isa" { continue }
let val: AnyObject = dict[key]!

Expand All @@ -363,7 +364,7 @@ internal class Serializer {
return lines
}
else {
return [opening + parts.joinWithSeparator("") + closing]
return [opening + parts.joined(separator: "") + closing]
}
}
}
Loading