Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
leoafarias committed Feb 29, 2024
1 parent 976d789 commit 1357e36
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 71 deletions.
69 changes: 37 additions & 32 deletions lib/src/models/config_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ class ConfigKeys {
static const ConfigKeys gitCachePath = ConfigKeys('git_cache_path');
static const ConfigKeys flutterUrl = ConfigKeys('flutter_url');
static const ConfigKeys priviledgedAccess = ConfigKeys('priviledged_access');
static const ConfigKeys runPubGetOnSdkChanges =
ConfigKeys('run_pub_get_on_sdk_changes');

static const values = <ConfigKeys>[
cachePath,
Expand Down Expand Up @@ -117,13 +119,17 @@ class Config {
/// If FVM should run with priviledged access
bool? priviledgedAccess;

// Run pub get on sdk changes
bool? runPubGetOnSdkChanges;

/// Constructor
Config({
required this.cachePath,
required this.useGitCache,
required this.gitCachePath,
required this.flutterUrl,
required this.priviledgedAccess,
required this.runPubGetOnSdkChanges,
});

factory Config.empty() {
Expand All @@ -133,6 +139,7 @@ class Config {
gitCachePath: null,
flutterUrl: null,
priviledgedAccess: null,
runPubGetOnSdkChanges: null,
);
}

Expand All @@ -143,6 +150,8 @@ class Config {
gitCachePath: map[ConfigKeys.gitCachePath.propKey] as String?,
flutterUrl: map[ConfigKeys.flutterUrl.propKey] as String?,
priviledgedAccess: map[ConfigKeys.priviledgedAccess.propKey] as bool?,
runPubGetOnSdkChanges:
map[ConfigKeys.runPubGetOnSdkChanges.propKey] as bool?,
);
}

Expand All @@ -154,6 +163,8 @@ class Config {
if (flutterUrl != null) ConfigKeys.flutterUrl.propKey: flutterUrl,
if (priviledgedAccess != null)
ConfigKeys.priviledgedAccess.propKey: priviledgedAccess,
if (runPubGetOnSdkChanges != null)
ConfigKeys.runPubGetOnSdkChanges.propKey: runPubGetOnSdkChanges,
};
}
}
Expand All @@ -173,6 +184,7 @@ class AppConfig extends Config {
required super.gitCachePath,
required super.flutterUrl,
required super.priviledgedAccess,
required super.runPubGetOnSdkChanges,
});

factory AppConfig.empty() {
Expand All @@ -184,6 +196,7 @@ class AppConfig extends Config {
gitCachePath: null,
flutterUrl: null,
priviledgedAccess: null,
runPubGetOnSdkChanges: null,
);
}

Expand All @@ -200,6 +213,7 @@ class AppConfig extends Config {
gitCachePath: envConfig.gitCachePath,
flutterUrl: envConfig.flutterUrl,
priviledgedAccess: envConfig.priviledgedAccess,
runPubGetOnSdkChanges: envConfig.runPubGetOnSdkChanges,
);
}

Expand All @@ -223,6 +237,7 @@ class AppConfig extends Config {
bool? disableUpdateCheck,
DateTime? lastUpdateCheck,
bool? priviledgedAccess,
bool? runPubGetOnSdkChanges,
}) {
return AppConfig(
disableUpdateCheck: disableUpdateCheck ?? this.disableUpdateCheck,
Expand All @@ -232,6 +247,8 @@ class AppConfig extends Config {
gitCachePath: gitCachePath ?? this.gitCachePath,
flutterUrl: flutterUrl ?? this.flutterUrl,
priviledgedAccess: priviledgedAccess ?? this.priviledgedAccess,
runPubGetOnSdkChanges:
runPubGetOnSdkChanges ?? this.runPubGetOnSdkChanges,
);
}

Expand All @@ -244,6 +261,7 @@ class AppConfig extends Config {
disableUpdateCheck: config?.disableUpdateCheck,
lastUpdateCheck: config?.lastUpdateCheck,
priviledgedAccess: config?.priviledgedAccess,
runPubGetOnSdkChanges: config?.runPubGetOnSdkChanges,
);
}

Expand All @@ -254,6 +272,7 @@ class AppConfig extends Config {
gitCachePath: config?.gitCachePath,
flutterUrl: config?.flutterUrl,
priviledgedAccess: config?.priviledgedAccess,
runPubGetOnSdkChanges: config?.runPubGetOnSdkChanges,
);
}

