From fd82fb52bc77c8c2838525e4ce6fbeb5ee3ae5f1 Mon Sep 17 00:00:00 2001 From: Denis Andrasec Date: Mon, 2 Dec 2024 17:30:25 +0100 Subject: [PATCH] patch pubspec.yaml --- src/flutter/code-tools.ts | 47 ++++++++++++++++++++++++--------- test/flutter/code-tools.test.ts | 33 ++++++++++++++++++++++- 2 files changed, 67 insertions(+), 13 deletions(-) diff --git a/src/flutter/code-tools.ts b/src/flutter/code-tools.ts index cb1b1a57..09902395 100644 --- a/src/flutter/code-tools.ts +++ b/src/flutter/code-tools.ts @@ -41,7 +41,22 @@ export function patchPubspec(pubspecFile: string | null): boolean { Sentry.captureException('No pubspec.yaml source file'); return false; } + let pubspecContent = fs.readFileSync(pubspecFile, 'utf8'); + + const dependenciesIndex = getDependenciesLocation(pubspecContent); + + pubspecContent = pubspecContent.slice(0, dependenciesIndex) + + ' sentry:\n' + + pubspecContent.slice(dependenciesIndex); + + const devDependenciesIndex = getDevDependenciesLocation(pubspecContent); + + pubspecContent = pubspecContent.slice(0, devDependenciesIndex) + + ' sentry-dart-plugin:\n' + + pubspecContent.slice(devDependenciesIndex); + fs.writeFileSync(pubspecFile, pubspecContent, 'utf8'); + return true; } @@ -88,21 +103,29 @@ export function patchMain(mainFile: string | null): boolean { return true; } -/** - * Returns the string index of the last import statement in the given code file. - * - * @param sourceCode - * @returns the insert index, or 0 if none found. - */ export function getLastImportLineLocation(sourceCode: string): number { const importRegex = /import\s+['"].*['"].*;/gim; + return getLastReqExpLocation(sourceCode, importRegex); +} + +export function getDependenciesLocation(sourceCode: string): number { + const dependencyRegex = /^dependencies:\s*$/gim; + return getLastReqExpLocation(sourceCode, dependencyRegex); +} - let importsMatch = importRegex.exec(sourceCode); +export function getDevDependenciesLocation(sourceCode: string): number { + const dependencyRegex = /^dev_dependencies:\s*$/gim; + return getLastReqExpLocation(sourceCode, dependencyRegex); +} + +// Helper + +function getLastReqExpLocation(sourceCode: string, regExp: RegExp): number { + let match = regExp.exec(sourceCode); let importIndex = 0; - while (importsMatch) { - importIndex = importsMatch.index + importsMatch[0].length + 1; - importsMatch = importRegex.exec(sourceCode); + while (match) { + importIndex = match.index + match[0].length + 1; + match = regExp.exec(sourceCode); } return importIndex; - return 0; -} +} \ No newline at end of file diff --git a/test/flutter/code-tools.test.ts b/test/flutter/code-tools.test.ts index 4ad79e01..b0c19fcb 100644 --- a/test/flutter/code-tools.test.ts +++ b/test/flutter/code-tools.test.ts @@ -1,7 +1,38 @@ //@ts-ignore -import { getLastImportLineLocation } from '../../src/flutter/code-tools'; +import { getDependenciesLocation, getDevDependenciesLocation, getLastImportLineLocation } from '../../src/flutter/code-tools'; describe('code-tools', () => { + const pubspec = `name: flutter_example +description: An example flutter app. +version: 1.0.0 +publish_to: 'none' # Remove this line if you wish to publish to pub.dev + +environment: + sdk: '>=2.17.0 <4.0.0' + flutter: '>=3.0.0' + +dependencies: + flutter: + sdk: flutter + +dev_dependencies: + flutter_lints: ^2.0.0 +`; + + describe('pubspec', () => { + it('returns proper line index for dependencies', () => { + expect(getDependenciesLocation(pubspec)).toBe( + pubspec.indexOf(' flutter:\n'), + ); + }); + + it('returns proper line index for dev-dependencies', () => { + expect(getDevDependenciesLocation(pubspec)).toBe( + pubspec.indexOf(' flutter_lints: ^2.0.0\n'), + ); + }); + }); + describe('getLastImportLineLocation', () => { it('returns proper line index', () => { const code = `import 'foo:bar';\n` + `//\n` + `class X {}`;