From 9f1b8433d28c58895b5323137638ed9de8971f81 Mon Sep 17 00:00:00 2001 From: leoafarias Date: Fri, 12 Nov 2021 10:39:16 -0500 Subject: [PATCH] Clean up logic for should detach --- lib/constants.dart | 4 ---- lib/src/utils/guards.dart | 16 +++------------- lib/src/utils/helpers.dart | 20 ++++++++++++++++++++ 3 files changed, 23 insertions(+), 17 deletions(-) diff --git a/lib/constants.dart b/lib/constants.dart index 9992fd25..c43dddd3 100644 --- a/lib/constants.dart +++ b/lib/constants.dart @@ -50,7 +50,3 @@ String get kFvmHome { /// Flutter Channels const kFlutterChannels = ['master', 'stable', 'dev', 'beta']; -/// List of Flutter/Dart commands that need to run detached to avoid fvm errors. -const kDetachedCommands = [ - 'pub cache repair', -]; diff --git a/lib/src/utils/guards.dart b/lib/src/utils/guards.dart index 08545eae..4796bd7f 100644 --- a/lib/src/utils/guards.dart +++ b/lib/src/utils/guards.dart @@ -1,9 +1,7 @@ -import 'dart:io' show Platform; - import 'package:io/io.dart'; -import '../../constants.dart'; import '../../exceptions.dart'; +import 'helpers.dart'; import 'logger.dart'; /// Guards against certain action by validatin and throwing errors @@ -15,11 +13,10 @@ class Guards { if (!await isExecutable(execPath)) { throw FvmInternalError('Cannot execute $execPath'); } - if (Guards.shouldRunDetached(args)) { - logger.trace(Platform.script.path); + if (shouldRunDetached(args)) { FvmLogger.spacer(); FvmLogger.info( - 'This command ${args.join(" ")} will modify FVM installation.', + 'This command "${args.join(" ")}" will modify FVM installation.', ); FvmLogger.info( '''Because of that is suggested you run the following command in your terminal directly''', @@ -39,11 +36,4 @@ class Guards { throw FvmUsageException('Command needs to run outside of FVM proxy'); } } - - /// Check if command needs to be run detached - static bool shouldRunDetached(List args) { - final argString = args.join(' '); - - return kDetachedCommands.any(argString.contains); - } } diff --git a/lib/src/utils/helpers.dart b/lib/src/utils/helpers.dart index a62b1936..a52a4645 100644 --- a/lib/src/utils/helpers.dart +++ b/lib/src/utils/helpers.dart @@ -194,3 +194,23 @@ Future checkForFvmUpdate() async { currentVersion: packageVersion, ).update(); } + +/// Check if fvm is in cache directory +bool isFvmInstalledGlobally() { + /// Segment of the path where Pub caches global packages + final pubCacheSegment = Platform.isWindows ? "Pub\Cache" : ".pub-cache"; + logger.trace(Platform.script.path); + return Platform.script.path.contains(pubCacheSegment); +} + +/// Check if command needs to be run detached +bool shouldRunDetached(List args) { + /// List of Flutter/Dart commands that need to run detached to avoid fvm errors. + const shouldDetachCommands = [ + 'pub cache repair', + 'pub cache clean', + ]; + final argString = args.join(' '); + final shouldDetach = shouldDetachCommands.any(argString.contains); + return shouldDetach && isFvmInstalledGlobally(); +}