|
| 1 | +import * as fs from 'node:fs'; |
| 2 | +import * as path from 'node:path'; |
| 3 | + |
| 4 | +// @ts-expect-error - clack is ESM and TS complains about that. It works though |
| 5 | +import clack from '@clack/prompts'; |
| 6 | + |
| 7 | +import { withTelemetry } from '../../telemetry'; |
| 8 | +import { |
| 9 | + confirmContinueIfNoOrDirtyGitRepo, |
| 10 | + printWelcome, |
| 11 | +} from '../../utils/clack'; |
| 12 | +import { lookupXcodeProject, selectXcodeTarget } from '../lookup-xcode-project'; |
| 13 | +import type { AppleWizardOptions } from '../options'; |
| 14 | +import { checkInstalledCLISnapshots } from './snapshots-cli-preflight'; |
| 15 | +import { configureSnapshotPreviewsXcodeProject } from './configure-snapshotpreviews-xcode-project'; |
| 16 | +import { ensureSnapshotTestFile } from './snapshot-test-file'; |
| 17 | +import { resolveSnapshotVerificationSchemeName } from './snapshot-verification-scheme'; |
| 18 | +import { |
| 19 | + SNAPSHOTPREVIEWS_MINIMUM_VERSION, |
| 20 | + SNAPSHOTPREVIEWS_PACKAGE_URL, |
| 21 | + SNAPSHOTPREVIEWS_PREFERENCES_PRODUCT, |
| 22 | + SNAPSHOTPREVIEWS_SNAPSHOT_TESTS_PRODUCT, |
| 23 | +} from './snapshotpreviews-package'; |
| 24 | + |
| 25 | +export async function runAppleSnapshotsWizard( |
| 26 | + options: AppleWizardOptions, |
| 27 | +): Promise<void> { |
| 28 | + return withTelemetry( |
| 29 | + { |
| 30 | + enabled: options.telemetryEnabled, |
| 31 | + integration: 'appleSnapshots', |
| 32 | + wizardOptions: options, |
| 33 | + }, |
| 34 | + () => runAppleSnapshotsWizardWithTelemetry(options), |
| 35 | + ); |
| 36 | +} |
| 37 | + |
| 38 | +async function runAppleSnapshotsWizardWithTelemetry( |
| 39 | + options: AppleWizardOptions, |
| 40 | +): Promise<void> { |
| 41 | + const projectDir = options.projectDir ?? process.cwd(); |
| 42 | + |
| 43 | + printWelcome({ |
| 44 | + wizardName: 'Sentry Apple Snapshots Wizard', |
| 45 | + promoCode: options.promoCode, |
| 46 | + }); |
| 47 | + |
| 48 | + await confirmContinueIfNoOrDirtyGitRepo({ |
| 49 | + ignoreGitChanges: options.ignoreGitChanges, |
| 50 | + cwd: projectDir, |
| 51 | + }); |
| 52 | + |
| 53 | + clack.log.info( |
| 54 | + [ |
| 55 | + `Apple Snapshots setup will use ${SNAPSHOTPREVIEWS_PACKAGE_URL}`, |
| 56 | + `${SNAPSHOTPREVIEWS_MINIMUM_VERSION}+ and link`, |
| 57 | + `${SNAPSHOTPREVIEWS_SNAPSHOT_TESTS_PRODUCT} to the hosted XCTest`, |
| 58 | + `target and ${SNAPSHOTPREVIEWS_PREFERENCES_PRODUCT} to the selected`, |
| 59 | + 'app target.', |
| 60 | + ].join(' '), |
| 61 | + ); |
| 62 | + |
| 63 | + const xcProject = await lookupXcodeProject({ projectDir }); |
| 64 | + |
| 65 | + const appTargetName = await selectXcodeTarget(xcProject, { |
| 66 | + noTargetMessage: 'No application target found.', |
| 67 | + promptMessage: 'Which app target should SnapshotPreviews use?', |
| 68 | + }); |
| 69 | + |
| 70 | + const hostedTestTargetNames = |
| 71 | + xcProject.getHostedUnitTestTargetNamesForApplicationTarget(appTargetName); |
| 72 | + const hostedTestTargetName = await selectXcodeTarget(xcProject, { |
| 73 | + targetNames: hostedTestTargetNames, |
| 74 | + noTargetMessage: [ |
| 75 | + `No hosted unit-test target was found for ${appTargetName}.`, |
| 76 | + 'Please configure a unit-test target with TEST_HOST pointing at that app target, then run the wizard again.', |
| 77 | + ].join(' '), |
| 78 | + promptMessage: 'Which test target should render SnapshotPreviews?', |
| 79 | + }); |
| 80 | + |
| 81 | + const previewTargetNames = [appTargetName]; |
| 82 | + |
| 83 | + clack.log.info( |
| 84 | + [ |
| 85 | + `${SNAPSHOTPREVIEWS_PREFERENCES_PRODUCT} will be linked to the selected`, |
| 86 | + 'app target when possible. Import SnapshotPreferences in Swift', |
| 87 | + 'preview files only if you want to use SnapshotPreviews modifiers.', |
| 88 | + ].join(' '), |
| 89 | + ); |
| 90 | + |
| 91 | + if (fs.existsSync(path.join(projectDir, 'Package.swift'))) { |
| 92 | + clack.log.info( |
| 93 | + [ |
| 94 | + 'Detected Package.swift. This wizard does not edit SwiftPM manifests.', |
| 95 | + `If SwiftPM preview targets use SnapshotPreferences modifiers, add`, |
| 96 | + `the ${SNAPSHOTPREVIEWS_PREFERENCES_PRODUCT} product dependency to those`, |
| 97 | + 'targets in Package.swift.', |
| 98 | + ].join(' '), |
| 99 | + ); |
| 100 | + } |
| 101 | + |
| 102 | + const packageResult = configureSnapshotPreviewsXcodeProject({ |
| 103 | + xcodeProject: xcProject, |
| 104 | + hostedTestTargetName, |
| 105 | + previewTargetNames, |
| 106 | + }); |
| 107 | + |
| 108 | + if (!packageResult.linked) { |
| 109 | + clack.log.error( |
| 110 | + 'SnapshotPreviews package products could not be linked to the selected targets. Please check the Xcode project target build phases and try again.', |
| 111 | + ); |
| 112 | + clack.outro('Apple Snapshots setup did not complete.'); |
| 113 | + return; |
| 114 | + } |
| 115 | + |
| 116 | + if (packageResult.failedSnapshotPreferencesTargetNames.length) { |
| 117 | + clack.log.warn( |
| 118 | + [ |
| 119 | + `${SNAPSHOTPREVIEWS_PREFERENCES_PRODUCT} could not be linked to`, |
| 120 | + packageResult.failedSnapshotPreferencesTargetNames.join(', '), |
| 121 | + 'because the target Frameworks build phase could not be updated.', |
| 122 | + 'Apple Snapshots setup will continue; link the product manually if', |
| 123 | + 'you want to use SnapshotPreviews modifiers in Swift previews.', |
| 124 | + ].join(' '), |
| 125 | + ); |
| 126 | + } |
| 127 | + |
| 128 | + const snapshotTestResult = ensureSnapshotTestFile({ |
| 129 | + xcodeProject: xcProject, |
| 130 | + hostedTestTargetName, |
| 131 | + }); |
| 132 | + if (!snapshotTestResult.included) { |
| 133 | + clack.log.error( |
| 134 | + 'SnapshotPreviews test file could not be added to the selected XCTest target. Please check the target Sources build phase and try again.', |
| 135 | + ); |
| 136 | + clack.outro('Apple Snapshots setup did not complete.'); |
| 137 | + return; |
| 138 | + } |
| 139 | + |
| 140 | + if (snapshotTestResult.changed || packageResult.changed) { |
| 141 | + xcProject.write(); |
| 142 | + clack.log.success('Updated the Xcode project for SnapshotPreviews.'); |
| 143 | + } else { |
| 144 | + clack.log.info('SnapshotPreviews Xcode project setup is already present.'); |
| 145 | + } |
| 146 | + |
| 147 | + await checkInstalledCLISnapshots({ |
| 148 | + projectDir, |
| 149 | + verificationGuidance: { |
| 150 | + appId: xcProject.getBundleIdentifierForTarget(appTargetName), |
| 151 | + hostedTestTargetName, |
| 152 | + projectDir, |
| 153 | + projectPath: xcProject.xcodeprojPath, |
| 154 | + schemeName: resolveSnapshotVerificationSchemeName({ |
| 155 | + hostedTestTargetName, |
| 156 | + xcodeprojPath: xcProject.xcodeprojPath, |
| 157 | + }), |
| 158 | + snapshotTestClassName: snapshotTestResult.className, |
| 159 | + }, |
| 160 | + }); |
| 161 | + |
| 162 | + clack.outro( |
| 163 | + 'Apple Snapshots setup complete. No Sentry auth, DSN, runtime SDK, dSYM, or CI workflow files were configured.', |
| 164 | + ); |
| 165 | +} |
0 commit comments