Skip to content

Commit

Permalink
add sentry dart plugin options and propeties to pubspec file
Browse files Browse the repository at this point in the history
  • Loading branch information
denrase committed Dec 3, 2024
1 parent 7516db9 commit 95a01e6
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 6 deletions.
41 changes: 40 additions & 1 deletion src/flutter/code-tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import * as clack from '@clack/prompts';
import chalk from 'chalk';
import {
sentryImport,
pubspecOptions,
sentryProperties,
// testErrorSnippet,
} from './templates';

Expand Down Expand Up @@ -35,7 +37,7 @@ export function findFile(dir: string, name: string): string | null {
return null;
}

export function patchPubspec(pubspecFile: string | null): boolean {
export function patchPubspec(pubspecFile: string | null, project: string, org: string): boolean {
if (!pubspecFile || !fs.existsSync(pubspecFile)) {
clack.log.warn('No pubspec.yaml source file found in filesystem.');
Sentry.captureException('No pubspec.yaml source file');
Expand All @@ -45,21 +47,58 @@ export function patchPubspec(pubspecFile: string | null): boolean {

const dependenciesIndex = getDependenciesLocation(pubspecContent);

// TODO: Check if already added sentry:

pubspecContent = pubspecContent.slice(0, dependenciesIndex) +
' sentry:\n' +
pubspecContent.slice(dependenciesIndex);

const devDependenciesIndex = getDevDependenciesLocation(pubspecContent);

// TODO: Check if already added sentry-dart-plugin:

pubspecContent = pubspecContent.slice(0, devDependenciesIndex) +
' sentry-dart-plugin:\n' +
pubspecContent.slice(devDependenciesIndex);

// TODO: Check if already added sentry:

pubspecContent += '\n'
pubspecContent += pubspecOptions(project, org);

fs.writeFileSync(pubspecFile, pubspecContent, 'utf8');

return true;
}

export function addProperties(pubspecFile: string | null, authToken: string) {
if (!pubspecFile || !fs.existsSync(pubspecFile)) {
clack.log.warn('No pubspec.yaml source file found in filesystem.');
Sentry.captureException('No pubspec.yaml source file');
return false;
}

try {
const pubspecDir = path.dirname(pubspecFile);
const sentryPropertiesFileName = 'sentry.properties';
const sentryPropertiesFile = path.join(pubspecDir, sentryPropertiesFileName);
const sentryPropertiesContent = sentryProperties(authToken);

fs.writeFileSync(sentryPropertiesFile, sentryPropertiesContent, 'utf8');

const gitignoreFile = path.join(pubspecDir, '.gitignore');
if (fs.existsSync(gitignoreFile)) {
fs.appendFileSync(gitignoreFile, `\n${sentryPropertiesFileName}\n`);
} else {
fs.writeFileSync(gitignoreFile, `${sentryPropertiesFileName}\n`, 'utf8');
}

return true;
} catch (e) {
return false;
}
}

export function patchMain(mainFile: string | null): boolean {
if (!mainFile || !fs.existsSync(mainFile)) {
clack.log.warn('No main.dart source file found in filesystem.');
Expand Down
25 changes: 21 additions & 4 deletions src/flutter/flutter-wizzard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,38 @@ async function runFlutterWizzardWithTelemetry(

// const dsn = selectedProject.keys[0].dsn.public;
const projectDir = process.cwd();
const pubspecFile = findFile(projectDir, 'pubspec.yaml');

// ======== STEP X. Add Sentry to pubspec.yaml ============
// ======== STEP X. Add Sentry and Sentry Dart Plugin to pubspec.yaml ============
clack.log.step(
`Adding ${chalk.bold('Sentry')} to your apps ${chalk.cyan('pubspec.yaml',)} file.`,
);
const pubspecPatched = traceStep('Patch pubspec.yaml', () =>
codetools.patchPubspec(findFile(projectDir, 'pubspec.yaml')),
);
const pubspecPatched = codetools.patchPubspec(
pubspecFile,
selectedProject.slug,
selectedProject.organization.slug
)
if (!pubspecPatched) {
clack.log.warn(
"Could not add Sentry to your apps pubspec.yaml file. You'll have to add it manually.\nPlease follow the instructions at https://docs.sentry.io/platforms/flutter/#install",
);
}
Sentry.setTag('pubspec-patched', pubspecPatched);

// ======== STEP X. Add sentry.properties with auth token ============

const propertiesAdded = codetools.addProperties(pubspecFile, authToken);
if (!propertiesAdded) {
clack.log.warn(
`We could not add "sentry.properties" file in your project directory in order to provide an auth token for Sentry CLI. You'll have to add it manually, or you can set the SENTRY_AUTH_TOKEN environment variable instead. See https://docs.sentry.io/cli/configuration/#auth-token for more information.`,
);
} else {
clack.log.info(
`We created "sentry.properties" file in your project directory in order to provide an auth token for Sentry CLI.\nIt was also added to your ".gitignore" file.\nAt your CI enviroment, you can set the SENTRY_AUTH_TOKEN environment variable instead. See https://docs.sentry.io/cli/configuration/#auth-token for more information.`,
);
}
Sentry.setTag('sentry-properties-added', pubspecPatched);

// ======== STEP X. Patch main.dart with setup and a test error snippet ============
clack.log.step(
`Patching ${chalk.bold('main.dart')} with setup and test error snippet.`,
Expand Down
15 changes: 14 additions & 1 deletion src/flutter/templates.ts
Original file line number Diff line number Diff line change
@@ -1 +1,14 @@
export const sentryImport = `import 'package:sentry_flutter/sentry_flutter.dart';\n`;
export const sentryImport = `import 'package:sentry_flutter/sentry_flutter.dart';\n`;

export function pubspecOptions(project: string, org: string): string {
return `sentry:
upload_source_maps: true
upload_sources: true
project: ${project}
org: ${org}
`
}

export function sentryProperties(authToken: string): string {
return `auth_token=${authToken}`;
}

0 comments on commit 95a01e6

Please sign in to comment.