Skycoder42 / dart_pre_commit

A small collection of pre commit hooks to format and lint dart code

Home Page:https://pub.dev/packages/dart_pre_commit

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add support for fvm

taosif7-dreamorbit opened this issue · comments

Since this plugin directly runs the flutter ... and dart ... commands, the projects that are using different version of flutter via fvm, fail to use the fvm flutter version for analysis. We need to add support for fvm in flutter and dart analyse commands.

A proposed solution is that we can detect .fvm folder in the project root and add "fvm" prefix before all flutter and dart commands in execution.

Hi. This sounds like an interesting tool. I think it would be a good idea to simply allow replacing the dart/flutter commands. Using the presence of the .fvm directory sounds like a good idea for that, but I am unsure if I want to explicitly support fvm. Maybe it would be better and easier to simply add two configuration keys that let you overwrite the dart/flutter binaries...

Sure, I'd love to work on this feature.
Please assign this to me

I want a little bit clarity on this statement, can you please describe more

Maybe it would be better and easier to simply add two configuration keys that let you overwrite the dart/flutter binaries...

Well, by default, the tool simply uses "flutter" and "dart". However, there could be a setting in the pubspec.yaml to overwrite these tools. Something like:

dart_test_tools:
  dart: fvm dart
  flutter: fvm flutter

This would allow the feature to work with other tools as well

Well, by default, the tool simply uses "flutter" and "dart". However, there could be a setting in the pubspec.yaml to overwrite these tools. Something like:

dart_test_tools:
  dart: fvm dart
  flutter: fvm flutter

This would allow the feature to work with other tools as well

Understood! This is great

As a workaround I adjusted the simple dart wrapper for a project I am working on:

import 'dart:io';

// run with `dart setup_git_hooks.dart` if you use fvm
// else run with `dart setup_git_hooks.dart --no-fvm`

Future<void> main(List<String> arguments) async {
  final useFvm = !arguments.contains('--no-fvm');
  final command = useFvm
      ? 'cd app && exec fvm flutter pub run dart_pre_commit'
      : 'cd app && exec flutter pub run dart_pre_commit';

  final preCommitHook = File('.git/hooks/pre-commit');
  await preCommitHook.parent.create();
  await preCommitHook.writeAsString(
    '''
#!/bin/sh
$command # specify custom options here
''',
  );

  if (!Platform.isWindows) {
    final result = await Process.run('chmod', ['a+x', preCommitHook.path]);
    stdout.write(result.stdout);
    stderr.write(result.stderr);
    exitCode = result.exitCode;
  }
}

This way each developer can decide themselves if they want to use dart_pre_commit with or without fvm. By default fvm is used. Simply append --no-fvm to the command and pure flutter is used instead.

With fvm

dart run tool/setup_git_hooks.dart

Without fvm

dart run tool/setup_git_hooks.dart --no-fvm

@Peetee06 I like this solution. I will add it to the README as another example

And done: 258af33

I think this should suffice for completing the issue. If you think otherwise, please reopen it.