Skip to content

Commit

Permalink
feat(fvm): Support v3 config file (#28665)
Browse files Browse the repository at this point in the history
  • Loading branch information
Laennart committed Apr 29, 2024
1 parent 8965a39 commit 644c4a1
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 23 deletions.
28 changes: 26 additions & 2 deletions lib/modules/manager/fvm/extract.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ describe('modules/manager/fvm/extract', () => {
).toBeNull();
});

it('returns a result', () => {
it('returns a result for .fvm/fvm_config.json', () => {
const res = extractPackageFile(
'{"flutterSdkVersion": "2.10.1", "flavors": {}}',
packageFile,
Expand All @@ -38,7 +38,19 @@ describe('modules/manager/fvm/extract', () => {
]);
});

it('supports non range', () => {
it('returns a result for .fvmrc', () => {
const res = extractPackageFile('{"flutter": "2.10.1"}', packageFile);
expect(res?.deps).toEqual([
{
currentValue: '2.10.1',
datasource: 'flutter-version',
depName: 'flutter',
packageName: 'flutter/flutter',
},
]);
});

it('supports non range for .fvm/fvm_config.json', () => {
const res = extractPackageFile(
'{"flutterSdkVersion": "stable", "flavors": {}}',
packageFile,
Expand All @@ -52,5 +64,17 @@ describe('modules/manager/fvm/extract', () => {
},
]);
});

it('supports non range for .fvmrc', () => {
const res = extractPackageFile('{"flutter": "stable"}', packageFile);
expect(res?.deps).toEqual([
{
currentValue: 'stable',
datasource: 'flutter-version',
depName: 'flutter',
packageName: 'flutter/flutter',
},
]);
});
});
});
33 changes: 14 additions & 19 deletions lib/modules/manager/fvm/extract.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,33 @@
import is from '@sindresorhus/is';
import { logger } from '../../../logger';
import { Json } from '../../../util/schema-utils';
import { FlutterVersionDatasource } from '../../datasource/flutter-version';
import type { PackageDependency, PackageFileContent } from '../types';

interface FvmConfig {
flutterSdkVersion: string;
}
import { FvmConfig } from './schema';

export function extractPackageFile(
content: string,
packageFile: string,
): PackageFileContent | null {
let fvmConfig: FvmConfig;
let flutterVersion: string | undefined;
try {
fvmConfig = JSON.parse(content);
const config = Json.pipe(FvmConfig).parse(content);
flutterVersion = config.flutter ?? config.flutterSdkVersion;

if (!flutterVersion) {
logger.debug(
{ contents: config },
'FVM config does not have a flutter version specified',
);
return null;
}
} catch (err) {
logger.debug({ packageFile, err }, 'Invalid FVM config');
return null;
}

if (!fvmConfig.flutterSdkVersion) {
logger.debug(
{ contents: fvmConfig },
'FVM config does not have flutterSdkVersion specified',
);
return null;
} else if (!is.string(fvmConfig.flutterSdkVersion)) {
logger.debug({ contents: fvmConfig }, 'flutterSdkVersion must be a string');
return null;
}

const dep: PackageDependency = {
depName: 'flutter',
currentValue: fvmConfig.flutterSdkVersion,
currentValue: flutterVersion,
datasource: FlutterVersionDatasource.id,
packageName: 'flutter/flutter',
};
Expand Down
2 changes: 1 addition & 1 deletion lib/modules/manager/fvm/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ export { extractPackageFile } from './extract';
export const supportedDatasources = [FlutterVersionDatasource.id];

export const defaultConfig = {
fileMatch: ['(^|/)\\.fvm/fvm_config\\.json$'],
fileMatch: ['(^|/)\\.fvm/fvm_config\\.json$', '(^|/)\\.fvmrc$'],
versioning: semverVersioning.id,
};
2 changes: 1 addition & 1 deletion lib/modules/manager/fvm/readme.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Keeps the `.fvm/fvm_config.json` file updated.
Keeps the `.fvmrc` file or older `.fvm/fvm_config.json` file updated.
7 changes: 7 additions & 0 deletions lib/modules/manager/fvm/schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { z } from 'zod';

export const FvmConfig = z.object({
flutterSdkVersion: z.string().optional(),
flutter: z.string().optional(),
});
export type FvmConfig = z.infer<typeof FvmConfig>;

0 comments on commit 644c4a1

Please sign in to comment.