Repository navigation
feat(cli): add Android and Flutter to sentry wizard #2528
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
+3,759
−47
Merged
Changes from all commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
949060d
feat(cli): add sentry wizard with Next.js
sentry-junior[bot] 76e162b
feat(cli): add the wizard's shared AST and URL helpers
sentry-junior[bot] 0113841
feat(cli): reject the wizard's removed sign-up flags
sentry-junior[bot] 7e5f894
Merge branch 'feat/cli-wizard-scaffold' into feat/cli-wizard-shared-a…
sentry-junior[bot] 0574155
feat(cli): add Android and Flutter to sentry wizard
sentry-junior[bot] fdeb80f
test(cli): pick an unported integration for the wizard's not-availabl…
sentry-junior[bot] 39e20d5
fix(cli): address review findings in sentry wizard
sentry-junior[bot] c4786b3
Merge branch 'feat/cli-wizard-scaffold' into feat/cli-wizard-shared-a…
sentry-junior[bot] 3921356
fix(cli): stop the Android wizard on expected project states
sentry-junior[bot] 2c98751
refactor(cli): reuse existing CLI helpers in the wizard AST utils
sentry-junior[bot] ac6b75d
feat(cli): use positionals and existing CLI flags in sentry wizard
sentry-junior[bot] 4c93aa8
fix(cli): use the credential host for the project issues URL
sentry-junior[bot] 2f2f91a
fix(mcp): regenerate the chart app after the for…of conversion
sentry-junior[bot] f0a48de
Merge remote-tracking branch 'origin/feat/cli-wizard-scaffold' into f…
sentry-junior[bot] 35bb707
fix(cli): update the Android and Flutter wizards for the new wizard base
sentry-junior[bot] 76bf36c
Merge origin/main into feat/cli-wizard-android-flutter
sentry-junior[bot] 5cd3f7a
fix(cli): write defaults.url to sentry.properties for self-hosted And…
sentry-junior[bot] aa3ac7e
fix(cli): harden the Flutter and Android wizard edits
sentry-junior[bot] 048dea4
fix(cli): find a main activity named without a package in the Android…
sentry-junior[bot] c158f2f
fix(cli): handle more Android and Flutter file shapes in the wizard
sentry-junior[bot] f293b67
fix(cli): tighten main activity and arrow main matching in the wizard
sentry-junior[bot] 3a6626c
fix(cli): ignore commented pubspec entries and imports in the Flutter…
sentry-junior[bot] 3f6cafd
refactor(cli): move the wizard XML reader to shared wizard utils
sentry-junior[bot] 3850e9b
Merge remote-tracking branch 'origin/main' into feat/cli-wizard-andro…
sentry-junior[bot] 97e1c17
refactor(cli): use the shared XML entity decoder in Apple Snapshots
sentry-junior[bot] 0f7b03a
fix(cli): only skip main.dart when it already calls SentryFlutter.init
sentry-junior[bot] 1ddc14e
fix(cli): only promise a Flutter test error when main.dart was patched
sentry-junior[bot] 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,234 @@ | ||
| /** | ||
| * The Android wizard: adds the Sentry Android Gradle plugin, configures the | ||
| * SDK in `AndroidManifest.xml`, patches the main activity with a test error, | ||
| * and writes `sentry.properties` for the plugin's ProGuard mapping upload. | ||
| * Ported from sentry-wizard's `src/android`. | ||
| */ | ||
|
|
||
| import { readdirSync } from "node:fs"; | ||
| import path from "node:path"; | ||
| import chalk from "chalk"; | ||
| import { Sentry, traceStep, withTelemetry } from "../telemetry.js"; | ||
| import { abortIfSpotlightNotSupported } from "../utils/abort-if-sportlight-not-supported.js"; | ||
| import { | ||
| type CliSetupConfig, | ||
| abort, | ||
| abortIfCancelled, | ||
| addSentryCliConfig, | ||
| confirmContinueIfNoOrDirtyGitRepo, | ||
| getOrAskForProjectData, | ||
| printWelcome, | ||
| propertiesCliSetupConfig, | ||
| } from "../utils/clack/index.js"; | ||
| import { offerProjectScopedMcpConfig } from "../utils/clack/mcp-config.js"; | ||
| import { clack } from "../utils/clack/prompts.js"; | ||
| import { fixLineEndings } from "../utils/line-endings.js"; | ||
| import type { WizardOptions } from "../utils/types.js"; | ||
| import { findActivitySourceFile, patchMainActivity } from "./code-tools.js"; | ||
| import { addGradlePlugin, getNamespace, selectAppFile } from "./gradle.js"; | ||
| import { addManifestSnippet, getMainActivity } from "./manifest.js"; | ||
|
|
||
| const proguardMappingCliSetupConfig: CliSetupConfig = { | ||
| ...propertiesCliSetupConfig, | ||
| name: "proguard mappings", | ||
| }; | ||
|
|
||
| /** Run the Android wizard in its telemetry span. */ | ||
| export async function runAndroidWizard(options: WizardOptions): Promise<void> { | ||
| return await withTelemetry( | ||
| { | ||
| enabled: options.telemetryEnabled, | ||
| integration: "android", | ||
| wizardOptions: options, | ||
| }, | ||
| () => runAndroidWizardWithTelemetry(options), | ||
| ); | ||
| } | ||
|
|
||
| async function runAndroidWizardWithTelemetry( | ||
| options: WizardOptions, | ||
| ): Promise<void> { | ||
| printWelcome({ | ||
| wizardName: "Sentry Android Wizard", | ||
| }); | ||
|
|
||
| await confirmContinueIfNoOrDirtyGitRepo({ | ||
| ignoreGitChanges: options.ignoreGitChanges, | ||
| cwd: undefined, | ||
| }); | ||
|
|
||
| const projectDir = process.cwd(); | ||
| const buildGradleFiles = findFilesWithExtensions(projectDir, [ | ||
| ".gradle", | ||
| "gradle.kts", | ||
| ]); | ||
|
|
||
| if (buildGradleFiles.length === 0) { | ||
| clack.log.error( | ||
| "No Gradle project found. Please run this command from the root of your project.", | ||
| ); | ||
| return await abort(); | ||
| } | ||
|
|
||
| const appFile = await traceStep("Select App File", () => | ||
| selectAppFile(buildGradleFiles), | ||
| ); | ||
|
|
||
| const projectData = await getOrAskForProjectData(options, "android"); | ||
|
|
||
| if (projectData.spotlight) { | ||
| return await abortIfSpotlightNotSupported("Android"); | ||
| } | ||
|
|
||
| const { selectedProject, selfHosted, sentryUrl, authToken } = projectData; | ||
|
|
||
| // Ask if user wants to enable Sentry Logs | ||
| const enableLogs = await abortIfCancelled( | ||
| clack.confirm({ | ||
| message: | ||
| "Do you want to enable Logs? (See https://docs.sentry.io/platforms/android/logs/)", | ||
| }), | ||
| ); | ||
| Sentry.setTag("enable-logs", enableLogs); | ||
|
|
||
| if (enableLogs) { | ||
| clack.log.info( | ||
| "Logs will be enabled with default settings. You can send logs using the Sentry.logger() APIs or use one of the integrations: https://docs.sentry.io/platforms/android/logs/#integrations.", | ||
| ); | ||
| } | ||
|
|
||
| // ======== STEP 1. Add Sentry Gradle Plugin to build.gradle(.kts) ============ | ||
| clack.log.step( | ||
| `Adding ${chalk.bold("Sentry Gradle plugin")} to your app's ${chalk.cyan( | ||
| "build.gradle", | ||
| )} file.`, | ||
| ); | ||
| const pluginAdded = await traceStep("Add Gradle Plugin", () => | ||
| addGradlePlugin( | ||
| appFile, | ||
| selectedProject.organization.slug, | ||
| selectedProject.slug, | ||
| ), | ||
| ); | ||
| Sentry.setTag("gradle-plugin-added", pluginAdded); | ||
| if (!pluginAdded) { | ||
| clack.log.error( | ||
| `The Gradle sync failed after adding the ${chalk.bold("Sentry Gradle plugin")} to ${chalk.cyan(appFile)}. Fix the build, or set up the plugin by hand.\nPlease follow the instructions at https://docs.sentry.io/platforms/android/#install`, | ||
| ); | ||
| return await abort(); | ||
| } | ||
|
|
||
| // ======== STEP 2. Configure Sentry SDK via AndroidManifest ============ | ||
| clack.log.step( | ||
| `Configuring Sentry SDK via ${chalk.cyan("AndroidManifest.xml")}`, | ||
| ); | ||
| const appDir = path.dirname(appFile); | ||
| const manifestFile = path.join(appDir, "src", "main", "AndroidManifest.xml"); | ||
|
|
||
| const manifestUpdated = traceStep("Update Android Manifest", () => | ||
| addManifestSnippet( | ||
| manifestFile, | ||
| selectedProject.keys[0].dsn.public, | ||
| enableLogs, | ||
| ), | ||
| ); | ||
| if (!manifestUpdated) { | ||
| clack.log.warn( | ||
| "Could not configure the Sentry SDK. You'll have to do it manually.\nPlease follow the instructions at https://docs.sentry.io/platforms/android/#configure", | ||
| ); | ||
| } | ||
| Sentry.setTag("android-manifest-updated", manifestUpdated); | ||
|
|
||
| // ======== STEP 3. Patch Main Activity with a test error snippet ============ | ||
| clack.log.step( | ||
| `Patching ${chalk.bold("Main Activity")} with a test error snippet.`, | ||
| ); | ||
| const mainActivity = traceStep("Find Main Activity", () => | ||
| getMainActivity(manifestFile), | ||
| ); | ||
| let packageName = mainActivity.packageName; | ||
| if (!packageName) { | ||
| // if no package name in AndroidManifest, look into gradle script | ||
| packageName = getNamespace(appFile); | ||
| } | ||
| const activityName = mainActivity.activityName; | ||
| Sentry.setTag("has-activity-name", !!activityName); | ||
| Sentry.setTag("has-package-name", !!packageName); | ||
| if (!activityName || !packageName) { | ||
| clack.log.error( | ||
| "Could not find the Activity with intent action MAIN, or the app's package name. You'll have to finish the setup by hand.\nPlease follow the instructions at https://docs.sentry.io/platforms/android/#verify", | ||
| ); | ||
| return await abort(); | ||
| } | ||
| const packageNameStable = packageName; | ||
| const activityFile = await traceStep("Find Main Activity Source File", () => | ||
| findActivitySourceFile(appDir, packageNameStable, activityName), | ||
| ); | ||
|
|
||
| const activityPatched = traceStep("Patch Main Activity", () => | ||
| patchMainActivity(activityFile), | ||
| ); | ||
| if (!activityPatched) { | ||
| clack.log.warn( | ||
| "Could not patch main activity. You'll have to manually verify the setup.\nPlease follow the instructions at https://docs.sentry.io/platforms/android/#verify", | ||
| ); | ||
| } | ||
| Sentry.setTag("main-activity-patched", activityPatched); | ||
|
|
||
| // ======== STEP 4. Add sentry.properties for the Gradle plugin ============ | ||
| clack.log.step( | ||
| `Adding the auth token for the ${chalk.bold( | ||
| "Sentry Android Gradle plugin", | ||
| )} to ${chalk.cyan("sentry.properties")}.`, | ||
| ); | ||
|
|
||
| await addSentryCliConfig( | ||
| { authToken, url: selfHosted ? sentryUrl : undefined }, | ||
| proguardMappingCliSetupConfig, | ||
| ); | ||
|
|
||
| // Offer optional project-scoped MCP config for Sentry with org and project scope | ||
| await offerProjectScopedMcpConfig( | ||
| selectedProject.organization.slug, | ||
| selectedProject.slug, | ||
| ); | ||
|
|
||
| // Fix mixed line endings caused by inserting LF content into CRLF files (Windows) | ||
| fixLineEndings(); | ||
|
|
||
| // ======== OUTRO ======== | ||
| const issuesPageLink = selfHosted | ||
| ? `${sentryUrl}organizations/${selectedProject.organization.slug}/issues/?project=${selectedProject.id}` | ||
| : `https://${selectedProject.organization.slug}.sentry.io/issues/?project=${selectedProject.id}`; | ||
|
|
||
| clack.outro(` | ||
| ${chalk.greenBright("Successfully installed the Sentry Android SDK!")} | ||
| ${chalk.cyan( | ||
| `You can validate your setup by launching your application and checking Sentry issues page afterwards | ||
| ${issuesPageLink}`, | ||
| )} | ||
| Check out the SDK documentation for further configuration: | ||
| https://docs.sentry.io/platforms/android/ | ||
| `); | ||
| } | ||
|
|
||
| //find files with the given extension | ||
| function findFilesWithExtensions( | ||
| dir: string, | ||
| extensions: string[], | ||
| filesWithExtensions: string[] = [], | ||
| ): string[] { | ||
| const cwd = process.cwd(); | ||
| const files = readdirSync(dir, { withFileTypes: true }); | ||
| for (const file of files) { | ||
| if (file.isDirectory()) { | ||
| const childDir = path.join(dir, file.name); | ||
| findFilesWithExtensions(childDir, extensions, filesWithExtensions); | ||
| } else if (extensions.some((ext) => file.name.endsWith(ext))) { | ||
| filesWithExtensions.push(path.relative(cwd, path.join(dir, file.name))); | ||
| } | ||
| } | ||
| return filesWithExtensions; | ||
| } | ||
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.