File: ValidateProductTaskAction.swift

package info (click to toggle)
swiftlang 6.2.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,856,264 kB
  • sloc: cpp: 9,995,718; ansic: 2,234,019; asm: 1,092,167; python: 313,940; objc: 82,726; f90: 80,126; lisp: 38,373; pascal: 25,580; sh: 20,378; ml: 5,058; perl: 4,751; makefile: 4,725; awk: 3,535; javascript: 3,018; xml: 918; fortran: 664; cs: 573; ruby: 396
file content (397 lines) | stat: -rw-r--r-- 19,581 bytes parent folder | download
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//

public import SWBCore
import SWBLibc
public import SWBUtil
import SWBProtocol
import Foundation

/// Concrete implementation of task for validating a a product.
public final class ValidateProductTaskAction: TaskAction {

    /// The parsed command line options.
    @_spi(Testing) public struct Options {

        static func emitUsage(_ name: String, _ outputDelegate: any TaskOutputDelegate) {
            outputDelegate.emitOutput { stream in
                stream <<< "usage: \(name) <application-path>\n"
            }
        }

        enum StoreIssueTreatment {
            case treatAsWarnings
            case treatAsErrors
        }
        let verbose: Bool
        let storeIssueTreatment: StoreIssueTreatment
        let validateForStore: Bool
        let validatingArchive: Bool
        let validateEmbeddedFrameworks: Bool
        let applicationPath: Path
        let infoplistSubpath: String
        let isShallowBundle: Bool

        @_spi(Testing) public init?(_ commandLine: AnySequence<String>, _ outputDelegate: any TaskOutputDelegate) {
            let verbose = false
            let storeIssueTreatment: StoreIssueTreatment = .treatAsWarnings
            var validateForStore = false
            var validatingArchive = false
            var applicationPath: Path! = nil
            var infoplistSubpath: String! = nil
            var validateExtension = true
            var validateEmbeddedFrameworks = true
            var isShallowBundle = false

            var hadErrors = false
            func error(_ message: String) {
                outputDelegate.emitError(message)
                hadErrors = true
            }
            func warning(_ message: String) {
                outputDelegate.emitWarning(message)
            }

            // 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 {
// Presently these options are disabled; they are only used for "offline store validation", which was disabled in the original XCWorkQueueCommandBuiltinInvocation_validationUtility and is not yet implemented here.
// When & if they are reenabled, they should be documented in emitUsage().
/*
                    case "-verbose":
                        verbose = true

                    case "-warnings":
                        storeIssueTreatment = .treatAsWarnings

                    case "-errors":
                        storeIssueTreatment = .treatAsErrors
*/
                    case "-validate-for-store":
                        validateForStore = true

                    // The default is yes, so we only need an opt-out for now. This also prevents us from having to pass in `-validate-extension` everywhere, even though that is the default behavior.
                    case "-no-validate-extension":
                        validateExtension = false

                    case "-no-validate-embedded-frameworks":
                        validateEmbeddedFrameworks = false

                    case "-shallow-bundle":
                        isShallowBundle = true

                    case "-infoplist-subpath":
                        guard let path = generator.next() else {
                            error("missing argument for option: \(arg)")
                            continue
                        }
                        infoplistSubpath = path

                    case _ where arg.hasPrefix("-"):
                        error("unrecognized option: \(arg)")

                    case _ where applicationPath == nil:
                        // Any other option is considered to be the application path.
                        applicationPath = Path(arg)

                    default:
                        // But we can only have one application path.
                        error("multiple application paths specified")
                    }
            }

            // Diagnose missing inputs.
            if applicationPath == nil || applicationPath!.isEmpty {
                error("no application path specified")
            }

            if infoplistSubpath == nil || infoplistSubpath!.isEmpty {
                error("no Info.plist path specified")
            }

            // Validate the application path.
            let ext = applicationPath.fileExtension.lowercased()
            if ext == "ipa" {
                validatingArchive = true
            }
            else if ext != "app" {
                if validateExtension {
                    error("unknown application extension '.\(ext): expected '.app' or '.ipa'")
                }
            }

