Skip to content

Commit

Permalink
Merge pull request #364 from leoafarias/feature/should-detach-improve…
Browse files Browse the repository at this point in the history
…ments

Clean up logic for should detach
  • Loading branch information
leoafarias authored Nov 12, 2021
2 parents 55d71e5 + 9f1b843 commit ae76f19
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 17 deletions.
4 changes: 0 additions & 4 deletions lib/constants.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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',
];
16 changes: 3 additions & 13 deletions lib/src/utils/guards.dart
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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''',
Expand All @@ -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<String> args) {
final argString = args.join(' ');

return kDetachedCommands.any(argString.contains);
}
}
20 changes: 20 additions & 0 deletions lib/src/utils/helpers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -194,3 +194,23 @@ Future<void> 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<String> 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();
}

0 comments on commit ae76f19

Please sign in to comment.