Expand All @@ -277,13 +296,10 @@ class ProjectConfig extends Config {
Map<String, String>? flavors;

/// If Vscode settings is not managed by FVM
bool? _updateVscodeSettings;
bool? updateVscodeSettings;

/// If FVM should update .gitignore
bool? _updateGitIgnore;

/// If should run pub get on sdk change
bool? _runPubGetOnSdkChanges;
bool? updateGitIgnore;

/// Constructor
ProjectConfig({
Expand All @@ -292,14 +308,12 @@ class ProjectConfig extends Config {
super.gitCachePath,
super.flutterUrl,
super.priviledgedAccess,
super.runPubGetOnSdkChanges,
this.flutterSdkVersion,
this.flavors,
bool? updateVscodeSettings,
bool? updateGitIgnore,
bool? runPubGetOnSdkChanges,
}) : _updateVscodeSettings = updateVscodeSettings,
_updateGitIgnore = updateGitIgnore,
_runPubGetOnSdkChanges = runPubGetOnSdkChanges;
this.updateVscodeSettings,
this.updateGitIgnore,
});

/// Returns ConfigDto from a map
factory ProjectConfig.fromMap(Map<String, dynamic> map) {
Expand All @@ -311,11 +325,11 @@ class ProjectConfig extends Config {
gitCachePath: envConfig.gitCachePath,
flutterUrl: envConfig.flutterUrl,
priviledgedAccess: envConfig.priviledgedAccess,
runPubGetOnSdkChanges: envConfig.runPubGetOnSdkChanges,
flutterSdkVersion: map['flutterSdkVersion'] ?? map['flutter'] as String?,
flavors: map['flavors'] != null ? Map.from(map['flavors'] as Map) : null,
updateVscodeSettings: map['updateVscodeSettings'] as bool?,
updateGitIgnore: map['updateGitIgnore'] as bool?,
runPubGetOnSdkChanges: map['runPubGetOnSdkChanges'] as bool?,
);
}

Expand All @@ -331,15 +345,6 @@ class ProjectConfig extends Config {
: null;
}

/// Returns update vscode settings
bool? get updateVscodeSettings => _updateVscodeSettings;

/// Returns update git ignore
bool? get updateGitIgnore => _updateGitIgnore;

/// Returns run pub get on sdk changes
bool? get runPubGetOnSdkChanges => _runPubGetOnSdkChanges;

/// Copies current config and overrides with new values
/// Returns a new ConfigDto
Expand Down Expand Up @@ -368,11 +373,12 @@ class ProjectConfig extends Config {
gitCachePath: gitCachePath ?? this.gitCachePath,
flutterUrl: flutterUrl ?? this.flutterUrl,
priviledgedAccess: priviledgedAccess ?? this.priviledgedAccess,
runPubGetOnSdkChanges:
runPubGetOnSdkChanges ?? this.runPubGetOnSdkChanges,
flutterSdkVersion: flutterSdkVersion ?? this.flutterSdkVersion,
flavors: mergedFlavors,
updateVscodeSettings: updateVscodeSettings ?? _updateVscodeSettings,
updateGitIgnore: updateGitIgnore ?? _updateGitIgnore,
runPubGetOnSdkChanges: runPubGetOnSdkChanges ?? _runPubGetOnSdkChanges,
updateVscodeSettings: updateVscodeSettings ?? this.updateVscodeSettings,
updateGitIgnore: updateGitIgnore ?? this.updateGitIgnore,
);
}

