Skip to content

Add plist enum to Xcodeproj #622

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 2, 2016
Merged
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
48 changes: 30 additions & 18 deletions Sources/Xcodeproj/Module+PBXProj.swift
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ extension Module {
}
}

var headerSearchPaths: (key: String, value: String)? {
var headerSearchPaths: (key: String, value: Any)? {
let headerPathKey = "HEADER_SEARCH_PATHS"
var headerPaths = dependencies.flatMap { module -> AbsolutePath? in
switch module {
Expand All @@ -174,36 +174,48 @@ extension Module {
}

guard !headerPaths.isEmpty else { return nil }

if headerPaths.count == 1, let first = headerPaths.first {
return (headerPathKey, first.asString)
}

let headerPathValue = headerPaths.map{ $0.asString }.joined(separator: " ")

return (headerPathKey, headerPathValue)
return (headerPathKey, headerPaths.map { $0.asString } )
}

func getDebugBuildSettings(_ options: XcodeprojOptions, xcodeProjectPath: AbsolutePath) throws -> String {
var buildSettings = try getCommonBuildSettings(options, xcodeProjectPath: xcodeProjectPath)
if let headerSearchPaths = headerSearchPaths {
buildSettings[headerSearchPaths.key] = headerSearchPaths.value
}
// FIXME: Need to honor actual quoting rules here.
return buildSettings.map{ "\($0) = '\($1)';" }.joined(separator: " ")
return toPlist(buildSettings).serialize()
}

func getReleaseBuildSettings(_ options: XcodeprojOptions, xcodeProjectPath: AbsolutePath) throws -> String {
var buildSettings = try getCommonBuildSettings(options, xcodeProjectPath: xcodeProjectPath)
if let headerSearchPaths = headerSearchPaths {
buildSettings[headerSearchPaths.key] = headerSearchPaths.value
}
// FIXME: Need to honor actual quoting rules here.
return buildSettings.map{ "\($0) = '\($1)';" }.joined(separator: " ")
return toPlist(buildSettings).serialize()
}

/// Converts build settings dictionary to a Plist object.
///
/// Adds string values in dictionaries as is and array values are quoted and then converted
/// to a string joined by whitespace.
private func toPlist(_ buildSettings: [String: Any]) -> Plist {
var buildSettingsPlist = [String: Plist]()
for (k, v) in buildSettings {
switch v {
case let value as String:
buildSettingsPlist[k] = .string(value)
case let value as [String]:
let escaped = value.map { "\"" + Plist.escape(string: $0) + "\"" }.joined(separator: " ")
buildSettingsPlist[k] = .string(escaped)
default:
fatalError("build setting dictionary should only contain String or [String]")
}
}
return .dictionary(buildSettingsPlist)
}

private func getCommonBuildSettings(_ options: XcodeprojOptions, xcodeProjectPath: AbsolutePath) throws -> [String: String] {
var buildSettings = [String: String]()
private func getCommonBuildSettings(_ options: XcodeprojOptions, xcodeProjectPath: AbsolutePath) throws -> [String: Any] {
var buildSettings = [String: Any]()
let plistPath = xcodeProjectPath.appending(component: infoPlistFileName)

if isTest {
Expand All @@ -220,7 +232,7 @@ extension Module {
//
// This means the built binaries are not suitable for distribution,
// among other things.
buildSettings["LD_RUNPATH_SEARCH_PATHS"] = "$(TOOLCHAIN_DIR)/usr/lib/swift/macosx"
buildSettings["LD_RUNPATH_SEARCH_PATHS"] = ["$(TOOLCHAIN_DIR)/usr/lib/swift/macosx"]
if isLibrary {
buildSettings["ENABLE_TESTABILITY"] = "YES"

Expand Down Expand Up @@ -259,13 +271,13 @@ extension Module {
// example would be `@executable_path/../lib` but there are
// other problems to solve first, e.g. how to deal with the
// Swift standard library paths).
buildSettings["LD_RUNPATH_SEARCH_PATHS"] = buildSettings["LD_RUNPATH_SEARCH_PATHS"]! + " @executable_path"
buildSettings["LD_RUNPATH_SEARCH_PATHS"] = buildSettings["LD_RUNPATH_SEARCH_PATHS"] as! [String] + ["@executable_path"]
}
}

if let pkgArgs = try? self.pkgConfigArgs() {
buildSettings["OTHER_LDFLAGS"] = (["$(inherited)"] + pkgArgs.libs).joined(separator: " ")
buildSettings["OTHER_SWIFT_FLAGS"] = (["$(inherited)"] + pkgArgs.cFlags).joined(separator: " ")
buildSettings["OTHER_LDFLAGS"] = ["$(inherited)"] + pkgArgs.libs
buildSettings["OTHER_SWIFT_FLAGS"] = ["$(inherited)"] + pkgArgs.cFlags
}

// Add framework search path to build settings.
Expand Down
67 changes: 67 additions & 0 deletions Sources/Xcodeproj/Plist.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
This source file is part of the Swift.org open source project

Copyright 2015 - 2016 Apple Inc. and the Swift project authors
Licensed under Apache License v2.0 with Runtime Library Exception

See http://swift.org/LICENSE.txt for license information
See http://swift.org/CONTRIBUTORS.txt for Swift project authors
*/

/// A enum representing data types for legacy Plist type.
/// see: https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/PropertyLists/OldStylePlists/OldStylePLists.html
enum Plist {
case string(String)
case array([Plist])
case dictionary([String: Plist])
}

extension Plist: ExpressibleByStringLiteral {
public typealias UnicodeScalarLiteralType = StringLiteralType
public typealias ExtendedGraphemeClusterLiteralType = StringLiteralType

public init(unicodeScalarLiteral value: UnicodeScalarLiteralType) {
self = .string(value)
}
public init(extendedGraphemeClusterLiteral value: ExtendedGraphemeClusterLiteralType) {
self = .string(value)
}
public init(stringLiteral value: StringLiteralType) {
self = .string(value)
}
}

extension Plist {
/// Serializes the Plist enum to string.
func serialize() -> String {
switch self {
case .string(let str):
return "\"" + Plist.escape(string: str) + "\""
case .array(let items):
return "(" + items.map { $0.serialize() }.joined(separator: ", ") + ")"
case .dictionary(let items):
return "{" + items.map { " \($0) = \($1.serialize()) " }.joined(separator: "; ") + "; };"
}
}

/// Escapes the string for plist.
/// Finds the instances of quote (") and backward slash (\) and prepends
/// the escape character backward slash (\).
static func escape(string: String) -> String {
func needsEscape(_ char: UInt8) -> Bool {
return char == UInt8(ascii: "\\") || char == UInt8(ascii: "\"")
}

guard let pos = string.utf8.index(where: needsEscape) else {
return string
}
var newString = String(string.utf8[string.utf8.startIndex..<pos])!
for char in string.utf8[pos..<string.utf8.endIndex] {
if needsEscape(char) {
newString += "\\"
}
newString += String(UnicodeScalar(char))
}
return newString
}
}
4 changes: 2 additions & 2 deletions Sources/Xcodeproj/pbxproj().swift
Original file line number Diff line number Diff line change
Expand Up @@ -234,12 +234,12 @@ public func pbxproj(srcroot: AbsolutePath, projectRoot: AbsolutePath, xcodeprojP
print(" };")
print(" \(module.debugConfigurationReference) = {")
print(" isa = XCBuildConfiguration;")
print(" buildSettings = { \(try module.getDebugBuildSettings(options, xcodeProjectPath: xcodeprojPath)) };")
print(" buildSettings = \(try module.getDebugBuildSettings(options, xcodeProjectPath: xcodeprojPath))")
print(" name = Debug;")
print(" };")
print(" \(module.releaseConfigurationReference) = {")
print(" isa = XCBuildConfiguration;")
print(" buildSettings = { \(try module.getReleaseBuildSettings(options, xcodeProjectPath: xcodeprojPath)) };")
print(" buildSettings = \(try module.getReleaseBuildSettings(options, xcodeProjectPath: xcodeprojPath))")
print(" name = Release;")
print(" };")

Expand Down
20 changes: 20 additions & 0 deletions Tests/XcodeprojTests/PlistTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
This source file is part of the Swift.org open source project

Copyright 2015 - 2016 Apple Inc. and the Swift project authors
Licensed under Apache License v2.0 with Runtime Library Exception

See http://swift.org/LICENSE.txt for license information
See http://swift.org/CONTRIBUTORS.txt for Swift project authors
*/

import XCTest
@testable import Xcodeproj

class PlistTests: XCTestCase {
func testBasics() {
XCTAssertEqual("\"hello \\\" world\"", Plist.string("hello \" world").serialize())
XCTAssertEqual("(\"hello world\", \"cool\")", Plist.array([.string("hello world"), .string("cool")]).serialize())
XCTAssertEqual("{ user = \"cool\" ; polo = (\"hello \\\" world\", \"cool\") ; };", Plist.dictionary(["user": .string("cool"), "polo": Plist.array([.string("hello \" world"), .string("cool")])]).serialize())
}
}