diff --git a/.swiftpm/xcode/xcshareddata/xcschemes/SwiftPM-Package.xcscheme b/.swiftpm/xcode/xcshareddata/xcschemes/SwiftPM-Package.xcscheme index 91aaea3c058..780e7e3820f 100644 --- a/.swiftpm/xcode/xcshareddata/xcschemes/SwiftPM-Package.xcscheme +++ b/.swiftpm/xcode/xcshareddata/xcschemes/SwiftPM-Package.xcscheme @@ -829,6 +829,16 @@ ReferencedContainer = "container:"> + + + + [Node] { + [.virtual(Self.name), .virtual(entitlement)] + } + + public static func getFileContents(inputs: [Node]) throws -> String { + guard let entitlementName = inputs.last?.extractedVirtualNodeName else { + throw Error.undefinedEntitlementName + } + let encoder = PropertyListEncoder() + encoder.outputFormat = .xml + let result = try encoder.encode([entitlementName: true]) + + let contents = String(decoding: result, as: UTF8.self) + return contents + } + + private enum Error: Swift.Error { + case undefinedEntitlementName + } + } public struct LinkFileList: AuxiliaryFileType { public static let name = "link-file-list" @@ -108,7 +138,7 @@ public enum WriteAuxiliary { } public static func getFileContents(inputs: [Node]) throws -> String { - guard let principalClass = inputs.last?.name.dropFirst().dropLast() else { + guard let principalClass = inputs.last?.extractedVirtualNodeName else { throw Error.undefinedPrincipalClass } @@ -206,6 +236,13 @@ public struct BuildManifest { commands[name] = Command(name: name, tool: tool) } + public mutating func addEntitlementPlistCommand(entitlement: String, outputPath: AbsolutePath) { + let inputs = WriteAuxiliary.EntitlementPlist.computeInputs(entitlement: entitlement) + let tool = WriteAuxiliaryFile(inputs: inputs, outputFilePath: outputPath) + let name = outputPath.pathString + commands[name] = Command(name: name, tool: tool) + } + public mutating func addWriteLinkFileListCommand( objects: [AbsolutePath], linkFileListPath: AbsolutePath diff --git a/Sources/LLBuildManifest/Node.swift b/Sources/LLBuildManifest/Node.swift index 9800cb34e64..82b5bfbe19e 100644 --- a/Sources/LLBuildManifest/Node.swift +++ b/Sources/LLBuildManifest/Node.swift @@ -2,7 +2,7 @@ // // This source file is part of the Swift open source project // -// Copyright (c) 2014-2019 Apple Inc. and the Swift project authors +// Copyright (c) 2014-2023 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 @@ -30,6 +30,12 @@ public struct Node: Hashable, Codable { self.name = name self.kind = kind } + + /// Extracts `name` property if this node was constructed as `Node//virtual`. + public var extractedVirtualNodeName: String { + precondition(kind == .virtual) + return String(self.name.dropFirst().dropLast()) + } public static func virtual(_ name: String) -> Node { precondition(name.first != "<" && name.last != ">", "<> will be inserted automatically") diff --git a/Sources/LLBuildManifest/Tools.swift b/Sources/LLBuildManifest/Tools.swift index 7b641d539d9..b9bf571cbf8 100644 --- a/Sources/LLBuildManifest/Tools.swift +++ b/Sources/LLBuildManifest/Tools.swift @@ -2,7 +2,7 @@ // // This source file is part of the Swift open source project // -// Copyright (c) 2014-2021 Apple Inc. and the Swift project authors +// Copyright (c) 2014-2023 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 @@ -155,7 +155,7 @@ public struct ShellTool: ToolProtocol { } } -public struct WriteAuxiliaryFile: ToolProtocol { +public struct WriteAuxiliaryFile: Equatable, ToolProtocol { public static let name: String = "write-auxiliary-file" public let inputs: [Node] diff --git a/Tests/BuildTests/LLBuildManifestTests.swift b/Tests/LLBuildManifestTests/LLBuildManifestTests.swift similarity index 75% rename from Tests/BuildTests/LLBuildManifestTests.swift rename to Tests/LLBuildManifestTests/LLBuildManifestTests.swift index becb6aacbb5..24b5be4bf6e 100644 --- a/Tests/BuildTests/LLBuildManifestTests.swift +++ b/Tests/LLBuildManifestTests/LLBuildManifestTests.swift @@ -2,7 +2,7 @@ // // This source file is part of the Swift open source project // -// Copyright (c) 2014-2021 Apple Inc. and the Swift project authors +// Copyright (c) 2014-2023 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 @@ -10,14 +10,39 @@ // //===----------------------------------------------------------------------===// -import Basics -import LLBuildManifest +import struct Basics.AbsolutePath +import class Foundation.PropertyListDecoder +@testable import LLBuildManifest +import SPMTestSupport +import class TSCBasic.InMemoryFileSystem import XCTest -import class TSCBasic.InMemoryFileSystem -// FIXME: This should be in its own test target. +private let testEntitlement = "test-entitlement" + final class LLBuildManifestTests: XCTestCase { + func testEntitlementsPlist() throws { + let FileType = WriteAuxiliary.EntitlementPlist.self + let inputs = FileType.computeInputs(entitlement: testEntitlement) + XCTAssertEqual(inputs, [.virtual(FileType.name), .virtual(testEntitlement)]) + + let contents = try FileType.getFileContents(inputs: inputs) + let decoder = PropertyListDecoder() + let decodedEntitlements = try decoder.decode([String: Bool].self, from: .init(contents.utf8)) + XCTAssertEqual(decodedEntitlements, [testEntitlement: true]) + + var manifest = BuildManifest() + let outputPath = AbsolutePath("/test.plist") + manifest.addEntitlementPlistCommand(entitlement: testEntitlement, outputPath: outputPath) + + let commandName = outputPath.pathString + XCTAssertEqual(manifest.commands.count, 1) + + let command = try XCTUnwrap(manifest.commands[commandName]?.tool as? WriteAuxiliaryFile) + + XCTAssertEqual(command, .init(inputs: inputs, outputFilePath: outputPath)) + } + func testBasics() throws { var manifest = BuildManifest()