Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: run dart fix --apply as post generation step #189

Merged
merged 6 commits into from
Sep 23, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 24 additions & 5 deletions .github/workflows/very_good_cli.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,12 @@ jobs:

steps:
- uses: actions/checkout@v2.3.4
- uses: subosito/flutter-action@v1.5.0

- uses: subosito/flutter-action@v1.5.3

- name: Install Dependencies
run: flutter pub get

- name: Install LCOV
run: sudo apt-get install -y lcov

- name: Format
run: flutter format --set-exit-if-changed .

Expand All @@ -45,12 +43,33 @@ jobs:
- name: Check Code Coverage
uses: VeryGoodOpenSource/very_good_coverage@v1.1.1

e2e:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2.3.4

- uses: subosito/flutter-action@v1.5.3

- name: Install LCOV
run: sudo apt-get install -y lcov

- name: Install Dependencies
run: flutter pub get

- name: Run Tests
run: flutter pub run test --run-skipped -t e2e

- name: Check Code Coverage
uses: VeryGoodOpenSource/very_good_coverage@v1.1.1

pana:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2.3.4
- uses: subosito/flutter-action@v1.5.0

- uses: subosito/flutter-action@v1.5.3

- name: Install Dependencies
run: |
Expand Down
2 changes: 2 additions & 0 deletions dart_test.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
tags:
pull-request-only:
skip: "Should only be run during pull request"
e2e:
skip: "End to end tests should be run in parallel with other unit tests"
1 change: 1 addition & 0 deletions lib/src/cli/cli.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:path/path.dart' as p;
import 'package:universal_io/io.dart';

part 'dart_cli.dart';
part 'flutter_cli.dart';

/// Abstraction for running commands via command-line.
Expand Down
19 changes: 19 additions & 0 deletions lib/src/cli/dart_cli.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
part of 'cli.dart';

/// Dart CLI
class Dart {
/// Determine whether dart is installed
static Future<bool> installed() async {
try {
await _Cmd.run('dart', ['--version']);
return true;
} catch (_) {
return false;
}
}

/// Apply all fixes (`dart fix --apply`).
static Future<void> applyFixes() {
return _Cmd.run('dart', ['fix', '--apply']);
}
}
2 changes: 1 addition & 1 deletion lib/src/cli/flutter_cli.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class Flutter {
/// Determine whether flutter is installed
static Future<bool> installed() async {
try {
await _Cmd.run('flutter', []);
await _Cmd.run('flutter', ['--version']);
return true;
} catch (_) {
return false;
Expand Down
85 changes: 50 additions & 35 deletions lib/src/templates/template.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,15 @@ class DartPkgTemplate extends Template {

@override
Future<void> onGenerateComplete(Logger logger, Directory outputDir) async {
final isFlutterInstalled = await Flutter.installed();
if (isFlutterInstalled) {
final installDependenciesDone = logger.progress(
'Running "flutter pub get" in ${outputDir.path}',
);
await Flutter.pubGet(cwd: outputDir.path);
installDependenciesDone();
}
await _installDartPackages(logger, outputDir);
await _applyDartFixes(logger, outputDir);
_logSummary(logger);
}

void _logSummary(Logger logger) {
logger
..info('\n')
..alert('Created a Very Good Dart package! 🦄')
..alert('Created a Very Good Dart Package! 🦄')
..info('\n');
}
}
Expand All @@ -76,21 +70,15 @@ class FlutterPkgTemplate extends Template {

@override
Future<void> onGenerateComplete(Logger logger, Directory outputDir) async {
final isFlutterInstalled = await Flutter.installed();
if (isFlutterInstalled) {
final installDependenciesDone = logger.progress(
'Running "flutter packages get" in ${outputDir.path}',
);
await Flutter.packagesGet(cwd: outputDir.path);
installDependenciesDone();
}
await _installFlutterPackages(logger, outputDir);
await _applyDartFixes(logger, outputDir);
_logSummary(logger);
}

void _logSummary(Logger logger) {
logger
..info('\n')
..alert('Created a Very Good Flutter package! 🦄')
..alert('Created a Very Good Flutter Package! 🦄')
..info('\n');
}
}
Expand All @@ -109,21 +97,15 @@ class FlutterPluginTemplate extends Template {

@override
Future<void> onGenerateComplete(Logger logger, Directory outputDir) async {
final isFlutterInstalled = await Flutter.installed();
if (isFlutterInstalled) {
final installDependenciesDone = logger.progress(
'Running "flutter packages get" in ${outputDir.path}',
);
await Flutter.packagesGet(cwd: outputDir.path, recursive: true);
installDependenciesDone();
}
await _installFlutterPackages(logger, outputDir);
await _applyDartFixes(logger, outputDir);
_logSummary(logger);
}

void _logSummary(Logger logger) {
logger
..info('\n')
..alert('Created a Very Good Flutter plugin! 🦄')
..alert('Created a Very Good Flutter Plugin! 🦄')
..info('\n');
}
}
Expand All @@ -142,14 +124,8 @@ class CoreTemplate extends Template {

@override
Future<void> onGenerateComplete(Logger logger, Directory outputDir) async {
final isFlutterInstalled = await Flutter.installed();
if (isFlutterInstalled) {
final installDependenciesDone = logger.progress(
'Running "flutter packages get" in ${outputDir.path}',
);
await Flutter.packagesGet(cwd: outputDir.path);
installDependenciesDone();
}
await _installFlutterPackages(logger, outputDir);
await _applyDartFixes(logger, outputDir);
_logSummary(logger);
}

Expand All @@ -173,3 +149,42 @@ class CoreTemplate extends Template {
);
}
}

Future<void> _installDartPackages(
Logger logger,
Directory outputDir,
) async {
final isFlutterInstalled = await Flutter.installed();
if (isFlutterInstalled) {
final installDependenciesDone = logger.progress(
'Running "flutter pub get" in ${outputDir.path}',
);
await Flutter.pubGet(cwd: outputDir.path);
installDependenciesDone();
}
}

Future<void> _installFlutterPackages(
Logger logger,
Directory outputDir,
) async {
final isFlutterInstalled = await Flutter.installed();
if (isFlutterInstalled) {
final installDependenciesDone = logger.progress(
'Running "flutter packages get" in ${outputDir.path}',
);
await Flutter.packagesGet(cwd: outputDir.path);
installDependenciesDone();
}
}

Future<void> _applyDartFixes(Logger logger, Directory outputDir) async {
final isDartInstalled = await Dart.installed();
if (isDartInstalled) {
final applyFixesDone = logger.progress(
'Running "dart fix --apply" in ${outputDir.path}',
);
await Dart.applyFixes();
applyFixesDone();
}
}
17 changes: 15 additions & 2 deletions test/e2e_test.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
@Tags(['e2e'])
import 'package:io/io.dart';
import 'package:mason/mason.dart';
import 'package:mocktail/mocktail.dart';
Expand Down Expand Up @@ -56,6 +57,10 @@ void main() {
);
expect(result, equals(ExitCode.success.code));

await untilCalled(
() => logger.alert('Created a Very Good Dart Package! 🦄'),
);

final formatResult = await Process.run(
'flutter',
['format', '--set-exit-if-changed', '.'],
Expand Down Expand Up @@ -104,6 +109,10 @@ void main() {
);
expect(result, equals(ExitCode.success.code));

await untilCalled(
() => logger.alert('Created a Very Good Flutter Package! 🦄'),
);

final formatResult = await Process.run(
'flutter',
['format', '--set-exit-if-changed', '.'],
Expand Down Expand Up @@ -152,6 +161,10 @@ void main() {
);
expect(result, equals(ExitCode.success.code));

await untilCalled(
() => logger.alert('Created a Very Good Flutter App! 🦄'),
);

final formatResult = await Process.run(
'flutter',
['format', '--set-exit-if-changed', '.'],
Expand Down Expand Up @@ -190,6 +203,6 @@ void main() {
expect(testCoverageResult.exitCode, equals(ExitCode.success.code));
expect(testCoverageResult.stderr, isEmpty);
expect(testCoverageResult.stdout, contains('lines......: 100.0%'));
}, timeout: const Timeout(Duration(minutes: 1)));
});
});
}, timeout: const Timeout(Duration(minutes: 3)));
}
18 changes: 18 additions & 0 deletions test/src/cli/dart_cli_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import 'package:test/test.dart';
import 'package:very_good_cli/src/cli/cli.dart';

