Skip to content

Commit

Permalink
Update fix
Browse files Browse the repository at this point in the history
  • Loading branch information
leoafarias committed Feb 29, 2024
1 parent 765df6c commit a78e53e
Show file tree
Hide file tree
Showing 10 changed files with 116 additions and 115 deletions.
2 changes: 2 additions & 0 deletions lib/src/commands/config_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
82 changes: 37 additions & 45 deletions lib/src/models/config_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ class FileConfig extends BaseConfig with FileConfigMappable {
}
}

@MappableClass()
@MappableClass(ignoreNull: true)
class AppConfig extends FileConfig with AppConfigMappable {
/// Disables update notification
Expand All @@ -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() {
Expand Down Expand Up @@ -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<String, String>? flavors;
Expand All @@ -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() {
Expand Down
44 changes: 24 additions & 20 deletions lib/src/models/config_model.mapper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -470,34 +470,34 @@ class AppConfigMapper extends ClassMapperBase<AppConfig> {

static bool? _$disableUpdateCheck(AppConfig v) => v.disableUpdateCheck;
static const Field<AppConfig, bool> _f$disableUpdateCheck =
Field('disableUpdateCheck', _$disableUpdateCheck);
Field('disableUpdateCheck', _$disableUpdateCheck, opt: true);
static DateTime? _$lastUpdateCheck(AppConfig v) => v.lastUpdateCheck;
static const Field<AppConfig, DateTime> _f$lastUpdateCheck =
Field('lastUpdateCheck', _$lastUpdateCheck);
Field('lastUpdateCheck', _$lastUpdateCheck, opt: true);
static String? _$cachePath(AppConfig v) => v.cachePath;
static const Field<AppConfig, String> _f$cachePath =
Field('cachePath', _$cachePath);
Field('cachePath', _$cachePath, opt: true);
static bool? _$useGitCache(AppConfig v) => v.useGitCache;
static const Field<AppConfig, bool> _f$useGitCache =
Field('useGitCache', _$useGitCache);
Field('useGitCache', _$useGitCache, opt: true);
static String? _$gitCachePath(AppConfig v) => v.gitCachePath;
static const Field<AppConfig, String> _f$gitCachePath =
Field('gitCachePath', _$gitCachePath);
Field('gitCachePath', _$gitCachePath, opt: true);
static String? _$flutterUrl(AppConfig v) => v.flutterUrl;
static const Field<AppConfig, String> _f$flutterUrl =
Field('flutterUrl', _$flutterUrl);
Field('flutterUrl', _$flutterUrl, opt: true);
static bool? _$priviledgedAccess(AppConfig v) => v.priviledgedAccess;
static const Field<AppConfig, bool> _f$priviledgedAccess =
Field('priviledgedAccess', _$priviledgedAccess);
Field('priviledgedAccess', _$priviledgedAccess, opt: true);
static bool? _$runPubGetOnSdkChanges(AppConfig v) => v.runPubGetOnSdkChanges;
static const Field<AppConfig, bool> _f$runPubGetOnSdkChanges =
Field('runPubGetOnSdkChanges', _$runPubGetOnSdkChanges);
Field('runPubGetOnSdkChanges', _$runPubGetOnSdkChanges, opt: true);
static bool? _$updateVscodeSettings(AppConfig v) => v.updateVscodeSettings;
static const Field<AppConfig, bool> _f$updateVscodeSettings =
Field('updateVscodeSettings', _$updateVscodeSettings);
Field('updateVscodeSettings', _$updateVscodeSettings, opt: true);
static bool? _$updateGitIgnore(AppConfig v) => v.updateGitIgnore;
static const Field<AppConfig, bool> _f$updateGitIgnore =
Field('updateGitIgnore', _$updateGitIgnore);
Field('updateGitIgnore', _$updateGitIgnore, opt: true);

@override
final MappableFields<AppConfig> fields = const {
Expand All @@ -512,6 +512,8 @@ class AppConfigMapper extends ClassMapperBase<AppConfig> {
#updateVscodeSettings: _f$updateVscodeSettings,
#updateGitIgnore: _f$updateGitIgnore,
};
@override
final bool ignoreNull = true;

static AppConfig _instantiate(DecodingData data) {
return AppConfig(
Expand Down Expand Up @@ -669,36 +671,36 @@ class ProjectConfigMapper extends ClassMapperBase<ProjectConfig> {

static String? _$flutterSdkVersion(ProjectConfig v) => v.flutterSdkVersion;
static const Field<ProjectConfig, String> _f$flutterSdkVersion =
Field('flutterSdkVersion', _$flutterSdkVersion);
Field('flutterSdkVersion', _$flutterSdkVersion, opt: true);
static Map<String, String>? _$flavors(ProjectConfig v) => v.flavors;
static const Field<ProjectConfig, Map<String, String>> _f$flavors =
Field('flavors', _$flavors);
Field('flavors', _$flavors, opt: true);
static String? _$cachePath(ProjectConfig v) => v.cachePath;
static const Field<ProjectConfig, String> _f$cachePath =
Field('cachePath', _$cachePath);
Field('cachePath', _$cachePath, opt: true);
static bool? _$useGitCache(ProjectConfig v) => v.useGitCache;
static const Field<ProjectConfig, bool> _f$useGitCache =
Field('useGitCache', _$useGitCache);
Field('useGitCache', _$useGitCache, opt: true);
static String? _$gitCachePath(ProjectConfig v) => v.gitCachePath;
static const Field<ProjectConfig, String> _f$gitCachePath =
Field('gitCachePath', _$gitCachePath);
Field('gitCachePath', _$gitCachePath, opt: true);
static String? _$flutterUrl(ProjectConfig v) => v.flutterUrl;
static const Field<ProjectConfig, String> _f$flutterUrl =
Field('flutterUrl', _$flutterUrl);
Field('flutterUrl', _$flutterUrl, opt: true);
static bool? _$priviledgedAccess(ProjectConfig v) => v.priviledgedAccess;
static const Field<ProjectConfig, bool> _f$priviledgedAccess =
Field('priviledgedAccess', _$priviledgedAccess);
Field('priviledgedAccess', _$priviledgedAccess, opt: true);
static bool? _$runPubGetOnSdkChanges(ProjectConfig v) =>
v.runPubGetOnSdkChanges;
static const Field<ProjectConfig, bool> _f$runPubGetOnSdkChanges =
Field('runPubGetOnSdkChanges', _$runPubGetOnSdkChanges);
Field('runPubGetOnSdkChanges', _$runPubGetOnSdkChanges, opt: true);
static bool? _$updateVscodeSettings(ProjectConfig v) =>
v.updateVscodeSettings;
static const Field<ProjectConfig, bool> _f$updateVscodeSettings =
Field('updateVscodeSettings', _$updateVscodeSettings);
Field('updateVscodeSettings', _$updateVscodeSettings, opt: true);
static bool? _$updateGitIgnore(ProjectConfig v) => v.updateGitIgnore;
static const Field<ProjectConfig, bool> _f$updateGitIgnore =
Field('updateGitIgnore', _$updateGitIgnore);
Field('updateGitIgnore', _$updateGitIgnore, opt: true);

@override
final MappableFields<ProjectConfig> fields = const {
Expand All @@ -713,6 +715,8 @@ class ProjectConfigMapper extends ClassMapperBase<ProjectConfig> {
#updateVscodeSettings: _f$updateVscodeSettings,
#updateGitIgnore: _f$updateGitIgnore,
};
@override
final bool ignoreNull = true;

static ProjectConfig _instantiate(DecodingData data) {
return ProjectConfig(
Expand Down
7 changes: 5 additions & 2 deletions lib/src/runner.dart
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,12 @@ class FvmCommandRunner extends CommandRunner<int> {
/// user.
Future<Function()?> _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;
}

Expand Down
23 changes: 14 additions & 9 deletions lib/src/services/config_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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);
}
Expand All @@ -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() {
Expand Down
33 changes: 0 additions & 33 deletions lib/src/utils/deprecation_util.dart
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/src/version.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,15 @@ packages:
source: hosted
version: "7.3.0"
build_verify:
dependency: "direct main"
dependency: "direct dev"
description:
name: build_verify
sha256: abbb9b9eda076854ac1678d284c053a5ec608e64da741d0801f56d4bbea27e23
url: "https://pub.dev"
source: hosted
version: "3.1.0"
build_version:
dependency: "direct main"
dependency: "direct dev"
description:
name: build_version
sha256: "4e8eafbf722eac3bd60c8d38f108c04bd69b80100f8792b32be3407725c7fa6a"
Expand Down
Loading

0 comments on commit a78e53e

Please sign in to comment.