-
-
Notifications
You must be signed in to change notification settings - Fork 85
feat(apple): Add interactive Apple Snapshots wizard #1296
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,165 @@ | ||
| import * as fs from 'node:fs'; | ||
| import * as path from 'node:path'; | ||
|
|
||
| // @ts-expect-error - clack is ESM and TS complains about that. It works though | ||
| import clack from '@clack/prompts'; | ||
|
|
||
| import { withTelemetry } from '../../telemetry'; | ||
| import { | ||
| confirmContinueIfNoOrDirtyGitRepo, | ||
| printWelcome, | ||
| } from '../../utils/clack'; | ||
| import { lookupXcodeProject, selectXcodeTarget } from '../lookup-xcode-project'; | ||
| import type { AppleWizardOptions } from '../options'; | ||
| import { checkInstalledCLISnapshots } from './snapshots-cli-preflight'; | ||
| import { configureSnapshotPreviewsXcodeProject } from './configure-snapshotpreviews-xcode-project'; | ||
| import { ensureSnapshotTestFile } from './snapshot-test-file'; | ||
| import { resolveSnapshotVerificationSchemeName } from './snapshot-verification-scheme'; | ||
| import { | ||
| SNAPSHOTPREVIEWS_MINIMUM_VERSION, | ||
| SNAPSHOTPREVIEWS_PACKAGE_URL, | ||
| SNAPSHOTPREVIEWS_PREFERENCES_PRODUCT, | ||
| SNAPSHOTPREVIEWS_SNAPSHOT_TESTS_PRODUCT, | ||
| } from './snapshotpreviews-package'; | ||
|
|
||
| export async function runAppleSnapshotsWizard( | ||
| options: AppleWizardOptions, | ||
| ): Promise<void> { | ||
| return withTelemetry( | ||
| { | ||
| enabled: options.telemetryEnabled, | ||
| integration: 'appleSnapshots', | ||
| wizardOptions: options, | ||
| }, | ||
| () => runAppleSnapshotsWizardWithTelemetry(options), | ||
| ); | ||
| } | ||
|
|
||
| async function runAppleSnapshotsWizardWithTelemetry( | ||
| options: AppleWizardOptions, | ||
| ): Promise<void> { | ||
| const projectDir = options.projectDir ?? process.cwd(); | ||
|
|
||
| printWelcome({ | ||
| wizardName: 'Sentry Apple Snapshots Wizard', | ||
| promoCode: options.promoCode, | ||
| }); | ||
|
|
||
| await confirmContinueIfNoOrDirtyGitRepo({ | ||
| ignoreGitChanges: options.ignoreGitChanges, | ||
| cwd: projectDir, | ||
| }); | ||
|
|
||
| clack.log.info( | ||
| [ | ||
| `Apple Snapshots setup will use ${SNAPSHOTPREVIEWS_PACKAGE_URL}`, | ||
| `${SNAPSHOTPREVIEWS_MINIMUM_VERSION}+ and link`, | ||
| `${SNAPSHOTPREVIEWS_SNAPSHOT_TESTS_PRODUCT} to the hosted XCTest`, | ||
| `target and ${SNAPSHOTPREVIEWS_PREFERENCES_PRODUCT} to the selected`, | ||
| 'app target.', | ||
| ].join(' '), | ||
| ); | ||
|
|
||
| const xcProject = await lookupXcodeProject({ projectDir }); | ||
|
|
||
| const appTargetName = await selectXcodeTarget(xcProject, { | ||
| noTargetMessage: 'No application target found.', | ||
| promptMessage: 'Which app target should SnapshotPreviews use?', | ||
| }); | ||
|
|
||
| const hostedTestTargetNames = | ||
| xcProject.getHostedUnitTestTargetNamesForApplicationTarget(appTargetName); | ||
| const hostedTestTargetName = await selectXcodeTarget(xcProject, { | ||
| targetNames: hostedTestTargetNames, | ||
| noTargetMessage: [ | ||
| `No hosted unit-test target was found for ${appTargetName}.`, | ||
| 'Please configure a unit-test target with TEST_HOST pointing at that app target, then run the wizard again.', | ||
| ].join(' '), | ||
| promptMessage: 'Which test target should render SnapshotPreviews?', | ||
| }); | ||
|
|
||
| const previewTargetNames = [appTargetName]; | ||
|
|
||
| clack.log.info( | ||
| [ | ||
| `${SNAPSHOTPREVIEWS_PREFERENCES_PRODUCT} will be linked to the selected`, | ||
| 'app target when possible. Import SnapshotPreferences in Swift', | ||
| 'preview files only if you want to use SnapshotPreviews modifiers.', | ||
| ].join(' '), | ||
| ); | ||
|
|
||
| if (fs.existsSync(path.join(projectDir, 'Package.swift'))) { | ||
| clack.log.info( | ||
| [ | ||
| 'Detected Package.swift. This wizard does not edit SwiftPM manifests.', | ||
| `If SwiftPM preview targets use SnapshotPreferences modifiers, add`, | ||
| `the ${SNAPSHOTPREVIEWS_PREFERENCES_PRODUCT} product dependency to those`, | ||
| 'targets in Package.swift.', | ||
| ].join(' '), | ||
| ); | ||
| } | ||
|
|
||
| const packageResult = configureSnapshotPreviewsXcodeProject({ | ||
| xcodeProject: xcProject, | ||
| hostedTestTargetName, | ||
| previewTargetNames, | ||
| }); | ||
|
|
||
| if (!packageResult.linked) { | ||
| clack.log.error( | ||
| 'SnapshotPreviews package products could not be linked to the selected targets. Please check the Xcode project target build phases and try again.', | ||
| ); | ||
| clack.outro('Apple Snapshots setup did not complete.'); | ||
| return; | ||
| } | ||
|
|
||
| if (packageResult.failedSnapshotPreferencesTargetNames.length) { | ||
| clack.log.warn( | ||
| [ | ||
| `${SNAPSHOTPREVIEWS_PREFERENCES_PRODUCT} could not be linked to`, | ||
| packageResult.failedSnapshotPreferencesTargetNames.join(', '), | ||
| 'because the target Frameworks build phase could not be updated.', | ||
| 'Apple Snapshots setup will continue; link the product manually if', | ||
| 'you want to use SnapshotPreviews modifiers in Swift previews.', | ||
| ].join(' '), | ||
| ); | ||
| } | ||
|
|
||
| const snapshotTestResult = ensureSnapshotTestFile({ | ||
| xcodeProject: xcProject, | ||
| hostedTestTargetName, | ||
| }); | ||
| if (!snapshotTestResult.included) { | ||
| clack.log.error( | ||
| 'SnapshotPreviews test file could not be added to the selected XCTest target. Please check the target Sources build phase and try again.', | ||
| ); | ||
| clack.outro('Apple Snapshots setup did not complete.'); | ||
| return; | ||
| } | ||
|
|
||
| if (snapshotTestResult.changed || packageResult.changed) { | ||
| xcProject.write(); | ||
| clack.log.success('Updated the Xcode project for SnapshotPreviews.'); | ||
| } else { | ||
| clack.log.info('SnapshotPreviews Xcode project setup is already present.'); | ||
| } | ||
|
|
||
| await checkInstalledCLISnapshots({ | ||
| projectDir, | ||
| verificationGuidance: { | ||
| appId: xcProject.getBundleIdentifierForTarget(appTargetName), | ||
| hostedTestTargetName, | ||
| projectDir, | ||
| projectPath: xcProject.xcodeprojPath, | ||
| schemeName: resolveSnapshotVerificationSchemeName({ | ||
| hostedTestTargetName, | ||
| xcodeprojPath: xcProject.xcodeprojPath, | ||
| }), | ||
| snapshotTestClassName: snapshotTestResult.className, | ||
| }, | ||
| }); | ||
|
|
||
| clack.outro( | ||
| 'Apple Snapshots setup complete. No Sentry auth, DSN, runtime SDK, dSYM, or CI workflow files were configured.', | ||
| ); | ||
| } | ||
77 changes: 77 additions & 0 deletions
77
src/apple/snapshots/configure-snapshotpreviews-xcode-project.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| import type { | ||
| SwiftPackageProductSpec, | ||
| SwiftPackageSpec, | ||
| XcodeProject, | ||
| } from '../xcode-manager'; | ||
| import { | ||
| SNAPSHOTPREVIEWS_MINIMUM_VERSION, | ||
| SNAPSHOTPREVIEWS_PACKAGE_URL, | ||
| SNAPSHOTPREVIEWS_PREFERENCES_PRODUCT, | ||
| SNAPSHOTPREVIEWS_SNAPSHOT_TESTS_PRODUCT, | ||
| } from './snapshotpreviews-package'; | ||
|
|
||
| export const snapshotPreviewsPackageSpec: SwiftPackageSpec = { | ||
| repositoryURL: SNAPSHOTPREVIEWS_PACKAGE_URL, | ||
| requirement: { | ||
| kind: 'upToNextMajorVersion', | ||
| minimumVersion: SNAPSHOTPREVIEWS_MINIMUM_VERSION, | ||
| }, | ||
| commentName: 'SnapshotPreviews', | ||
| }; | ||
|
|
||
| export const snapshottingTestsProductSpec: SwiftPackageProductSpec = { | ||
| package: snapshotPreviewsPackageSpec, | ||
| productName: SNAPSHOTPREVIEWS_SNAPSHOT_TESTS_PRODUCT, | ||
| }; | ||
|
|
||
| export const snapshotPreferencesProductSpec: SwiftPackageProductSpec = { | ||
| package: snapshotPreviewsPackageSpec, | ||
| productName: SNAPSHOTPREVIEWS_PREFERENCES_PRODUCT, | ||
| }; | ||
|
|
||
| export function configureSnapshotPreviewsXcodeProject({ | ||
| xcodeProject, | ||
| hostedTestTargetName, | ||
| previewTargetNames = [], | ||
| }: { | ||
| xcodeProject: XcodeProject; | ||
| hostedTestTargetName: string; | ||
| previewTargetNames?: string[]; | ||
| }): { | ||
| changed: boolean; | ||
| failedSnapshotPreferencesTargetNames: string[]; | ||
| linked: boolean; | ||
| } { | ||
| if (!xcodeProject.getUnitTestTargetNames().includes(hostedTestTargetName)) { | ||
| return { | ||
| changed: false, | ||
| failedSnapshotPreferencesTargetNames: [], | ||
| linked: false, | ||
| }; | ||
| } | ||
|
|
||
| const snapshottingTestsResult = xcodeProject.ensureSwiftPackageProductLinked( | ||
| hostedTestTargetName, | ||
| snapshottingTestsProductSpec, | ||
| ); | ||
| const snapshotPreferencesResults = previewTargetNames.map((targetName) => ({ | ||
| result: xcodeProject.ensureSwiftPackageProductLinked( | ||
| targetName, | ||
| snapshotPreferencesProductSpec, | ||
| ), | ||
| targetName, | ||
| })); | ||
| const linkResults = [ | ||
| snapshottingTestsResult, | ||
| ...snapshotPreferencesResults.map(({ result }) => result), | ||
| ]; | ||
| const failedSnapshotPreferencesTargetNames = snapshotPreferencesResults | ||
| .filter(({ result }) => !result.linked) | ||
| .map(({ targetName }) => targetName); | ||
|
|
||
| return { | ||
| changed: linkResults.some((result) => result.changed), | ||
|
sentry[bot] marked this conversation as resolved.
|
||
| failedSnapshotPreferencesTargetNames, | ||
| linked: snapshottingTestsResult.linked, | ||
| }; | ||
|
cameroncooke marked this conversation as resolved.
|
||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.