Skip to content

Commit

Permalink
Merge pull request #568 from leoafarias/fix/which-command
Browse files Browse the repository at this point in the history
Fix/which command
  • Loading branch information
leoafarias authored Oct 4, 2023
2 parents b8ef172 + d08f7c0 commit 1504bba
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 36 deletions.
47 changes: 30 additions & 17 deletions lib/src/commands/doctor_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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],
Expand Down
24 changes: 14 additions & 10 deletions lib/src/commands/global_command.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}
12 changes: 10 additions & 2 deletions lib/src/utils/console_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> 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.
Expand Down
11 changes: 8 additions & 3 deletions lib/src/utils/which.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -20,15 +23,17 @@ 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) {
for (var ext in possibleExtensions) {
String winPath = '$fullPath$ext';
exec = File(winPath);
if (exec.existsSync()) {
return exec.absolute.path;
final exectPath = exec.absolute.path;
return binDir ? dirname(exectPath) : exectPath;
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions test/commands/use_command_test.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
23 changes: 23 additions & 0 deletions test/utils/which_test.dart
Original file line number Diff line number Diff line change
@@ -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',
);
});
}

0 comments on commit 1504bba

Please sign in to comment.