1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
|
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift open source project
//
// Copyright (c) 2025 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 the list of Swift project authors
//
//===----------------------------------------------------------------------===//
import class Foundation.PropertyListSerialization
import class Foundation.NSError
public import SWBCore
import SWBLibc
public import SWBUtil
/// Concrete implementation of task for processing the provisioning profile for a product.
public final class ProcessProductProvisioningProfileTaskAction: TaskAction
{
public override init()
{
// Presently this class' initializer doesn't do anything extra - all parameters are passed in as command-line options.
super.init()
}
/// The parsed command line options.
private struct Options
{
/// The output format of the task.
enum FormatKind: String
{
/// Preserve the format of the input file.
case sameAsInput = "none"
/// Convert to binary.
case binary = "binary"
/// Convert to XML format.
case xml = "xml"
// Note that Foundation no longer supports writing the OpenStep plist format.
init?(name: String)
{
switch name
{
case "openstep":
// Foundation no longer supports writing OpenStep-format property lists, so we fall back to XML.
self = .xml
default:
// Otherwise we initialize from the raw value, if possible.
if let value = FormatKind(rawValue: name)
{
self = value
return
}
else
{
return nil
}
}
}
}
static func emitUsage(_ name: String, _ outputDelegate: any TaskOutputDelegate)
{
outputDelegate.emitOutput
{ stream in
stream <<< "usage: \(name) [-format <name>] <input-file> -o <output-path>\n"
stream <<< " --format {none|binary|xml}\n"
stream <<< " The output format of the entitlements.\n"
stream <<< " -o <path>\n"
stream <<< " Specify the output path to which to write the entitlements.\n"
}
}
/// The type of conversion to perform.
let format: FormatKind
/// The path to the input file.
let inputPath: Path
/// The path to the output file.
let outputPath: Path
init?(_ commandLine: AnySequence<String>, _ outputDelegate: any TaskOutputDelegate)
{
var format = FormatKind.sameAsInput
var inputPath: Path? = nil
var foundOutputPathOption = false
var outputPath: Path? = nil
var hadErrors = false
func error(_ message: String)
{
outputDelegate.emitError(message)
hadErrors = true
}
// Parse the arguments.
let generator = commandLine.makeIterator()
// Skip the executable.
let programName = generator.next() ?? "<<missing program name>>"
argumentParsing:
while let arg = generator.next()
{
switch arg
{
case "-format":
// The '-format' option takes a single argument: 'xml', 'binary', 'openstep' or 'none'.
guard let name = generator.next() else
{
error("missing argument for option: \(arg)")
continue
}
guard let kind = FormatKind(name: name) else
{
error("failed to parse option: \(arg) \(name)")
continue
}
format = kind
case "-o":
// The '-o' argument take a single parameter: the output path.
foundOutputPathOption = true
guard let value = generator.next() else
{
error("missing argument for option: \(arg)")
continue
}
outputPath = Path(value)
case _ where arg.hasPrefix("-"):
// Any other option starting with '-' is unrecognized.
error("unrecognized argument: \(arg)")
continue
case _ where inputPath == nil:
// Any other option is considered to be an input path.
inputPath = Path(arg)
default:
// But we can only have one input path.
error("multiple input paths specified")
}
}
// Diagnose missing input path.
if inputPath == nil
{
error("no input file specified")
inputPath = Path("<<error>>")
}
// Diagnose missing output path option.
if outputPath == nil && !foundOutputPathOption
{
error("missing required option: -o")
outputPath = Path("<<error>>")
}
// If there were errors, emit the usage and return an error.
if hadErrors
{
outputDelegate.emitOutput("\n")
Options.emitUsage(programName, outputDelegate)
return nil
}
// Initialize contents.
self.format = format
self.inputPath = inputPath!
self.outputPath = outputPath!
}
}
public override class var toolIdentifier: String
{
return "process-product-provisioning-profile"
}
override public func performTaskAction(
_ task: any ExecutableTask,
dynamicExecutionDelegate: any DynamicTaskExecutionDelegate,
executionDelegate: any TaskExecutionDelegate,
clientDelegate: any TaskExecutionClientDelegate,
outputDelegate: any TaskOutputDelegate
) async -> CommandResult {
// Parse the arguments.
guard let options = Options(task.commandLineAsStrings, outputDelegate) else
{
return .failed
}
// Make paths absolute.
let input = task.workingDirectory.join(options.inputPath) // Not presently used
let output = task.workingDirectory.join(options.outputPath)
// Read the input file.
let contents: ByteString
do
{
contents = try executionDelegate.fs.read(input)
}
catch let e
{
outputDelegate.emitError("unable to read input file '\(input.str)': \(e.localizedDescription)")
return .failed
}
// Convert the input data to the desired output format.
let outContents: ByteString
var outputFormat: PropertyListSerialization.PropertyListFormat? = nil
switch options.format
{
// If the output format is the same as the input format, then we just use the input contents.
case .sameAsInput:
outContents = contents
// For any explicit format, we first determine the output format, then perform the conversion.
case .binary:
outputFormat = outputFormat ?? .binary
fallthrough
case .xml:
outputFormat = outputFormat ?? .xml
// Validate the contents of the input file as a property list.
guard let (plist, _) = validateAsPropertyList(contents, outputDelegate) else { return .failed }
let outBytes: [UInt8]
do
{
try outBytes = plist.asBytes(outputFormat!)
}
catch
{
let errorDescription = (error as NSError).localizedDescription
outputDelegate.emitError("unable to create \(options.format.rawValue) property list data: \(errorDescription)")
return .failed
}
outContents = ByteString(outBytes)
}
// Finally we can write the output file.
do
{
try executionDelegate.fs.write(output, contents: outContents)
}
catch let error as NSError
{
outputDelegate.emitError("could not write profile file: \(output.str): \(error.localizedDescription)")
return .failed
}
return .succeeded
}
/// Validate that the given `contents` are a property list, with the root item being a dictionary, and return the parsed property list.
///
/// If validation fails, then messages will be emitted to the `outputDelegate`, and nil will be returned.
private func validateAsPropertyList(_ contents: ByteString, _ outputDelegate: any TaskOutputDelegate) -> (PropertyListItem, PropertyListSerialization.PropertyListFormat)?
{
// Validate it as a property list. The top-level item must be a dictionary.
let plist: PropertyListItem
let format: PropertyListSerialization.PropertyListFormat
do {
try (plist, format) = PropertyList.fromBytesWithFormat(contents.bytes)
} catch {
let errorDescription = (error as NSError).localizedDescription
outputDelegate.emitError("unable to read input file as a property list: \(errorDescription)")
return nil
}
guard case .plDict = plist else
{
outputDelegate.emitError("input file is not a dictionary")
return nil
}
return (plist, format)
}
// Serialization
public override func serialize<T: Serializer>(to serializer: T)
{
serializer.serializeAggregate(1)
{
super.serialize(to: serializer)
}
}
public required init(from deserializer: any Deserializer) throws
{
try deserializer.beginAggregate(1)
try super.init(from: deserializer)
}
}
|