diff --git a/lib/src/commands/doctor_command.dart b/lib/src/commands/doctor_command.dart index 12364f11..1c21345a 100644 --- a/lib/src/commands/doctor_command.dart +++ b/lib/src/commands/doctor_command.dart @@ -47,9 +47,7 @@ class DoctorCommand extends BaseCommand { void printFVMDetails(FVMContext context) {} void _printProject(Project project) { logger.info('Project:'); - final table = createTable() - ..insertColumn(header: 'Project', alignment: TextAlignment.left) - ..insertColumn(header: project.name, alignment: TextAlignment.left); + final table = createTable(['Project', project.name]); table.insertRows([ ['Directory', project.path], @@ -85,9 +83,7 @@ class DoctorCommand extends BaseCommand { logger ..spacer ..info('IDEs:'); - final table = createTable() - ..insertColumn(header: 'IDEs', alignment: TextAlignment.left) - ..insertColumn(header: '', alignment: TextAlignment.left); + final table = createTable(['IDEs', 'Value']); table.insertRow([kVsCode]); // Check for .vscode directory @@ -152,22 +148,39 @@ class DoctorCommand extends BaseCommand { logger.write(table.toString()); } - void _printEnvironmentDetails(String? flutterWhich, String? dartWhich) { + void _printEnvironmentDetails( + String? flutterWhich, + String? dartWhich, + ) { logger ..spacer ..info('Environment:'); - final table = createTable() - ..insertColumn( - header: 'Environment Detail', alignment: TextAlignment.left) - ..insertColumn(header: 'Path/Value', alignment: TextAlignment.left); + + var table = createTable( + ['Environment Variables', 'Value'], + ); + + table.insertRows([ + ['Flutter PATH', flutterWhich ?? 'Not found'], + ['Dart PATH', dartWhich ?? 'Not found'], + ]); + + for (var key in ConfigKeys.values) { + table.insertRow([key.envKey, ctx.environment[key.envKey] ?? 'N/A']); + } + + table.insertRows([ + ['Flutter PATH', flutterWhich ?? 'Not found'], + ['Dart PATH', dartWhich ?? 'Not found'], + ]); + + logger.write(table.toString()); + + table = createTable( + ['Platform', 'Value'], + ); table.insertRows([ - ['Flutter Path', flutterWhich ?? 'Not found'], - ['Dart Path', dartWhich ?? 'Not found'], - [ - ConfigKeys.cachePath.envKey, - ctx.environment[ConfigKeys.cachePath.envKey] ?? 'Not set' - ], ['OS', '${Platform.operatingSystem} ${Platform.operatingSystemVersion}'], ['Dart Locale', Platform.localeName], ['Dart runtime', Platform.version], diff --git a/lib/src/commands/global_command.dart b/lib/src/commands/global_command.dart index 493f9f63..68813d4b 100644 --- a/lib/src/commands/global_command.dart +++ b/lib/src/commands/global_command.dart @@ -1,10 +1,11 @@ +import 'package:fvm/constants.dart'; import 'package:fvm/src/models/cache_flutter_version_model.dart'; -import 'package:fvm/src/models/flutter_version_model.dart'; import 'package:fvm/src/services/global_version_service.dart'; import 'package:fvm/src/services/logger_service.dart'; import 'package:fvm/src/services/project_service.dart'; import 'package:fvm/src/utils/console_utils.dart'; import 'package:fvm/src/utils/context.dart'; +import 'package:fvm/src/utils/helpers.dart'; import 'package:fvm/src/utils/which.dart'; import 'package:mason_logger/mason_logger.dart'; import 'package:tint/tint.dart'; @@ -46,19 +47,18 @@ class GlobalCommand extends BaseCommand { // Sets version as the global GlobalVersionService.fromContext.setGlobal(cacheVersion); - final flutterInPath = which('flutter'); + final flutterInPath = which('flutter', binDir: true); // Get pinned version, for comparison on terminal - final pinnedVersion = ProjectService.fromContext.findVersion(); + final project = ProjectService.fromContext.findAncestor(); + + final pinnedVersion = project.pinnedVersion; CacheFlutterVersion? pinnedCacheVersion; if (pinnedVersion != null) { //TODO: Should run validation on this - final flutterPinnedVersion = FlutterVersion.parse(pinnedVersion); - pinnedCacheVersion = CacheService.fromContext.getVersion( - flutterPinnedVersion, - ); + pinnedCacheVersion = CacheService.fromContext.getVersion(pinnedVersion); } final isDefaultInPath = flutterInPath == ctx.globalCacheBinPath; @@ -93,9 +93,13 @@ class GlobalCommand extends BaseCommand { ..spacer; } - logger.info( - 'Your IDE might override the PATH to the Flutter in their terminal to the one configured within the project.', - ); + if (isVsCode()) { + logger + ..notice( + '$kVsCode might override the PATH to the Flutter in their terminal', + ) + ..info('Run the command outside of the IDE to verify.'); + } return ExitCode.success.code; } } diff --git a/lib/src/utils/console_utils.dart b/lib/src/utils/console_utils.dart index 39198b39..12b01369 100644 --- a/lib/src/utils/console_utils.dart +++ b/lib/src/utils/console_utils.dart @@ -4,12 +4,20 @@ import 'package:fvm/src/models/cache_flutter_version_model.dart'; import '../../exceptions.dart'; import '../services/logger_service.dart'; -Table createTable() { - return Table() +Table createTable([List columns = const []]) { + final table = Table() ..borderColor = ConsoleColor.white ..borderType = BorderType.grid ..borderStyle = BorderStyle.square ..headerStyle = FontStyle.bold; + + for (final column in columns) { + table.insertColumn( + header: column, + alignment: TextAlignment.left, + ); + } + return table; } /// Allows to select from cached sdks. diff --git a/lib/src/utils/which.dart b/lib/src/utils/which.dart index 90e98f59..b81fca21 100644 --- a/lib/src/utils/which.dart +++ b/lib/src/utils/which.dart @@ -2,7 +2,10 @@ import 'dart:io'; import 'package:path/path.dart'; -String? which(String command) { +String? which( + String command, { + bool binDir = false, +}) { String? pathEnv = Platform.environment['PATH']; String? pathExtEnv = Platform.isWindows ? Platform.environment['PATHEXT'] : null; @@ -20,7 +23,8 @@ String? which(String command) { File exec = File(fullPath); if (exec.existsSync()) { - return exec.absolute.path; + final exectPath = exec.absolute.path; + return binDir ? dirname(exectPath) : exectPath; } if (Platform.isWindows && pathExtEnv != null) { @@ -28,7 +32,8 @@ String? which(String command) { String winPath = '$fullPath$ext'; exec = File(winPath); if (exec.existsSync()) { - return exec.absolute.path; + final exectPath = exec.absolute.path; + return binDir ? dirname(exectPath) : exectPath; } } } diff --git a/pubspec.lock b/pubspec.lock index 7d536c81..a425adb7 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -5,18 +5,18 @@ packages: dependency: transitive description: name: _fe_analyzer_shared - sha256: eb376e9acf6938204f90eb3b1f00b578640d3188b4c8a8ec054f9f479af8d051 + sha256: ae92f5d747aee634b87f89d9946000c2de774be1d6ac3e58268224348cd0101a url: "https://pub.dev" source: hosted - version: "64.0.0" + version: "61.0.0" analyzer: dependency: transitive description: name: analyzer - sha256: "69f54f967773f6c26c7dcb13e93d7ccee8b17a641689da39e878d5cf13b06893" + sha256: ea3d8652bda62982addfd92fdc2d0214e5f82e43325104990d4f4c4a2a313562 url: "https://pub.dev" source: hosted - version: "6.2.0" + version: "5.13.0" archive: dependency: transitive description: diff --git a/test/commands/use_command_test.dart b/test/commands/use_command_test.dart index 7a823386..9e48030e 100644 --- a/test/commands/use_command_test.dart +++ b/test/commands/use_command_test.dart @@ -1,3 +1,4 @@ +@Timeout(Duration(minutes: 5)) import 'package:fvm/src/models/flutter_version_model.dart'; import 'package:fvm/src/services/cache_service.dart'; import 'package:fvm/src/services/project_service.dart'; diff --git a/test/utils/which_test.dart b/test/utils/which_test.dart new file mode 100644 index 00000000..0409f1dc --- /dev/null +++ b/test/utils/which_test.dart @@ -0,0 +1,23 @@ +import 'package:fvm/src/utils/which.dart'; +import 'package:test/test.dart'; + +void main() { + // Benchmark test for `which` function + // Using a simplistic loop to measure performance. + // For a more accurate benchmark, consider using a benchmarking package. + test('Benchmark: which function', () { + const totalIterations = 1000; + // Setup specific environment variables, as above. + var startTime = DateTime.now(); + for (int i = 0; i < totalIterations; i++) { + which('command', binDir: false); + } + var endTime = DateTime.now(); + var elapsedTime = endTime.difference(startTime); + print( + 'Time taken for $totalIterations iterations: ${elapsedTime.inMilliseconds}ms'); + print( + 'Time taken for 1 iteration: ${elapsedTime.inMilliseconds / totalIterations}ms', + ); + }); +}