From a78e53e3ed273d8550881d4640d0f84cacdf0b89 Mon Sep 17 00:00:00 2001 From: Leo Farias Date: Thu, 29 Feb 2024 16:16:20 -0500 Subject: [PATCH] Update fix --- lib/src/commands/config_command.dart | 2 + lib/src/models/config_model.dart | 82 +++++++++++-------------- lib/src/models/config_model.mapper.dart | 44 +++++++------ lib/src/runner.dart | 7 ++- lib/src/services/config_repository.dart | 23 ++++--- lib/src/utils/deprecation_util.dart | 33 ---------- lib/src/version.dart | 2 +- pubspec.lock | 4 +- pubspec.yaml | 7 ++- test/src/models/config_model_test.dart | 27 ++++++++ 10 files changed, 116 insertions(+), 115 deletions(-) create mode 100644 test/src/models/config_model_test.dart diff --git a/lib/src/commands/config_command.dart b/lib/src/commands/config_command.dart index c2658a38..8656e87e 100644 --- a/lib/src/commands/config_command.dart +++ b/lib/src/commands/config_command.dart @@ -39,6 +39,8 @@ class ConfigCommand extends BaseCommand { logger.info( 'Setting ${key.paramKey} to: ${yellow.wrap(value.toString())}', ); + + logger.info(updatedMap.toString()); updatedConfig = updatedConfig.merge(updatedMap); } } diff --git a/lib/src/models/config_model.dart b/lib/src/models/config_model.dart index 7e187870..8c17d789 100644 --- a/lib/src/models/config_model.dart +++ b/lib/src/models/config_model.dart @@ -159,7 +159,7 @@ class FileConfig extends BaseConfig with FileConfigMappable { } } -@MappableClass() +@MappableClass(ignoreNull: true) class AppConfig extends FileConfig with AppConfigMappable { /// Disables update notification @@ -171,16 +171,16 @@ class AppConfig extends FileConfig with AppConfigMappable { /// Constructor const AppConfig({ - required this.disableUpdateCheck, - required this.lastUpdateCheck, - required super.cachePath, - required super.useGitCache, - required super.gitCachePath, - required super.flutterUrl, - required super.priviledgedAccess, - required super.runPubGetOnSdkChanges, - required super.updateVscodeSettings, - required super.updateGitIgnore, + this.disableUpdateCheck, + this.lastUpdateCheck, + super.cachePath, + super.useGitCache, + super.gitCachePath, + super.flutterUrl, + super.priviledgedAccess, + super.runPubGetOnSdkChanges, + super.updateVscodeSettings, + super.updateGitIgnore, }); static AppConfig empty() { @@ -208,55 +208,47 @@ class AppConfig extends FileConfig with AppConfigMappable { AppConfig merge(BaseConfig? config) { if (config == null) return this; + AppConfig newConfig; if (config is EnvConfig) { - return copyWith( - cachePath: config.cachePath, + newConfig = AppConfig( disableUpdateCheck: disableUpdateCheck, - flutterUrl: config.flutterUrl, - gitCachePath: config.gitCachePath, + cachePath: config.cachePath, useGitCache: config.useGitCache, + gitCachePath: config.gitCachePath, + flutterUrl: config.flutterUrl, ); } if (config is ProjectConfig) { - return copyWith( + newConfig = AppConfig( cachePath: config.cachePath, - flutterUrl: config.flutterUrl, + useGitCache: config.useGitCache, gitCachePath: config.gitCachePath, + flutterUrl: config.flutterUrl, priviledgedAccess: config.priviledgedAccess, runPubGetOnSdkChanges: config.runPubGetOnSdkChanges, - updateGitIgnore: config.updateGitIgnore, updateVscodeSettings: config.updateVscodeSettings, - useGitCache: config.useGitCache, + updateGitIgnore: config.updateGitIgnore, ); } if (config is AppConfig) { - return copyWith( - cachePath: config.cachePath, - disableUpdateCheck: config.disableUpdateCheck, - flutterUrl: config.flutterUrl, - gitCachePath: config.gitCachePath, - lastUpdateCheck: config.lastUpdateCheck, - priviledgedAccess: config.priviledgedAccess, - runPubGetOnSdkChanges: config.runPubGetOnSdkChanges, - updateGitIgnore: config.updateGitIgnore, - updateVscodeSettings: config.updateVscodeSettings, - useGitCache: config.useGitCache, - ); + return copyWith.$merge(config); } - return copyWith( + newConfig = AppConfig( cachePath: config.cachePath, - flutterUrl: config.flutterUrl, - gitCachePath: config.gitCachePath, useGitCache: config.useGitCache, + gitCachePath: config.gitCachePath, + flutterUrl: config.flutterUrl, ); + + return copyWith.$merge(newConfig); } } /// Project config -@MappableClass() +@MappableClass(ignoreNull: true) class ProjectConfig extends FileConfig with ProjectConfigMappable { final String? flutterSdkVersion; final Map? flavors; @@ -265,16 +257,16 @@ class ProjectConfig extends FileConfig with ProjectConfigMappable { /// Constructor const ProjectConfig({ - required this.flutterSdkVersion, - required this.flavors, - required super.cachePath, - required super.useGitCache, - required super.gitCachePath, - required super.flutterUrl, - required super.priviledgedAccess, - required super.runPubGetOnSdkChanges, - required super.updateVscodeSettings, - required super.updateGitIgnore, + this.flutterSdkVersion, + this.flavors, + super.cachePath, + super.useGitCache, + super.gitCachePath, + super.flutterUrl, + super.priviledgedAccess, + super.runPubGetOnSdkChanges, + super.updateVscodeSettings, + super.updateGitIgnore, }); static ProjectConfig empty() { diff --git a/lib/src/models/config_model.mapper.dart b/lib/src/models/config_model.mapper.dart index 6b77670c..88ca6b4d 100644 --- a/lib/src/models/config_model.mapper.dart +++ b/lib/src/models/config_model.mapper.dart @@ -470,34 +470,34 @@ class AppConfigMapper extends ClassMapperBase { static bool? _$disableUpdateCheck(AppConfig v) => v.disableUpdateCheck; static const Field _f$disableUpdateCheck = - Field('disableUpdateCheck', _$disableUpdateCheck); + Field('disableUpdateCheck', _$disableUpdateCheck, opt: true); static DateTime? _$lastUpdateCheck(AppConfig v) => v.lastUpdateCheck; static const Field _f$lastUpdateCheck = - Field('lastUpdateCheck', _$lastUpdateCheck); + Field('lastUpdateCheck', _$lastUpdateCheck, opt: true); static String? _$cachePath(AppConfig v) => v.cachePath; static const Field _f$cachePath = - Field('cachePath', _$cachePath); + Field('cachePath', _$cachePath, opt: true); static bool? _$useGitCache(AppConfig v) => v.useGitCache; static const Field _f$useGitCache = - Field('useGitCache', _$useGitCache); + Field('useGitCache', _$useGitCache, opt: true); static String? _$gitCachePath(AppConfig v) => v.gitCachePath; static const Field _f$gitCachePath = - Field('gitCachePath', _$gitCachePath); + Field('gitCachePath', _$gitCachePath, opt: true); static String? _$flutterUrl(AppConfig v) => v.flutterUrl; static const Field _f$flutterUrl = - Field('flutterUrl', _$flutterUrl); + Field('flutterUrl', _$flutterUrl, opt: true); static bool? _$priviledgedAccess(AppConfig v) => v.priviledgedAccess; static const Field _f$priviledgedAccess = - Field('priviledgedAccess', _$priviledgedAccess); + Field('priviledgedAccess', _$priviledgedAccess, opt: true); static bool? _$runPubGetOnSdkChanges(AppConfig v) => v.runPubGetOnSdkChanges; static const Field _f$runPubGetOnSdkChanges = - Field('runPubGetOnSdkChanges', _$runPubGetOnSdkChanges); + Field('runPubGetOnSdkChanges', _$runPubGetOnSdkChanges, opt: true); static bool? _$updateVscodeSettings(AppConfig v) => v.updateVscodeSettings; static const Field _f$updateVscodeSettings = - Field('updateVscodeSettings', _$updateVscodeSettings); + Field('updateVscodeSettings', _$updateVscodeSettings, opt: true); static bool? _$updateGitIgnore(AppConfig v) => v.updateGitIgnore; static const Field _f$updateGitIgnore = - Field('updateGitIgnore', _$updateGitIgnore); + Field('updateGitIgnore', _$updateGitIgnore, opt: true); @override final MappableFields fields = const { @@ -512,6 +512,8 @@ class AppConfigMapper extends ClassMapperBase { #updateVscodeSettings: _f$updateVscodeSettings, #updateGitIgnore: _f$updateGitIgnore, }; + @override + final bool ignoreNull = true; static AppConfig _instantiate(DecodingData data) { return AppConfig( @@ -669,36 +671,36 @@ class ProjectConfigMapper extends ClassMapperBase { static String? _$flutterSdkVersion(ProjectConfig v) => v.flutterSdkVersion; static const Field _f$flutterSdkVersion = - Field('flutterSdkVersion', _$flutterSdkVersion); + Field('flutterSdkVersion', _$flutterSdkVersion, opt: true); static Map? _$flavors(ProjectConfig v) => v.flavors; static const Field> _f$flavors = - Field('flavors', _$flavors); + Field('flavors', _$flavors, opt: true); static String? _$cachePath(ProjectConfig v) => v.cachePath; static const Field _f$cachePath = - Field('cachePath', _$cachePath); + Field('cachePath', _$cachePath, opt: true); static bool? _$useGitCache(ProjectConfig v) => v.useGitCache; static const Field _f$useGitCache = - Field('useGitCache', _$useGitCache); + Field('useGitCache', _$useGitCache, opt: true); static String? _$gitCachePath(ProjectConfig v) => v.gitCachePath; static const Field _f$gitCachePath = - Field('gitCachePath', _$gitCachePath); + Field('gitCachePath', _$gitCachePath, opt: true); static String? _$flutterUrl(ProjectConfig v) => v.flutterUrl; static const Field _f$flutterUrl = - Field('flutterUrl', _$flutterUrl); + Field('flutterUrl', _$flutterUrl, opt: true); static bool? _$priviledgedAccess(ProjectConfig v) => v.priviledgedAccess; static const Field _f$priviledgedAccess = - Field('priviledgedAccess', _$priviledgedAccess); + Field('priviledgedAccess', _$priviledgedAccess, opt: true); static bool? _$runPubGetOnSdkChanges(ProjectConfig v) => v.runPubGetOnSdkChanges; static const Field _f$runPubGetOnSdkChanges = - Field('runPubGetOnSdkChanges', _$runPubGetOnSdkChanges); + Field('runPubGetOnSdkChanges', _$runPubGetOnSdkChanges, opt: true); static bool? _$updateVscodeSettings(ProjectConfig v) => v.updateVscodeSettings; static const Field _f$updateVscodeSettings = - Field('updateVscodeSettings', _$updateVscodeSettings); + Field('updateVscodeSettings', _$updateVscodeSettings, opt: true); static bool? _$updateGitIgnore(ProjectConfig v) => v.updateGitIgnore; static const Field _f$updateGitIgnore = - Field('updateGitIgnore', _$updateGitIgnore); + Field('updateGitIgnore', _$updateGitIgnore, opt: true); @override final MappableFields fields = const { @@ -713,6 +715,8 @@ class ProjectConfigMapper extends ClassMapperBase { #updateVscodeSettings: _f$updateVscodeSettings, #updateGitIgnore: _f$updateGitIgnore, }; + @override + final bool ignoreNull = true; static ProjectConfig _instantiate(DecodingData data) { return ProjectConfig( diff --git a/lib/src/runner.dart b/lib/src/runner.dart index e16b8945..76837472 100644 --- a/lib/src/runner.dart +++ b/lib/src/runner.dart @@ -62,9 +62,12 @@ class FvmCommandRunner extends CommandRunner { /// user. Future _checkForUpdates() async { try { + final lastUpdateCheck = ctx.lastUpdateCheck ?? DateTime.now(); if (ctx.updateCheckDisabled) return null; - final oneDayAgo = DateTime.now().subtract(const Duration(days: 1)); - if (ctx.lastUpdateCheck?.isBefore(oneDayAgo) ?? false) { + final oneDay = lastUpdateCheck.add(const Duration(days: 1)); + + print(DateTime.now().isBefore(oneDay)); + if (DateTime.now().isBefore(oneDay)) { return null; } diff --git a/lib/src/services/config_repository.dart b/lib/src/services/config_repository.dart index 941061ad..60d17808 100644 --- a/lib/src/services/config_repository.dart +++ b/lib/src/services/config_repository.dart @@ -7,6 +7,7 @@ import '../utils/constants.dart'; import '../utils/extensions.dart'; import '../utils/helpers.dart'; import '../utils/pretty_json.dart'; +import 'logger_service.dart'; const String flutterGitUrl = 'FLUTTER_GIT_URL'; @@ -24,13 +25,17 @@ class ConfigRepository { static AppConfig loadAppConfig() { final appConfig = AppConfig.loadFromPath(_configPath); - if (appConfig != null) return appConfig; + if (appConfig == null) { + save(AppConfig.empty()); + } - return AppConfig.empty(); + return AppConfig.loadFromPath(_configPath)!; } static void save(AppConfig config) { final jsonContents = prettyJson(config.toMap()); + logger.warn('Saving config to $_configPath'); + print(jsonContents); _configPath.file.write(jsonContents); } @@ -56,24 +61,24 @@ class ConfigRepository { static void update({ String? cachePath, - bool? useGitCache, String? gitCachePath, String? flutterUrl, bool? disableUpdateCheck, DateTime? lastUpdateCheck, bool? priviledgedAccess, + bool? useGitCache, }) { final currentConfig = loadAppConfig(); - final newConfig = currentConfig.copyWith( - cachePath: cachePath, + final newConfig = AppConfig( disableUpdateCheck: disableUpdateCheck, - flutterUrl: flutterUrl, - gitCachePath: gitCachePath, lastUpdateCheck: lastUpdateCheck, - priviledgedAccess: priviledgedAccess, + cachePath: cachePath, useGitCache: useGitCache, + gitCachePath: gitCachePath, + flutterUrl: flutterUrl, + priviledgedAccess: priviledgedAccess, ); - save(newConfig); + save(currentConfig.copyWith.$merge(newConfig)); } static EnvConfig _loadEnvironment() { diff --git a/lib/src/utils/deprecation_util.dart b/lib/src/utils/deprecation_util.dart index 6c2e79cf..d2022dd7 100644 --- a/lib/src/utils/deprecation_util.dart +++ b/lib/src/utils/deprecation_util.dart @@ -1,45 +1,12 @@ -import 'dart:convert'; import 'dart:io'; import 'package:mason_logger/mason_logger.dart'; -import 'package:path/path.dart'; import '../models/config_model.dart'; -import '../services/config_repository.dart'; import '../services/logger_service.dart'; -import 'constants.dart'; -import 'context.dart'; void deprecationWorkflow() { _warnDeprecatedEnvVars(); - final fvmDir = ctx.fvmDir; - final legacySettingsFile = File(join(fvmDir, '.settings')); - - if (!legacySettingsFile.existsSync()) { - return; - } - - final payload = legacySettingsFile.readAsStringSync(); - try { - final settings = jsonDecode(payload); - final settingsCachePath = settings['cachePath'] as String?; - if (settingsCachePath != null && settingsCachePath != fvmDir) { - var appConfig = ConfigRepository.loadAppConfig(); - appConfig = appConfig.copyWith(cachePath: fvmDir); - ConfigRepository.save(appConfig); - legacySettingsFile.deleteSync(recursive: true); - logger.success( - 'We have moved the settings file ${legacySettingsFile.path}' - 'Your settings have been migrated to $kAppConfigFile' - 'Your cachePath is now $settingsCachePath. FVM will exit now. Please run the command again.', - ); - // Exit to prevent execution with wrong cache path - exit(ExitCode.success.code); - } - } catch (_) { - logger.warn('Could not parse legacy settings file'); - legacySettingsFile.deleteSync(recursive: true); - } } // TODO: Removed on future version of the app diff --git a/lib/src/version.dart b/lib/src/version.dart index deba512e..a28987a6 100644 --- a/lib/src/version.dart +++ b/lib/src/version.dart @@ -1,2 +1,2 @@ // Generated code. Do not modify. -const packageVersion = '3.0.12'; +const packageVersion = '3.0.10'; diff --git a/pubspec.lock b/pubspec.lock index f5665535..fb25e9b6 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -106,7 +106,7 @@ packages: source: hosted version: "7.3.0" build_verify: - dependency: "direct main" + dependency: "direct dev" description: name: build_verify sha256: abbb9b9eda076854ac1678d284c053a5ec608e64da741d0801f56d4bbea27e23 @@ -114,7 +114,7 @@ packages: source: hosted version: "3.1.0" build_version: - dependency: "direct main" + dependency: "direct dev" description: name: build_version sha256: "4e8eafbf722eac3bd60c8d38f108c04bd69b80100f8792b32be3407725c7fa6a" diff --git a/pubspec.yaml b/pubspec.yaml index 827df0ea..2c7ee21d 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,7 +1,7 @@ name: fvm description: A simple cli to manage Flutter SDK versions per project. Support channels, releases, and local cache for fast switching between versions. -version: 3.0.12 +version: 3.0.10 homepage: https://github.com/leoafarias/fvm environment: @@ -29,8 +29,7 @@ dependencies: pubspec: ^2.3.0 jsonc: ^0.0.3 dart_mappable: ^4.2.0 - build_verify: ^3.1.0 - build_version: ^2.1.1 + dev_dependencies: cli_pkg: ^2.5.0 @@ -42,4 +41,6 @@ dev_dependencies: dart_code_metrics_presets: ^2.9.0 build_runner: ^2.4.8 dart_mappable_builder: ^4.2.0 + build_verify: ^3.1.0 + build_version: ^2.1.1 diff --git a/test/src/models/config_model_test.dart b/test/src/models/config_model_test.dart new file mode 100644 index 00000000..19b0135f --- /dev/null +++ b/test/src/models/config_model_test.dart @@ -0,0 +1,27 @@ +import 'package:fvm/src/models/config_model.dart'; +import 'package:test/test.dart'; + +void main() { + group('person test', () { + test( + '', + () { + final appConfig = AppConfig( + cachePath: 'cachePath', + flutterUrl: 'flutterUrl', + useGitCache: true, + ); + + final newConfig = appConfig.copyWith( + cachePath: 'newCachePath', + flutterUrl: 'newFlutterUrl', + useGitCache: null, + ); + + expect(newConfig.cachePath, 'newCachePath'); + expect(newConfig.flutterUrl, 'newFlutterUrl'); + expect(newConfig.useGitCache, true); + }, + ); + }); +}