            // 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.verbose = verbose
            self.storeIssueTreatment = storeIssueTreatment
            self.validateForStore = validateForStore
            self.validatingArchive = validatingArchive
            self.validateEmbeddedFrameworks = validateEmbeddedFrameworks
            self.applicationPath = applicationPath
            self.infoplistSubpath = infoplistSubpath
            self.isShallowBundle = isShallowBundle
        }
    }

    public override class var toolIdentifier: String {
        return "validate-product"
    }

    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 applicationPath = task.workingDirectory.join(options.applicationPath)

        // Read the Info.plist for the application.
        let infoPlistItem: PropertyListItem
        do {
            infoPlistItem = try PropertyList.fromPath(applicationPath.join(options.infoplistSubpath), fs: executionDelegate.fs)
        }
        catch {
            outputDelegate.emitError("Failed to read Info.plist of app \(applicationPath.str): \(error.localizedDescription)")
            return .failed
        }
        guard case .plDict(let infoPlist) = infoPlistItem else {
            outputDelegate.emitError("Failed to read Info.plist of app \(applicationPath.str): Contents of file are not a dictionary")
            return .failed
        }

        // Validate the iPad multitasking/splitview support in the Info.plist.  This never causes the tool to fail, but it may emit warnings.
        validateiPadMultiTaskingSplitViewSupport(infoPlist, outputDelegate: outputDelegate)

        if let configuredTarget = task.forTarget, let platform = configuredTarget.parameters.activeRunDestination?.platform {
            // Validate that this is actually an App Store category.
            validateAppStoreCategory(infoPlist, platform: platform, targetName: configuredTarget.target.name, schemeCommand: executionDelegate.schemeCommand, options: options, outputDelegate: outputDelegate)

            // Validate compliance of embedded frameworks w.r.t. installation and App Store submission.
            if options.validateEmbeddedFrameworks {
                do {
                    let success = try validateFrameworks(at: applicationPath, isShallow: options.isShallowBundle, outputDelegate: outputDelegate)
                    return success ? .succeeded : .failed
                } catch {
                    outputDelegate.emitError("Unexpected error while validating embedded frameworks: \(error.localizedDescription)")
                    return .failed
                }
            }
        }

        return .succeeded
    }

    private static let deepBundleInfoPlistSubpath = "Versions/Current/Resources/Info.plist"
    private static let shallowBundleInfoPlistSubpath = "Info.plist"

    @_spi(Testing) public func validateFrameworks(at applicationPath: Path, isShallow: Bool, outputDelegate: any TaskOutputDelegate, fs: any FSProxy = localFS) throws -> Bool {
        let frameworksSubpath = isShallow ? "Frameworks" : "Contents/Frameworks"
        let infoPlistSubpath = isShallow ? Self.shallowBundleInfoPlistSubpath : Self.deepBundleInfoPlistSubpath

        let frameworksPath = applicationPath.join(frameworksSubpath)
        guard fs.exists(frameworksPath) else {
            return true
        }

        var frameworkPaths = [Path]()
        try fs.traverse(frameworksPath) {
            if $0.fileExtension == "framework" {
                frameworkPaths.append($0)
            }
        }

        var hasErrors = false
        for frameworkPath in frameworkPaths {
            let infoPlistPath = frameworkPath.join(infoPlistSubpath)
            guard fs.exists(infoPlistPath) else {
                // It seems reasonably common that developers mix up the formats, so we check that to be more helpful.
                if isShallow, fs.exists(frameworkPath.join(Self.deepBundleInfoPlistSubpath)) {
                    outputDelegate.emitError("Framework \(frameworkPath.str) contains \(Self.deepBundleInfoPlistSubpath), expected \(Self.shallowBundleInfoPlistSubpath) at the root level since the platform uses shallow bundles")
                } else if !isShallow, fs.exists(frameworkPath.join(Self.shallowBundleInfoPlistSubpath)) {
                    outputDelegate.emitError("Framework \(frameworkPath.str) contains \(Self.shallowBundleInfoPlistSubpath), expected \(Self.deepBundleInfoPlistSubpath) since the platform does not use shallow bundles")
                } else {
                    outputDelegate.emitError("Framework \(frameworkPath.str) did not contain an Info.plist")
                }
                hasErrors = true
                continue
            }

            let infoPlistItem: PropertyListItem
            do {
                infoPlistItem = try PropertyList.fromPath(infoPlistPath, fs: fs)
            }
            catch let error {
                outputDelegate.emitError("Failed to read Info.plist of framework \(frameworkPath.str): \(error)")
                hasErrors = true
                continue
            }
            guard case .plDict(let infoPlist) = infoPlistItem else {
                outputDelegate.emitError("Failed to read Info.plist of framework \(frameworkPath.str): Contents of file are not a dictionary")
                hasErrors = true
                continue
            }

            // We do a more thorough validation for embedded platforms since installation will otherwise be prevented with more confusing diagnostics.
            if !isShallow {
                continue
            }

            // This is being special cased since it appears we always get a dictionary even for invalid plists.
            if infoPlist.isEmpty {
                outputDelegate.emitError("Info.plist of framework \(frameworkPath.str) was empty")
                hasErrors = true
                continue
            }

            guard let rawBundleIdentifier = infoPlist["CFBundleIdentifier"], case .plString(let bundleIdentifier) = rawBundleIdentifier, !bundleIdentifier.isEmpty else {
                outputDelegate.emitError("Framework \(frameworkPath.str) did not have a CFBundleIdentifier in its Info.plist")
                hasErrors = true
                continue
            }

            if bundleIdentifier != bundleIdentifier.asLegalBundleIdentifier {
                outputDelegate.emitError("Framework \(frameworkPath.str) had an invalid CFBundleIdentifier in its Info.plist: \(bundleIdentifier)")
                hasErrors = true
            }
        }

        return !hasErrors
    }

    @_spi(Testing) @discardableResult public func validateiPadMultiTaskingSplitViewSupport(_ infoPlist: [String: PropertyListItem], outputDelegate: any TaskOutputDelegate) -> Bool {
        // Note that there is a lot of type validation which *could* be performed here (e.g., emitting an error if some value is not the expected type), but I've only copied the validation being performed in XCWorkQueueCommandBuiltinInvocation_validationUtility, from which this was ported.

        // This validation only applies to apps built for iOS 9 or later
        guard infoPlist["DTPlatformName"]?.stringValue == "iphoneos", let platformVersionStr = infoPlist["DTPlatformVersion"]?.stringValue, let platformVersion = try? Version(platformVersionStr), platformVersion >= Version(9, 0, 0) else {
            return true
        }

        // If this is not an iPad-supporting app (device family 2), then we're done.
        var supportsIpad = false
        for deviceFamily in infoPlist["UIDeviceFamily"]?.arrayValue ?? [] {
            if deviceFamily.stringValue == "2" || deviceFamily.intValue == 2 {
                supportsIpad = true
                break
            }
        }
        guard supportsIpad else {
            return true
        }

        // If UIRequiresFullScreen is true, then the app is valid.
        if infoPlist["UIRequiresFullScreen"]?.looselyTypedBoolValue ?? false {
            return true
        }

        // Remember whether we passed validation.
        var passedValidation = true

        // All device orientations must be supported.
        let supportedOrientationsInput = infoPlist["UISupportedInterfaceOrientations~ipad"]?.arrayValue ?? infoPlist["UISupportedInterfaceOrientations"]?.arrayValue ?? []
        let supportedOrientations = Set(supportedOrientationsInput.compactMap({ $0.stringValue }))
        let expectedOrientations = Set<String>(["UIInterfaceOrientationPortrait", "UIInterfaceOrientationPortraitUpsideDown", "UIInterfaceOrientationLandscapeLeft", "UIInterfaceOrientationLandscapeRight"])
        if expectedOrientations != supportedOrientations {
            outputDelegate.emitWarning("All interface orientations must be supported unless the app requires full screen.")
            passedValidation = false
        }

        var matchingLaunchStoryboard = false
        if (infoPlist["UILaunchStoryboardName~ipad"]?.stringValue ?? infoPlist["UILaunchStoryboardName"]?.stringValue) != nil {
            matchingLaunchStoryboard = true
        } else {
            let launchStoryboards = infoPlist["UILaunchStoryboards~ipad"]?.dictValue ?? infoPlist["UILaunchStoryboards"]?.dictValue ?? [:]
            if let defaultLaunchStoryboard = launchStoryboards["UIDefaultLaunchStoryboard"]?.stringValue {
                let storyboardDefs = launchStoryboards["UILaunchStoryboardDefinitions"]?.arrayValue ?? []
                let matchingStoryboardDef = storyboardDefs.first(where: { storyboardDef in
                    return storyboardDef.dictValue?["UILaunchStoryboardIdentifier"]?.stringValue == defaultLaunchStoryboard
                })
                if matchingStoryboardDef?.dictValue?["UILaunchStoryboardFile"] != nil {
                    matchingLaunchStoryboard = true
                }
            }
        }

        let deploymentTarget = infoPlist["MinimumOSVersion"]?.stringValue.map { try? Version($0) } ?? nil
        if let deploymentTarget, deploymentTarget >= Version(14) {
            var matchingLaunchConfiguration = false
            if (infoPlist["UILaunchScreen~ipad"]?.dictValue ?? infoPlist["UILaunchScreen"]?.dictValue) != nil {
                matchingLaunchConfiguration = true
            } else {
                let launchConfigs = infoPlist["UILaunchScreens~ipad"]?.dictValue ?? infoPlist["UILaunchScreens"]?.dictValue ?? [:]
                if let defaultLaunchConfig = launchConfigs["UIDefaultLaunchScreen"]?.stringValue {
                    let configDefs = launchConfigs["UILaunchScreenDefinitions"]?.arrayValue ?? []
                    let matchingConfigDef = configDefs.first(where: { configDef in
                        return configDef.dictValue?["UILaunchScreenIdentifier"]?.stringValue == defaultLaunchConfig
                    })
                    if matchingConfigDef?.dictValue != nil {
                        matchingLaunchConfiguration = true
                    }
                }
            }

            // A default launch configuration or storyboard must be provided.
            if !matchingLaunchConfiguration && !matchingLaunchStoryboard {
                outputDelegate.emitWarning("A launch configuration or launch storyboard or xib must be provided unless the app requires full screen.")
                passedValidation = false
            }
        } else {
            // A default launch storyboard must be provided.
            if !matchingLaunchStoryboard {
                outputDelegate.emitWarning("A launch storyboard or xib must be provided unless the app requires full screen.")
                passedValidation = false
            }
        }

        return passedValidation
    }

    /// Validate that there is an App Store category that is set. Note that we only do this for an "archive" action.
    @_spi(Testing) @discardableResult public func validateAppStoreCategory(_ infoPlist: [String: PropertyListItem], platform: String, targetName: String, schemeCommand: SchemeCommand?, options: Options, outputDelegate: any TaskOutputDelegate) -> Bool {
        // Note that that we could inject this logic into the validation rules so that it is tied off of the VALIDATE_PRODUCT, however, because the MacOS templates do not have VALIDATE_PRODUCT=YES for Release builds, like the iOS templates, we still won't get the behavior that we actually want. Because of this, we simply key off of a build setting created by the archive action. See rdar://problem/45590882.

        // This validation is purposefully only enabled for the archive command or if the target has opted-in to validation.
        guard schemeCommand == .archive || options.validateForStore else {
            return true
        }

        // Only run this validation for MacOS application targets. Validation for other platforms can happen elsewhere.
        guard platform == "macosx" else {
            return true
        }

        // Note that setting an empty string for the category is not sufficient to remove this warning; it must be a category.
        // FIXME: It might be nice to also add checks here for a valid App Store category.
        guard (infoPlist["LSApplicationCategoryType"]?.stringValue ?? "") != "" else {
            outputDelegate.emitWarning("No App Category is set for target '\(targetName)'. Set a category by using the General tab for your target, or by adding an appropriate LSApplicationCategory value to your Info.plist.")
            return false
        }

        return true
    }
}