Expand All @@ -381,9 +387,9 @@ class ProjectConfig extends Config {
cachePath: config.cachePath,
flutterSdkVersion: config.flutterSdkVersion,
useGitCache: config.useGitCache,
updateVscodeSettings: config._updateVscodeSettings,
updateGitIgnore: config._updateGitIgnore,
runPubGetOnSdkChanges: config._runPubGetOnSdkChanges,
updateVscodeSettings: config.updateVscodeSettings,
updateGitIgnore: config.updateGitIgnore,
runPubGetOnSdkChanges: config.runPubGetOnSdkChanges,
priviledgedAccess: config.priviledgedAccess,
gitCachePath: config.gitCachePath,
flutterUrl: config.flutterUrl,
Expand Down Expand Up @@ -413,11 +419,10 @@ class ProjectConfig extends Config {
return {
...super.toMap(),
if (flutterSdkVersion != null) 'flutter': flutterSdkVersion,
if (_updateVscodeSettings != null)
'updateVscodeSettings': _updateVscodeSettings,
if (_updateGitIgnore != null) 'updateGitIgnore': _updateGitIgnore,
if (_runPubGetOnSdkChanges != null)
'runPubGetOnSdkChanges': _runPubGetOnSdkChanges,
if (updateVscodeSettings != null)
'updateVscodeSettings': updateVscodeSettings,
if (updateGitIgnore != null) 'updateGitIgnore': updateGitIgnore,
'runPubGetOnSdkChanges': runPubGetOnSdkChanges,
if (flavors != null && flavors!.isNotEmpty) 'flavors': flavors,
};
}
Expand Down
35 changes: 12 additions & 23 deletions lib/src/services/config_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,56 +48,45 @@ class ConfigRepository {
static Config loadEnv() {
final environments = Platform.environment;

bool? gitCache;
String? gitCachePath;
String? flutterUrl;
String? cachePath;
bool? priviledgedAccess;
final config = Config.empty();

// Default to Flutter's environment variable if present; can still be overridden
if (environments.containsKey(flutterGitUrl)) {
flutterUrl = environments[flutterGitUrl];
config.flutterUrl = environments[flutterGitUrl];
}

for (final variable in ConfigKeys.values) {
final value = environments[variable.envKey];
final legacyFvmHome = environments['FVM_HOME'];

if (variable == ConfigKeys.cachePath) {
cachePath = value ?? legacyFvmHome;
break;
config.cachePath = value ?? legacyFvmHome;
}

if (value == null) continue;

if (variable == ConfigKeys.useGitCache) {
gitCache = stringToBool(value);
break;
config.useGitCache = stringToBool(value);
}

if (variable == ConfigKeys.gitCachePath) {
gitCachePath = value;
break;
config.gitCachePath = value;
}

if (variable == ConfigKeys.flutterUrl) {
flutterUrl = value;
break;
config.flutterUrl = value;
}

if (variable == ConfigKeys.priviledgedAccess) {
priviledgedAccess = stringToBool(value);
break;
config.priviledgedAccess = stringToBool(value);
}

if (variable == ConfigKeys.runPubGetOnSdkChanges) {
config.runPubGetOnSdkChanges = stringToBool(value);
}
}

return Config(
cachePath: cachePath,
useGitCache: gitCache,
gitCachePath: gitCachePath,
flutterUrl: flutterUrl,
priviledgedAccess: priviledgedAccess,
);
return config;
}

static String get _configPath => kAppConfigFile;
Expand Down
38 changes: 25 additions & 13 deletions lib/src/services/project_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,30 @@ import '../utils/extensions.dart';
import '../utils/pretty_json.dart';
import 'base_service.dart';

class ProjectRepository {
const ProjectRepository._();
static Project findAncestor(Directory directory) {
// Checks if the directory is root
final isRootDir = path.rootPrefix(directory.path) == directory.path;

// Gets project from directory
final project = Project.loadFromPath(directory.path);

// If project has a config return it
if (project.hasConfig && project.hasPubspec) {
return project;
}

// Return working directory if has reached root
if (isRootDir) {
return Project.loadFromPath(directory.path);
}

// Recursive look up
return findAncestor(directory.parent);
}
}

/// Flutter Project Services
/// APIs for interacting with local Flutter projects
///
Expand All @@ -32,19 +56,7 @@ class ProjectService extends ContextService {
// Get directory, defined root or current
directory ??= Directory(context.workingDirectory);

// Checks if the directory is root
final isRootDir = path.rootPrefix(directory.path) == directory.path;

// Gets project from directory
final project = Project.loadFromPath(directory.path);

// If project has a config return it
if (project.hasConfig && project.hasPubspec) return project;

// Return working directory if has reached root
if (isRootDir) return Project.loadFromPath(context.workingDirectory);

return findAncestor(directory: directory.parent);
return ProjectRepository.findAncestor(directory);
}

/// Search for version configured
Expand Down
22 changes: 19 additions & 3 deletions lib/src/utils/context.dart
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class FVMContext {
workingDirectory ??= Directory.current.path;

// Load config from file in config path

final projectConfig = ProjectConfig.loadFromPath(workingDirectory);
final envConfig = ConfigRepository.loadEnv();

Expand Down Expand Up @@ -105,7 +106,16 @@ class FVMContext {
String get fvmDir => config.cachePath ?? kAppDirHome;

/// Flag to determine if should use git cache
bool get gitCache => config.useGitCache ?? true;
bool get gitCache {
return config.useGitCache != null ? config.useGitCache! : true;
}

/// Run pub get on sdk changes
bool get runPubGetOnSdkChanges {
return config.runPubGetOnSdkChanges != null
? config.runPubGetOnSdkChanges!
: true;
}

String get gitCachePath {
// If git cache is not overriden use default based on fvmDir
Expand All @@ -121,10 +131,16 @@ class FVMContext {
DateTime? get lastUpdateCheck => config.lastUpdateCheck;

/// Flutter SDK Path
bool get updateCheckDisabled => config.disableUpdateCheck ?? false;
bool get updateCheckDisabled {
return config.disableUpdateCheck != null
? config.disableUpdateCheck!
: false;
}

/// Priviledged access
bool get priviledgedAccess => config.priviledgedAccess ?? true;
bool get priviledgedAccess {
return config.priviledgedAccess != null ? config.priviledgedAccess! : true;
}

/// Where Default Flutter SDK is stored
String get globalCacheLink => join(fvmDir, 'default');
Expand Down

0 comments on commit 1357e36

Please sign in to comment.