Skip to content

Commit

Permalink
patch pubspec.yaml
Browse files Browse the repository at this point in the history
  • Loading branch information
denrase committed Dec 2, 2024
1 parent 1ccd8cd commit fd82fb5
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 13 deletions.
47 changes: 35 additions & 12 deletions src/flutter/code-tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}
}
33 changes: 32 additions & 1 deletion test/flutter/code-tools.test.ts
Original file line number Diff line number Diff line change
@@ -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` + `//<insert-location>\n` + `class X {}`;
Expand Down

0 comments on commit fd82fb5

Please sign in to comment.