Skip to content

Commit bcff075

Browse files
authored
feat: install packages automatically (#130)
1 parent c4cc9dd commit bcff075

File tree

6 files changed

+99
-14
lines changed

6 files changed

+99
-14
lines changed

.github/workflows/very_good_cli.yaml

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,24 +21,22 @@ jobs:
2121
runs-on: ubuntu-latest
2222
steps:
2323
- uses: actions/checkout@v2.3.4
24-
- uses: dart-lang/setup-dart@v1
24+
- uses: subosito/flutter-action@v1.5.0
2525

2626
- name: Install Dependencies
27-
run: dart pub get
27+
run: flutter pub get
2828

2929
- name: Format
30-
run: dart format --set-exit-if-changed .
30+
run: flutter format --set-exit-if-changed .
3131

3232
- name: Analyze
33-
run: |
34-
dart analyze --fatal-infos --fatal-warnings lib
35-
dart analyze --fatal-infos --fatal-warnings test
33+
run: flutter analyze lib test
3634

3735
- name: Verify Build
38-
run: dart test --run-skipped -t pull-request-only
36+
run: flutter pub run test --run-skipped -t pull-request-only
3937

4038
- name: Run Tests
41-
run: dart test -x pull-request-only --coverage=coverage && pub run coverage:format_coverage --lcov --in=coverage --out=coverage/lcov.info --packages=.packages --report-on=lib
39+
run: flutter test -x pull-request-only --no-pub --coverage --test-randomize-ordering-seed random
4240

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

lib/src/commands/create.dart

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import 'package:universal_io/io.dart';
99
import 'package:usage/usage_io.dart';
1010
import 'package:very_good_analysis/very_good_analysis.dart';
1111
import 'package:very_good_cli/src/command_runner.dart';
12+
import 'package:very_good_cli/src/flutter_cli.dart';
1213
import 'package:very_good_cli/src/templates/very_good_core_bundle.dart';
1314

1415
// A valid Dart identifier that can be used for a package, i.e. no
@@ -72,8 +73,17 @@ class CreateCommand extends Command<int> {
7273
DirectoryGeneratorTarget(outputDirectory, _logger),
7374
vars: {'project_name': projectName},
7475
);
75-
7676
generateDone('Bootstrapping complete');
77+
78+
final isFlutterInstalled = await Flutter.installed();
79+
if (isFlutterInstalled) {
80+
final installDependenciesDone = _logger.progress(
81+
'Running "flutter packages get" in ${outputDirectory.path}',
82+
);
83+
await Flutter.packagesGet(outputDirectory.path);
84+
installDependenciesDone();
85+
}
86+
7787
_logSummary(fileCount);
7888

7989
unawaited(_analytics.sendEvent(

lib/src/flutter_cli.dart

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import 'package:universal_io/io.dart';
2+
3+
/// Flutter CLI
4+
class Flutter {
5+
/// Install flutter dependencies (`flutter packages get`).
6+
static Future<void> packagesGet([String? cwd]) {
7+
return _Cmd.run('flutter', ['packages', 'get'], workingDirectory: cwd);
8+
}
9+
10+
/// Determine whether flutter is installed
11+
static Future<bool> installed() async {
12+
try {
13+
await _Cmd.run('flutter', []);
14+
return true;
15+
} catch (_) {
16+
return false;
17+
}
18+
}
19+
}
20+
21+
class _Cmd {
22+
static Future<ProcessResult> run(
23+
String cmd,
24+
List<String> args, {
25+
bool throwOnError = true,
26+
String? workingDirectory,
27+
}) async {
28+
final result = await Process.run(cmd, args,
29+
workingDirectory: workingDirectory, runInShell: true);
30+
31+
if (throwOnError) {
32+
_throwIfProcessFailed(result, cmd, args);
33+
}
34+
return result;
35+
}
36+
37+
static void _throwIfProcessFailed(
38+
ProcessResult pr,
39+
String process,
40+
List<String> args,
41+
) {
42+
if (pr.exitCode != 0) {
43+
final values = {
44+
'Standard out': pr.stdout.toString().trim(),
45+
'Standard error': pr.stderr.toString().trim()
46+
}..removeWhere((k, v) => v.isEmpty);
47+
48+
var message = 'Unknown error';
49+
if (values.isNotEmpty) {
50+
message = values.entries.map((e) => '${e.key}\n${e.value}').join('\n');
51+
}
52+
53+
throw ProcessException(process, args, message, pr.exitCode);
54+
}
55+
}
56+
}

pubspec.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ dev_dependencies:
2020
build_runner: ^2.0.0
2121
build_verify: ^2.0.0
2222
build_version: ^2.0.0
23-
coverage: ^1.0.2
2423
mocktail: ^0.1.1
2524
test: ^1.17.0
2625

test/src/commands/create_test.dart

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,12 @@ void main() {
107107
final result = await command.run();
108108
expect(result, equals(ExitCode.success.code));
109109
verify(() => logger.progress('Bootstrapping')).called(1);
110-
verify(() => logger.info(
111-
'${lightGreen.wrap('✓')} '
112-
'Generated 62 file(s):',
113-
));
110+
verify(
111+
() => logger.info(
112+
'${lightGreen.wrap('✓')} '
113+
'Generated 62 file(s):',
114+
),
115+
);
114116
verify(() => logger.alert('Created a Very Good App! 🦄')).called(1);
115117
verify(
116118
() => generator.generate(

test/src/flutter_cli_test.dart

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import 'package:test/test.dart';
2+
import 'package:universal_io/io.dart';
3+
import 'package:very_good_cli/src/flutter_cli.dart';
4+
5+
void main() {
6+
group('Flutter CLI', () {
7+
group('packages get', () {
8+
test('throws when there is no pubspec.yaml', () {
9+
expectLater(
10+
Flutter.packagesGet(Directory.systemTemp.path),
11+
throwsException,
12+
);
13+
});
14+
15+
test('completes when there is a pubspec.yaml', () {
16+
expectLater(Flutter.packagesGet(), completes);
17+
});
18+
});
19+
});
20+
}

0 commit comments

Comments
 (0)