Skip to content

Commit

Permalink
Merge pull request #363 from leoafarias/feature/detached-notice
Browse files Browse the repository at this point in the history
Command detach check
  • Loading branch information
leoafarias authored Nov 12, 2021
2 parents 8b0a5c5 + b453974 commit 55d71e5
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
4 changes: 4 additions & 0 deletions lib/constants.dart
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,7 @@ 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',
];
3 changes: 2 additions & 1 deletion lib/src/utils/commands.dart
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ Future<int> _runCmd(
Map<String, String>? environment,
}) async {
// Project again a non executable path
await Guards.canExecute(execPath);
await Guards.canExecute(execPath, args);

final processManager = ProcessManager();

// Switch off line mode
Expand Down
38 changes: 37 additions & 1 deletion lib/src/utils/guards.dart
Original file line number Diff line number Diff line change
@@ -1,13 +1,49 @@
import 'dart:io' show Platform;

import 'package:io/io.dart';

import '../../constants.dart';
import '../../exceptions.dart';
import 'logger.dart';

/// Guards against certain action by validatin and throwing errors
class Guards {
Guards._();

/// Check if can execute path or throws error
static Future<void> canExecute(String execPath) async {
static Future<void> canExecute(String execPath, List<String> args) async {
if (!await isExecutable(execPath)) {
throw FvmInternalError('Cannot execute $execPath');
}
if (Guards.shouldRunDetached(args)) {
logger.trace(Platform.script.path);
FvmLogger.spacer();
FvmLogger.info(
'This command ${args.join(" ")} will modify FVM installation.',
);
FvmLogger.info(
'''Because of that is suggested you run the following command in your terminal directly''',
);
FvmLogger.spacer();
FvmLogger.fine("$execPath ${args.join(' ')}");

FvmLogger.spacer();

FvmLogger.info(
'''If after this command FVM cannot be found in your terminal. Please run the following:''',
);

FvmLogger.spacer();
FvmLogger.fine("$execPath pub global activate fvm");

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);
}
}

0 comments on commit 55d71e5

Please sign in to comment.