void main() {
group('Dart', () {
group('.installed', () {
test('returns true when dart is installed', () {
expectLater(Dart.installed(), completion(isTrue));
});
});

group('.applyFixes', () {
test('completes normally', () {
expectLater(Dart.applyFixes(), completes);
});
});
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ name: example
''';

void main() {
group('Flutter CLI', () {
group('packages get', () {
group('Flutter', () {
group('.packagesGet', () {
test('throws when there is no pubspec.yaml', () {
expectLater(
Flutter.packagesGet(cwd: Directory.systemTemp.path),
Expand Down Expand Up @@ -59,7 +59,7 @@ void main() {
});
});

group('pub get', () {
group('.pubGet', () {
test('throws when there is no pubspec.yaml', () {
expectLater(
Flutter.pubGet(cwd: Directory.systemTemp.path),
Expand Down
6 changes: 3 additions & 3 deletions test/src/commands/create_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@ void main() {
getPackagesMsg: 'Running "flutter pub get" in .tmp',
templateName: 'dart_pkg',
expectedBundle: dartPackageBundle,
expectedLogSummary: 'Created a Very Good Dart package! 🦄',
expectedLogSummary: 'Created a Very Good Dart Package! 🦄',
);
});

Expand All @@ -513,7 +513,7 @@ void main() {
getPackagesMsg: 'Running "flutter packages get" in .tmp',
templateName: 'flutter_pkg',
expectedBundle: flutterPackageBundle,
expectedLogSummary: 'Created a Very Good Flutter package! 🦄',
expectedLogSummary: 'Created a Very Good Flutter Package! 🦄',
);
});

Expand All @@ -522,7 +522,7 @@ void main() {
getPackagesMsg: 'Running "flutter packages get" in .tmp',
templateName: 'flutter_plugin',
expectedBundle: flutterPluginBundle,
expectedLogSummary: 'Created a Very Good Flutter plugin! 🦄',
expectedLogSummary: 'Created a Very Good Flutter Plugin! 🦄',
);
});
});
Expand Down